# Context growth

Why each reply costs more as a session goes on, and how fresh sessions, filtered output, subagents and well-timed compaction keep the re-read small.

> Context growth is the way every message, tool result and log stays in the conversation and is re-read on every later reply. The longer the session, the more each reply re-reads.

Source: https://ai-sw-factory.mellicci.dev/cost/context-growth

**Diagram:** A large log that lands in the conversation is re-read on every later reply, so the context only grows.

- Reply 1 — small context
- → grows
- Big log lands — 40,000 tokens, once
- → stays
- Reply 2 to 30 — each re-reads the log
- →
- Fresh session — starts small again

A reply is not billed for your latest message alone. The model re-reads the whole conversation on every reply, as [Tokens, turns and context](https://ai-sw-factory.mellicci.dev/cost/tokens-turns-and-context) explains. Anything that enters the context is therefore paid for again on every later reply, not once.

That makes a session a one-way ratchet. Messages, tool results, file contents and logs only pile up, so the re-read gets longer with every turn, even when your own prompts stay short.

## Why it matters

Cost per reply rises with session length, so a long session is dearer per reply than a short one doing the same kind of work. Most of what is re-read may have nothing to do with the task you are on now.

Three patterns drive most of the growth:

- **One conversation across unrelated tasks.** Every reply re-reads context that is irrelevant to the current task.
- **Large, unfiltered output in the main conversation.** A log lands once but is re-read on every future reply.
- **Compacting at the last moment.** A late compaction produces a larger, lossier summary, and the turns right after often cost more while the model re-orients.

## How it works

Each lever acts at a different point: before the material lands, instead of landing it, or after it is already there.

| Lever | What it does to the re-read | What it costs you |
|---|---|---|
| Fresh session per unrelated task | Resets the context to the standing setup | You carry over what matters by hand |
| Filter output before it lands ([hook](https://ai-sw-factory.mellicci.dev/fundamentals/hooks) or script) | Only the relevant lines enter | You must decide what is relevant in advance |
| Delegate noisy work to a [subagent](https://ai-sw-factory.mellicci.dev/fundamentals/subagents) | Only its summary returns | The summary can miss detail you needed |
| Compact deliberately | Replaces history with a shorter summary | The summary is lossy |

Compaction has a timing trade-off. Too late, and the summary is larger and lossier, with extra turns spent re-orienting. Too early, and you lose detail you still need.

## Example

The `ticket-service` team's agent spend doubled last month. One developer pasted a test log of about 40,000 tokens into the conversation to ask about a failing suite, then kept working in that session for the next 30 replies.

| Over the next 30 replies | Log tokens re-read | Notes |
|---|---|---|
| Raw log stays in the conversation | 1,200,000 (40,000 x 30) | Every reply re-reads the whole log |
| Filtered to the failing tests (800 tokens) | 24,000 (800 x 30) | A script kept only the failing test names and errors |
| Subagent reads the log, returns a 1,000-token summary | 30,000 (1,000 x 30) | The 40,000 tokens are read once, in the subagent's own context |

The log itself did not change; only where it landed did. The filtered and delegated versions re-read about 2 to 3 percent of the raw version, at the price of trusting a filter or a summary to keep what matters. On a warm session most of the 1,200,000 re-read tokens are cache reads, far cheaper than fresh input, but still billed on every reply, and the log also takes up room in the context window that the actual work needs.

## What helps, what hurts

Helps:

- Starting a fresh session when the task changes, because the context then holds only what the new task needs.
- Filtering big output with a [hook](https://ai-sw-factory.mellicci.dev/fundamentals/hooks) or a script that keeps the relevant lines, because what never lands is never re-read.
- Handing noisy work to a [subagent](https://ai-sw-factory.mellicci.dev/fundamentals/subagents), because only its summary joins the main context.
- Compacting at a natural break, before the session is nearly full. See [Cache lifetime](https://ai-sw-factory.mellicci.dev/cost/cache-lifetime) for what a break does to caching.

Hurts:

- Keeping one session open all week across unrelated tasks.
- Pasting whole logs, or letting a command dump its full output, into the main conversation.
- Waiting for the last moment to compact.
- Compacting so early and so often that detail you still need is gone.

## Trade-offs

- **Fresh sessions lose history.** You rebuild context by hand, and a short handover note can omit something that mattered.
- **Filters can hide the cause.** If the failure is in a line you filtered out, the agent never sees it.
- **Subagent summaries are lossy.** You get a shorter context and a second-hand account.
- **A fresh session or compaction changes the start of the context.** That affects what can be served from the cache, so the next reply may re-read at the higher fresh price. See [Prompt caching](https://ai-sw-factory.mellicci.dev/cost/prompt-caching).

## See it in your own sessions

One way to see this in your own sessions is [Agent Tokenwatch](https://github.com/mellicci/agent-tokenwatch) ([project page](https://mellicci.dev/work/agent-tokenwatch)), an open-source command-line tool for Claude Code, Codex CLI and GitHub Copilot CLI. It records token counts and structure locally, never prompts, code or tool contents. Its session retrospective shows which replies cost most and how the context grew over the session. Its cost coach can walk through a "huge tool output" or "long session" moment from your own telemetry.

## Related

- [Tokens, turns and context](https://ai-sw-factory.mellicci.dev/cost/tokens-turns-and-context) - what is re-read on every reply.
- [Prompt caching](https://ai-sw-factory.mellicci.dev/cost/prompt-caching) - why a re-read can be cheap, and what breaks it.
- [Cache lifetime](https://ai-sw-factory.mellicci.dev/cost/cache-lifetime) - what happens to the cache over a break.
- [Subagents](https://ai-sw-factory.mellicci.dev/fundamentals/subagents) - keep noisy work out of the main context.
- [Hooks](https://ai-sw-factory.mellicci.dev/fundamentals/hooks) - filter output before it lands.

## Key terms

- **Context growth** - the accumulation of messages, tool results and logs in a session, all of which are re-read on every later reply.
- **Compaction** - replacing the conversation so far with a shorter summary, so later replies re-read less.
- **Fresh session** - a new conversation that starts from the standing setup only, so old material is not re-read.
- **Output filtering** - reducing large tool or log output to the relevant lines before it enters the conversation.
- **Subagent summary** - the short result a delegated worker returns; the noisy work it did stays in its own context.
