Skip to content

Context handling

What the model actually sees on a turn, where it comes from, and how growth is managed.

What enters the context

Every turn, the model receives:

  • The system prompt, assembled per session (agent/prompts.py): the harness ground rules, the active simulator's description and domain hints, and runtime context (paths, plotting availability).
  • The skills index: every skill's name and description. Skill bodies are not in the context until the agent reads one. That is the progressive-disclosure contract, and it is why always-on rules live in the prompt, not in skill bodies (see improving the agent).
  • The memory index MEMORY.md (see memory). Individual notes enter only when read.
  • The conversation so far: messages, tool calls, and tool results.

Tool results enter as real content, not summaries, but they are not kept forever at full size: a large single result is offloaded at once, and older ones move to files as the window fills (see Growth and its limits below). The Julia kernel's streamed output is rendered through a terminal emulator (so progress bars collapse to their final state instead of thousands of carriage-return frames) and tail-capped at 256 KB; result values and error messages are capped at 64 KiB on the kernel side.

The TUI's folded steps are display only. Ctrl+O shows every step's full output for you, and the model's context is unaffected either way.

Persistence

Conversation state lives in checkpoints.sqlite per session (langgraph's checkpointer). That is what makes mid-session model switching work: the agent is rebuilt with the new model on the same checkpoint thread, and the conversation carries over. The trace (trace.sqlite) is a separate, append-only record for humans and scorers. It is never fed back to the model.

Growth and its limits

A session's context grows with the conversation, so growth is managed in layers, cheapest first, each running ahead of the next so the expensive ones fire only when the cheaper ones are not enough:

  • Eviction of a large single result. Any tool result over ~20k tokens is written to large_tool_results/<id> under the session state dir and replaced inline with a head/tail preview plus a pointer the agent can read_file. The full result stays recoverable; the context holds a stub. This is deepagents' FilesystemMiddleware.
  • Offloading of older tool results. Once the conversation reaches 70% of the compaction point, the oldest tool results (source reads, REPL output) are written to large_tool_results/<id> and replaced by a short preview and the file's path, until it is back under 45%. The newest six (three on a window under 100K) always stay, and so does the attempt log, which the agent refers back to by value. The agent reads a moved result back with read_file. The change is stored in the conversation, in one chunk per step, so the prompt stays the same between calls for provider caching; the trace keeps the full text. Each step is a context_offload trace event (agent/offload.py), which the context view counts; the chat does not announce it. This replaced clearing old results to a bare [cleared] on every call, which left the agent nothing to read back.
  • Summarization. When offloading is not enough and the conversation reaches the compaction point (85% of the window by default, earlier for a model with a large output allowance), the older turns are replaced by a structured summary (session intent, key decisions, artifacts, next steps) while the newest turns stay verbatim. The summarized turns are offloaded to conversation_history/<internal-session-id>.md first, so they remain recoverable, and the summary embeds that path. This is deepagents' stock backend-recoverable SummarizationMiddleware, configured with the session's compaction point and replacing the one create_deep_agent installs (same name), and non-mutating; TraceRecorder records each compaction. We lean on the stock middleware so upstream improvements arrive without porting.

Every consumer reads one context budget (context_budget.py), resolved when the agent is built and on every model switch: the window, where it came from, and the points where offloading and compaction run. The summarizer, the offloading, the status bar and /context all use it, and the trace records it with the model (a model event). The window comes from:

  • the provider package's bundled model data, read directly (no key, no model build), with the Gemini API asked for Gemini models newer than that data;
  • Ollama: a hosted (:cloud) model's maximum, which it runs at; a local model's num_ctx, the size it was actually loaded with (the largest that stays on the GPU, see Models), not the daemon's theoretical maximum;
  • otherwise an assumed 200K tokens, which /context says. If the real window is smaller, the provider rejects a long request, and the summarizer then compacts and retries, so the turn carries on.

  • /compact runs a summarization pass on demand against the checkpointed thread. Every compaction (automatic or manual) is recorded once as a context_compaction trace event, and the full conversation remains in the trace; compaction is non-mutating, so nothing is lost from the record.

  • Both front ends show a note when a compaction happens, including an automatic one in the middle of a turn, and the web replays it with the conversation. The summary itself stays out of the chat: the summary model call is tagged as internal, and TraceRecorder declares langchain's InternalCallTransformer, which deepagents does not, so the call never reaches the message stream.
  • Keep sessions task-shaped regardless: offloading and the summary preserve the working set and the conclusions, not every byte. Durable knowledge belongs in memory, which survives the session boundary by design.
  • Token usage per model turn is recorded in the trace (model_usage events), so the cost of a workflow is measurable, and the bench records it per sample.

Subagents are the structural answer for context-heavy sub-tasks: a subagent runs in its own context window and returns a result, so the parent's context pays for the conclusion, not the exploration. The seam exists per simulator (subagent_factories); a general source-exploration subagent (so that browsing installed package source never enters the main context at all) is the planned next step.

Seeing it

The context view (/context in both front ends; in the browser also the ring under the message box) shows how full the context is and what fills it. Its figures come from the trace, so a resumed session shows the same as a live one, and both front ends show the same numbers:

  • How full. The last model call's input plus its output: the reply joins the conversation the next call sends, and the summarizer compares the same figure with the compaction point.
  • What fills it. The recorder measures each request as it is sent, part by part: system prompt, tool definitions, skills, memory, the conversation summary, your messages, the agent's messages, and tool results. The parts are counted by characters and scaled to the provider's measured total, so the total is exact and the split a close estimate (agent/context_measure.py). In the browser, the tool results expand to the largest ones.
  • Free space up to the compaction point, the compaction buffer, and how many tool results and compactions have been moved out so far.
  • Where auto-compaction runs, as a share of the window. In the browser, a click on it offers other points, saved for every session and applied to this one at once; the terminal reads it from compact_at in the user config. Few sessions need it: compacting earlier keeps requests smaller and cheaper, later keeps more of the conversation verbatim. A model's context size (in its settings) is the stronger lever.

The ring (the terminal shows a ctx percentage in its status bar) updates after every model call, when a turn ends however it ends, on reconnect, after /compact and after a model switch. After /compact or a switch the figure is an estimate (marked ~) until the next call measures it. It turns amber, then red, as the context nears the compaction point.