# Token Diet Context

> Cut your AI agent's API bill by up to 80%: deterministic zero-token context compaction + KV prefix-cache verifier (token_diet.py verify) that stops cache-busting prompt edits.

Token Diet Context is a paid AI agent skill on Agensi by Scrimal, in LLM Ops. It loads into your AI agent through the Agensi MCP server, always in its latest approved version.

- Price: $7.99 (one-time purchase)
- Creator: [Scrimal](https://www.agensi.io/creators/scrimal)
- Category: [LLM Ops](https://www.agensi.io/skills/llm-ops)
- Version: 1.1.1
- Last updated: 2026-12-02
- Security: scan passed (2026-12-02); every new listing is also reviewed by a person
- Listing: https://www.agensi.io/skills/token-diet-context
- Tags: llm-ops, ai-agents, devops, context, token-economy, kv-cache, compaction, summary

## About this skill

You start a coding session, edit one line at the top of your `CLAUDE.md` or session notes, and suddenly your Anthropic/OpenAI API bill jumps **10x**—because mutating the prefix invalidated your provider's **KV Prefix Cache** and forced a full context re-read on every single turn.

Meanwhile, built-in `/compact` commands burn extra LLM tokens just to summarize your notes and often erase the exact architectural invariants you needed. **Token Diet Context** solves both problems deterministically at **zero LLM token cost**—battle-tested on a **70.5M-token, 460+ step run for just $1.43/day**.

### Why It Pays for Itself on Day One
* **Protects Your KV Prefix Cache (`verify`):** Compares byte-for-byte prefix stability between session turns (`INTACT >= 90%`, `PARTIAL_SHIFT`, `BROKEN < 70%`) so your cached prompt tokens stay at a 90% discount instead of resetting to full price.
* **Zero-Token Deterministic Compaction (`compact`):** Compresses bloated `pinned.md` state files locally using stdlib Python—no LLM summarization call, no hallucinated deletions.
* **Append-Only State Architecture:** Splits session memory into an immutable top header (`Goal`, `Invariants`, `Decisions`) and an append-only bottom log so the prefix hash never shifts mid-session.
* **Multilingual Token Budget Gate (`budget`):** Accurately estimates both English/code (~4 chars/token) and Cyrillic/CJK (~1.5 chars/token) without `tiktoken`, enforcing hard token ceilings with CI exit codes (`0` OK, `1` WARN, `2` FAIL).

### What's Inside
1. `scripts/token_diet.py` — Standalone zero-dependency CLI (`compact`, `budget`, `verify`) with atomic file writes and CI exit codes (`0/1/2`).
2. `references/diet_contract.md` — Copy-paste session state layout (`pinned_template`) engineered for maximum KV-cache hit rates.
3. `hooks/pi.md` — Ready-to-wire hook config for automated budget and prefix-stability checks.

## Requires

- Python 3.10+

Zero external dependencies (no tiktoken required) and zero network requests. Runs locally on your session Markdown files with atomic writes.

## Known limitations

Token counts use calibrated character-ratio heuristics (~4 chars/token for ASCII/code, ~1.5 for non-ASCII) rather than model-specific BPE tokenizers, giving fast zero-dependency estimates within ~10–15% of exact counts.

## How to use this skill

1. Connect your AI agent to Agensi once: add the MCP server https://mcp.agensi.io/mcp/auth and sign in with your Agensi account (OAuth, no API key). Steps for Claude, ChatGPT, Claude Code, Codex, Cursor, VS Code and other agents: https://www.agensi.io/docs/mcp-setup
2. Ask your agent: "Use the Token Diet Context skill from Agensi" and describe your task.
3. Buy it on the listing or let your agent ask you first; your agent then loads it from Agensi when it needs it. Every paid skill has a money-back guarantee: https://www.agensi.io/docs/money-back-guarantee

## Frequently asked questions

### What specific problem does this skill solve for long-running agent sessions?

Token Diet Context solves the 'context collapse' problem where long-running AI sessions become slow and expensive due to redundant logs and repetitive notes. It uses a deterministic Python guard to enforce strict token budgets and stabilize the KV cache for faster, cheaper inference.

### Which AI models and frameworks is this protocol compatible with?

This is a framework-agnostic Python guard that works with any LLM supporting KV-caching, including GPT-4 and Claude. It operates as a zero-dependency script, making it compatible with both custom Python agents and established orchestration frameworks.

### What is included in the digital download for this skill?

You receive SKILL.md (the progressive-disclosure & KV-cache protocol), scripts/token_diet.py (zero-dependency CLI with compact, budget, and verify commands + exit codes 0/1/2), references/diet_contract.md (an append-only pinned.md template engineered for prefix stability), and hooks/pi.md for automated pre-turn checks.

### How does this protocol differ from simply prompting an agent to 'be concise'?

Unlike soft prompting, which agents often ignore as context grows, this skill uses a hard-coded Python script to monitor and prune data before it reaches the LLM. It acts as a physical 'circuit breaker' that prevents the agent from even seeing bloated or redundant information.

### What does the initial setup process look like for a developer?

Setup takes under two minutes: copy the append-only layout from references/diet_contract.md into your pinned.md session file, then run token_diet.py compact and verify (or wire hooks/pi.md) to keep your prompt prefix immutable and within budget.

### Does the token counter require model-specific libraries like tiktoken?

The included script uses a deterministic character-based approximation for token counting to ensure zero-dependency performance. While not an exact match for model-specific encoders like tiktoken, it is calibrated to provide a safe buffer that prevents context overflow across all major providers.

---

Source: [Agensi](https://www.agensi.io), the marketplace of skills for AI agents. Any MCP-compatible agent (Claude, ChatGPT, Claude Code, Codex, Cursor, VS Code and more) connects at https://mcp.agensi.io/mcp/auth and signs in with an Agensi account; setup for every agent: https://www.agensi.io/docs/mcp-setup. Append .md to any skill, guide or docs address on agensi.io for its markdown version.
