Skip to content
Mastering Claude

Home / Context and cost

Context and cost8 minApplication

The four levers of a long session

A session that runs long rests on just four levers: the cache, which reuses a stable prefix, clearing out stale tool outputs, compaction, which summarises the history, and partitioning, which delegates bulky work to isolated sub agents.

A Claude Code session that runs for several hours does not endlessly swell the same block of text sent to the model with every message. Four distinct mechanisms limit what that session drags along behind it: the cache, which reuses an identical start of conversation rather than recomputing it, clearing out tool outputs that have become useless, compaction, which replaces the history with a summary, and work delegated to sub agents, separate conversations that handle part of the task and return only their conclusion.

The cache, the sort, the summary, the delegation

The prompt cache retains the computation done on a start of conversation, called the prefix, for as long as that prefix stays identical down to the byte from one message to the next. As soon as anything changes at the very start of that prefix, everything that follows has to be recomputed. That is why position matters: general instructions and project context are best kept at the front, unmoving, while questions that change come afterwards.

Clearing out stale tool outputs, what this course calls masking, clears away the results of old calls that no longer serve the current reply, a real mechanism even though no public Anthropic documentation names it in exactly those words. Compaction, meanwhile, summarises the message history into shorter text once the conversation nears its limit, a topic covered in detail in compacting, clearing, reading just what is needed. Partitioning, lastly, hands off a portion of the work, a long search, reading several files, to a sub agent that works in its own conversation and returns only its conclusion, which avoids loading the main conversation with the detail of its search.

One single move is enough to activate the first lever

These four mechanisms do not all switch on in every session. A short conversation, that stays well clear of the context limit, triggers no compaction, and a task that requires no extensive search delegates nothing to a sub agent. The cache, by contrast, switches on from the second message of the same conversation, with no particular setting to configure, on one condition: that the start of the message stays identical.

Message 1: project instructions (identical every time) + question A
Message 2: same project instructions + question B

The second message reuses the computation already done on the project instructions, provided they have not moved by a single character between the two messages.

Figure 1

The four levers stacked

Cache
Reuses the computation already done on a start of conversation identical down to the byte.
Clearing stale outputs
Clears away the results of old tool calls that no longer serve the current reply.
Compaction
Replaces the message history with a summary once the conversation nears its limit.
Partitioning
Hands off a portion of the work to a sub agent that returns only its conclusion.
The stack places the cache at the top, stable and active with no particular setting, then clearing out stale tool outputs and compaction in the middle, and partitioning into sub agents at the base, reserved for bulky work.
Calibrate it yourself

A colleague opens a Claude Code session in the morning and puts their general instructions in the first message. Over several hours, they then work through dozens of different questions on the same project, each sent as a new message within the same conversation.

Write, in one sentence, what this situation establishes, and in one sentence what it does not establish.

What to remember
  • A long session rests on four distinct mechanisms, the cache, clearing out stale tool outputs, compaction and delegation to sub agents, not on a single global setting.
  • The prompt cache requires a start of conversation identical down to the byte to keep applying from one message to the next.
  • Compaction summarises the message history once the conversation nears its limit, without changing any project files.
  • A sub agent handles a portion of the work in its own conversation and returns only its conclusion to the main conversation.
  • Putting the project's general instructions in the very first message is what lets the cache apply from the very next message.
Do this now

Before your next long session in Claude Code, write your general instructions and the project's stable context in the very first message, then ask your follow up questions afterwards in the same conversation.

Check the source

Every datable claim in this lesson links here to the public text behind it. A source that does not open proves nothing.