Why Long AI Coding Sessions Get Expensive
- Authors

- Name
- Nino
- Occupation
- Senior Tech Editor
Type "rename this variable" as the 8th message of a Claude Code session. Now type it again as the 80th. Same words, same answer, and with typical sample numbers, the second one costs about three times as much. Turn prompt caching off and that multiplier jumps to six.
Nothing is broken. A long session walks into a quadratic curve twice: once inside the model, where every token looks at every earlier token, and once on your bill, where every turn ships the whole conversation again. Caching makes the repeat cheaper, but it does not change the mathematical shape of the underlying growth.
The Mechanics of Quadratic Attention
Claude Code and similar developer tools make stateless API calls behind the scenes. The model does not remember anything between requests, meaning the CLI tool must re-send the full context history. Furthermore, a single user message is rarely just one request. Every tool call sends its result back as a new request carrying everything that came before it.
Inside the model, each token generates three vectors: a query (what it is looking for), a key (what it offers), and a value (what it hands over). A token compares its query with every key, a softmax function turns the scores into weights, and the output is the weighted mix of values:
Attention(Q, K, V) = softmax(Q·Kᵀ / √d_k) · V
The matrix multiplication Q·Kᵀ evaluates every query against every key. For tokens, this creates an grid. Language models only look backwards, which trims the grid to a triangle of roughly cells. That remains strictly quadratic. Double the token count, and you incur four times the work.
Even with optimizations like FlashAttention—which fixes memory overhead by avoiding the storage of the full attention matrix in high-speed SRAM—the time complexity remains quadratic because every cell must still be computed.
Token Count & API Overhead Scaling:
Turn 1: [S + a] -> Cost unit: 1
Turn 2: [S + a] + [S + 2a] -> Cost unit: 3
Turn 3: [S + a] + [S + 2a] + [S + 3a]-> Cost unit: 6
...
Turn K: Cumulative sum scales at O(K²)
Why Your Bill Grows Quadric-Fold
None of this shows up in published per-token pricing matrices directly. Providers often state that a 900k-token request is billed at the same per-token rate as a 9k-token request. So what drives the quadratic explosion on your monthly bill?
Consider a session starting with tokens (system prompt, base instructions, developer tools, configuration files) and each turn adding roughly tokens. Turn sends roughly S + k·a, meaning turns accumulate to approximately:
S·K + a·K² / 2
We encounter the same triangle shape again. Every turn forms a new row, and every row re-sends everything above it.
The Economics of Prompt Caching
When the start of a request exactly matches a prefix processed recently, modern inference endpoints—and managed gateways like n1n.ai—skip the recompute phase and bill that cached portion at a heavily discounted rate.
For instance, consider typical pricing for advanced models like Claude 3.5 Sonnet:
- Standard Input: ~$2.00 per million tokens
- Cache Write: ~$2.50 per million tokens
- Cache Read: ~$0.10 per million tokens
- Output Generation: ~$10.00 per million tokens
A 20x discount on reads sounds transformative, but the cached segment is precisely the part that grows. You still re-read the entire history on every single turn, just at a reduced unit price. A fraction of a quadratic growth curve is still a quadratic curve.
In a sample 80-message session starting with a 20k-token baseline and 3k tokens added per message, message 8 might cost around 0.16. Across all 80 messages, the total climbs to roughly 112 without it. Cache reads account for about two-thirds of the cached bill—essentially re-sending your history, but on sale.
Practical Strategies to Defeat the Curve
To keep your infrastructure costs sustainable when building agentic workflows, you need to manage your context windows deliberately. When routing requests through n1n.ai, you gain access to unified billing, robust prompt caching management, and seamless model switching without changing your application code.
1. Leverage Session Resets Wisely
Executing /clear resets the triangle entirely. The system prompt and tool definitions remain safely cached by the provider infrastructure, meaning /clear costs virtually nothing while instantly flattening your token growth curve back to zero.
2. Understand Subagents and MCP Servers
Subagents maintain isolated context windows. Their noisy intermediary outputs stay out of your primary conversation thread, preventing them from being re-sent on every subsequent message. Conversely, connecting Model Context Protocol (MCP) servers whose tools load upfront will immediately bloat your base prompt , driving up the fixed cost of every single turn.
3. Watch the TTL Timers
Cache time-to-live (TTL) limits matter. On many API keys, prompt caches expire after 5 minutes of inactivity. Stepping away for a coffee break late in a session will cause the cache to drop, forcing your next message to rewrite the entire accumulated context at the full write price. If you use n1n.ai for your multi-model pipelines, check your gateway configuration and TTL headers to maximize cache hits.
Best Practices Summary
| Action | Impact on Context Triangle | Cost Implication |
|---|---|---|
| Long Continuous Session | Grows as | Exponential cost accumulation late in the session |
Using /clear | Resets to zero | Preserves system prompt cache; cuts subsequent costs |
| Adding MCP Tools | Increases baseline | Permanently raises the cost floor for every turn |
| Unmanaged Idle Time | Invalidates cache TTL | Forces expensive cache-write operations |
Treat an AI coding session like a git branch, not a permanent home. Dedicate one task per session, trigger /clear whenever the objective shifts, keep configuration files concise, and choose your model at the start.
Get a free API key at n1n.ai