mech.app

The mech.app newsletter

Agentic AI, minus the noise.

Get practical field notes on AI agents, automation, developer tools and security delivered to your inbox.

No spam. Unsubscribe anytime.

Dev Tools

Billion-Context: How 5x Token Compression Lets 100K-Window Agents Handle Month-Long Sessions

A context-compression plugin that reduces token costs by 5x, fits agent sessions into 100K windows, and persists state across billions of tokens.

Source: github.com
Billion-Context: How 5x Token Compression Lets 100K-Window Agents Handle Month-Long Sessions

Production agent sessions hit a hard wall when context windows fill up. You either truncate history and lose continuity, upgrade to million-token models and pay 10x more, or restart the session and break multi-day workflows. Billion-context is a compression plugin that solves this by shrinking token usage 5x and fitting month-long sessions into 100K windows.

The project is trending on GitHub (rank 8 for TypeScript, 1,122 stars) because it addresses the cost and window-size bottleneck that every long-running agent deployment encounters. This is not a model wrapper or a vector database. It is a middleware layer that compresses tool outputs, sandboxes execution, and manages what stays in the active window versus what gets archived.

The Problem: Context Windows Fill Faster Than You Think

Agent sessions accumulate tokens in three places:

  1. System prompts and instructions (static, but repeated in every turn)
  2. Tool call history (grows linearly with each action)
  3. Tool output (often verbose JSON, logs, or file contents)

A single tool call to list files in a repository can return 50KB of JSON. A code execution sandbox might dump 200KB of logs. After 20 turns, you have burned through 80K tokens, and the agent has barely started.

Standard mitigation strategies:

  • Truncate old turns: Breaks multi-step reasoning and loses context.
  • Summarize periodically: Adds latency and loses fidelity.
  • Upgrade to 1M-token models: Costs 5-10x more per request.

Billion-context takes a different approach. It compresses tool outputs before they re-enter the context window, sandboxes execution to reduce noise, and archives compressed history to disk so sessions can span months without expanding the active window.

Architecture: Compression Pipeline and State Management

The plugin sits between the agent orchestrator and the LLM API. It intercepts tool calls, processes outputs, and injects compressed summaries back into the context.

Core Components

ComponentRoleImplementation
Sandbox HookIsolates tool execution, filters stderr, truncates verbose outputMCP-compatible hooks, 98% size reduction
Compression EngineApplies semantic compression to tool outputsLLM-based summarization with quality thresholds
Context ManagerDecides what stays in active window vs. archiveToken budget tracking, sliding window eviction
Archive StorePersists compressed history to diskJSON or SQLite, indexed by session ID and timestamp
Retrieval LayerFetches archived context when agent needs historical dataKeyword search or vector similarity (optional)

Flow Diagram

Agent Request
    ↓
Context Manager (check token budget)
    ↓
Tool Call (sandboxed execution)
    ↓
Sandbox Hook (filter + truncate)
    ↓
Compression Engine (summarize output)
    ↓
Context Manager (insert compressed output, evict old turns)
    ↓
Archive Store (persist evicted turns)
    ↓
LLM API (active window only)
    ↓
Agent Response

Compression Strategy

Billion-context does not compress everything equally. It applies different strategies based on content type:

  • Structured data (JSON, XML): Extract schema and key fields, discard verbose arrays.
  • Logs and traces: Keep error lines and summary stats, drop repeated info messages.
  • Code outputs: Preserve function signatures and return values, truncate intermediate steps.
  • Natural language: Summarize with LLM, preserve key entities and decisions.

The plugin tracks compression quality by measuring information loss. If a compressed output drops below a fidelity threshold (configurable), it keeps the full version in the archive and only injects a minimal stub into the active window.

Token Budget and Eviction Policy

The context manager enforces a token budget for the active window. Default is 80K tokens (leaving 20K for the next turn in a 100K model). When the budget is exceeded, the manager evicts the oldest turns and archives them.

Eviction is not naive FIFO. The manager scores each turn based on:

  • Recency: Recent turns are more likely to be relevant.
  • Reference count: Turns that the agent has referenced multiple times stay longer.
  • Compression ratio: Highly compressible turns are evicted first (they can be reconstructed cheaply).

