# Context and compaction

> How zelk keeps a long session inside the model's context window without losing what matters.

A model sees a bounded window of tokens. A session that runs for hours
produces far more than that, so zelk manages what the model sees, while the
session's log keeps everything.

## The request

Each request sends the session's system prompt and tools first, in a fixed
order, then context that changes (project instructions,
[memory](/concepts/memory/)), then the conversation. The first part stays
byte for byte the same from turn to turn, so providers that cache a
prompt's prefix serve it from cache.

zelk estimates each request's size from the last measured one, and learns
each model's window from its provider where the provider says. Where it
doesn't, as with OpenAI's API, `-context-window` (or
`WithContextWindow` in Go) declares it.

## The ladder

zelk reduces context in steps, cheapest first:

1. **Bounded output.** Tools cap what they return, and save complete large
   outputs where the agent can page through them.
2. **Clearing old results.** Once the conversation grows past 100,000
   tokens, older large tool results are replaced in one batch by short
   stubs with a preview and where to find the full output. No model call is
   needed.
3. **A summary.** Near the window's limit, a model summarizes the older part
   of the conversation, and the summary replaces it. Recent turns stay
   exactly as they were.
4. **Native compaction.** A provider that compacts its own context does so
   alongside, and that provider's later requests use its window.

A worked example: one long session on a model with a 200K-token window that may write 32K tokens in a turn. The window is what the next request holds; the log beside it is what the session has recorded.

- **Up to 100K tokens, nothing is trimmed.** Every turn adds to the next request. The system prompt and tools come first and never change, so the provider serves them from cache.
- **Past 100K, old results are cleared.** Once a batch would remove at least 20K tokens, the 2 oldest tool results become stubs: a status, a short preview, and where the full output is. The 6 newest results stay whole, and no model call is needed.
- **Past 148K, a model summarizes.** The threshold is nine tenths of the window less the model's output limit (0.9 × 200K − 32K = 148K), never under a quarter of the window. Clearing can't touch what you and the model said, or the 6 newest results, so the window creeps up anyway, to 149,400 tokens here. A model then summarizes the older part of the conversation (about 9K tokens), the newest history that fits within 20K tokens stays exactly as it was (15,000 here), and zelk carries these facts itself, whatever the summary says (about 4K tokens): the goal, your recent messages, the task list, the approved plan, instructions, running processes, recent files, latest verification. The next request is 40,000 tokens.
- **The log keeps everything.** Every message, call and result stays in the session log, and nothing is removed, so none of this costs anything the log can’t give back.

A request the provider rejects as too long triggers the same steps once, and
then one new attempt.

You can compact on purpose too: `/compact` in the terminal UI, with
optional words on what to keep, or the `zelk.Compact` command in Go.

## What a summary can't lose

A summary is written by a model, and a model can leave things out. So zelk
carries some facts across compaction itself, whatever the summary says:

- the goal, and the user's recent messages, verbatim up to 8 KiB;
- the task list, and the plan you last approved;
- the active skill's instructions and the project's instructions;
- running background processes;
- the files most recently read or changed, to read again before relying on
  them;
- the latest verification results and whether they are still fresh.

## The log keeps everything

Compaction changes what the model sees, never the log. Every message and
tool result stays in the session's log, so rewinding, forking and
inspecting a session see the whole of it, and a replay reproduces each
compaction exactly as it happened.

## Settings

| CLI | Go | Effect |
|---|---|---|
| `-context-window N` | [`WithContextWindow(N)`](/reference/go/zelk/#withcontextwindow) | the window of a model whose provider reports none |
| | [`WithCompaction(event.Compaction{…})`](/reference/go/zelk/#withcompaction) | when to summarize, how much recent history to keep, and when to clear old tool results |
| | [`WithoutCompaction()`](/reference/go/zelk/#withoutcompaction) | no compaction: a request that overflows ends the run with an error |

## Where to go deeper

The [compaction contract](/internals/compaction/) gives the exact
thresholds, how the summary is requested and checked, what happens when a
summary fails, and how it is tested.
