Troubleshooting Abnormal Token Usage in AI Coding Tools
This article explains why token consumption in AI coding tools can spike even when usage habits stay the same, and how to investigate it. It abstracts the issue into three common causes in large-context agent systems: repeated auto-compaction that frees little space, cache misses triggered by rewritten prefixes, and the multiplier effect of multi-agent scheduling. It then outlines the observability metrics needed to separate real work from rising cost, followed by governance strategies such as compaction benefit checks, stable prefixes, global budgets, tiered memory, and alerting. The goal is to bring the usage curve back in line with actual workload.
1. Symptom: Why the Usage Curve Suddenly Rises
Many developers using AI coding tools have reported a clear jump in model call consumption without changing their habits: a scenario that used to account for about 20% of total usage climbed to nearly half within a short period, and a quota that previously supported hours of work is now exhausted in about twenty minutes. The common pattern is “behavior unchanged, consumption curve changed.” This means the investigation should focus not on the user side, but on runtime context management and scheduling mechanisms.
Note that this article does not evaluate or attribute the issue to any specific product. Instead, it frames the problem as “resource consumption anomalies in large-context agent systems,” which are common across mainstream AI coding solutions.
2. An Abstract Model of Three Main Causes
2.1 Repeated Triggering of Automatic Context Compaction
When the session context approaches the window limit (for example, around 240K tokens), the system triggers automatic compaction, summarizing historical content to free up space. The anomalies are:
- The token reduction is limited: roughly 240K–250K before compaction and still 140K–160K after;
- The main agent compacts more than ten times a day, and multiple sub-agents each compact more than ten times, for a total of dozens of compactions;
- After consecutive compactions, the input size of the next normal request does not drop significantly.
This indicates a mismatch among the compaction threshold, the target ratio, and the release strategy: compaction is triggered frequently but does not free up proportional space, creating a “compact—still over limit—compact again” loop.
A schematic decision logic can be written as:
def should_compress(ctx_tokens, threshold=244_800, target_ratio=0.5):if ctx_tokens < threshold:return False# After compaction, usage must fall below the target level; otherwise this compaction yields no real benefitreturn ctx_tokens * target_ratio < threshold
Without a benefit check like target_ratio, ineffective compaction will occur.
2.2 Secondary Amplification from Lower Cache Hit Rates
In normal multi-turn conversations, the cache can be reused when the prefix is stable. Once compaction runs, the historical prefix is rewritten, the existing cache can no longer be hit, and the summary calls produced by compaction stack with the “cold start” of subsequent requests, amplifying cost a second time. The symptom: the cache hit rate rises abnormally after compaction (indicating the same rewritten content is being read repeatedly), yet overall billed input tokens do not decrease.
2.3 The Multiplier Effect of Multi-Agent Scheduling
When the main agent and multiple sub-agents work in parallel, context compaction is triggered independently by each. Without a global budget and a shared context view, the number of compactions scales roughly linearly with the number of sub-agents, producing structural waste of “saving at one point, overspending globally.”
3. Observability: Make Consumption Explainable First
It is recommended to collect at least the following metrics, aggregated at minute-level granularity:
- Input/output token counts, cache-hit token counts, and cache-miss token counts;
- Number of automatic compaction triggers, token counts before and after each compaction, and compaction duration;
- Cumulative requests per session, sub-agent concurrency, and token share by task type;
- Token cost per unit of effective output (such as one successful code change).
With these metrics, you can distinguish “actually doing more work” from “doing the same work at a higher cost.”
4. Governance Strategies
- Compaction benefit check: estimate the post-compaction level before compacting; if no space is actually freed, switch to “segmented archiving + summary index” instead of a full rewrite.
- Prefix stability protection: place system prompts, tool definitions, and project conventions in a stable prefix area so they are not affected by compaction, improving cache reuse.
- Global budget control: set token budgets and compaction quotas for the session and sub-agents; when approaching the threshold, reduce concurrency first rather than compacting repeatedly.
- Tiered memory: keep hot context within the window, move warm data to object storage, and send cold data to a log service for retrieval on demand.
- Alerting and circuit breaking: set a threshold for “cost per unit of output,” and automatically downgrade to a lightweight model or disable some sub-agents when anomalies occur.
5. Conclusion
Abnormal call volume is most likely not a single defect, but the combined result of compaction strategy, caching mechanisms, and multi-agent scheduling. Developers should first build a fine-grained observability system, then apply the trio of “compaction benefit check + stable prefix + global budget” to bring the consumption curve back to a level that matches the real workload. If needed, enterprise teams can build the observability and tiered-memory pipeline described above using Baidu AI Cloud’s general monitoring and alerting, log service, and object storage capabilities.
