5 Hidden Fees in AI Coding: Context Caching Misses, Retries, Tool Calls, and More
By Eric Bush · July 8, 2026 · 9 min read
The Gap Between Expected and Actual Cost
You did the math: your coding tasks use roughly 50K input tokens and 5K output tokens per request. At Claude Sonnet 4.6 rates ($3/$15 per million tokens), that should cost about $0.225 per request. But your actual bill is 30–60% higher. Where is the extra money going?
The answer lies in five hidden costs that do not appear in simple pricing calculators. These are not bugs or scams — they are real token consumption that happens beneath the surface of your AI coding workflow. Understanding them is the first step to controlling them.
Hidden Fee #1: Context Cache Misses
Anthropic's prompt caching offers a 90% discount on cached input tokens — reading cached content costs $0.30 per million tokens instead of $3.00 for Sonnet 4.6. But this only works when the cache hits. Every cache miss means paying full price, and caches are more fragile than most developers realize.
What causes cache misses: Any change to the cached prefix invalidates it. Adding a single character to your system prompt, changing tool definitions, or altering the order of messages — all break the cache. The minimum cacheable block is 1,024 tokens for Claude (2,048 for Haiku), and the cache has a 5-minute TTL for ephemeral caches.
The cost impact: Suppose you have a 30K-token system prompt (common with large codebases). Cached reads cost $0.009. Uncached reads cost $0.09 — 10x more. If your cache hit rate drops from 90% to 60% due to prompt instability, that 30K system prompt alone costs an extra $0.027 per request. Across 200 daily requests, that is $5.40/day or $162/month in unnecessary spend.
The fix: Keep your system prompt and tool definitions stable. Place static content (codebase context, documentation) at the beginning of the prompt where it can be cached. Only append dynamic content (user messages, recent edits) at the end. Monitor cache hit rates in your API logs.
Hidden Fee #2: Retry and Recovery Loops
AI coding agents like Claude Code run in loops: generate code, run tests, see failures, try again. This is the intended workflow. But when a task is fundamentally too hard for the model, or the instructions are ambiguous, the agent can retry 5–15 times before giving up — each attempt burning the full context.
The cost impact: A single agentic task with 80K input context that retries 8 times consumes 640K input tokens and potentially 40K+ output tokens. At Sonnet 4.6 rates, that is $1.92 in input and $0.60 in output — $2.52 for a single failed task. If this happens twice per day, it adds $150/month to your bill.
The fix: Set maximum retry limits in your agent configuration. Claude Code allows configuring iteration caps. A good default is 5 retries — if the model has not solved it in 5 attempts, it likely needs human guidance rather than more tokens. Also implement early termination: if the same error repeats 3 times consecutively, stop and report rather than continuing to spend.
Hidden Fee #3: Tool Call Token Overhead
When you define tools (functions) for an AI model to call, the tool definitions themselves consume input tokens on every request. A typical coding agent might have 10–20 tool definitions (read file, write file, run command, search, etc.), each consuming 200–500 tokens in the system prompt.
The cost impact: 15 tool definitions averaging 350 tokens each = 5,250 tokens of overhead per request. At Sonnet 4.6 rates, that is $0.016 per request — trivial in isolation, but across 200 daily requests it costs $3.15/day or $94/month. And each tool call response adds output tokens for the function invocation format (JSON structure, parameter formatting).
The compounding effect: In multi-turn conversations, tool definitions are resent with every message. A 20-turn debugging session resends those 5,250 tool-definition tokens 20 times. If you use prompt caching effectively, this is mitigated. If not, it multiplies.
The fix: Keep tool definitions concise. Remove tools that are not needed for the current session. Use dynamic tool loading — only include tools relevant to the current task type. And ensure tool definitions are in the cached prefix of your prompt.
Hidden Fee #4: System Prompt Bloat
System prompts in AI coding tools have grown significantly. Claude Code's default system prompt plus your project context (CLAUDE.md, file contents, conversation history) can easily reach 20,000–50,000 input tokens per request.
The cost impact: A 40K-token system prompt at Claude Fable 5 rates ($10/M input) costs $0.40 per request just for context — before any actual code content is sent. At 50 requests per day, that is $20/day or $600/month purely in system prompt costs. Even at Sonnet rates ($3/M), it is $0.12 per request or $180/month.
Common causes: Including entire file contents in system prompts when only relevant sections are needed. Appending full conversation history without summarization. Loading all project documentation regardless of the current task. Each addition seems small but compounds quickly.
The fix: Audit what is in your system prompt. Use targeted context retrieval — only include files relevant to the current task. Summarize or truncate conversation history after 10–15 turns. Leverage prompt caching so at least the static portions are billed at reduced rates.
Hidden Fee #5: Output Padding and Formatting
AI models do not just output code — they output explanations, markdown formatting, code fence markers, file path annotations, and conversational text around the actual code. In a typical Claude Code response, 30–50% of output tokens are formatting and explanation, not code.
The cost impact: If you need 100 lines of code (~1,500 tokens), the model might generate 3,000–4,000 total output tokens including explanations. At Sonnet output rates ($15/M), that is $0.045–$0.060 instead of the expected $0.023. Across hundreds of requests, the padding doubles your output token spend.
The fix: Use structured output modes when available. Include instructions like "respond with code only, no explanations" for straightforward generation tasks. For Claude Code in agentic mode, the tool-call format is already relatively efficient — but for raw API usage, controlling output verbosity significantly reduces costs.
Quantifying Your Hidden Cost Exposure
For a typical team of 5 developers using AI coding tools daily, here is the estimated monthly hidden cost from each source:
Cache misses (70% hit rate instead of 95%): $80–$160/month extra. Retry loops (2 runaway tasks per developer per week): $100–$200/month extra. Tool call overhead (uncached): $50–$90/month extra. System prompt bloat (40K+ context): $100–$180/month extra. Output padding: $40–$80/month extra.
Total hidden costs: $370–$710/month for a 5-person team. That can represent 30–50% of the total AI bill. Addressing even half of these sources saves $185–$355/month.
Action Plan: Reduce Hidden Costs in One Day
Prioritize by impact: 1. Enable and stabilize prompt caching (biggest single saving). 2. Set retry limits to 5 iterations maximum. 3. Audit system prompt size — cut to under 20K tokens where possible. 4. Move static tool definitions into the cached prefix. 5. Request concise output for routine code generation tasks.
Monitor your API usage dashboard weekly. Look at cache hit rates, average tokens per request (input and output), and failed/retried request counts. These metrics surface hidden costs before they compound into surprise bills.
Want to calculate exact costs for your project?
Frequently Asked Questions
How do I check my prompt cache hit rate?
Anthropic's API response headers include cache hit/miss information. In your usage dashboard, look for 'cache_creation_input_tokens' vs 'cache_read_input_tokens'. A healthy hit rate is above 85%. Below 70% means your cache is being invalidated too frequently.
Do tool calls cost more than regular messages?
Tool definitions add input tokens (sent with every request), and tool call outputs add structured output tokens. The per-token price is the same, but the overhead from definitions and formatting means tool-heavy workflows cost 10-20% more than equivalent plain-text interactions.
How can I tell if retry loops are inflating my bill?
Check your API logs for sequences of requests with the same task context but different outputs. Many agent frameworks log iteration counts. If you see tasks with 8+ iterations regularly, those are retry loops. Multiply the per-request cost by iteration count to see true task cost.
Is there a way to reduce system prompt size without losing context?
Yes. Use retrieval-augmented approaches: only load file contents relevant to the current task, summarize long conversation histories, and use reference links instead of inlining entire documents. Most system prompts can be reduced 40-60% without quality loss by removing redundant or irrelevant context.
Which hidden cost should I fix first?
Start with prompt caching stability — it has the highest impact and is purely a configuration fix. Then set retry limits (prevents worst-case scenarios). System prompt reduction and tool optimization require more effort but provide sustained savings.
Related Articles
AI Model Context Protocol (MCP): Hidden Token Costs of Tool Calls
MCP enables AI coding agents to call external tools, but each tool adds thousands of tokens to every request. We quantify the overhead and show how to minimize hidden costs from tool descriptions, function formatting, and response parsing.
Prompt Caching vs Context Compression: Which Saves More on Long Coding Sessions
Two strategies dominate AI coding cost reduction: prompt caching and context compression. We compare how each works, when to use them, and which delivers better savings for different coding workflows.
Better Models, Worse Tools: The Hidden Retry Cost of Agent Tool Schema Failures
Armin Ronacher observed newer Claude models emitting extra tool schema fields under some schemas. The key cost lesson is not universal model failure, but retry, validation, and schema-design overhead in AI coding agents.