| 💾 Saved. d722711 k33g 9h ago | 1 | # How to keep a long session inside the context window |
| 2 | |
| 3 | This guide shows how to stop a long session from overflowing the model's context. It assumes you already run `mm` and know which server serves your model. |
| 4 | |
| 5 | ## Steps |
| 6 | |
| 7 | 1. Tell the agent the size of the window. Either set `contextWindow` in the YAML to the value you started the server with, or leave it at `0` on llama.cpp, where the agent reads `n_ctx` from `/props`. The banner shows the result as `ctx: <size> (config)` or `ctx: <size> (/props)`. |
| 8 | |
| 9 | 2. Turn the automatic compression on: |
| 10 | |
| 11 | ```yaml |
| 12 | context: |
| 13 | enabled: true |
| 14 | threshold: 75 |
| 15 | keepLastTurns: 3 |
| 16 | ``` |
| 17 | |
| 18 | Before each question, when the history reaches 75 % of the window, the old turns are replaced by one model-written summary and the last three question turns are kept as they are. |
| 19 | |
| 20 | 3. Watch the report. With `showStats: true` (the default) each compression prints one dimmed line: |
| 21 | |
| 22 | ``` |
| 23 | 🗜️ compressed 14 messages → 1 summary + 7 kept (18.2k → 4.1k tokens, 6.3s) |
| 24 | ``` |
| 25 | |
| 26 | ## Variants |
| 27 | |
| 28 | - **The window stays unknown** (Docker Model Runner tells nothing, and `contextWindow` is `0`). Only the `maxMessages` trigger can fire; the agent says so in a start-up warning. Set `maxMessages` to a message count (one command costs two messages) or set `contextWindow`. |
| 29 | - **Compress by hand.** Type `/compact` at the prompt, or send it from the editor in ACP mode. It ignores the threshold but still keeps the last `keepLastTurns` turns, and says `🗜️ nothing to compact` when there is nothing older. |
| 30 | - **The server was started after the agent.** The window is re-probed before the first question while it is still unknown and compression is on; you do not need to restart. |
| 31 | - **Your own summary prompt.** Set `context.prompt` to replace the built-in seven-section prompt. |
| 32 | - **A compression that fails** (server down, watchdog) leaves the history unchanged and prints `[compact: failed, history kept: …]`. |
| 33 | |
| 34 | ## See also |
| 35 | |
| 36 | - Every `context.*` key and its default: [configuration reference](../reference/configuration.md) |
| 37 | - Why the summary keeps recent turns raw and merges earlier summaries: [context compression](../explanation/context-compression.md) |