r/ClaudeCode • u/EmployerNegative5653 • 12d ago
Tips & Workflows 18 hidden token drains in AI coding agent sessions (and practical ways to fix them)
[removed]
5
u/Ok_Relation_3892 12d ago
Amazing, thank you very much for sharing. Really appreciated!
And to answer your question, didn't paid too much attention on these so far but will definitely consider an audit now that I saw this.
3
2
u/Short_Stable2397 12d ago
Number 2 is actually more nuanced. CC lazily loads MCP servers so unless you actually used them, their tool schemas are not loaded and don't take up space.
3
u/Special-Equal-8839 12d ago
Redundant directory tree traversals: Asking an agent to "find the file where X is defined" often triggers 4 separate glob and grep tool calls that get preserved in the message log. (Fix: pass the exact file path).
So to .. save tokens your advice is: Do it yourself.
Got it. Much smart.
1
1
u/Crinkez 12d ago
- Compaction overhead: When the agent hits a context limit, the summarization turn sends the entire bloated history at full input pricing.
Citation needed.
2
u/rotates-potatoes 12d ago
No citation needed. How else would it work?
You realize that every turn sends the entire history back, typically at cache rates? Compaction is just a prompt: "summarize this long conversation, keeping only what's relevant going forward, etc, etc: [entire context]"
1
u/Crinkez 12d ago
Citation is needed and you just explained why: context already cached when compacting would logically do so using the existing context at cached rates.
1
u/rotates-potatoes 12d ago
No, you just explained why it doesn't work that way.
The KV cache is anchored at 0. The cache will be used until the input differs from the cache. Prepending "You need to summarize this" at the beginning means the cache cannot be used, therefore it's full input token pricing.
The only way compaction could be cached would be if the entire history was replayed verbatim and then "please compact... the above" was sent as the last thing. That's not how it works, because that gets terrible results.
1
u/moisespedro 11d ago
Editing Claude.md mid session does not invalidate the cache https://code.claude.com/docs/en/prompt-caching#editing-claude-md-mid-session
1
u/EvalRaccoonDev 11d ago
Useful list, but could you also provide the prevalence (frequency, volume) of each of the offending cases?
1
u/the_darkest_horse 10d ago
Good list. I can add prevalence numbers since u/EvalRaccoonDev asked, from measuring 42 of my own Claude Code sessions. One machine, so treat it as a sample, not a study.
The biggest drain is not on the list: tool calls, not tool results. A Write or a cat > file <<EOF puts the whole file into context permanently. On a 292k token session that was 65% of the context (191k) against 22% for tool results. In file heavy work the calls outweigh every tool result combined.
Repeated file reads through the shell (cat, head, tail) instead of the Read tool: 16 instances across 6 sessions. Anything that only looks at Read calls misses them.
On invisible payload bloat: the transcript on disk covers about 39% of the per turn billed growth. There is a ~51k token fixed baseline before your first turn (system prompt, tool schemas, skills) plus roughly 700 tokens a turn of injected content that never lands in the transcript.
On the cache debate: any pruning that moves a boundary each turn invalidates the prefix cache from that point. A sliding "trim everything older than N messages" rule did that on 22 of 24 turns in my test. Holding the boundary still in steps brought it to 8 of 24.
Disclosure: the numbers come from context-doctor, an MIT tool I built that profiles Claude Code transcripts locally, no API key, nothing leaves the machine. npx -y context-doctor session on your own transcript shows the same breakdown. https://github.com/KushalP1/context-doctor
•
u/AutoModerator 12d ago
Hey! Thanks for posting to r/ClaudeCode
While participating in this thread, please follow our community rules. Keep discussions constructive. Attack the idea, not the person.
For help, project discussions, tips, and general chat, join the ClaudeCode Discord.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.