A developer shares a detailed case study on reducing an AI agent's token consumption by 72% through system prompt reduction, tighter retrieval, tool output pruning, and other techniques, with minimal impact on success rate.
Result first: I took a 3-role agent (planner → tool-user → writer) from a median 11,920 tokens/task down to 3,320 (-72%) over 200 eval runs. Success rate stayed flat (93.8% ➝ 93.1%, ±1.2%). Breakdown of where the savings came from and what didn’t work. Setup so you can compare apples to apples - Dataset: 200 tasks (mix of web-like lookups, simple analysis, short report). Avg 5.6 turns baseline. - Buckets I tracked per task: system prompt, context/RAG, tool outputs, agent messages, final answer. - Baseline median tokens/task: - System: 1,240 - Context/RAG: 3,020 - Tool outputs: 4,980 - Agent messages (planner + tool-user chatter): 1,880 - Final answer: 800 - Total: 11,920 What actually moved the needle 1) System prompt diet (1,240 ➝ 280, -960 tokens) - Rewrote role/instructions as a compact checklist; removed duplicative policy text and long examples from system. - Referred to tools by short IDs (T1, T2…) and documented them once in code (not in the prompt). - Pinned a single canonical schema and linked to it by name (schema: “AnswerV1”) instead of repeating it. 2) Don’t send a whole knowledge base; retrieve tightly (3,020 ➝ 900, -2,120 tokens) - Switched to retrieval with 256-token chunks, top-6. 128-token chunks hurt accuracy; 512 inflated context without much gain. - Also sent only the exact snippets, not the doc preamble/footers. 3) Project tool outputs to what you actually need (4,980 ➝ 3,380, -1,600 tokens) - Tools returned full blobs. I changed them to return only the fields used downstream (e.g., title, date, 2–3 salient facts). - For tables, selected columns instead of whole rows; capped rows to N relevant. 4) Pass deltas, not the whole scratchpad (-400 tokens/turn; net -820 per task) - Kept long-term memory server-side and passed only new or changed facts as F12: …, F13: … - Avoided echoing past reasoning back to the model each turn. 5) Cap planning loops with a cheap heuristic (planner turns 5.2 ➝ 2.1; -1,100 tokens) - If no new entities/tools introduced in last step, force a summary/decision. - Also short-circuited when confidence and evidence count cross a threshold. 6) Don’t ask the model to spill its reasoning unless you truly need it (-300 to -900 tokens) - I kept internal reasoning implicit and requested final structured outputs only. When I needed intermediate steps, I asked for them in compact bullet fragments, not essays. 7) Response-length contracts for the final message (800 ➝ 380, -420 tokens) - Hard caps: “<= 5 bullets, <= 12 words each” or “<= 120 words”. Also set max_output_tokens appropriately so it doesn’t ramble. 8) Classify-then-think routing (-700 tokens avg) - Lightweight gate decides: trivial lookup vs deep research. About 38% of tasks took the short path. Misroutes (~2%) were corrected by a follow-up check. 9) Normalize and cache prompts/answers (hit rate 18%, -240 tokens avg) - Canonicalized queries (lowercasing, removing stopwords where safe) so identical intent hits cache. - Cached final answers for deterministic subproblems and reused them by ID in context. 10) Tighten tool-LLM protocol (-150 to -300 tokens/turn) - Function-style calls with terse argument names; tool returns machine-friendly fields. No prose in the back-and-forth. After changes (median per task) - System: 280 - Context/RAG: 900 - Tool outputs: 3,380 - Agent messages: 460 - Final answer: 380 - Total: 3,320 How I measured - Logged tokens per message and tagged by bucket. Per-run report showed top token sinks and per-turn growth. - Set per-bucket budgets: system ≤ 300, context ≤ 1,000, tool outputs ≤ 3,500, chatter ≤ 600, final ≤ 400. Any breach triggered an automatic diff report. Templates that helped in practice - System: “Roles (1 line), Tools (IDs), Output schema name, Guardrails (bulleted), Prohibitions (bulleted).” One screen only. - Planner to tool-user: “Plan step N, goal, missing info list, next minimal tool call. No restating prior context.” - Tool-user call args: tight JSON with only the fields the tool needs. - Final: “AnswerV1: {short bullets or fields}. No preamble.” Things that didn’t work (or backfired) - Compressing RAG to 128-token chunks: -300 tokens but -5.4% success; the model missed key context. - Over-aggressive summarization of tool results: dropping units/precision caused wrong calculations later. - Forcing a single mega-agent: fewer turns, but total tokens went up ~+18% due to repeated self-reminding. - Max-output-tokens too low: model spilled into a second message, costing extra tokens and a turn. - “Abbreviation hacks” (srch, src, etc.) in prompts: saved ~30 tokens total, not worth the readability hit. - Streaming/JSON minify/gzip: doesn’t change billed tokens; it only affects transport. - Few-shot examples in system: helpful, but expensive. Moved them to a separate on-demand retrieval when the model flagged uncertainty; net -600 tokens when not needed. Quick checklist you can run today - Keep system prompt ≤ 300 tokens. - Retrieve ≤ 6 snippets at ~256 tokens each; never dump whole docs. - Make every tool return only the fields you’ll use next. - Send deltas of state, not the whole memory. - Cap planning loops and require a decision when evidence stops growing. - Ask for final outputs only, with strict length/structure. - Gate easy vs hard tasks. - Cache deterministic sub-answers and reuse by ID. - Track tokens by bucket and set hard budgets per run. If you post your current per-bucket breakdown, I can point at the cheapest wins first.
The author reduced token usage for an AI agent by 2.5x by switching from Firebase to InsForge, an open-source backend platform for agentic coding, cutting tokens from 5.5M to 2.3M and eliminating manual interventions.
An engineer tracked his coding agent's token usage over a week, finding that only 0.67% of tokens were spent on actual tasks, with 99% consumed by tool directories, skill descriptions, and system prompts. He provides optimization strategies, including shell output filtering which saved 46.9% of tokens.
This thread shares strategies to reduce token usage in AI agents, including prompt caching, context summarization, using smaller models, trimming tool outputs, subagents, RAG, and tight system prompts.
Cursor AI has reduced token costs in its tool by 7% without compromising agent quality, achieved through tighter prompts, selective tool loading, better caching, and compressed file reads.
The article analyzes a 2026 paper by Bai et al. showing that subagents and context bloat cause token costs in long agent runs to be ~1000x higher than chat, and presents three practical fixes (PLAN.md, read budget, out-of-band notes) that reduce token usage by 70-90%.