Investigating Abnormal Token Consumption After LLM Context Compression
This article walks through a practical troubleshooting approach for unexpected token usage spikes that appear after context compression in LLM-based coding tools. It explains how to turn vague impressions into measurable metrics, then examines three root-cause hypotheses: compression that fails to actually free space, hidden costs from invalidated cache prefixes, and amplification effects from multi-agent parallelism. A final checklist of actionable optimizations and a clear diagnostic path help developers locate and contain unexplained quota drain.
1. Symptom: Why the Consumption Curve Suddenly Rises
Recently, many developers using AI coding tools have reported a noticeable jump in their account’s token consumption share within a few days of a version update: a scenario that normally accounts for about 20% of daily usage has climbed to around 50% in a short time. Quota that used to support several hours of interaction is now exhausted in 20 to 30 minutes, even though the actual coding workload and number of conversation turns have not increased proportionally.
A more representative set of data comes from a heavy user: within a single day, automatic context compression was triggered more than 70 times, with 17 compressions in the main agent and 21, 17, and 17 compressions in three sub-agents respectively. Before each compression, the context size was about 240,000 to 250,000 tokens; after compression, 140,000 to 160,000 tokens remained. After compression completed, the cache hit rate was abnormally high, whereas in normal multi-turn conversations the original cache usually cannot be reused directly after compression.
The core contradiction in this phenomenon is: the user’s perceived workload has not changed, but the bill has. To determine whether this is a change in model-side behavior, a client-side strategy defect, or a design side effect of the compression mechanism itself, you must first turn “perception” into something “measurable.”
2. Quantify First: Turn Vague Feelings into Metrics
The first step in troubleshooting is to establish a baseline and avoid drawing conclusions from impressions. It is recommended to collect at least the following five categories of metrics, aggregated by hour:
- Count the four token categories per request separately: input, output, cache hit, and cache write;
- Measure the total token cost per unit of task (one requirement delivery), rather than the cost per request;
- Track the number of compression triggers, the context size at trigger time, and the retained size after compression;
- Track the cache hit rate and the number of cache invalidations;
- Track the number of sub-agents, their parallelism, and their respective context lengths.
The following is illustrative local statistics logic (pseudocode, expressing the idea only):
# Aggregate by task dimension, not by request dimensiontask_cost = {"input": 0, "output": 0,"cache_hit": 0, "cache_write": 0,"compactions": 0}def on_request(req, resp):task_cost["input"] += resp.usage.input_tokenstask_cost["output"] += resp.usage.output_tokenstask_cost["cache_hit"] += resp.usage.cached_tokenstask_cost["cache_write"]+= resp.usage.cache_creation_tokensdef on_compaction(before, after):task_cost["compactions"] += 1log.info("compact", before=before, after=after,retained_ratio=after / before)
The key criterion is: if the cost per unit of task rises while task complexity stays the same, the problem most likely lies in the context management pipeline, not in the model “becoming more expensive.”
3. Root-Cause Hypothesis 1: Compression Does Not Actually Free Space
The most alarming point in the original feedback is that “compression barely frees any space.” On the user side, the observed automatic compression threshold is about 240,000 tokens, and after three consecutive compressions the input size of the next normal request remains high.
In engineering terms, this usually corresponds to several kinds of implementation defects:
- The compression result does not replace the original history; it merely appends a summary message, while the historical messages remain fully retained in the context;
- The summary coexists with the original text, creating a double occupancy of “summary + full text”;
- Multiple agents each hold a full copy; after the main agent compresses, the sub-agents still carry the complete pre-compression context;
- Tool calls and file-reading results are not included in the compression scope, so large blocks of code and logs are carried repeatedly.
You can use the following invariant for validation: after compression, the context length must be significantly smaller than the compression threshold, and should show a sawtooth decline as turns progress, rather than a monotonic increase.
def assert_compaction_effective(before, after, threshold):# After compression, enough headroom must remain; otherwise treat it as ineffective compressionassert after < threshold * 0.6, "compression did not free space"assert after < before * 0.7, "insufficient compression benefit"
4. Root-Cause Hypothesis 2: Hidden Costs from Invalidated Cache Prefixes
Context compression rewrites the message sequence, while prefix caching depends on the prefix being exactly identical. Once compression occurs in the middle of a session, everything after the compression point becomes a “new prefix” that must be written to the cache again; this cache-write cost is usually significantly higher than ordinary input and cannot be reused in the short term.
At the same time, if the compression strategy tweaks the summary wording, timestamps, or ordering on every turn, it causes the prefix to keep fluctuating, so the cache is always in a “just written, immediately invalidated” state. The manifestation is: the proportion of cache-related tokens soars, while the effective hit rate actually drops.
Governance directions include: move volatile content (time, counts, random identifiers) out of the prefix; fix stable system constraints and project conventions at the very front of the context; avoid triggering compression multiple times within a single turn; and adopt a “minimum necessary context” rather than full inheritance for sub-agents.
5. Root-Cause Hypothesis 3: Multi-Agent Parallelism Amplification Effect
The main agent compressed 17 times and the three sub-agents each compressed more than ten times, which shows that parallelism amplifies context maintenance costs. If sub-agents inherit the main agent’s full history, then every compression on the main side triggers a chain of compressions on the sub-agent side, and the cost grows multiplicatively.
It is recommended to set a hard context budget for sub-agents, pass only the minimum slice needed for the task, and immediately reclaim their context after the task ends, avoiding long-term residency.
6. An Actionable Optimization Checklist
- Set a context watermark and proactively tidy up before reaching the threshold, rather than waiting for forced compression;
- Adopt a two-layer design of “structured summary + original-text archiving” for compression, where archived content does not enter subsequent requests;
- Apply length truncation and summarization to tool outputs, and retrieve code snippets on demand rather than carrying them in full;
- Fix the prefix structure to reduce cache jitter;
- Build a per-task cost dashboard and alerting, with automatic notifications on abnormal fluctuations;
- Validate with a canary release: first compare the per-task cost on a small number of tasks in the new version, then roll out fully.
7. Conclusion
Overall, the abnormal consumption is more likely a combined effect of the context compression and caching mechanisms under a specific load, rather than a single “defect switch.” The diagnostic path is clear: first quantify the per-task cost, then verify whether compression actually frees space, and finally check prefix stability and the multi-agent context inheritance strategy. By solidly implementing these three layers, most problems of “quota mysteriously evaporating” can be located and contained.