This scoring function is tunable. For debugging sessions, you might weight recency higher. For long-running automation, you might prioritize reference count.

Retrieval: When the Agent Needs Old Context

If the agent asks a question that requires archived context (e.g., “What was the error message from three days ago?”), the retrieval layer fetches the relevant turns and injects them back into the active window.

Retrieval options:

  1. Keyword search: Fast, works for structured queries, no embeddings required.
  2. Vector similarity: Slower, better for semantic queries, requires embedding model.
  3. Hybrid: Keyword filter followed by vector rerank.

The plugin does not force a retrieval strategy. It exposes hooks so you can plug in your own search backend (Elasticsearch, Pinecone, or a local SQLite FTS index).

Integration: MCP Hooks and Multi-Platform Support

Billion-context integrates via Model Context Protocol (MCP) hooks. It supports 17 platforms, including Claude Code, OpenAI Codex, Gemini CLI, and Kimi.

Installation:

npm install -g billion-context --prefix=~/.local

Configuration (example for Claude Code):

{
  "mcpServers": {
    "billion-context": {
      "command": "billion-context",
      "args": ["--budget", "80000", "--archive", "~/.cache/agent-sessions"]
    }
  }
}

The plugin runs as a sidecar process. It does not modify the agent runtime or require changes to your orchestration code. You point your MCP client at the plugin, and it intercepts tool calls transparently.

Performance and Cost Impact

Measured on a 30-day agent session (coding assistant, 500 tool calls):

  • Without compression: 2.1M tokens consumed, $42 in API costs (GPT-4 pricing).
  • With compression: 420K tokens consumed, $8.40 in API costs.
  • Compression overhead: 1.2 seconds per tool call (LLM summarization).
  • Storage footprint: 18 MB of archived JSON (uncompressed).

The 5x token reduction is consistent across different workloads (coding, data analysis, customer support). The compression overhead is acceptable for most use cases. If latency is critical, you can disable LLM-based summarization and rely on rule-based truncation (faster, but lower quality).

Failure Modes and Observability

Common Failures

  1. Compression quality drops: The LLM summarizer produces lossy output, and the agent loses critical information.

    • Mitigation: Set a fidelity threshold. If compression quality is too low, keep the full output.
  2. Archive retrieval is too slow: The agent stalls waiting for archived context.

    • Mitigation: Use an indexed search backend (SQLite FTS or Elasticsearch).
  3. Token budget is too tight: The active window fills up mid-turn, and the agent cannot complete the request.

    • Mitigation: Increase the budget or reduce the number of tools in the active window.

Observability Hooks

The plugin emits structured logs for:

  • Token counts (before and after compression)
  • Eviction events (which turns were archived)
  • Retrieval requests (which archived turns were fetched)
  • Compression quality scores (per turn)

You can pipe these logs to your observability stack (Datadog, Grafana, or a local log file).

When to Use Billion-Context

Use it when:

  • Your agent sessions run for hours or days (customer support, code reviews, long-running automation).
  • You are hitting context window limits and do not want to upgrade to million-token models.
  • You need to reduce token costs without sacrificing session continuity.
  • You want to sandbox tool execution and filter verbose outputs.

Avoid it when:

  • Your sessions are short (under 50 turns). The compression overhead is not worth it.
  • You need real-time latency (under 100ms per turn). LLM-based compression adds 1-2 seconds.
  • Your tools already return minimal outputs. Compression will not help.
  • You are using a model with native 1M+ context windows and cost is not a concern.

Technical Verdict

Billion-context is infrastructure for long-running agent sessions. It does one thing well: compress context so you can fit month-long workflows into 100K windows. The 5x token reduction is real, the integration is clean (MCP hooks), and the eviction policy is tunable.

The trade-off is latency. LLM-based compression adds 1-2 seconds per turn. If your agent is already slow (multi-step reasoning, external API calls), this is acceptable. If you need sub-second responses, stick with rule-based truncation or pay for larger windows.

The plugin is production-ready for coding assistants, customer support bots, and automation agents. It is not a fit for real-time chat or low-latency use cases.