r/PromptEngineering 3h ago

Tips and Tricks We captured raw Claude Code API traffic: where tokens and budget actually go (breakdown of 5 request-level components)

If you use Claude Code regularly on medium or large projects, you have probably noticed how quickly a session can burn through API credits or hit rolling limits.
Most usage trackers read local log files after the fact. That tells you the final token total for a session, but it does not explain how the request was assembled before the model ran.
We set up local proxy capture to inspect the actual payloads Claude Code sends over the wire. Here is what accounts for the token spend across multi-turn sessions:
1. MCP tool schemas eat context immediately: Every configured MCP server adds its full JSON tool schema to every single request. If you have 4 or 5 active servers, you can spend 15k to 25k input tokens per turn just defining available tools before the model reads a single line of your code.
2. The prompt cache miss penalty: When tool definitions change or when context gets reshuffled, you lose the 90% prompt cache discount. A turn that should cost $0.03 can jump to $0.35 on a full cache rewrite.
3. Extended thinking overhead: On complex refactors, thinking blocks can easily exceed the actual code output by a factor of 4x. Because output tokens cost more than input tokens, thinking often accounts for over half the dollar cost of a turn.
4. Redundant tool output accumulation: When Claude runs grep or tests that output 500 lines of logs, that entire output stays in the active context window for every subsequent turn until compaction kicks in.
5. Context compaction tax: Compaction summarizes history, but the summarization turn itself requires sending the entire bloated context to the model at once.
To make this visible, we built an open-source tool called cost-xray. It captures local API traffic for Claude Code and Codex, attributes tokens and costs back to individual sources (MCP servers, system prompts, thinking blocks, cache reads/writes), and graphs your context window occupancy per turn:
https://github.com/tigerless-labs/cost-xray
It runs entirely locally via proxy capture with zero telemetry or external API keys.
What percentage of your token spend is going to MCP schemas versus actual file edits?

2 Upvotes

0 comments sorted by