unicornAll work

Agent context management

A long agent bloats the window and the bill — summarise, prune, offload, /compact.

In short An agent, like a chat, has a finite context window (working memory and context). But an agent runs many turns of the loop, and each turn grows the context: tool results and step history pile up. After N turns the window overflows, and the cost (you pay per token) shoots up. Context management is the set of tricks that keep a long-running agent inside the window and cheap.

window limit compacted loop turns → context grows → /compact pulls it back
Left alone, context hits the limit; the tricks bring it back in bounds.

Why an agent has it worse than a chat

In a chat, context grows from your messages. For an agent it's faster: each tool call returns a result (sometimes huge — a whole file, a whole search dump), and it all settles into the context. By the tenth turn there can be many times more than you ever typed.

Two consequences: 1. Window overflow — the early parts fall off, the agent "forgets" the start of the task (working memory and context). 2. Cost — you pay for the whole context on EVERY turn (Claude API basics): a bloated context makes each next step more expensive.

The techniques

On the platform (API, Claude Platform 101) — four moves

  1. Summarisation — periodically fold the accumulated history into a brief summary and continue with it, not the whole wall.
  2. Pruning / selective context — don't drag everything into the next turn: keep only what the current step needs, drop the spent.
  3. Offloading — keep large data outside the context (in a file/store) and pull it in a piece at a time.
  4. Compact tool results — return not "all the raw material," but the needed digest.

The general principle: keep only what's actually needed right now in the window — for both quality and cost.

In Claude Code (Claude Code 101) — commands

Analogy: your desk during a long project. Periodically clear away spent papers (pruning), make a short summary instead of a pile of drafts (summarisation), keep thick folders in the cabinet and pull a page as needed (offloading). Otherwise there's no room left on the desk.

The practical rule

A long session/agent starts to "go dumb" or get pricey → almost always a bloated context is to blame. The cure: fold up (/compact / summary), clear out (/clear / prune), offload the large. A direct continuation of working memory and context, but for a multi-step agent.

Related

The source is the Claude Platform 101 and Claude Code 101 courses. Related: working memory and context, the agent loop and tools, Claude API basics.

All theory