Skip to content

Where Did 56.8% of Your Coding-Agent Tokens Go? Profiling 8.5M Multi-Agent Tokens

Published: 2026-11-30 · Repository: cuhawk/ctx-profiler (v0.1.0, 48/48 pytest passing) · Raw CSV: context_waste_report.csv


Claim

Across 182 turns (8,501,429 total tokens) spanning 6 realistic multi-turn coding-agent sessions (Claude Code, OpenAI Codex, Cline, OpenClaw, and generic OpenAI traces), only 2.28% (193,474 tokens) is useful new work—while 56.84% (4,832,321 tokens) is pure avoidable waste (27.47% repeated file re-reads, 21.57% oversized tool dumps, 7.81% <think> history replay). Applying deterministic compaction policies cuts total token consumption by -61.65% ($36.38 saved per 100 Sonnet-class sessions).


Method & Golden Accounting Invariant

Most token tools guess waste with heuristic multipliers. ctx-profiler enforces a MECE (mutually exclusive, collectively exhaustive) integer invariant on every turn, session, and corpus:

$$\sum_{c \in \text{6 categories}} \text{tokens}(c) \equiv \text{total_session_tokens} \quad \text{(exact to the integer token)}$$

Every turn is reconciled against provider usage counts using Largest-Remainder apportionment across 6 buckets: 1. file_rereads — unchanged file paths read repeatedly into context (including shell cat rereads; resets cleanly if a file is edited or after compaction) 2. tool_output_bloat — oversized test/grep/log dumps exceeding threshold 3. reasoning_think_replay — prior <think>...</think> or reasoning blocks re-injected into subsequent turns 4. system_and_tool_schemas — repeated system prompts & MCP tool JSON schemas 5. history_replay — prior conversational turns re-sent each step 6. useful_new_work — fresh prompts, first-time file reads, diffs, and concise assistant replies

ctx-profiler E2 Context Waste Benchmark


Measured Corpus Breakdown (ctx-profiler bench, claude-sonnet-4 cache-aware pricing)

Session Fixture Agent Turns Total Tokens File Re-Reads Tool Bloat <think> Replay Schemas Avoidable Waste % combined-all Cut
01_claude_code_fastapi_debug claude-code 32 2,438,603 1,118,883 339,691 4,180 40,224 59.98% -60.59%
02_codex_ts_refactor codex 30 1,584,583 481,915 385,012 3,853 18,390 54.95% -55.84%
03_cline_multi_file_search_edit cline 26 1,287,459 259,368 584,640 30,373 0 67.92% -67.69%
04_claude_code_mcp_heavy claude-code 30 1,078,884 132,963 133,476 0 364,590 24.70% -55.14%
05_openclaw_qwen_think_loop openclaw 36 1,193,084 139,269 0 609,549 24,913 62.76% -62.96%
06_openai_trace_monorepo openai 28 918,816 202,619 390,735 15,795 70,532 66.30% -71.97%
CORPUS_TOTAL All 5 182 8,501,429 2,335,017 (27.5%) 1,833,554 (21.6%) 663,750 (7.8%) 518,649 (6.1%) 56.84% -61.65%

Deterministic Compaction Simulator Impact

Policy Before Tokens After Tokens Reduction Est. Savings / 100 Sessions
dedup-rereads 8,501,429 6,180,914 -27.30% $15.78
cap-tool-output-2k 8,501,429 6,635,189 -21.95% $13.49
strip-think-replay 8,501,429 7,837,682 -7.81% $3.74
lazy-mcp-schemas 8,501,429 8,110,575 -4.60% $3.38
combined-all 8,501,429 3,260,075 -61.65% $36.38

One Result That Contradicted Expectation

In 04_claude_code_mcp_heavy_workspace (40 MCP tools attached), pure waste (file_rereads + tool_output_bloat + think_replay) was only 24.70%—less than half of the other sessions! Why? Because 33.79% (364,590 tokens) was consumed silently by system_and_tool_schemas, which sits outside the classic waste trio until you turn on lazy-mcp-schemas (jumping total reduction from 24.7% to -55.14%).


One-Command Reproduction

git clone https://github.com/cuhawk/ctx-profiler
cd ctx-profiler
pip install -e .
ctx-profiler analyze fixtures/
ctx-profiler simulate fixtures/ --model claude-sonnet-4
ctx-profiler bench

Try the interactive browser version in Core AI Systems Tools → Token-Budget Simulator.


What I'd Distrust in This Experiment

  1. Synthetic fixture corpus vs. your personal traces: The 6 shipped fixtures were constructed to mirror real failure modes (claude-code#16157/#38335, codex#41220, cline#4389, open-webui#23339), not scraped from private user logs. Run ctx-profiler analyze ~/.claude/projects/ on your own transcripts before quoting a percentage for your workflow.
  2. Cache-invalidation side effects of compaction: Mutating or truncating earlier turns mid-session can invalidate prompt-cache prefixes on cloud APIs (turning $0.30/1M cached reads into $3.00/1M uncached prefills). The simulator models steady-state reduced context, not transient prefix-cache misses at the moment of truncation.
  3. Quality preservation isn't automatic: Capping a failing stack trace at 2K tokens can remove the root-cause line at the bottom if you truncate naively from the tail instead of keeping head + tail. Always pair compaction with task pass@1 evals.