Recipes

Context Mode: sandboxed context-window compression for coding agents

Discovered from README cross-links · 2 days ago

Software & DevOps

A dev wired context-mode into their agent stack to stop context blowups.

What they did

They were running long agentic coding sessions with codex and opencode and kept hitting the same wall: every file read, test run, and log dump landed raw in the context window until the agent either forgot the task or started hallucinating around truncated output. They installed context-mode from npm, pointed their agent configs at it, and made it the layer that sits between the agent and its tools. Instead of letting shell output and file contents flow straight into the prompt, they routed those calls through context-mode's sandbox so results get compressed and summarized before the model sees them. They then ran a multi-hour refactor across a mid-sized TypeScript repo, alternating between codex for planning and opencode for execution, and watched whether the agent could hold the thread across dozens of tool calls without losing earlier decisions. They also kept a plain baseline session without context-mode to compare how quickly each one degraded.

How it went

The context-mode sessions stayed coherent noticeably longer — the agent kept referencing decisions from early in the run instead of re-deriving them, and they stopped seeing the mid-task amnesia that had plagued the baseline. Token usage per turn dropped because raw logs were no longer pasted in wholesale. It is not magic: very large single files still needed manual chunking, and the compression occasionally dropped a detail the agent later wanted, so they learned to ask for specific excerpts rather than whole-file dumps.

Worth knowing

context-mode is a layer you have to wire into each agent's tool config, so it is not drop-in for every harness. The compression is lossy by design — anything the agent needs verbatim (exact error strings, line numbers) should be requested explicitly. License is ELv2, which is worth reading before commercial use. Setup details for codex and opencode differ, and the README assumes you already know your agent's config format.

Wiring

Install context-mode via npm, then register it as the tool intermediary in each agent's config. For codex, add it to the tool definitions so shell and file-read calls pass through context-mode's sandbox before results reach the model. For opencode, do the same in its tool config, pointing the exec and read handlers at context-mode. Order matters: context-mode must be registered before the agent starts, since it wraps the tool layer rather than running alongside it. Verify by running a tool call and checking that output is compressed rather than raw. Keep a baseline config without context-mode for comparison.

Try it yourself

Set up context-mode in this repo and wire it into my agent tooling. Steps: 1) install context-mode from npm and confirm the version; 2) find where my agent configs live (codex and opencode) and add context-mode as the intermediary for shell and file-read tools; 3) run a short smoke test — have the agent read three files and run the test suite — and show me the before/after context size; 4) document the config changes in a CONTEXT_MODE.md so I can revert. Do not modify application source.

Source

Tools in this stack