What Is Context Mode?
Context Mode is a plugin for AI coding agents that keeps large tool output out of the model's context window. When the agent reads a big file, runs a test suite or fetches a web page, Context Mode runs that work on your machine, stores the full output in a local SQLite index, and hands the model only the part it asked for. It installs into 11 agents: Claude Code, Codex, Gemini CLI, Qwen Code, Pi, OMP, OpenCode, Copilot CLI, Kiro, VS Code (Copilot) and ZCode.
It is aimed at people who run long agent sessions and watch the window fill with logs, JSON and HTML that the model reads once and never needs again. Every install has two halves. Hooks run inside the agent host before and after each tool call, and can allow, deny or replace it. An MCP server gives the model nine ctx_* tools it can call itself. Both run as local processes, the data lives in ~/.context-mode, and the README lists the only four network calls the plugin makes, all started by you or your agent.
Mert Koseoglu (mksglu) writes it. A rewrite reached 2.0.0 on November 4, 2026, and 2.2.4 followed on November 25; the repository had 34,531 stars when we checked on November 27, 2026. The license is the Elastic License 2.0, and the README is careful to call that source-available rather than open source: you may use, change and share the code, at work too, without paying, but you may not offer it to others as a hosted service.
The 98% Figure Counts Tool Output Bytes, Not Your Whole Bill
The project became known through a Hacker News post titled "MCP server that reduces Claude Code context consumption by 98%", which reached the front page with more than 570 points on February 28, 2026. The current README backs the number with a published benchmark, and the useful part is knowing exactly what it counts.
The rule: take every scenario whose raw output is 10 KB or more, run each one five times, and compare the bytes that reached the context with the raw bytes. Across the 36 rows that qualify, the median saving is 99.4%. A scripted debugging session with eight tool outputs and three searches went from 281,261 B to 3,266 B, which is 98.8%. Some rows from the November 1, 2026 run on macOS arm64:
| Task | Raw | Reached the context | Saved |
|---|---|---|---|
gh issue list --json, 40 issues | 182,943 B | 850 B | 99.5% |
vitest run --reporter=verbose, 1,106 tests | 62,909 B | 429 B | 99.3% |
terraform plan, 72 resources | 34,081 B | 166 B | 99.5% |
| Full Read of an 89 KB source file | 91,362 B | 375 B | 99.6% |
Context7 query-docs | 6,917 B | 473 B | 93.2% |
| Whole debugging session | 281,261 B | 3,266 B | 98.8% |
Three caveats keep the figure in proportion:
- It measures tool output only. Your prompts, the model's replies and the system prompt are unchanged, so the saving on total tokens is smaller. On small tasks the plugin adds a fixed prompt cost of about 2,000 tokens, according to
docs/how-it-works.md. - Search is cheaper, not free. A
ctx_searchreply carries the matching text, so the retrieval category saves a median of 77.2%. - The unit is bytes. Token counts in the README are estimates at 4 bytes per token, and 1 KB means 1,024 B. To turn a row such as 1,039,898 B into megabytes, or to size your own logs before deciding whether this matters to you, the Unit Converter handles digital data units.
The bench is reproducible: cd bench && node run.mjs runs offline on plain Node, and each row links to its input file and sha256.
The PreToolUse Hook Does the Saving, and Version 2 Denies Instead of Hinting
The MCP tools only help if the model uses them, and a model left to itself will happily cat a 300 KB log. So the work is done by the PreToolUse hook, which checks every call in a fixed order: your own deny rules, WebFetch, curl and wget, inline HTTP and build commands, then a full-file Read and one-file shell dumps such as cat or tail.
The design record for this rule set, ADR-0008, explains why version 2 changed it. Version 1 only hinted when the model read a big file. In a measurement of 322,000 tool calls, Read carried 62% of all large-output bytes, while the existing deny rules for WebFetch, curl, inline HTTP and build tools caught 0.11% of them. So v2 denies the first full-file Read of any file over OUTPUT_CAP, 16,384 B, and the deny message names three ways forward: the same Read with offset and limit, ctx_execute with the file path, or a bounded command such as grep -n. A Read with a line range is never denied, which keeps Read-before-Edit working.
Two more rules matter in practice:
- Run, then replace. On hosts that let a hook rewrite a result, curl, HTTP and build commands are no longer blocked on a guess about their size. They run, and if the output is over 4,096 B the
PostToolUsehook indexes all of it and replaces what the model sees with the best-matching chunks plus onectx_searchline for the rest. - Wrappers do not hide curl. On hosts without that ability, and for subagents, curl is still denied up front, and the parser unwraps
sudo,env,timeout,xargs,bash -cstrings and$( )before it reads the command name, sonpm ci && curl -s https://x.io/a.json && npm testis still caught. Curl that writes to a file with-o, or pipes intowcorhead -c, is allowed.
A hook always exits 0. On any internal error it gives the host's allow reply and appends a line to ~/.context-mode/hook-errors.log, so a broken plugin degrades to no plugin rather than a stuck agent. The README measures the hook's added time at 7.5 to 12.3 ms over a bare Node start, depending on the agent.
On Claude Code, the first check reads permissions.deny from .claude/settings.local.json, .claude/settings.json and ~/.claude/settings.json, and applies those rules to ctx_execute code too. The trap is a settings file that does not parse: Context Mode then prints a warning to stderr and applies no deny rules from any of the three files, unless you set CONTEXT_MODE_REQUIRE_SECURITY=1, which makes it refuse calls until you fix the file. After editing those files by hand, paste them into a JSON Formatter to catch a trailing comma before it quietly switches your deny list off.
Code Mode Means the Agent Writes a Script, and the Sandbox Is Not a Security Boundary
The idea the README calls Code Mode is the part worth adopting even if you never install the plugin: instead of reading data into the context, the model writes a short script that reads it and prints the answer.
In the README's example, the agent is asked which of 5,000 records at a JSON API has the longest title. Rather than fetching a megabyte into the conversation, it sends ctx_execute a dozen lines of JavaScript that fetch the data, loop over it and console.log a small JSON object. The data was 1,039,898 B; the agent received 254 B. ctx_batch does the same for several commands at once: it runs them, indexes everything, and returns only the passages that match the queries in the call.
How ctx_execute runs:
- It writes the script to a temp folder and spawns the runtime from an argument list in the project folder: shell, PowerShell, JavaScript, TypeScript, Python, or anything else on
PATH. - The timeout defaults to, and is capped at, 600,000 ms, after which the whole process group is killed (
taskkill /F /Ton Windows). - Output is cut at 16,384 B.
- The child gets your environment minus preload variables such as
NODE_OPTIONS.
That last point is the one to read carefully. The README states plainly that code from ctx_execute "runs on your host as your user, with your files, your network and your environment", and that "sandbox" here means only that the output stays out of the context. It is not a container. Keep your agent's own permission prompts on, because a script the model writes can do anything you can.
The Full Output Is Kept in Two FTS5 Indexes, and a 2 KB Block Survives Compaction
Nothing that Context Mode keeps out of the context is thrown away. Each project gets one database, context.db, under ~/.context-mode, built on Node's own node:sqlite and run in WAL mode so the hook process and the MCP server can write to it at the same time.
Output is split into chunks of at most 4,096 B. A Markdown chunk stays inside one heading section, and a code block that fits stays whole. Every chunk goes into two FTS5 tables: one Porter-stemmed for words, one trigram table for paths, identifiers and fragments of words. ctx_search ranks both with BM25 and merges the lists with Reciprocal Rank Fusion, where a hit scores 1/(60 + rank). Before returning nothing, it tries a spelling fix from the indexed vocabulary. The scope is the session, the worktree (the default) or the whole project, each reply holds five hits, and each snippet is at most 800 B. The README times a search at 1.62 ms median.
The same database carries a journal of every prompt, edit, command, tool error and decision. When the host compacts the conversation, the agent gets back a state block of at most 2,048 B built from the highest-scoring journal rows, errors first. A row too long to fit becomes a ctx_resume(seq) pointer instead of being truncated. In the bench, a session of 40 tool outputs and one compaction produced a 442,893 B transcript, and the block that put the agent back on track was 2,019 B.
How that block arrives depends on the host. Claude Code, Codex and Qwen Code receive it at SessionStart after a compaction; Gemini CLI, Copilot CLI and VS Code get it on the next prompt; the docs list no compaction delivery for Kiro and mark ZCode's as unverified.
Installing It: Node 24.15 Is the Requirement That Trips People Up
Each agent has its own install command in the README. On Claude Code:
claude plugin marketplace add mksglu/context-mode
claude plugin install context-mode@context-modeAny agent can also go through npm, and setup doubles as the repair command, saving your config before it changes anything:
npm i -g context-mode
context-mode setup # or: context-mode setup --agents codexRestart the agent, then run /ctx-doctor inside it. Each check prints PASS or the command that fixes it, and the maintainer asks for that report with every bug. /ctx-stats shows how many bytes were kept out, per session and all time. To remove the plugin, run context-mode setup --remove, then npm rm -g context-mode.
The plugin needs Node 24.15.0 or later, because it relies on node:sqlite. The catch is that the hooks and server are started with plain node. Under nvm, fnm, asdf, mise or Volta, that is whatever version the current project pins. Open issue #1414 describes the result in a project pinned to Node 22: the MCP server starts in a reduced mode that serves only ctx_doctor, and every hook fails open with NODE_TOO_OLD, even when a newer Node is installed elsewhere on the machine. If the savings stop in one repository, check ~/.context-mode/hook-errors.log first. On Windows you also need Git for Windows, because the shell tools run in Git Bash.
The Costs: One Process per Session, a WebFetch Rule You Cannot Turn Off, and a Paid Gateway
The open issues show where the plugin rubs:
- Memory scales with sessions. Each agent session spawns its own MCP server over stdio. Issue #1334 reports 32 to 66 MB per process, which adds up for anyone running many parallel agents, and asks for a shared daemon mode.
- WebFetch is always routed. The hook denies WebFetch and points the model at
ctx_indexplusctx_search. Issue #1336 comes from a user with their own web-reading hook who patches the minifiedhook.mjsafter every update, because no setting turns that one rule off. The maintainer has kept it open as a feature request. - It moves fast. Nineteen stable releases shipped between November 4 and November 25, 2026. That fixes bugs quickly, and it also means pinning a version if you depend on exact behaviour.
Then there is the business model. The plugin is free, and the README keeps it separate from Context Mode Gateway, a hosted subscription service installed with npx @context-mode/cli that applies similar ideas to every request and adds memory shared across sessions and agents. With the Gateway on, your requests pass through its server on the way to Anthropic or OpenAI. The plugin itself does not need it, and nothing in the plugin's local loop depends on an account.
Who Should Install It, and What to Use Instead
Install it if your sessions regularly hit compaction, if your agent works with chatty tools (test runners, gh with --json, Terraform plans, browser snapshots, Docker logs), or if you pay per token and want to see where the bytes go. The deny-with-a-better-call design teaches the model the habit of reading slices and writing scripts, which is most of the win.
Skip it if your sessions are short, since the fixed prompt cost can outweigh the savings, or if you cannot run Node 24.15 everywhere your agent works.
Alternatives solve neighbouring problems:
- claude-mem records what the agent did and uses a model to summarize it into memory for the next session. It is aimed at remembering decisions across sessions, while Context Mode is aimed at keeping a single session's window clear. Our claude-mem write-up covers how it works.
- Discipline without a plugin. Rules in
CLAUDE.mdorAGENTS.mdtelling the agent to read with line ranges and pipe long output throughheadorgrepcost nothing, but nothing enforces them. The ADR's numbers are the argument for enforcement: hints alone left Read carrying 62% of the large-output bytes. - A bigger context window postpones compaction but still bills you for every byte the tools return.



