bots-garden/mini-mepublic Fork
⑂ v2.1.1
Commits
⬇ Clone ▾
git clone https://git.rickub.com/bots-garden/mini-me.git
git clone ssh://git@rickub.com/bots-garden/mini-me.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

Download ZIP
context-compression.md · 42 lines · 5.4 KBmarkdown
Blame HistoryOpen raw

Context compression — explanation

What is this about?

Nothing in an agent loop ever shortens the history. Measured against a fake engine, the messages sent to the model grew 2 → 5 → 7 → 9 over four requests, and every bash output, up to maxOutput characters, and every loaded skill stay until /quit. On a local model the context window is fixed when the server loads, and the whole history is re-read at every turn on the laptop's hardware. A long session therefore first shows up as a false watchdog stall, then as an error from the server.

The compact package replaces the old question turns by one summary written by the model itself, and keeps the last few turns raw.

Why it is designed this way

A hybrid: summary plus raw recent turns. The recent turns are where the model is working; summarising them makes it re-run what it just did. The default keeps three question turns, enough for "do X", "now fix it", "and test it". Everything older becomes a pair of messages inserted right after the system prompt: a user message carrying the notes, and a short model acknowledgement.

The cut falls on a turn boundary. A tool call and its result must never be split; reusing whole turns guarantees it.

Two triggers, because the window is not always known. The primary trigger is a share of the context window, 75 % by default, leaving a quarter for the next question, its tool outputs and the answer. The server's own input-token count from the last call is preferred to the local estimate when it is larger, because it sees the tools' JSON and the chat template. When the window is unknown, Docker Model Runner tells nothing and contextWindow may be 0, only the message-count trigger can fire, and the agent says so at start-up.

One key for the window. The banner and the trigger read the same contextWindow; two keys for the same number would drift apart.

Earlier summaries are merged, not re-summarised. The two inserted messages carry a marker; a later compression recognises them and folds the new old turns into the existing summary, because summarising a summary loses detail exponentially.

A fixed-shape summary. The built-in prompt asks for seven sections with exact paths, command lines and error messages, and forbids quoting file contents: a file may have changed since it was read, and the system prompt already forbids stating what a command in this answer did not return. The summary request replaces the agent's own system prompt with a note-taker's, because "you are a coding agent, run commands" sent with a history full of tool calls invites the model to keep acting.

Failure costs nothing. If the summary request fails, server down, watchdog, empty result, the history is left untouched and the next question goes out as before. The compression exists to keep the next question possible; it must never cost one.

The watchdog is twice as patient here. The summary is the longest prefill of the session and nothing streams while it is written.

/compact skips the threshold, not the boundary. It still keeps the last turns and says so when nothing is older. This compression, the summary of old question turns, only ever runs between two generations, because Genkit holds its own copy of the conversation while a turn runs.

Inside a long turn, a second, lighter compression (context.compressInTurn, on by default). One question can be enough to overflow the window: observed with Inkling-2-Mini on a 65536-token window, one "resume" became 77 messages and 38 tool calls before the server refused a 65811-token request — the between-questions compression never got its chance. So before each model call of a turn, past the same threshold, the current turn is made smaller. First the cheap step: the tool exchanges but the last four lose their bulk, each result and each long argument (a whole file given to write_file) keeping its first line — scene.go: created, 516 line(s) — and a note saying what was dropped. The model can read a file again; it cannot win its window back. Only if that is not enough are the same exchanges summarised, and the notes go at the end of the turn's question — a user message, so the user/assistant alternation chat templates expect holds; a later compression merges them instead of stacking a second set. The middleware that sees every model call replaces the request's messages, and Genkit builds the next calls from them, so the smaller history is also what the session keeps. It does not wait for enabled: a turn that outgrows the window fails whatever the between-questions setting says.

