Back to Articles

Context Mode: How AI Coding Agents Cut Context Windows by 98% Using Sandboxed Tool Outputs

[ View on GitHub ]

Context Mode: How AI Coding Agents Cut Context Windows by 98% Using Sandboxed Tool Outputs

Hook

Your AI coding agent just read 47 files to answer a simple question about your codebase, burning through 700KB of context tokens. What if it could write one 3.6KB script instead and get better results?

Context

AI coding agents like Claude Code, Cursor, and Copilot have a fundamental problem: they're data hoarders. When you ask them to analyze your codebase, they read files directly, dumping entire contents into the context window. A simple question about error handling across your application triggers dozens of file reads, each adding thousands of tokens. Ask about git history? The agent runs git log and floods the context with commit messages. Request a browser automation task? You get megabytes of HTML snapshots.

The problem compounds over time. As conversations grow, agents lose track of earlier work. The typical solution is re-injecting historical data when the context compacts, which means you're paying twice—once for the original operation and again every time the agent needs to remember what happened. This creates a vicious cycle: more tool calls generate more output, which fills the context faster, triggering more compaction, requiring more re-injection. Context Mode emerged from this frustration, built by developers tired of watching their AI assistants drown in their own output. It implements the Model Context Protocol (MCP) to sit between the agent and its tools, transforming how AI coding assistants consume and retain information.

Technical Insight

Context Mode's architecture is deceptively simple: it's an MCP server that intercepts tool calls before they reach the agent's context window. Instead of letting raw tool output flood into the conversation, it sandboxes the results in a SQLite database with Full-Text Search (FTS5) indexing. The agent receives a compressed confirmation—typically just a resource ID and summary—while the full data sits in storage, indexed and searchable.

The key innovation is the four-layer optimization strategy. First, output sandboxing catches verbose tool responses. When an agent runs a file search that returns 50 matches, Context Mode stores the results and returns: "Stored 50 results in resource://search/abc123. Top 3: auth.ts (12 matches), api.ts (8 matches), utils.ts (5 matches)." This transforms a 50KB response into 200 bytes. Second, session continuity via BM25 search replaces naive re-injection. When conversations compact, instead of dumping all historical tool outputs back into context, Context Mode uses BM25 ranking to retrieve only semantically relevant past operations based on the current conversation state.

The third layer is where it gets interesting: code-first analysis. Context Mode provides ctx_execute() primitives that let agents generate analysis scripts instead of reading files directly. Here's the paradigm shift:

// Traditional approach: Agent reads files directly
await readFile('src/auth.ts')  // 15KB
await readFile('src/api.ts')   // 22KB
await readFile('src/utils.ts') // 18KB
// Total: 55KB for 3 files, more if it needs to scan more

// Context Mode approach: Agent generates analysis code
await ctx_execute({
  code: `
    const files = glob.sync('src/**/*.ts');
    const exports = files.map(f => ({
      file: f,
      exports: parse(readFileSync(f, 'utf8'))
        .body
        .filter(n => n.type === 'ExportNamedDeclaration')
        .map(n => n.declaration.id.name)
    }));
    return exports.filter(e => e.exports.length > 0);
  `
});
// Returns: Stored analysis in resource://exec/xyz789 (2.1KB)
// Agent receives: "Found 47 exports across 12 files" + summary

This one script replaces what would have been 47 file reads. The agent generates domain-specific code to extract exactly what it needs, and Context Mode sandboxes the execution results. The data remains queryable via the SQLite backend, so subsequent questions like "Which files export authentication functions?" hit the indexed results instead of re-reading files.

The fourth layer is compressed output formatting. Context Mode enforces "terse caveman" style responses—stripped articles, abbreviated words, dense formatting. A typical git operation response goes from:

Successfully created new branch 'feature/auth-refactor' based on 'main'. 
The branch has been checked out and you are now working on it. 
You can begin making your changes.

To:

Branch feature/auth-refactor created from main, checked out

This 65-75% compression applies to both tool outputs and agent responses, creating a multiplier effect on context savings.

The SQLite schema is purpose-built for this use case. Each tool invocation creates a record with the full output, indexed via FTS5. When the agent needs historical context, BM25 ranking scores past operations against the current conversation embedding:

CREATE VIRTUAL TABLE tool_outputs USING fts5(
  session_id, 
  tool_name, 
  timestamp, 
  input_summary, 
  output_data, 
  tokenize='porter unicode61'
);

SELECT 
  tool_name, 
  input_summary,
  snippet(tool_outputs, 4, '<mark>', '</mark>', '...', 32) as excerpt,
  bm25(tool_outputs) as relevance
FROM tool_outputs
WHERE tool_outputs MATCH ?
ORDER BY relevance
LIMIT 5;

For platforms with hook support—currently just Claude Code—Context Mode can automatically route tool calls without configuration. It injects middleware into the agent's execution path, intercepting calls before they resolve. For the other 13 supported platforms (Cursor, Zed, Copilot, etc.), you need a routing file that explicitly maps tools to Context Mode's MCP endpoints. This is the main friction point in adoption.

The production data from early adopters is compelling: one team at a large tech company reported their codebase analysis conversations went from averaging 180K tokens per session to 9K tokens, while maintaining comparable answer quality. The key insight is that AI agents don't need to see everything—they need to see the right things, and they're perfectly capable of writing code to extract those things if you give them the primitives.

Gotcha

Context Mode's biggest limitation is platform support asymmetry. Only Claude Code has full hook support with automatic routing—install the MCP server and you're done. For the other 13 platforms, you need to manually configure routing files that map each tool to Context Mode's sandboxed versions. This means maintaining configuration as your toolset evolves, and debugging routing issues when tools don't behave as expected. If you're not on Claude Code, expect setup friction.

The "code-first" paradigm also has an adoption curve. It works brilliantly when agents generate correct analysis scripts, but debugging misbehaving scripts adds a meta-layer of complexity. If your agent generates a script with a syntax error or logic bug, you're now debugging generated code instead of just reading files. The system assumes the AI agent is sophisticated enough to write robust Node.js/TypeScript analysis code, which isn't always true for simpler models or specialized domains. The BM25 retrieval strategy is another trade-off: you're trusting that semantic search will surface relevant historical context. In practice, this works for most use cases, but there are edge cases where traditional full-history retention would catch subtle connections that BM25 misses. If your work requires guaranteed preservation of all conversational context—perhaps for compliance or debugging complex agent behavior—the search-based approach may feel too lossy.

Verdict

Use Context Mode if you're hitting context limits on large codebase analysis, your AI agent makes repetitive file reads or generates verbose tool outputs (git logs, API responses, browser automation), or you're on Claude Code where setup is trivial. The 98% reduction is real for high-volume tool usage, and the code-first paradigm genuinely changes how agents interact with codebases—generating targeted analysis scripts beats naive file reading. Skip it if you're working on small projects where context isn't a bottleneck (under 50 files, simple tasks), you're on a platform without hook support and can't justify maintaining routing configurations, you need guaranteed full-history context preservation rather than search-based retrieval, or your AI agent struggles with generating correct analysis code. The tool assumes a sophisticated agent and willingness to trade some context completeness for massive compression gains.

// ADD TO YOUR README
[![Featured on Starlog](https://starlog.is/api/badge/ai-dev-tools/mksglu-context-mode.svg)](https://starlog.is/api/badge-click/ai-dev-tools/mksglu-context-mode)