How to Spend Tokens Wisely in the Age of AI Coding
A practical Token cost governance guide for AI coding workflows. Based on a real session review, it explains the four Token types (direct input, cache write, cache read, output), how to measure usage by task, tool, and context, and ten actionable strategies to improve cache hit rates, protect stable prefixes, and cut rework costs.
Introduction: A Usage Bill That Made a Developer Uncomfortable
After a full day of iterative engineering work, the terminal closed and the usage report opened: a single session had consumed roughly 9.7 million Tokens. Cache reads accounted for 91.7%, cache writes for 8.1%, and direct input plus model output combined for less than 0.2%.
The first reaction was shock. But after breaking down the details, the conclusion reversed: the direct input actually billed at standard rates was only about 29,700 Tokens, while the vast majority was cache reads priced at roughly one tenth of direct input. In other words, “burning through ten million Tokens” and the “actual bill” are not in a linear relationship.
What deserves attention is not the total volume but the structure: which contexts are being moved back and forth repeatedly, which tool outputs are being amplified without reason, and which caches are invalidated in full by a single unrelated change. This article abstracts that review into a reusable Token governance method.
1. First, Understand the Four “Identities” of Tokens
Before discussing optimization, you need a cost model. Mainstream large model APIs typically divide Tokens into four categories, each with completely different pricing and performance implications:
- Direct input Tokens: the context actually sent to the model in this request, at standard pricing and the most expensive.
- Cache write Tokens: when the context exceeds the platform’s minimum cache length (commonly on the order of 1024 Tokens), the system can persist it as reusable cache; writes are typically billed at about 1.25x direct input.
- Cache read Tokens: reusing existing context after a cache hit, priced at about one tenth of direct input.
- Output Tokens: content generated by the model, usually several times the unit price of input, but a very small share in coding tasks.
This yields the first core rule: the higher the share of cache reads, the lower the marginal cost per unit of information. Conversely, if the bill shows frequent cache writes and few cache reads, the context is being repeatedly “rebuilt” rather than “reused”, which is the real waste signal.
A rough health indicator:
Cache hit rate ≈ cache reads / (cache reads + cache writes + direct input)A hit rate consistently below 50% usually means unstable context or sessions that are reset frequently.
2. Attribute Consumption to Tasks: Measure First, Then Optimize
Optimization without attribution is guesswork. It is recommended to slice session logs and usage data along three dimensions:
- Task dimension: how much requirement clarification, code retrieval, solution generation, edit implementation, and test verification each consume.
- Tool dimension: how much context file reads, repository-wide search, command execution, and test runs each contribute.
- Context dimension: what share system instructions, project conventions, conversation history, and tool returns each take.
A common imbalance in practice: repository-wide search and whole-file reads consume most of the input, while the portion actually used for generation and verification is less than ten percent. Once measurement is done, the optimization goal is clear: cut the moving, not the thinking.
3. Ten Actionable Token Optimization Strategies
Strategy 1: Provide Only Necessary Context
Replace “throw the entire repository at the model” with “provide the entry file, relevant modules, interface definitions, and acceptance criteria”. The more precise the context, the more stable the cache and the higher the hit rate.
Strategy 2: Search First, Edit Later
Use keyword search, symbol location, and call relationship analysis to determine the scope of changes, then read the smallest fragment. Avoid “read ten files first and then decide which one to change”.
Strategy 3: Small Edits, Prompt Verification
Change one semantic unit at a time and verify immediately. Large changes let errors accumulate; on rollback the context is replayed repeatedly, and cost rises quadratically.
Strategy 4: Constrain Tool Output
Limit the line range read per call, the number of search results, and the tail lines of logs. Example:
Read range: lines 120-180 of the fileSearch limit: 20 matches, return only file path and line numberLogs: last 100 lines only + error keyword filtering
Strategy 5: Reuse Project Documentation and Conventions
Distill the tech stack, directory conventions, and commit and test conventions into a stable project documentation file that stays in context long term. A stable prefix is the prerequisite for cache hits.
Strategy 6: Protect the Cache from “Pollution”
Strip volatile content (current time, temporary paths, random IDs) out of the stable prefix and place it at the end of the session. A single unrelated change that alters the prefix can instantly invalidate tens of thousands of Tokens of cache, which is the main cause of “quota evaporation” incidents.
Strategy 7: Define Acceptance Criteria Clearly
Write the “definition of done” in the requirement description to reduce back-and-forth probing. Rework is the most expensive form of Token consumption.
Strategy 8: Split Requirements by Priority
Break a large requirement into a sequence that can be verified independently, closing each item one by one. Avoid spreading work in parallel, which causes contexts to interfere with each other.
Strategy 9: Separate and Close Sessions Promptly
Use independent sessions for different tasks and end them when the task is done; do not let a long session become an unbounded pool of accumulated context.
Strategy 10: Build a Usage Dashboard and Alerts
Set thresholds separately for the four categories: input, cache writes, cache reads, and output, and trigger monitoring alerts when anomalies occur, turning “reviewing the bill after the fact” into “intervening while it happens”.
4. A Full Review of a Cache Invalidation Incident
In one iteration, a developer inserted temporary debugging information at the start of a session, changing the stable prefix. All subsequent requests over dozens of rounds failed to hit the existing cache; cache writes surged and cache reads plummeted, and a single operation caused roughly a 23% jump in the day’s available quota.
The root cause was not the model but the context ordering design. The fix is simple: stable content first, volatile content last, and temporary information never in the prefix. After the adjustment, the cache hit rate recovered and billing for the same workload dropped significantly.
5. Turn Optimization into Team Standards
Individual techniques only compound when they become process. Teams are advised to standardize three things: a context organization template (stable prefix + task context + acceptance criteria); a tool call budget (limits on single reads and searches); and a usage review mechanism (attribute once per week and identify the three most expensive operation types).
If using a cloud development environment, usage archiving and trend analysis can be combined with log services, monitoring alerts, and object storage to form a sustainable cost governance loop.
Conclusion
The essence of Token optimization is not “use less” but “use it right”: replace repository-wide moving with accurate context, trade a stable prefix for a high cache hit rate, and replace repeated trial and error with clear acceptance criteria. Only when cache reads become the main body of the bill and the rework rate keeps falling will efficiency and cost improve at the same time.
