Skip to content
Source on GitHub (opens in a new tab)

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.

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.

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.
  1. Every turn adds to the next request. The system prompt and tools come first and never change, so the provider serves them from cache.
  2. 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.
  3. 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.

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.

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.

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

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.