> ## Documentation Index
> Fetch the complete documentation index at: https://docs.9thprotocol.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Context & compaction

> How 9p keeps a long session inside the model's context window.

Every turn resends the whole conversation. Long sessions therefore grow until
they hit the model's context window, at which point, without intervention,
every subsequent request fails.

9p handles this by **compacting**: summarising older history into one message
and keeping recent turns verbatim.

## Automatic

When estimated usage passes **70% of the model's context window**, 9p compacts
before the next request:

```
⊙ compacted context ~148000 → ~12000 tokens
```

The window comes from the model catalog, so a 1M-context model compacts far
later than a 128k one.

Compaction happens **between turns, never mid-exchange**. At the top of a turn,
every tool call from the previous turn already has its result, so history is in
a state that can be cut safely.

## Manual

```
/compact
```

Compact now, regardless of the threshold. Useful before a long task, or after
finishing one whose details no longer matter.

`/usage` shows the current estimate:

```
12 calls · 184.2k in (176.1k cached) · 4.3k out · context ~38000 tokens
```

## What survives

The summary is written by a real model call, cheap and toolless, and asked to
keep, in order:

1. What you're trying to achieve, in your words
2. Decisions and constraints, especially anything you corrected or rejected
3. Files changed, with paths and what changed
4. Commands run and what they revealed
5. What's in progress and the next step
6. Anything broken, blocked, or deferred

Superseded intermediate steps and tool-call mechanics are dropped.

<Note>
  A mechanical truncation would be cheaper, but it discards exactly the decisions
  and constraints that make the rest of the session coherent. Paying for one small
  summarisation call is worth not re-explaining your project.
</Note>

## `/compact` vs `/clear`

|            | Keeps                      | Use when                     |
| ---------- | -------------------------- | ---------------------------- |
| `/compact` | a summary of what happened | continuing the same work     |
| `/clear`   | nothing                    | starting something unrelated |

`/clear` is cheaper and gives a genuinely clean slate, but it also resets the
provider's prompt cache, so don't reach for it mid-task. See
[Credits](/credits#cache-reads-are-most-of-the-bill).

## Keeping sessions cheap

* Use `/clear` between unrelated tasks so unrelated history never accumulates
* Prefer [sub-agents](/sub-agents) for wide searches. They return findings, not
  file dumps, so the main context stays small
* Keep [`9P.md`](/memory) tight; it's re-sent every turn

## Limits

* Token counts are **estimates** (\~4 chars per token), not a real tokenizer.
  They drive a threshold, so erring high just compacts slightly early.
* If no safe cut point exists, 9p leaves history untouched rather than risk a
  malformed request.
* A failed or empty summary aborts compaction, history is never destroyed on a
  bad response.
