Inside a Coding Agent's Context Window

What actually fills a 47K-token session, why causal attention makes prefix caches fragile, and how three harness choices swing your cost 15x.

Jump to section 4 sections
  1. What actually fills the prompt
  2. Why one changed byte breaks the rest of the prompt
  3. The quadratic triangle (and why early compaction backfires)
  4. How the three choices compound over 20 turns

When you type a one-line follow-up into a coding agent on turn 18, it feels like sending a short text message. It isn’t. LLM APIs are stateless, so your harness sends the entire conversation from byte zero on every single turn: system instructions, every tool definition, every file the agent opened, every stack trace, and every reply so far.

That one detail explains almost every strange bill and slowdown developers hit with coding agents. You don’t need a new model to fix most of it; you need to see what is actually sitting in the prompt buffer.

What actually fills the prompt

In a typical debugging session, the words you type are less than 1% of the prompt. Almost the entire context window is source files and raw tool output.

Take a concrete 12-step bugfix on a Rust server. The user writes two short prompts (300 tokens combined). The agent reads three files (~14K tokens) and runs cargo test once while a test is panicking with full backtraces and debug logs (~19.8K tokens). That single uncropped test failure now makes up 42% of the entire prompt, and unless the harness bounded it before appending, every turn after that carries those 19,800 tokens along for the rest of the session.

cargo test (uncropped failure): 19,800 tokens (41.9% of the prompt) - Full backtraces + 480 lines of verbose tokio debug output
Try this:

Scrub across the strip to compare the two tiny user slices against the single orange test failure in the middle, then flip Uncropped test log (19.8K) to watch capping it at 120 lines free 18.4K tokens immediately.

Before turn 1 even starts, there is also a fixed prefix tax from tool definitions. Every tool your harness exposes has to be described as JSON (parameters, types, enums, descriptions), usually costing around 400 to 500 tokens per tool. Six or eight crisp primitives (read, edit, write, grep, find, bash) take roughly 3.6K tokens. Attaching 45 MCP tools “just in case” locks up 20K+ tokens before the model has read a single line of your repo.

Why one changed byte breaks the rest of the prompt

If inference servers had to recompute attention over 50,000 tokens from scratch on every turn, coding agents would be too slow and too expensive to use. Instead, providers cache the KV (key-value) state of your prompt’s leading prefix and let you reuse it at roughly a tenth of normal input price.

What catches harness authors off guard is how strictly one-directional that cache is. In a decoder-only transformer, attention is causal: every token’s key and value vectors mathematically depend on every token to its left, and on nothing to its right.

Change even one byte near the front of the prompt, such as putting a live Current time: 14:03:22 in the system prompt or dynamically registering an MCP tool halfway through a session, and the KV state of that block changes. Because every later block attends to it, the invalidation ripples strictly rightward even though the later conversation text didn’t change a single character. Blocks to the left stay cached; everything from the edit onward turns invalid.

Editing "Tools" forces 42K tokens to recompute (5.2x cost vs append-only, 7% hit).
Try this:

Tap or sweep from right (Turn 4) back to left (System) and watch the crimson dependency arrows propagate rightward while per-turn cost jumps more than 5x.

Three practical harness rules fall straight out of causal attention. First, keep the system prompt completely static; if the model needs the current time, git branch, or open tab list, inject it into the latest user turn instead of mutating block 0. Second, freeze the tool list for the life of a session. Third, treat the message array as an append-only log during active work.

The quadratic triangle (and why early compaction backfires)

Because every turn re-sends everything before it, cumulative input tokens across an N-turn session don’t grow linearly. They fill a 2D triangle: turn 1 sends 1 slice, turn 2 sends 2, turn 3 sends 3, and so on. Without prefix caching, you pay full price for the entire area of that triangle.

Prefix caching turns an O(N^2) bill into something close to O(N). Only the first turn and the thin diagonal edge of new tokens pay full price; the massive interior of the triangle drops to 0.1x.

Now look at what happens when a harness tries to be clever and summarizes older turns every 4 steps to keep the prompt compact. Rewriting turns 1 through 4 into a fresh summary edits the front of the conversation, which turns that entire turn into a cache miss. Even though the rows afterward are slightly shorter, paying full prefill on those reset rows costs more than leaving the warm green triangle untouched.

Prefix caching turns the interior green (0.1x), leaving only the thin copper diagonal at full price (14.9c). Tap or hover any row to inspect.

Instead of summarizing early, cap noisy tool output before it ever enters the message array. A 15-line head-and-tail wrapper stops a 20K-token log from poisoning the triangle while keeping your prefix 100% immutable:

ts
export function capToolOutput(raw: string, maxLines = 120): string {
  const lines = raw.split("\n");
  if (lines.length <= maxLines) return raw;
  const head = Math.floor(maxLines * 0.4);
  const tail = maxLines - head;
  const omitted = lines.length - maxLines;
  return [
    ...lines.slice(0, head),
    `\n... [${omitted} lines omitted; read full log file if needed] ...\n`,
    ...lines.slice(-tail),
  ].join("\n");
}

How the three choices compound over 20 turns

None of these three mistakes looks fatal on a single turn. Put them together across a normal 20-turn coding session on the exact same model (modeled at $3 / $0.30 per million input tokens), and the bill swings by more than 15x purely from harness hygiene.

Toggle all three levers below against the ghosted worst-case bars to see how much each choice actually moves the needle.

20-turn session: $4.80 total input cost (0% cached, 131K final context).
Try this:

Flip only Live timestamp in system to Static system prompt first. Notice that fixing prefix caching alone collapses the red full-price bars down to thin slivers, and combining all three drops the 20-turn session from $4.80 down to $0.27.