Title: How to Solve More Problems with Fewer Tokens in AI Coding Assistants
Summary: Many developers are surprised by how quickly AI coding assistants consume millions of tokens in a single session. This article breaks down the four types of tokens—input, cache creation, cache read, and output—and explains why cache reads dominate long sessions. It then presents ten practical, battle-tested strategies for reducing token costs, including keeping context prefixes stable, splitting sessions by task, summarizing history, feeding only relevant files, and controlling output verbosity. By making every token count, you can dramatically cut costs without sacrificing productivity.
Body:
Why Your Token Consumption Is Higher Than You Think
Many developers using AI coding assistants share a similar puzzle: they only asked a few questions, yet a single session consumed millions of tokens. This isn’t an illusion—it’s the combined result of context accumulation and how API billing works.
A Real Session in Numbers
Consider a project session that ran continuously for a full day, consuming roughly 9.7 million tokens in total. That sounds alarming, but the breakdown tells a different story:
- Direct Input: about 297K, only 0.3% of the total
- Cache Creation: about 790K, 8.1%
- Cache Read: about 8.91M, 91.7%
- Output: about 15K, 0.15%
Over 90% of the tokens were cache reads. In the billing systems of major LLM APIs, cache reads typically cost about one-tenth of standard input, while cache creation runs about 1.25x the standard input price. This means “total tokens” is not the same as “total cost”—in long sessions, repeated context hits land on the cache, so the actual expense can be far lower than the raw number suggests.
Four Token Types, Wildly Different Costs
Understanding the four token types is the foundation of every optimization strategy:
- Input Tokens: The context actually sent to the model with each request, billed at the standard rate—the most expensive regular category.
- Cache Creation Tokens: When the context exceeds a certain threshold (e.g., 1,024 tokens), the system caches the prefix on the server for reuse. Writing to the cache costs slightly more than standard input because it involves storage.
- Cache Read Tokens: Billed when subsequent requests hit a cached prefix, at roughly one-tenth of the standard input price. This is the underlying reason longer sessions can be more cost-efficient.
- Output Tokens: The model’s generated response, usually priced at 3–5x the input rate. Fortunately, output makes up a tiny share in coding workflows.
The core rule: the higher the cache read ratio, the better value you’re getting. So the goal of optimization isn’t simply reducing total tokens—it’s improving cache hit rates and avoiding cache invalidation.
Ten Battle-Tested Token Optimization Strategies
1. Keep the Context Prefix Stable to Avoid Cache Invalidation
Caching works on prefix matching. If you edit earlier messages mid-conversation, reorder the system prompt, or change the tool list, the prefix changes and the entire cache is invalidated—forcing a full re-creation billed at 1.25x. There are documented cases where a single cache expiration wiped out 23% of a budget instantly. To prevent this:
- Put fixed content like the system prompt and tool definitions at the very front of the context;
- Avoid frequently changing configuration mid-session;
- When switching tasks, start a new session rather than modifying historical context.
2. Divide and Conquer: Split Sessions by Task
Mixing unrelated tasks into one long session forces irrelevant context into every request. Splitting sessions by module or file scope, keeping only the necessary background in each, dramatically shrinks the token base.
3. Replace Full History with Summaries
Midway through a long session, ask the model to produce a concise summary of the work completed—decisions, interface agreements, open issues—then start a new session based on that summary. This is essentially “manual context compression”: tens of thousands of tokens of history condensed into a few hundred tokens of state.
4. Feed Precise Files, Not the Whole Codebase
Don’t let the assistant read entire directories by default. Use search, outlines, or file listings to locate relevant files first, then bring only a small number into context. For large files, reference only the relevant functions or sections. Many tools support syntax for “mentioning specific files”—use it.
5. Ask with Retrieved References, Not Vague Requests
When asking questions, provide exact function names, class names, line numbers, or error stacks instead of asking the model to “look around”. A retrieval-style question minimizes exploration cost and prevents the model from repeatedly reading irrelevant files.
6. Control Output: Ask for Concise, Structured Responses
Output tokens carry the highest unit price. Instructing the model to “only output the modified function”, “use diff format”, or “don’t repeat unchanged code” significantly cuts output costs—and also reduces the context burden on subsequent turns.
7. Clean Up Dead Context Promptly
Failed attempts, outdated error messages, and rejected approaches all linger in the context and keep getting billed. Periodically clean up, or start a fresh session keeping only the valid state.
8. Verify in Small Steps, Avoid Big Rewrites
Having the model generate a large batch of code before verifying is risky: if the direction is wrong, the rewrite is expensive. Use small increments and frequent verification so the model iterates in the right direction, reducing token waste from rework.
9. Check Usage Reports Before Critical Turns
Make a habit of reviewing usage analytics, watching the cache read ratio and spikes in cache creation. Frequent spikes in cache creation mean the prefix keeps invalidating—investigate your session structure. Ongoing monitoring turns optimization into a data-driven habit.
10. Give Complete Requirements Up Front
Drip-feeding requirements forces the model to repeatedly overturn existing implementations. Stating constraints, tech stack, and goals clearly in the first turn reduces back-and-forth clarification and helps the cache prefix stabilize earlier.
A Practical Example: Structured Context Management
# Session opening template## Project background- Tech stack: Python 3.11 + FastAPI + PostgreSQL- Goal: add async export functionality to the order module## Relevant files (only the essentials)- app/services/order_export.py (current implementation)- app/models/order.py (data models)## Constraints- Output only the modified functions, in diff style- No new dependencies
A fixed opening block plus clearly scoped task descriptions helps stabilize prefix caching, while limiting output scope compresses the most expensive category: output tokens.
Conclusion
Token optimization isn’t about “saying less”—it’s about routing every token down the cheapest, most effective path: a stable prefix buys cache hits, precise context buys effective reasoning, and restrained output buys low cost. If you’re just getting started, begin with three things—split sessions, feed precise files, and control output. That alone can cut costs by an order of magnitude while solving just as many problems.
