token-diet
Skill3 starsChanged 7 months agoMITView on GitHub
Production-ready token optimization: reduce costs 40–75% through retrieval pruning, smart caching, and model routing. Use whenever optimizing API costs, latency, or managing long context—especially for RAG pipelines, high-volume systems, multi-turn conversations, or when context exceeds 2K tokens.
2 other projects use a version of this.
Adopted by
Most recently taken up by
- Kulaxyz/token-diettheir own version, first seen Jul 3, 2026
- qiuskye/claude-skillstheir own version, first seen Jun 19, 2026
Other versions2 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 2
- Kulaxyz/token-diet★ 472same name
- qiuskye/claude-skills★ 1same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
The file
We split this skill into the rules it gives an agent
9 rules. About 0 are common, 8 are distinctive, and 1 is very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈8
Distinctive · estimate
This author's own choices. Worth a look.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 22
Goal: Never send unnecessary tokens.
- Distinctive: line 31
Keep only sentences containing query terms or answer-bearing context
- Distinctive: line 33
If retrieval yields <5 relevant chunks, fetch 5 more before pruning
- Very specific: line 60
Implementation: Use API cache headers (cache_control: {"type": "ephemeral"} for Anthropic.
- Distinctive: line 74
Safety guard: If confidence low or error detected → retry on stronger model (don't downgrade past first attempt).
- Distinctive: line 112
If contradiction detected between new facts and old state
- Distinctive: line 114
If summary token cost > fresh state token cost
- Distinctive: line 155
Why minimal: Every line you don't send saves tokens.
- Distinctive: line 189
Golden rule: If unsure whether compression/routing broke something, disable it and rerun.
≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Token Diet v3.0 (Production-Ready)
Deployment-grade token optimization with execution order, ROI metrics, guardrails, and measurable outcomes.
Execution Flow (Order Matters)
Q → Retrieve → Prune → Cache → Route → Build Prompt → Compress → Call LLM → Measure → Update StateWhy this order: Early pruning eliminates waste before caching/routing decisions. Compression happens last (post safety checks). Measurement feeds back into next iteration.
1. Retrieval Pruning (Highest ROI)
Goal: Never send unnecessary tokens.
Pipeline:
query → embed → topK(20) → dedupe → sentence-trim → topK(5–8)Concrete rules:
- Drop near-duplicate chunks (>85% similarity)
- Keep only sentences containing query terms or answer-bearing context
- Hard cap: ≤1200 tokens total context
- If retrieval yields <5 relevant chunks, fetch 5 more before pruning
Win: 30–60% reduction immediately (no quality loss if done right).
2. Smart Caching (Implement Early)
Split prompt into 3 blocks:
[STATIC_SYS] → cache (reuse across requests)
[DYNAMIC_CTX] → no cache (changes per request)
[USER_INPUT] → no cache (one-off query)Cache candidates:
- System instructions
- Schemas (JSON, SQL, output format)
- Tool definitions
- Few-shot examples (if static)
Decision rule:
if block_reused_in_next_N_requests → cache
else → inline (no caching overhead)Implementation: Use API cache headers (cache_control: {"type": "ephemeral"} for Anthropic; similar for other providers).
3. Conditional Routing (Cheap-First Model Selection)
Decision tree:
if task ∈ {format, extract, classify, summarize}
→ small model (Haiku, GPT-4o-mini, Gemini-Flash)
else if task ∈ {reason, synthesize, creative}
→ large model (Sonnet, GPT-4o, Gemini-Pro)Safety guard: If confidence low or error detected → retry on stronger model (don't downgrade past first attempt).
Result: 60–80% cost reduction on simple tasks without quality hit.
4. Tool Batching (Avoid Round-Trips)
Bad (5 calls + reread context 5x):
get_user(1) → get_user(2) → get_user(3)Good (1 call):
get_users([1, 2, 3])Also batch:
- Embedding calls (vector DBs)
- Database reads
- Validation checks
5. Stateful Memory (Replace Naive Summarization)
Structure state as JSON:
{
"facts": ["user_id=42", "role=admin"],
"constraints": ["budget_limit=$1000", "deadline=2026-04-30"],
"open_tasks": ["approval_pending", "docs_missing"],
"summary": "(optional) Human-readable 150-token version"
}When to regenerate:
- If contradiction detected between new facts and old state
- After ≥5 turns
- If summary token cost > fresh state token cost
Why: Structured state lets LLM reason precisely. Optional summary is for readability/debugging, not required.
6. Linguistic Compression (Last-Mile Only)
Apply only right before the API call:
"Please analyze the dataset and provide a concise summary"
→ "Analyze data. Summarize concise."DO NOT compress:
- Code (breaks syntax)
- JSON schema (breaks parsing)
- Legal/safety text (liability risk)
- URLs, identifiers
Benefit: ~15% token reduction on prose prompts; safe only if applied surgically.
7. Prompt Assembly Template
Minimal structure (reorder as needed):
[SYS — cached]
Context:
{pruned_context_1200_tokens_max}
State:
{facts + constraints (JSON)}
Task:
{compressed_user_input}Why minimal: Every line you don't send saves tokens. No filler, no explanations that don't move the needle.
8. Measurement (Non-Optional)
Track per request:
tokens_in
tokens_out
latency_ms
cache_hit_rate (%)
retrieval_tokens_before / after
cost_usdAdd alerts:
- Context >1500 tokens → investigate retrieval
- Cache hit rate <70% → check static block design
- Cost spike >20% vs. baseline → disable routing/compression, debug
Why: Without measurement, you're flying blind. Guardrails only work if you see violations early.
9. Safety Guards (Prevent Silent Failure)
| Problem | Signal | Action |
| Answer quality drops | User feedback or eval score ↓ | Disable compression first, then increase context |
| Hallucination risk rises | Repeated factual errors | Increase context, not summary; rebuild state from raw history |
| Silent drift | State contradicts history | Rebuild state from scratch; check if constraints stale |
| Over-pruning | Query unmatched to results | Fetch more chunks before pruning; lower similarity threshold |
Golden rule: If unsure whether compression/routing broke something, disable it and rerun. Measure the difference. Only re-enable if safe.
Ultra-Compressed (Caveman Runtime Spec)
Retrieve → prune hard
Cache static
Route cheap→expensive
Batch all
State > summary
Compress last
Measure always
Guard driftNet Effect (Realistic)
| Metric | Target | Conditions |
| Token reduction | 50–80% | All 9 steps applied in order |
| Latency | ↓20–50% | Caching + routing active |
| Cost | ↓40–75% | Routing to smaller models + cache hits >70% |
| Quality | Stable | Guards enforced; no compression on code/schema/legal |
Typical breakdown:
- Pruning: 30–40% reduction
- Caching: 20–30% reduction (if reuse >50%)
- Routing: 30–50% cost reduction (simple tasks to cheap model)
- Compression: 10–15% reduction (prose only)
- Combined: Multiplicative, not additive.
Common Mistakes
| Mistake | Result | Fix |
| Skip measurement | Invisible failures | Instrument from day 1 |
| Compress code/JSON | Syntax errors, parsing fails | Never compress structured data |
| Prune too aggressively | Relevant context lost → hallucinations | Keep ≥1200 tokens context; test quality on pruned versions |
| Cache everything | No room for dynamic context | Cache only truly static blocks |
| Summarize too early | Miss important detail → wrong answer | Use structured state; regenerate if contradiction |
| Ignore routing guardrails | Quality collapse on hard tasks | Always allow retry on stronger model |
Author
- Name: Vijay Dixon
- Email: vijaydixon2026+aitools@gmail.com
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

