context-mode
Concepts

Context protection

The four layers that keep raw data out of the context window.

Think in Code is the principle; context protection is the machinery that enforces it. context-mode keeps raw data out of the window with four layers that stack — a tool call passes through each one, and only a small, relevant result survives to the model.

Layer 1 — Sandboxed execution

Your code runs in a subprocess, not in the conversation. The model can read gigabytes of files, query a database, or shell out to a build — and none of it touches context. Only what the program prints to stdout returns. This is the floor the other layers build on: nothing enters the window unless you explicitly print it.

Layer 2 — Intent-driven filtering

Sometimes the print itself is large. When output is sizeable, pass an intent string describing what you are actually looking for. Output over about 5 KB is automatically indexed, and only the sections that match your intent return to the model — ranked by the same BM25/full-text engine the knowledge base uses, not by embeddings. The rest stays in the knowledge base, retrievable later with ctx_search — so a verbose command still yields a focused answer.

ctx_execute with an intent filter
ctx_execute({
  language: "shell",
  code: "npm test 2>&1",
  intent: "failing test names and the assertion that broke",
})

The full test output is captured and indexed. Only the failures that match the intent come back; if you later need a passing case or a timing number, you search for it instead of paying for it up front.

Layer 3 — FTS5 knowledge base

Indexed content lives in a local FTS5 store with BM25 ranking and never enters context on its own. You query it on demand with ctx_search, which returns ranked snippets rather than whole documents. Everything you have already executed, fetched, or indexed becomes searchable — without re-reading any of it.

Layer 4 — Session continuity across compaction

Events persist in SQLite, so the working state survives a window reset. When a session is compacted or resumed, context-mode rebuilds what it knows from the store instead of re-injecting raw context. The history stays searchable; the window stays clean.

How the layers compose

A single data-heavy command flows through all four: it runs in the sandbox (layer 1), large output is filtered by intent and indexed (layer 2), the index backs on-demand search (layer 3), and the record outlives compaction (layer 4). The net effect is the roughly 95-99% context saving context-mode is built to deliver.

Next

On this page