Context and compaction
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
Section titled “The request”Each request sends the session’s system prompt and tools first, in a fixed order, then context that changes (project instructions, 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
Section titled “The ladder”zelk reduces context in steps, cheapest first:
- Bounded output. Tools cap what they return, and save complete large outputs where the agent can page through them.
- 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.
- 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.
- Native compaction. A provider that compacts its own context does so alongside, and that provider’s later requests use its window.
- 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 100,000 tokens, once a batch would remove at least 20,000, the 2 oldest tool results become stubs: a status, a short preview, and where the full output is. The 6 most recent stay whole, and no model call is needed.
- Clearing keeps trimming, but it can’t touch what you and the model said, or the 6 newest results, so the window creeps up anyway. Near the limit a model summarizes the older part, and zelk carries the facts a summary could drop itself. Recent turns stay exactly as they were, and the log still holds every one of the originals.
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
Section titled “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
Section titled “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
Section titled “Settings”| CLI | Go | Effect |
|---|---|---|
-context-window N |
WithContextWindow(N) |
the window of a model whose provider reports none |
WithCompaction(event.Compaction{…}) |
when to summarize, how much recent history to keep, and when to clear old tool results | |
WithoutCompaction() |
no compaction: a request that overflows ends the run with an error |
Where to go deeper
Section titled “Where to go deeper”The compaction contract gives the exact thresholds, how the summary is requested and checked, what happens when a summary fails, and how it is tested.