Rejected alternatives

  • Truncating the oldest messages. Rejected: it would drop tool results whose calls remain, and lose the goal of the session.
  • Summarising everything, recent turns included. Rejected: the model then redoes its last steps.
  • A separate context.contextWindow key. Rejected in favour of the single top-level key.
  • Inside a turn, summarising at once, or a summary pair in the middle of the turn. Rejected: shrinking costs no model call and is often enough; and a pair of messages between two tool exchanges breaks the alternation some chat templates check (Gemma's raises an error).

How it relates to the rest

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# Context compression — explanation

## What is this about?

Nothing in an agent loop ever shortens the history. Measured against a fake engine, the messages sent to the model grew 2 → 5 → 7 → 9 over four requests, and every `bash` output, up to `maxOutput` characters, and every loaded skill stay until `/quit`. On a local model the context window is fixed when the server loads, and the whole history is re-read at every turn on the laptop's hardware. A long session therefore first shows up as a false watchdog stall, then as an error from the server.

The `compact` package replaces the old question turns by one summary written by the model itself, and keeps the last few turns raw.

## Why it is designed this way

**A hybrid: summary plus raw recent turns.** The recent turns are where the model is working; summarising them makes it re-run what it just did. The default keeps three question turns, enough for "do X", "now fix it", "and test it". Everything older becomes a pair of messages inserted right after the system prompt: a user message carrying the notes, and a short model acknowledgement.

**The cut falls on a turn boundary.** A tool call and its result must never be split; reusing whole turns guarantees it.

**Two triggers, because the window is not always known.** The primary trigger is a share of the context window, 75 % by default, leaving a quarter for the next question, its tool outputs and the answer. The server's own input-token count from the last call is preferred to the local estimate when it is larger, because it sees the tools' JSON and the chat template. When the window is unknown, Docker Model Runner tells nothing and `contextWindow` may be `0`, only the message-count trigger can fire, and the agent says so at start-up.

**One key for the window.** The banner and the trigger read the same `contextWindow`; two keys for the same number would drift apart.

**Earlier summaries are merged, not re-summarised.** The two inserted messages carry a marker; a later compression recognises them and folds the new old turns into the existing summary, because summarising a summary loses detail exponentially.

**A fixed-shape summary.** The built-in prompt asks for seven sections with exact paths, command lines and error messages, and forbids quoting file contents: a file may have changed since it was read, and the system prompt already forbids stating what a command in this answer did not return. The summary request replaces the agent's own system prompt with a note-taker's, because "you are a coding agent, run commands" sent with a history full of tool calls invites the model to keep acting.

**Failure costs nothing.** If the summary request fails, server down, watchdog, empty result, the history is left untouched and the next question goes out as before. The compression exists to keep the next question possible; it must never cost one.

**The watchdog is twice as patient here.** The summary is the longest prefill of the session and nothing streams while it is written.

**`/compact` skips the threshold, not the boundary.** It still keeps the last turns and says so when nothing is older. This compression, the summary of old question turns, only ever runs between two generations, because Genkit holds its own copy of the conversation while a turn runs.

**Inside a long turn, a second, lighter compression** (`context.compressInTurn`, on by default). One question can be enough to overflow the window: observed with Inkling-2-Mini on a 65536-token window, one "resume" became 77 messages and 38 tool calls before the server refused a 65811-token request — the between-questions compression never got its chance. So before each model call of a turn, past the same threshold, the current turn is made smaller. First the cheap step: the tool exchanges but the last four lose their bulk, each result and each long argument (a whole file given to `write_file`) keeping its first line — `scene.go: created, 516 line(s)` — and a note saying what was dropped. The model can read a file again; it cannot win its window back. Only if that is not enough are the same exchanges summarised, and the notes go at the end of the turn's question — a user message, so the user/assistant alternation chat templates expect holds; a later compression merges them instead of stacking a second set. The middleware that sees every model call replaces the request's messages, and Genkit builds the next calls from them, so the smaller history is also what the session keeps. It does not wait for `enabled`: a turn that outgrows the window fails whatever the between-questions setting says.

## Rejected alternatives

- **Truncating the oldest messages.** Rejected: it would drop tool results whose calls remain, and lose the goal of the session.
- **Summarising everything, recent turns included.** Rejected: the model then redoes its last steps.
- **A separate `context.contextWindow` key.** Rejected in favour of the single top-level key.
- **Inside a turn, summarising at once, or a summary pair in the middle of the turn.** Rejected: shrinking costs no model call and is often enough; and a pair of messages between two tool exchanges breaks the alternation some chat templates check (Gemma's raises an error).

## How it relates to the rest

- Turning it on and reading the report: [how to keep a long session inside the context window](../how-to/manage-the-context-window.md).
- The `context.*` keys: [configuration reference](../reference/configuration.md).
- Where the window comes from: [providers](providers.md).