The bundled [`plugins/headroom-agent-hooks`](https://github.com/headroomlabs-ai/headroom/tree/main/plugins/headroom-agent-hooks) plugin exposes lightweight startup hooks for **Claude Code** and **GitHub Copilot CLI**. It ensures a matching durable `headroom init` deployment exists and starts it if needed — so agents never begin work against a dead proxy. ## What the hooks do [#what-the-hooks-do] Each hook runs one hidden helper: ```bash headroom init hook ensure ``` It checks for a matching durable `headroom init` deployment and starts it if needed. The plugin registers it at two moments (15s timeout each): | Hook | Matcher | Effect | |------|---------|--------| | `SessionStart` | `startup\|resume` | deployment is ready before the first turn | | `PreToolUse` | `Bash\|PowerShell` | shell tools always hit a live proxy | ## Install [#install] - **Claude Code**: the plugin ships a `.claude-plugin/plugin.json` manifest — install it as a Claude Code plugin from the headroom repository. - **GitHub Copilot CLI**: a matching `.github/plugin/plugin.json` manifest is included for Copilot's plugin mechanism. Both paths use the same `hooks/hooks.json` definitions, so behavior is identical. Repeated agent wakes usually rebuild the same expensive prompt shape. The useful split is simple: keep the byte-stable prefix first, keep the volatile wake-specific digest later, and keep run-specific context at the end so the repeated prefix stays cacheable. ## Repeated wake anatomy [#repeated-wake-anatomy] Each wake tends to carry four different layers: - stable instructions, project rules, and tool contracts - the current task and other instruction-bearing fields - the volatile per-wake memory digest - recent loop state, tool output, and child-agent context The stable layers should stay byte-identical across wakes. The volatile layers should move to the live zone, where they can change without invalidating the cacheable prefix. ## CacheAligner is detector-only [#cachealigner-is-detector-only] CacheAligner does not rewrite messages. It inspects the prefix, emits warnings for volatile content, and records observability data so callers can fix their own assembly logic. The detector reports: | Field | What it tells you | |---|---| | `warnings` | Which parts of the prefix look unstable | | `cache_metrics.stable_prefix_bytes` | Size of the stable prefix in bytes | | `cache_metrics.stable_prefix_tokens_est` | Estimated token size of the stable prefix | | `cache_metrics.stable_prefix_hash` | Hash of the stable prefix for repeated-wake comparison | | `cache_metrics.prefix_changed` | Whether the stable prefix drifted since the last wake | | `cache_metrics.previous_hash` | Hash from the previous wake, when available | | `markers` | The emitted `stable_prefix_hash` marker for downstream observability | If CacheAligner warns about drift, keep the prefix stable in the caller. The transform is a detector, not a repair pass. ## Stable prefix layout [#stable-prefix-layout] Put the byte-stable prefix first, then the live wake digest, then any run-specific context. That layout keeps the provider cache path predictable: 1. Stable instructions stay identical. 2. The wake digest changes without disturbing the cacheable prefix. 3. Recent tool output and child-agent context stay outside the repeatable prefix. ## Real wake measurements [#real-wake-measurements] Use the issue comment guidance when measuring real wakes: | Field | Record | |---|---| | Prompt section id | Name the section that changed or repeated | | Byte/token estimate | Capture the section size before and after curation | | Digest version | Track which digest schema was used | | Cache-hit expectation | Note whether the section should stay cacheable | | Compressed size | Record the compacted payload size | | Section type | Mark the section as instruction-bearing or safe to compress | That checklist helps separate the repeated prefix from the volatile digest before you decide where CCR belongs. ## CCR digest curation [#ccr-digest-curation] CCR keeps compression reversible. The compressed digest can stay compact while the original backing detail remains recoverable from the local store. The pieces that matter here are: - `headroom_retrieve` for on-demand recovery of stored originals - `HEADROOM_CCR_TTL_SECONDS` for sizing the local store lifetime - `compression_strategy` as the authoritative discriminator on stored CCR entries For routing decisions, the same rule in plain terms is: headroom_retrieve recovers originals, HEADROOM_CCR_TTL_SECONDS sizes the local lifetime, compression_strategy identifies the producing path, and shape inference is not the routing authority. When a stored original expires, regenerate the digest or re-read the source content. Do not infer routing from payload shape. Use the stored `compression_strategy` metadata to understand how the original was produced. ## Digest routing [#digest-routing] Route fields by how much exact wording they need at wake time. | Field | Suggested handling | Why | |---|---|---| | Current task | Verbatim | It is instruction-bearing and changes the next action | | Hard constraints | Verbatim | These are safety and acceptance boundaries | | Definitions of done | Verbatim | The wording needs to survive every wake intact | | Irreversible decisions | Verbatim summary plus retrievable backing detail | The summary stays short, the backing detail stays exact | | Open threads | Compact summary plus CCR-backed backing detail | The live thread can shrink while the source stays recoverable | | Learnings | Compact summary | They inform the next wake without needing exact prose | | File/search/tool outputs | CCR-backed compression | These are large backing details that should stay retrievable | | Prose notes | Compact summary, CCR-backed when bulky | Keep the digest readable without losing source detail | | Bulk JSON-ish arrays | SmartCrusher or ContentRouter, with CCR-backed backing detail when needed | Structured blobs are usually the first thing to explode in size | The important boundary is simple: instruction-bearing fields stay verbatim, or they get a verbatim compact summary plus retrievable backing detail. CCR-backed content is the backing detail, not the instruction itself. Hard constraints stay verbatim; current task text stays verbatim; file/search/tool outputs use CCR-backed backing detail when they are large enough to compress. ## Integration modes [#integration-modes] Choose the integration mode by where the orchestrator controls message assembly. | Mode | Use when | Notes | |---|---|---| | Proxy | You want spawned agents and normal client traffic to pass through a local provider endpoint | Good for `headroom proxy --mode cache` and provider base URL routing | | Library | The orchestrator owns message assembly and wants to shape the digest before launch | Best when the caller can decide what becomes stable prefix versus live digest | | MCP | Agents need on-demand compression and retrieval tools | Best when `headroom_retrieve` should be available as a tool | | Proxy plus MCP | You need traffic shaping and tool-level retrieval together | Useful when both the provider edge and the agent toolset matter | ## Local-first deployment [#local-first-deployment] Keep the deployment local to the user or session: - run one local proxy or MCP process per user session - do not assume a central proxy - treat the local CCR store as process-local unless the deployment explicitly shares it - size the TTL for the longest realistic autonomous run - assume a store can expire before the run finishes, then regenerate or re-read the source content That model keeps the data boundary obvious. The orchestrator can still recover backing detail, but the cacheable prefix stays small and stable. ## Source-backed caveats [#source-backed-caveats] - Prompt-cache hits require a byte-identical stable prefix. - CacheAligner identifies drift, it does not repair prompt assembly. - CCR retrieval depends on store lifetime and stored hashes. - Provider-neutral guidance still applies, so keep the guide away from Orcha-specific runtime claims. - Use `compression_strategy` to read stored CCR intent, not payload shape. Headroom integrates with [Agno](https://github.com/agno-agi/agno) (formerly Phidata) to compress context for AI agents. Wrap any Agno model for automatic optimization, and use hooks for observability. ## Installation [#installation] ```bash pip install "headroom-ai[agno]" agno ``` ## Quick start [#quick-start] ```python from agno.agent import Agent from agno.models.openai import OpenAIChat from headroom.integrations.agno import HeadroomAgnoModel model = HeadroomAgnoModel(wrapped_model=OpenAIChat(id="gpt-4o")) agent = Agent(model=model) response = agent.run("What's the capital of France?") print(f"Tokens saved: {model.total_tokens_saved}") print(model.get_savings_summary()) # {'total_requests': 1, 'total_tokens_saved': 245, 'average_savings_percent': 12.3, # 'total_tokens_before': 1990, 'total_tokens_after': 1745} ``` Works with any Agno provider: ```python from agno.models.anthropic import Claude from agno.models.google import Gemini claude_model = HeadroomAgnoModel(wrapped_model=Claude(id="claude-sonnet-4-20250514")) gemini_model = HeadroomAgnoModel(wrapped_model=Gemini(id="gemini-2.0-flash")) ``` ## Observability hooks [#observability-hooks] Use hooks for detailed tracking without modifying your model: ```python from headroom.integrations.agno import ( HeadroomAgnoModel, HeadroomPreHook, HeadroomPostHook, ) model = HeadroomAgnoModel(wrapped_model=OpenAIChat(id="gpt-4o")) pre_hook = HeadroomPreHook() post_hook = HeadroomPostHook(token_alert_threshold=10000) agent = Agent( model=model, pre_hooks=[pre_hook], post_hooks=[post_hook], ) response = agent.run("Analyze this large dataset...") # Check for alerts if post_hook.alerts: print(f"{len(post_hook.alerts)} requests exceeded threshold") ``` Or use the convenience factory: ```python from headroom.integrations.agno import create_headroom_hooks pre_hook, post_hook = create_headroom_hooks( token_alert_threshold=5000, log_level="DEBUG", ) ``` ## Tool-heavy agents [#tool-heavy-agents] Tool outputs (JSON, logs, search results) see the biggest compression gains at 70-90% reduction: ```python from agno.tools.duckduckgo import DuckDuckGoTools model = HeadroomAgnoModel(wrapped_model=OpenAIChat(id="gpt-4o")) agent = Agent( model=model, tools=[DuckDuckGoTools()], show_tool_calls=True, ) response = agent.run("Research the latest AI developments") print(f"Tokens saved: {model.total_tokens_saved}") ``` ## Async support [#async-support] ```python import asyncio async def process(): model = HeadroomAgnoModel(wrapped_model=OpenAIChat(id="gpt-4o")) response = await model.aresponse(messages) async for chunk in model.aresponse_stream(messages): print(chunk, end="", flush=True) asyncio.run(process()) ``` ## Standalone message optimization [#standalone-message-optimization] Optimize messages without wrapping a model: ```python from headroom.integrations.agno import optimize_messages optimized, metrics = optimize_messages(messages, model="gpt-4o") print(f"Tokens saved: {metrics['tokens_saved']}") ``` ## Session management [#session-management] Reset metrics between sessions: ```python model = HeadroomAgnoModel(wrapped_model=OpenAIChat(id="gpt-4o")) # Session 1 agent.run("First conversation...") print(model.get_savings_summary()) # Reset for new session model.reset() # Session 2 starts fresh agent.run("Second conversation...") ``` ## Supported providers [#supported-providers] | Provider | Agno Model | Auto-Detected | |----------|-----------|---------------| | OpenAI | `OpenAIChat`, `OpenAILike` | Yes | | Anthropic | `Claude`, `AwsBedrock` | Yes | | Google | `Gemini`, `VertexAI` | Yes | | Groq | `Groq` | Yes | | Mistral | `Mistral` | Yes | | Ollama | `Ollama` | Yes | Headroom wraps the Anthropic TypeScript SDK to automatically compress messages before every `messages.create()` call. All other methods pass through unchanged. ## Installation [#installation] ```bash npm install headroom-ai @anthropic-ai/sdk ``` The TypeScript SDK sends messages to a local Headroom proxy for compression. Start the proxy before using the SDK: ```bash pip install "headroom-ai[proxy]" headroom proxy ``` ## Quick start [#quick-start] ```ts twoslash import { withHeadroom } from 'headroom-ai/anthropic'; import Anthropic from '@anthropic-ai/sdk'; const client = withHeadroom(new Anthropic()); const response = await client.messages.create({ model: 'claude-sonnet-4-5-20250929', messages: longConversation, max_tokens: 1024, }); ``` Every call to `client.messages.create()` compresses messages first. The response format is identical to the unwrapped client. ## How it works [#how-it-works] `withHeadroom()` returns a proxy around your Anthropic client that intercepts `messages.create()`: 1. Sends your `messages` to the Headroom proxy's `/v1/compress` endpoint as they are, in Anthropic format 2. Forwards the request to Anthropic with the compressed messages in place ### Message format [#message-format] Nothing is converted. `POST /v1/compress` compresses Anthropic content blocks natively — see [Message format](/docs/proxy#message-format) — and returns the same shape it received, so every block reaches Anthropic exactly as you built it: - `thinking` and `redacted_thinking` blocks keep their `signature` and `data`, which Anthropic requires back unmodified within a tool-use turn - `cache_control` markers stay on the blocks that carry them - `tool_use` and `tool_result` blocks keep their ids; only the content of large `tool_result` blocks changes - images, documents, citations and server-tool blocks pass through untouched If the proxy is unreachable and `fallback` is enabled, the original messages are forwarded unchanged. ## Options [#options] Pass compression options as the second argument: ```ts twoslash import { withHeadroom } from 'headroom-ai/anthropic'; import Anthropic from '@anthropic-ai/sdk'; const client = withHeadroom(new Anthropic(), { model: 'claude-sonnet-4-5-20250929', baseUrl: 'http://localhost:8787', }); ``` ## Streaming [#streaming] Streaming works normally. Compression happens before the request: ```ts twoslash import { withHeadroom } from 'headroom-ai/anthropic'; import Anthropic from '@anthropic-ai/sdk'; const client = withHeadroom(new Anthropic()); const stream = await client.messages.create({ model: 'claude-sonnet-4-5-20250929', messages: longConversation, max_tokens: 1024, stream: true, }); ``` ## Tool use [#tool-use] Tool results are where compression has the biggest impact. Large JSON payloads from tool calls are compressed automatically: ```ts twoslash import { withHeadroom } from 'headroom-ai/anthropic'; import Anthropic from '@anthropic-ai/sdk'; const client = withHeadroom(new Anthropic()); const response = await client.messages.create({ model: 'claude-sonnet-4-5-20250929', max_tokens: 1024, messages: [ { role: 'user', content: 'What went wrong?' }, { role: 'assistant', content: [ { type: 'tool_use', id: 'toolu_1', name: 'get_logs', input: { service: 'api' } }, ], }, { role: 'user', content: [ { type: 'tool_result', tool_use_id: 'toolu_1', content: hugeLogOutput, // Compressed automatically }, ], }, ], tools: [{ name: 'get_logs', description: 'Get logs', input_schema: { type: 'object', properties: {} } }], }); ``` Complete API reference for the Headroom Python and TypeScript SDKs. ## Core [#core] ### HeadroomClient [#headroomclient] The main entry point for the Headroom SDK. ```ts twoslash import { HeadroomClient } from 'headroom-ai'; const client = new HeadroomClient({ baseUrl: 'http://localhost:8787', apiKey: 'your-api-key', timeout: 30_000, fallback: true, retries: 2, }); ``` **Constructor Parameters** ```python from headroom import HeadroomClient, OpenAIProvider, HeadroomConfig, SmartCrusherConfig, CacheAlignerConfig from openai import OpenAI client = HeadroomClient( original_client=OpenAI(), provider=OpenAIProvider(), default_mode="optimize", config=HeadroomConfig( smart_crusher=SmartCrusherConfig(min_tokens_to_crush=100), cache_aligner=CacheAlignerConfig(enabled=True), ), ) ``` ### chat.completions.create() [#chatcompletionscreate] Create a chat completion with optional optimization. The TypeScript SDK uses `compress()` to optimize messages before sending them to your LLM client: ```ts twoslash import { compress } from 'headroom-ai'; const result = await compress(messages, { model: 'gpt-4o', tokenBudget: 100_000, }); // Then pass result.messages to your LLM client ``` Accepts all standard OpenAI/Anthropic parameters plus Headroom-specific overrides: ```python response = client.chat.completions.create( model="gpt-4o", messages=[...], headroom_mode="optimize", headroom_keep_turns=5, ) ``` `headroom_tool_profiles` is accepted but not currently wired through `TransformPipeline.apply()`, so passing it changes nothing. To keep specific tool outputs from being compressed as aggressively, use `config.smart_crusher.max_items_after_crush` or `config.smart_crusher.enabled = False` instead — see [Compression Too Aggressive](/docs/troubleshooting#compression-too-aggressive). ### chat.completions.simulate() [#chatcompletionssimulate] Preview optimization without making an API call. ```python plan = client.chat.completions.simulate( model="gpt-4o", messages=[...], ) print(f"Tokens: {plan.tokens_before} -> {plan.tokens_after}") print(f"Savings: {plan.tokens_saved/plan.tokens_before*100:.1f}%") print(f"Transforms: {plan.transforms}") ``` **Returns:** `SimulationResult` ### compress() (TypeScript) [#compress-typescript] Top-level function to compress messages via the Headroom proxy. ```ts twoslash import { compress } from 'headroom-ai'; const result = await compress(messages, { model: 'gpt-4o', baseUrl: 'http://localhost:8787', timeout: 15_000, fallback: true, retries: 2, tokenBudget: 100_000, }); ``` ### get_stats() [#get_stats] Quick stats for the current session (no database query). ```python stats = client.get_stats() # Returns dict with "session", "config", and "transforms" keys ``` ### get_metrics() [#get_metrics] Query stored metrics from the database. ```python from datetime import datetime, timedelta metrics = client.get_metrics( start_time=datetime.utcnow() - timedelta(hours=1), limit=100, ) ``` ### get_summary() [#get_summary] Aggregate statistics across all stored metrics. ```python summary = client.get_summary() # Returns dict with total_requests, total_tokens_before, total_tokens_after, # total_tokens_saved, avg_tokens_saved, avg_cache_alignment, # audit_count, optimize_count ``` ### validate_setup() [#validate_setup] Validate that the client is configured correctly. ```python result = client.validate_setup() # Returns {"valid": bool, "provider": {...}, "storage": {...}, # "config": {...}, "cache_optimizer": {...}}, each with "ok"/"error" if not result["valid"]: for key in ("provider", "storage", "config", "cache_optimizer"): if not result[key]["ok"]: print(f" - {key}: {result[key]['error']}") ``` --- ## Configuration [#configuration] ### SmartCrusherConfig [#smartcrusherconfig] ```python from headroom import SmartCrusherConfig config = SmartCrusherConfig( min_tokens_to_crush=200, max_items_after_crush=15, variance_threshold=2.0, preserve_change_points=True, ) ``` ### CacheAlignerConfig [#cachealignerconfig] ```python from headroom import CacheAlignerConfig config = CacheAlignerConfig( enabled=True, use_dynamic_detector=True, normalize_whitespace=True, entropy_threshold=0.7, ) ``` ### Context management [#context-management] Context management is now handled automatically inside the pipeline (live-zone-only compression). Headroom never drops messages from the conversation history; it compresses only the newest content blocks (latest user message, latest tool result) and keeps the cache hot zone — system prompt, tools, and older turns — untouched. Use the `headroom_keep_turns` / `headroom_output_buffer_tokens` per-request overrides to tune behavior. The `RollingWindowConfig`, `IntelligentContextConfig`, and `ScoringWeights` classes have been retired from the Python package (`from headroom import RollingWindowConfig` fails) and from the compression pipeline itself; the TypeScript SDK's `HeadroomConfig` type still exports the equivalent field names as unused legacy type surface, wired to nothing. ### HeadroomConfig [#headroomconfig] ', description: 'Override context limits per model' }, smartCrusher: { type: 'SmartCrusherConfig', description: 'Smart crusher configuration' }, cacheAligner: { type: 'CacheAlignerConfig', description: 'Cache aligner configuration' }, cacheOptimizer: { type: 'CacheOptimizerConfig', description: 'Cache optimizer configuration' }, ccr: { type: 'CCRConfig', description: 'CCR (Compress-Cache-Retrieve) configuration' }, prefixFreeze: { type: 'PrefixFreezeConfig', description: 'Prefix freeze configuration' }, contentRouterEnabled: { type: 'boolean', description: 'Enable content-type routing' }, generateDiffArtifact: { type: 'boolean', description: 'Generate diff artifacts for debugging' }, }} /> The top-level config object that contains all sub-configurations: ```python from headroom import HeadroomConfig config = HeadroomConfig() config.smart_crusher.min_tokens_to_crush = 100 config.cache_aligner.enabled = True # Note: rolling_window has been removed — use headroom_keep_turns per-request instead ``` ### RelevanceScorerConfig [#relevancescorerconfig] --- ## Results [#results] ### CompressResult (TypeScript) [#compressresult-typescript] ### SimulationResult (Python) [#simulationresult-python] ### WasteSignals (Python) [#wastesignals-python] `plan.waste_signals` is a plain `dict[str, int]`, not a class instance: ### RequestMetrics (Python) [#requestmetrics-python] Cost estimates are computed on demand inside `HeadroomClient` (`estimate_cost()` on the provider) rather than stored on `RequestMetrics`. --- ## Providers [#providers] ### OpenAIProvider [#openaiprovider] ```python from headroom import OpenAIProvider provider = OpenAIProvider() counter = provider.get_token_counter("gpt-4o") tokens = counter.count_text("Hello, world!") limit = provider.get_context_limit("gpt-4o") # 128000 cost = provider.estimate_cost(input_tokens=1000, output_tokens=500, model="gpt-4o") ``` ### AnthropicProvider [#anthropicprovider] ```python from headroom import AnthropicProvider from anthropic import Anthropic provider = AnthropicProvider( client=Anthropic(), ) counter = provider.get_token_counter("claude-3-5-sonnet-latest") tokens = counter.count_messages(messages) # Accurate count via API ``` ### GoogleProvider [#googleprovider] ```python from headroom.providers import GoogleProvider provider = GoogleProvider() ``` --- ## Relevance Scoring [#relevance-scoring] ### create_scorer() [#create_scorer] Factory function to create scorers: ```python from headroom import create_scorer # Auto-select best available scorer scorer = create_scorer() # Explicitly choose type scorer = create_scorer(tier="hybrid", alpha=0.7) ``` ### BM25Scorer [#bm25scorer] Fast keyword-based scoring (zero dependencies): ```python from headroom import BM25Scorer scorer = BM25Scorer() scores = scorer.score_batch(["item 1", "item 2"], "search query") ``` ### EmbeddingScorer [#embeddingscorer] Semantic similarity scoring (requires `headroom-ai[relevance]`): ```python from headroom import EmbeddingScorer, embedding_available if embedding_available(): scorer = EmbeddingScorer(model_name="BAAI/bge-small-en-v1.5") scores = scorer.score_batch(items, query) ``` ### HybridScorer [#hybridscorer] Combines BM25 and embeddings: ```python from headroom import HybridScorer scorer = HybridScorer(alpha=0.5) # 50% BM25, 50% embedding scores = scorer.score_batch(items, query) ``` --- ## Transforms (Direct Use) [#transforms-direct-use] ### SmartCrusher [#smartcrusher] ```python from headroom import SmartCrusher import json crusher = SmartCrusher() result = crusher.crush(content=json.dumps({"results": [...]}), query="user query") ``` ### CacheAligner [#cachealigner] ```python from headroom import CacheAligner, Tokenizer, OpenAIProvider provider = OpenAIProvider() tokenizer = Tokenizer(provider.get_token_counter("gpt-4o"), "gpt-4o") aligner = CacheAligner() result = aligner.apply(messages, tokenizer) ``` ### TransformPipeline [#transformpipeline] ```python from headroom import TransformPipeline, SmartCrusher, CacheAligner pipeline = TransformPipeline(transforms=[ SmartCrusher(), CacheAligner(), ]) # model_limit is required for direct pipeline use — HeadroomClient # supplies it automatically from the provider's context limits result = pipeline.apply(messages, "gpt-4o", model_limit=128_000) ``` --- ## Errors [#errors] | Exception | Meaning | |-----------|---------| | `HeadroomError` | Base class for all errors | | `HeadroomConnectionError` | Cannot reach proxy | | `HeadroomAuthError` | 401 from proxy | | `HeadroomCompressError` | Compression failed (includes `statusCode`, `errorType`) | | `ConfigurationError` | Invalid configuration | | `ProviderError` | Provider issues | | `StorageError` | Storage failures | | `TokenizationError` | Token counting failed | | `CacheError` | Cache operations failed | | `ValidationError` | Validation failures | | `TransformError` | Transform execution failed | Use `mapProxyError(status, type, message)` to convert proxy error responses to the correct class. | Exception | Meaning | |-----------|---------| | `HeadroomError` | Base class for all Headroom errors | | `ConfigurationError` | Invalid config values | | `ProviderError` | Provider issue (unknown model, etc.) | | `StorageError` | Database issue | | `CompressionError` | Compression failed (rare) | | `ValidationError` | Setup validation failed | All exceptions include a `details` dict with additional context. --- ## Utilities [#utilities] ### Tokenizer [#tokenizer] ```python from headroom import Tokenizer, count_tokens_text, count_tokens_messages, OpenAIProvider provider = OpenAIProvider() token_counter = provider.get_token_counter("gpt-4o") # Quick counting tokens = count_tokens_text("Hello, world!", token_counter) # With tokenizer instance tokenizer = Tokenizer(token_counter, "gpt-4o") tokens = tokenizer.count_text("Hello") tokens = tokenizer.count_messages(messages) ``` ### generate_report() [#generate_report] Generate an HTML report from stored metrics: ```python from datetime import datetime, timedelta, timezone from headroom import generate_report path = generate_report( store_url="sqlite:////absolute/path/to/headroom.db", output_path="report.html", start_time=datetime.now(timezone.utc) - timedelta(days=1), ) # returns the path to the generated HTML file ``` --- ## TypeScript Message Types [#typescript-message-types] The TypeScript SDK uses the standard OpenAI message format with `SystemMessage`, `UserMessage`, `AssistantMessage`, and `ToolMessage` variants. Headroom sits between your application and the LLM provider. It intercepts the request, compresses the parts that carry the most redundant tokens — tool outputs, file reads, logs, search results — and forwards the optimized request upstream. The provider's response is returned unchanged. ## High-level flow [#high-level-flow] ``` +---------------------------------------------------------------+ | YOUR APPLICATION | +---------------------------------------------------------------+ | v +---------------------------------------------------------------+ | HEADROOM | | Proxy (FastAPI) · Python compress() · TS compress() | | | | | v | | Transform pipeline ──▶ ContentRouter | | (detect content type, route to one compressor) | | | | | v | | Backend (direct · LiteLLM · any-llm) | +---------------------------------------------------------------+ | v +---------------------------------------------------------------+ | OPENAI · ANTHROPIC · GOOGLE · BEDROCK · 100+ | +---------------------------------------------------------------+ ``` ## Entry points [#entry-points] Headroom can be used three ways, all feeding the same compression pipeline: | Entry point | How it works | Code changes | |-------------|-------------|--------------| | **Proxy mode** | Run `headroom proxy` and point your client's base URL at it | Zero — just change the base URL | | **SDK mode** | Call `compress()` (Python or TypeScript) on your messages before you send them, or wrap an existing client with `HeadroomClient` for automatic per-call optimization and stats | Minimal — one function call, or a client wrapper | | **Integrations** | LangChain, Vercel AI SDK, Agno, Strands, LiteLLM, MCP adapters | Framework-specific setup | In proxy mode the server is a FastAPI app with per-provider handlers (Anthropic, OpenAI, Gemini, Bedrock) that each run the same compression pipeline before forwarding through the selected [backend](/docs/proxy#cloud-providers). ## The compression pipeline [#the-compression-pipeline] The proxy assembles a small, ordered pipeline. Every transform is independent, safe to skip, and **fails open** — on any error it returns the content unchanged and the request still goes through. 1. **Tool-result interceptor** *(canary opt-in)* — light structural interceptors such as ast-grep Read outlining. Requires `HEADROOM_ROLLOUT_CHANNEL=canary` plus `--intercept-tool-results`. 2. **CacheAligner** *(off by default)* — a detector that reports dynamic-prefix drift (dates, UUIDs, session tokens). It **never mutates, moves, or rewrites** content. It is disabled by default and hard-disabled inside the proxy; it exists to surface prefix-stability metrics, not to change your messages. 3. **ContentRouter** — the workhorse that does essentially all of the compression. See below. The pipeline **never drops or reorders messages**. Earlier versions shipped a "context manager" stage (rolling-window / intelligent-context scoring) that deleted old turns to make the request fit the context window. That stage was **removed** — the pipeline no longer runs any dropping/scoring logic, and the `RollingWindowConfig`, `IntelligentContextConfig`, and `ScoringWeights` classes are gone from the Python package (the TypeScript SDK's types still export the equivalent field names, but wired to nothing). Headroom now does *live-zone-only* compression: it compresses content in place and leaves the message list intact. See [Context Management](/docs/context-management). ### ContentRouter [#contentrouter] ContentRouter detects the type of each content block and dispatches it to exactly one compressor: | Detected content | Compressor | Typical savings | |---|---|---| | JSON arrays (tool outputs) | SmartCrusher | 70–90% | | Source code | CodeAwareCompressor (opt-in; off by default) | 40–70% | | Search / grep results | SearchCompressor | 80–95% | | Build / test logs | LogCompressor | 85–95% | | Diffs | DiffCompressor | 40–80% | | HTML | HTMLExtractor (trafilatura) | 70–90% | | Tabular (CSV/TSV/markdown tables) | TabularCompressor | 60–90% | | Structured config (YAML/TOML/INI) | ConfigCompressor | 40–70% | | Plain text | TextCrusher | 30–60% | | Anything else | Kompress (ML fallback) | varies | To avoid recompressing the same content, ContentRouter keeps a **two-tier, TTL-bounded cache**: a *skip set* of content already known not to compress, and a *result cache* of previously compressed output. Default TTL is 30 minutes. ### Rust core [#rust-core] The heaviest compressors run in a native Rust extension — `headroom._core`, built with PyO3 — that the proxy loads at startup. SmartCrusher and the search/log/diff compressors and content detection are Rust-backed; the Python classes you import are thin, API-compatible shims over them. The ML fallback (**Kompress**, a ModernBERT token compressor) runs separately through ONNX Runtime, either locally or offloaded to a remote endpoint (see [Text & Logs](/docs/text-and-logs)). ## Compression modes and provider caches [#compression-modes-and-provider-caches] Headroom runs in one of two modes (`--mode`, default `cache`): - **`cache` mode (default)** — compresses only the newest delta in each turn and forwards prior turns byte-faithfully, so the provider's prefix cache is never invalidated mid-conversation. Best for coding agents and any long, multi-turn session. - **`token` mode** — prioritizes raw token removal and may recompress earlier turns, trading some cache stability for maximum savings. Because prefix caching is where most of the cost savings live on multi-turn workloads, keeping the stable prefix intact matters. What each provider's cache buys you: | Provider | Mechanism | Savings on cached tokens | |----------|-----------|--------------------------| | Anthropic | `cache_control` on the stable prefix | up to ~90% | | OpenAI | automatic prefix caching | up to ~50% | | Google | `CachedContent` API | up to ~75% | See [Cache Optimization](/docs/cache-optimization) and [Savings Profiles](/docs/proxy#savings-profiles) for how the two modes interact with the built-in profiles. ## CCR: Compress-Cache-Retrieve [#ccr-compress-cache-retrieve] Compression is **reversible**. When ContentRouter compresses a tool output, the original is stored in a local Compress-Cache-Retrieve (CCR) store. If the model needs the full data, it calls a `headroom_retrieve` tool and gets the original back. ``` Compress: 1000 items -> 15 items (original stored in CCR) Cache: hash-indexed local SQLite store Retrieve: model calls headroom_retrieve("abc123") -> original 1000 items ``` CCR is **on by default**. Disable the markers and the injected retrieval tool with `--no-ccr`, or run a marker-free, format-native lossless mode with `--lossless`. See [Reversible Compression (CCR)](/docs/ccr). ## TOIN: Tool Output Intelligence Network [#toin-tool-output-intelligence-network] TOIN learns which fields matter for a given tool over repeated calls — which items get retrieved, which fields carry signal — and feeds that back into SmartCrusher's importance scoring so compression gets sharper for the tools you actually use. TOIN is **local and observation-only**: it aggregates statistics on your own machine (or your own proxy instance). The learned patterns stay on your machine and are not shared across users. When the anonymous session beacon is on (the default; see [Security model](/docs/security-model#the-beacon-is-on-by-default)), a content-free structural descriptor of each tool output (bucketed field count, nesting depth, shape flags) and token counts are included in it; the same switches turn that off. For a brand-new tool type it falls back to statistical heuristics and improves as it observes more calls. ## What Headroom does not rewrite [#what-headroom-does-not-rewrite] - **Your prompt text** — the natural-language instructions you write are preserved. Compression targets bulk content blocks (tool outputs, file reads, logs), not your intent. - **System prompts** — preserved by default so the hottest part of the prefix cache stays stable. A savings profile can opt into compacting them. - **Code** — passes through unchanged unless AST-based code compression is explicitly enabled (off by default). - **Model responses** — returned from the provider unchanged. - **Short content** — blocks below the minimum-token threshold pass through (overhead would exceed savings). Headroom integrates with [AutoGen](https://github.com/microsoft/autogen) (`autogen-agentchat` >=0.7) to compress tool outputs before they enter the agent's model context. Tool-heavy agents that return large JSON arrays, database results, or verbose logs see the largest token reductions. ## Installation [#installation] ```bash pip install headroom-ai autogen-agentchat ``` ## Quick start [#quick-start] Wrap tools in one line: ```python from autogen_agentchat.agents import AssistantAgent from autogen_core.tools import FunctionTool from headroom.integrations.autogen import wrap_tools_with_headroom def search_database(query: str) -> str: """Search the database and return results.""" return json.dumps({"results": [...], "total": 1000}) tool = FunctionTool(search_database, description="Search the database") wrapped = wrap_tools_with_headroom([tool]) agent = AssistantAgent( name="researcher", model_client=model_client, tools=wrapped, ) ``` ## Per-tool metrics [#per-tool-metrics] Track compression stats across all tool invocations: ```python from headroom.integrations.autogen import get_tool_metrics metrics = get_tool_metrics() print(metrics.get_summary()) # { # 'total_invocations': 25, # 'total_compressions': 18, # 'total_chars_saved': 450000, # 'average_compression_ratio': 0.35, # 'by_tool': { # 'search_database': {'invocations': 15, 'compressions': 12, 'chars_saved': 320000}, # } # } ``` Reset between sessions: ```python from headroom.integrations.autogen import reset_tool_metrics reset_tool_metrics() ``` ## Custom configuration [#custom-configuration] Control the compression threshold: ```python wrapped = wrap_tools_with_headroom( [search_tool, log_tool], min_chars_to_compress=500, # Default: 1000 ) ``` Use a dedicated metrics collector: ```python from headroom.integrations.autogen import ToolMetricsCollector, wrap_tools_with_headroom collector = ToolMetricsCollector() wrapped = wrap_tools_with_headroom( [search_tool], metrics_collector=collector, ) print(collector.get_summary()) ``` ## Wrapping individual tools [#wrapping-individual-tools] For finer control, wrap tools individually: ```python from headroom.integrations.autogen import HeadroomToolWrapper wrapper = HeadroomToolWrapper( search_tool, min_chars_to_compress=500, ) # Get the wrapped FunctionTool compressed_tool = wrapper.as_function_tool() agent = AssistantAgent( name="researcher", model_client=model_client, tools=[compressed_tool], ) ``` ## Async support [#async-support] AutoGen tools are natively async. The wrapper handles both sync and async tool functions transparently: ```python async def async_search(query: str) -> str: """Async database search.""" results = await db.search(query) return json.dumps(results) tool = FunctionTool(async_search, description="Async search") wrapped = wrap_tools_with_headroom([tool]) # Compression works identically for async tools ``` ## How it works [#how-it-works] AutoGen routes tool execution through `FunctionTool`, which wraps a plain Python function. The function's return value is stringified and becomes `FunctionExecutionResult.content` — what the LLM reads on its next turn. `HeadroomToolWrapper` creates a new `FunctionTool` with a wrapper function that: 1. Calls the original function 2. Checks if the stringified output exceeds `min_chars_to_compress` 3. If so, compresses via Headroom's `compress_tool_result()` 4. Records metrics and returns the compressed string The wrapper preserves the original tool's name, description, and parameter schema, so it works as a drop-in replacement. ### Why not `tool_call_summary_formatter`? [#why-not-tool_call_summary_formatter] AutoGen's `AssistantAgent` accepts a `tool_call_summary_formatter` parameter, which looks like a natural hook. However, it only controls the **final summary message** emitted after the tool loop exits — it does not touch the raw `FunctionExecutionResult` that gets added to `model_context` (what the LLM actually reads). Wrapping the function is the only clean interception point. Headroom's core promise: compress context without losing accuracy. This page covers compression benchmarks, accuracy evaluations, and latency overhead. Every number below is measured locally and reproducible (see [Reproducing Results](#reproducing-results)). For local inference, the main benefit is often faster prompt processing rather than lower API spend. See [Local LLM prefill benchmarking](/docs/local-llm-prefill) for a reproducible passthrough-vs-optimized proxy workflow. ## Compression Performance [#compression-performance] Measured on Apple M-series (CPU), Headroom 0.37.0, via `python benchmarks/bench_latency.py` (10 warm iterations per scenario; latency is p50 compression overhead, not an LLM call). | Content Type | Original | Compressed | Saved | Ratio | Latency (p50) | |---|---|---|---|---|---| | JSON array — search results (100 items) | 10,200 | 5,300 | 4,900 | **48%** | 0.20ms | | JSON array — search results (500 items) | 50,200 | 25,800 | 24,400 | **49%** | 0.77ms | | Structured logs (100 entries) | 7,600 | 3,600 | 4,000 | **53%** | 0.17ms | | Structured logs (500 entries) | 35,700 | 16,200 | 19,400 | **54%** | 0.51ms | | Documentation text (20K tokens) | 20,100 | 1,600 | 18,400 | **92%** | 1.4ms | | Python source (~200 lines) | 2,600 | 2,600 | 0 | 0.0% | 1.3ms | | **Total** | **126,400** | **55,100** | **71,300** | **56%** | **4.4ms** | Python source shows 0% compression: SmartCrusher only compresses JSON arrays, and code passes through to preserve correctness (verified: `headroom/transforms/smart_crusher.py:242`). ## Accuracy Benchmarks [#accuracy-benchmarks] ### HTML Extraction [#html-extraction] **Dataset**: Scrapinghub Article Extraction Benchmark (181 HTML pages with ground truth), via `allenai/scrapinghub-article-extraction-benchmark` on Hugging Face. | Metric | Value | |---|---| | **F1 Score** | 0.919 | | **Precision** | 0.875 | | **Recall** | 0.985 | | **Compression** | 94.8% | For LLM applications, recall is critical -- 98.5% means nearly all article content is preserved. The slight precision drop (some extra content) does not hurt LLM accuracy. ### JSON Compression (SmartCrusher) [#json-compression-smartcrusher] **Test**: 100-entry JSON log array with one anomalous error entry (error code, resolution, and affected count) injected at position 67, compressed via the public `compress()` API with an OpenAI-format tool-result message. | Metric | Value | |---|---| | Input tokens (before) | 4,937 | | Input tokens (after) | 3,053 | | Compression | **38.2%** | | Error entry preserved in output | Yes (verified by substring check) | SmartCrusher preserves first N items (schema), last N items (recency), all anomalies (errors, warnings), and statistical distribution. This test confirms the anomalous entry survives compression; it does not include an LLM-graded "correct answers" comparison, which would require a paid API call to reproduce. ### QA Accuracy Preservation [#qa-accuracy-preservation] This comparison (`tests/test_evals/test_html_oss_benchmarks.py::TestQAAccuracyPreservation`) runs SQuAD questions against both the original HTML and the extracted text using an LLM judge, and only executes when `OPENAI_API_KEY` is set. No committed result artifact for it exists in this repo, so no number is published here. Set `OPENAI_API_KEY` and run `pytest tests/test_evals/test_html_oss_benchmarks.py -k qa_accuracy -v -s` to reproduce it yourself. ## Latency Overhead [#latency-overhead] ### SDK Compression Latency [#sdk-compression-latency] Measured per-scenario on Apple M-series (CPU) via `python benchmarks/bench_latency.py --scenario json --iterations 10`. This is the local compression pipeline's own overhead — no LLM call is involved. | Scenario | Tokens In | Tokens Out | Saved | p50 (ms) | p95 (ms) | |----------|-----------|------------|-------|----------|----------| | JSON: Search Results (100 items) | 10.2K | 5.3K | 4.9K | 0.20 | 0.21 | | JSON: Search Results (500 items) | 50.2K | 25.8K | 24.4K | 0.77 | 1.1 | | JSON: Search Results (1K items) | 100.5K | 51.7K | 48.8K | 1.4 | 1.6 | | JSON: API Responses (500 items) | 38.9K | 21.8K | 17.0K | 0.77 | 0.80 | | JSON: Database Rows (1K rows) | 43.7K | 16.2K | 27.5K | 0.78 | 0.81 | | JSON: String Array (100 strings) | 1.1K | 226 | 825 | 0.15 | 0.16 | | JSON: String Array (500 strings) | 4.9K | 228 | 4.6K | 0.11 | 0.12 | | JSON: Number Array (200 numbers) | 1.2K | 146 | 1.1K | 0.17 | 0.18 | | JSON: Mixed Array (250 items) | 2.3K | 1.1K | 1.2K | 0.24 | 0.25 | ### Cost-Benefit Analysis [#cost-benefit-analysis] Net latency benefit = LLM time saved from fewer tokens minus compression overhead (at Claude Sonnet 4.5's 0.03ms/token prefill rate, $3.0/MTok input pricing): | Scenario | Compress (ms) | LLM Saved (ms) | Net Benefit | Savings per 1K Requests | |----------|---------------|-----------------|-------------|------------------------| | JSON: Search Results (100 items) | 0.20 | 146 | **+145.8ms** | $14.60 | | JSON: Search Results (500 items) | 0.77 | 731 | **+730.2ms** | $73.09 | | JSON: Search Results (1K items) | 1.4 | 1,464 | **+1,462.6ms** | $146.40 | | JSON: API Responses (500 items) | 0.77 | 511 | **+510.2ms** | $51.10 | | JSON: Database Rows (1K rows) | 0.78 | 824 | **+822.9ms** | $82.36 | Compression paid for itself in latency for all 25 compressing scenarios measured (JSON, structured logs, agentic multi-turn, and long-document text) against Claude Sonnet 4.5. Slower and more expensive models (Opus) benefit even more, since the same compression overhead offsets a larger per-token prefill cost. ### Pipeline Step Timing [#pipeline-step-timing] Measured via the same `bench_latency.py` run's per-transform breakdown (content detection + routing is the dominant step in every JSON scenario): | Scenario | `content_router` p50 | % of total pipeline time | |---|---|---| | JSON: String Array (500 strings) | 0.08ms | 68% | | JSON: Search Results (100 items) | 0.16ms | 82% | | JSON: Mixed Array (250 items) | 0.21ms | 85% | | JSON: Database Rows (1K rows) | 0.72ms | 95% | | JSON: Search Results (5K items) | 6.6ms | 99% | ContentRouter accounted for 68--99% of pipeline cost across the 14 JSON scenarios measured (mean ~87%), the rest going to tokenization and the compression transform itself. ## Reproducing Results [#reproducing-results] ```bash git clone https://github.com/headroomlabs-ai/headroom.git cd headroom pip install -e ".[evals,html]" pytest tests/test_evals/ -v -s # Latency and compression-ratio tables above python benchmarks/bench_latency.py ``` LLM providers cache prompt prefixes to avoid reprocessing identical input on repeated calls. Headroom's **CacheAligner** is detector-only, so it surfaces prefix drift, reports observability data, and leaves message assembly to the caller. CacheAligner is **disabled by default** and hard-disabled inside the proxy — it never runs on your traffic unless you explicitly enable it, and even then it only *reports* metrics; it never repairs a prefix. In proxy mode, prefix-cache stability comes from **cache mode** (`--mode cache`, the default), which compresses only the newest delta and forwards prior turns byte-faithfully. See [Savings Profiles](/docs/proxy#savings-profiles). ## What CacheAligner reports [#what-cachealigner-reports] System prompts often contain dynamic content, such as dates, session IDs, and timestamps, that changes between requests. Even a single character difference at the start of a prompt invalidates the entire provider cache. CacheAligner does not extract, move, normalize, reorder, strip, compress, or rewrite content. It detects volatile content and reports the stable prefix hash plus cache metrics so you can fix the prefix at the source: | Signal | Meaning | |---|---| | `warnings` | The prefix contains unstable content | | `cache_metrics.stable_prefix_bytes` | Stable prefix size in bytes | | `cache_metrics.stable_prefix_tokens_est` | Stable prefix size in estimated tokens | | `cache_metrics.stable_prefix_hash` | Stable prefix hash for repeated-wake comparison | | `cache_metrics.prefix_changed` | The prefix drifted since the previous wake | | `markers` | The emitted `stable_prefix_hash` marker for observability | The prefix must stay byte-identical across requests for provider KV caches to reuse previously computed attention states. ## Provider-specific strategies [#provider-specific-strategies] Each LLM provider implements caching differently. Headroom applies the optimal strategy for each. ### Anthropic [#anthropic] Anthropic supports explicit `cache_control` blocks that mark content as cacheable. Cached input tokens cost **90% less** than regular input tokens. Keep the stable prefix byte-identical, then place provider cache markers where your client or orchestrator already assembles the request. Headroom's job is to surface prefix instability, not repair it. | Metric | Value | |---|---| | Cache read discount | 90% off input price | | Cache write cost | 25% premium on first write | | Cache TTL | 5 minutes (extended on hit) | ### OpenAI [#openai] OpenAI uses automatic **prefix caching**. If consecutive requests share the same message prefix, the provider reuses cached KV states. No explicit API markers are needed, but the prefix must be byte-identical. CacheAligner tells you when the prefix changed, which is the only signal you need to keep OpenAI prefix caching effective. | Metric | Value | |---|---| | Cache read discount | 50% off input price | | Activation | Automatic (prefix match) | | Min prefix length | 1024 tokens | ### Google [#google] Google provides the **CachedContent API**, which lets you explicitly cache large context (system instructions, documents, tools) and reference it across requests. Cached tokens cost **75% less**. Keep the prefix stable in your integration layer; Headroom reports when the cacheable zone drifts. The CachedContent lifecycle itself also stays in your integration layer. | Metric | Value | |---|---| | Cache read discount | 75% off input price | | Mechanism | Explicit CachedContent API objects | | Min cache size | 32,768 tokens | ## What this means in practice [#what-this-means-in-practice] Keep the stable prefix first, keep volatile content out of it, and treat CacheAligner warnings as a signal that the caller needs to move assembly logic. CacheAligner surfaces prefix instability, provider caches reward byte-identical prefixes, and the caller owns the actual message layout. Anthropic prefix trackers are retained for at least the resolved provider cache lifetime plus a 60-second cleanup margin, or the configured session cleanup TTL, whichever is longer. A configured cache lifetime also applies before the first confirmed cache write and when a request omits its TTL. Retention is tracked per conversation lineage: a one-hour session does not extend an unrelated five-minute session. Expired trackers are discarded before lookup; a later longer TTL cannot revive their frozen state. Cleanup and recreation retain the live lineage index, keeping sibling conversations and tool profiles isolated. Retaining a tracker is not a guarantee that the provider still has its cache. ## Cold-prefix recompaction (when the cache lapses) [#cold-prefix-recompaction-when-the-cache-lapses] Byte-identical forwarding only pays off while the prompt cache is warm. When a session goes idle past the provider's cache TTL, that cache is dead — so re-sending the prefix verbatim buys nothing, and it's the one safe moment to rewrite it. The **cold-prefix hook** detects a lapsed cache and recompacts the whole prefix (cross-turn dedupe + superseded-read drop + lossless folds) instead, then re-caches the smaller result. It only fires on a *confirmed*-cold turn — a wrong call would bust a warm cache — so it reads the real TTL (exact for Claude Code via its cache-control env vars; learned over time for other providers). For models that re-send **reasoning** as plain text every turn (Kimi/GLM/DeepSeek), a companion hook Kompresses that reasoning on warm turns and drops it on cold ones. Both are off by default. See [Cold-prefix hook & reasoning compaction](/docs/configuration#cold-prefix-hook--reasoning-compaction) for the exact flags and when each is safe to enable. Headroom's CCR (Compress-Cache-Retrieve) architecture makes compression **reversible**. When content is compressed, the original data is cached locally. If the LLM needs the full data, it retrieves it instantly. Unlike traditional lossy compression, CCR guarantees that every piece of original data remains accessible in the compression store, even when the compressed content the LLM sees is much smaller. Actual token savings depend heavily on how redundant the source data is -- see [How Compression Works](/docs/how-compression-works#content-type-detection) for measured ranges per content type. ## The problem with traditional compression [#the-problem-with-traditional-compression] Traditional compression forces a difficult tradeoff: - **Aggressive compression** risks losing data the LLM needs - **Conservative compression** misses out on token savings CCR eliminates this tradeoff entirely. Compress aggressively, retrieve on demand. ## Architecture [#architecture] CCR flows through four phases: ``` TOOL OUTPUT (1000 items) -> SmartCrusher compresses to 20 items -> Original cached with hash=abc123 -> Retrieval tool injected into context LLM PROCESSING Option A: LLM solves task with 20 items -> Done (90% savings) Option B: LLM calls headroom_retrieve(hash=abc123) -> Response Handler returns full data automatically ``` ## Phase 1: Compression Store [#phase-1-compression-store] When SmartCrusher compresses tool output: 1. The original content is stored, keyed by hash, with LRU-style eviction once the store hits capacity. In the proxy, this store defaults to a SQLite file (`ccr_store.db` in the workspace directory) rather than a pure in-process dict -- so it survives a proxy restart and is shared across worker processes. Set `HEADROOM_CCR_BACKEND=memory` to opt back into an in-process-only store. Other backend names are resolved via the `headroom.ccr_backend` setuptools entry-point group (e.g. a `redis` backend, if one is installed and registered) -- none besides `sqlite`/`memory` ship in this repo. See `headroom/cache/compression_store.py:1000-1017`. 2. A hash key is generated for retrieval 3. A marker is added to the compressed output: ``` [1000 items compressed to 20. Retrieve more: hash=abc123] ``` ## Phase 2: Tool Injection [#phase-2-tool-injection] Headroom injects a `headroom_retrieve` tool into the LLM's available tools: ```json { "name": "headroom_retrieve", "description": "Retrieve original uncompressed data from Headroom cache", "parameters": { "hash": "The hash key from the compression marker" } } ``` The LLM sees this tool alongside your application's tools and can call it whenever the compressed data is insufficient. If your client already registers retrieval through MCP, Headroom keeps that client-owned tool instead of adding a second bare `headroom_retrieve`. Supported spellings include OpenCode's `headroom_headroom_retrieve` and MCP wrappers such as `mcp__headroom__headroom_retrieve` and `mcp_Headroom_headroom_retrieve`. The same rule applies to session-aware injection and the per-request fallback, so Headroom does not advertise an unavailable bare retrieval tool alongside the client's executable alias. These ownership aliases are limited to the default `headroom` MCP server name. An unrelated server such as `mcp__other__headroom_retrieve` does not replace the proxy's retrieval tool. Optional CCR system instructions use the executable name selected from the available tool definitions. ## Phase 3: Response Handler [#phase-3-response-handler] When the LLM calls `headroom_retrieve`: 1. The Response Handler intercepts the tool call 2. Data is retrieved from the local cache (around 1ms) 3. The result is added to the conversation 4. The API call continues automatically On the Anthropic and OpenAI proxy paths, only retrieval-only responses naming the proxy's bare `headroom_retrieve` are resolved server-side. Mixed responses and all MCP-registered retrieval aliases are forwarded for the client to execute. Buffered native Gemini requests resolve `headroom_retrieve` server-side and return the model's final response. Streaming native Gemini requests keep the existing forwarding behavior. When a response contains `headroom_retrieve` alongside a client-owned function call, Headroom preserves both calls for the client instead of resolving the mixed response. Google's OpenAI-compatible Gemini endpoint can also return `finish_reason=MALFORMED_FUNCTION_CALL` on large function-response continuations after CCR retrieval; that separate limitation remains tracked in [issue #2041](https://github.com/headroomlabs-ai/headroom/issues/2041). Native Google batch contexts retain the effective forwarded Tool objects, including injected retrieval declarations and sibling Google Search or code execution tools. Continuations use those declarations alongside the original conversation rather than rebuilding a function-declaration wrapper. ## Phase 4: Context Tracker [#phase-4-context-tracker] Across multiple turns, the Context Tracker maintains awareness of all compressed content: 1. Remembers what was compressed in earlier turns 2. Analyzes new queries for relevance to compressed content 3. Proactively expands relevant data before the LLM asks ``` Turn 1: User searches for files -> 500 files compressed to 15, cached (hash=abc123) -> LLM answers with 15 files Turn 5: User asks "What about the auth middleware?" -> Context Tracker detects "auth" may match cached content -> Proactively expands compressed data -> LLM finds auth_middleware.py in the full list ``` ## Retrieving originals [#retrieving-originals] CCR works automatically on the Anthropic and OpenAI proxy paths, but you can also retrieve cached data programmatically: ```ts twoslash import { compress } from "headroom-ai"; import type { CCRConfig } from "headroom-ai"; // CCR is enabled by default when compressing through the proxy. const result = await compress(messages, { model: "gpt-4o", }); // Access compressed messages — CCR markers are embedded automatically console.log(result.messages); // CCR configuration options const ccrConfig: CCRConfig = { enabled: true, injectTool: true, // Inject headroom_retrieve tool injectRetrievalMarker: true, // Add retrieval markers to compressed output feedbackEnabled: true, // Learn from retrieval patterns storeMaxEntries: 1000, // Max cached items storeTtlSeconds: 3600, // Cache TTL }; ``` The full CCR loop -- markers injected, `headroom_retrieve` tool added to the request, and the model's retrieval call resolved automatically -- only runs through `headroom proxy` (`headroom/proxy/handlers/*.py` wire tool injection and the response handler; the direct SDK client does not). Point an OpenAI client at the proxy to get it: ```python from openai import OpenAI # Start `headroom proxy` first (defaults to port 8787); use --no-optimize # for a passthrough baseline or --no-ccr to disable CCR specifically. client = OpenAI(base_url="http://127.0.0.1:8787/v1", api_key="local") response = client.chat.completions.create( model="gpt-4o", messages=messages, ) ``` `HeadroomClient` (the direct SDK integration, no proxy) still routes JSON tool outputs through SmartCrusher and embeds `<>` markers when a compression ratio warrants one, and the original is stored and retrievable via `headroom.cache.compression_store`. It does **not**, however, inject the `headroom_retrieve` tool or intercept the model's retrieval call for you -- that wiring lives only in the proxy's request handlers. Resolve markers yourself with `headroom.ccr.marker_resolution.resolve_markers_in_text` if you're integrating CCR outside the proxy. ## Retention [#retention] Proxy CCR originals are kept for 1800 seconds (30 minutes) by default. For longer autonomous agent runs, set `HEADROOM_CCR_TTL_SECONDS` before starting the proxy: ```bash HEADROOM_CCR_TTL_SECONDS=7200 headroom proxy ``` Check the effective setting at `/v1/retrieve/stats` under `store.default_ttl_seconds`. ## Message-level CCR [#message-level-ccr] > **Retired:** The "Message-level CCR via IntelligentContext" feature (where `IntelligentContext` would store dropped messages in CCR with a retrieval marker) was part of the `IntelligentContextConfig` API that has since been removed. Context management is now handled automatically by the pipeline without a separate configurable IntelligentContext stage. Tool-output CCR via SmartCrusher and ContentRouter remains fully supported. ## CCR-enabled components [#ccr-enabled-components] | Component | What it compresses | CCR integration | |---|---|---| | **SmartCrusher** | JSON arrays (tool outputs) | Stores original array, marker includes hash | | **ContentRouter** | Code, logs, search results, text | Stores original content by strategy | ## Why CCR matters [#why-ccr-matters] | Approach | Risk | Savings | |---|---|---| | No compression | None | 0% | | Traditional compression | Data loss | Depends on content and target ratio | | CCR compression | None (reversible) | Same range as traditional compression -- CCR changes the risk, not the ratio | CCR gives you the savings of aggressive compression with zero risk: SmartCrusher, LogCompressor, SearchCompressor, and Kompress can all compress as aggressively as their configuration allows, because the original is always one retrieval away. See [How Compression Works](/docs/how-compression-works#content-type-detection) for measured savings ranges per compressor. ## Purpose [#purpose] This page is the quick visual map for Headroom automation. Use it when opening a PR, reviewing a PR, cutting a release, or deciding which workflow owns a failure. The short version: - Pull requests are gated by PR governance, path-filtered CI, targeted e2e workflows, and human review. - Release publishing is not triggered by every merge to `main`. `release-please` maintains a release PR; merging that release PR creates the tag and GitHub Release that trigger publishing. - Docker images are built as multi-architecture digests first, then merged into tagged manifests. - Docs are deployed by Vercel's own Git integration, not by a GitHub Actions job -- `.github/workflows/docs.yml` ("Validate Docs") only builds the Next.js/Fumadocs app on pull requests touching `docs/**` to catch a broken build before merge; it deploys nothing. ## PR Flow [#pr-flow] ```mermaid flowchart TD A[Open, edit, synchronize, or mark PR ready] --> B[PR Governance] B --> B1{Template complete and ready?} B1 -- no --> B2[Add needs author action label and governance comment] B1 -- yes --> B3[Add ready for review label when no blocking status exists] A --> C[Path filters decide workflow surface] C --> D{Code paths changed?} D -- yes --> E[CI changes job] E --> F[lint: ruff, format, mypy] E --> G[build-wheel: Rust extension wheel] E --> H[prefetch-model: Hugging Face cache] G --> I[test shards 1 to 4] H --> I G --> J[test-extras, test-agno, dashboard UI] E --> K[build: release-profile wheel and sdist smoke] C --> L{E2E paths changed?} L -- yes --> M[Docker native e2e and platform wrapper checks] C --> N{Init or wrap paths changed?} N -- yes --> O[Init E2E, Wrap E2E, native init or wrap smoke tests] C --> P{Rust paths changed?} P -- yes --> Q[Rust fmt, clippy, tests, wheel build, audit] C --> R{Devcontainer paths changed?} R -- yes --> S[Devcontainer validation and linked worktree smoke] C --> T{Release-critical paths changed?} T -- yes --> U[Release dry-run: wheel matrix and smoke-import gates] C --> V{Workflow files changed?} V -- yes --> W[Workflow validation with actionlint and act dry-run] F --> X{All required checks green?} I --> X J --> X K --> X M --> X O --> X Q --> X S --> X U --> X W --> X B3 --> X X -- no --> Y[Fix, rebase, or request changes] X -- yes --> Z[Human review and merge when approved] ``` ## PR Decision Tree [#pr-decision-tree] ```mermaid flowchart TD A[Review an open PR] --> B{Draft?} B -- yes --> C[Do not approve. Comment with remaining readiness steps.] B -- no --> D{Governance label says needs author action?} D -- yes --> E[Fix or ask author to complete template and real behavior proof] D -- no --> F{Merge state dirty or behind?} F -- dirty --> G[Resolve conflicts before reviewing final code] F -- behind --> H[Rebase or update branch, then rerun checks] F -- clean or unknown --> I{Any failing check?} I -- yes --> J[Read failing logs, classify as stale-main, infra, or code bug] J --> K{Can maintainer safely fix without changing author intent?} K -- yes --> L[Patch, test locally, push with lease] K -- no --> M[Request changes with exact file and line references] I -- no --> N{Code review complete?} N -- no --> O[Review diff, tests, docs, and behavior proof] N -- yes --> P[Approve] ``` ## Fork Workflow Approval [#fork-workflow-approval] GitHub may leave product workflows in `action_required` for first-time or fork contributors. Approve only after the diff is safe enough to execute in CI. ```mermaid flowchart TD A[Fork PR has action_required workflows] --> B{Diff is understandable and not suspicious?} B -- no --> C[Do not approve. Ask for changes or close if unsafe.] B -- yes --> D{Workflow runs use pull_request with read-scoped token?} D -- no --> E[Inspect workflow permissions before approving] D -- yes --> F[Approve queued CI runs] F --> G[Wait for fresh check results on latest head SHA] ``` ## Release Flow [#release-flow] ```mermaid flowchart TD A[Merge ordinary PR to main] --> B[Release Please on push to main] B --> C{Commit is releasable?} C -- docs, ci, chore only --> D[No release PR change] C -- fix, feat, breaking change --> E[Create or update Release PR] E --> F[Release PR contains version bump and changelog] F --> G{Ready to ship?} G -- no --> H[Keep merging regular PRs; bot updates Release PR] G -- yes --> I[Merge Release PR] I --> J[release-please tags vX.Y.Z and publishes GitHub Release] J --> K[release.yml starts on release: published] K --> L[detect-version] L --> M[build: sync versions, verify versions, changelog, npm packs] M --> N[build-wheels matrix] N --> O[collect-dist] N --> P[smoke-import wheels] O --> Q{Smoke import green?} P --> Q Q -- no --> R[Stop before publishing broken wheels] Q -- yes --> S[publish PyPI] M --> T[publish npm packages] M --> U[publish GitHub Package Registry packages] P --> V[publish Docker images through docker.yml] S --> W{PyPI published or PYPI_SKIP=true?} W -- no --> X[Do not update release assets] W -- yes --> Y[Create or update GitHub Release assets and notes] T --> Y U --> Y V --> Y ``` ## Release Decision Tree [#release-decision-tree] ```mermaid flowchart TD A[Need a release?] --> B{Release PR exists?} B -- no --> C[Merge at least one releasable conventional commit to main] B -- yes --> D{Release PR checks green and changelog correct?} D -- no --> E[Fix source PRs or release config, then let release-please update] D -- yes --> F{Registry skip variables needed?} F -- yes --> G[Set PYPI_SKIP, NPM_SKIP, or GH_PACKAGES_SKIP deliberately] F -- no --> H[Merge Release PR] G --> H H --> I[Watch release.yml] I --> J{Failure before publish?} J -- yes --> K[Fix and rerun before any package is public] J -- no --> L{Failure after partial publish?} L -- yes --> M[Use rerun or skip variables to reach consistent GitHub Release state] L -- no --> N[Release complete] ``` ## Docker Publish Flow [#docker-publish-flow] `docker.yml` can run directly on `push`, `workflow_dispatch`, or `release: published`, and it is also called by `release.yml`. ```mermaid flowchart TD A[Docker workflow starts] --> B[Matrix: variant x architecture] B --> C[Build each platform image by digest only] C --> D[Smoke-test image imports pydantic_core and headroom._core] D --> E{Smoke test green?} E -- no --> F[Stop before tag manifest] E -- yes --> G[Upload digest marker] G --> H[Per-variant manifest merge] H --> I[Apply tags to multi-arch manifest] I --> J{Release event?} J -- yes --> K[Version tags and latest retag rules] J -- no --> L[Branch, PR, dev, or manual tags as configured] ``` ## Docs Validate + Deploy Flow [#docs-validate--deploy-flow] `.github/workflows/docs.yml` ("Validate Docs") only validates -- it has no deploy step. Deployment of the Next.js/Fumadocs site at `docs.headroomlabs.ai` is owned entirely by Vercel's own Git integration, outside GitHub Actions (`.github/workflows/docs.yml:1-9`). ```mermaid flowchart TD A[Docs change in PR] --> B{docs/** or docs.yml changed?} B -- no --> C[Validate Docs workflow does not run] B -- yes --> D[Validate Docs workflow: npm ci and npm run build] D --> E{Build green?} E -- no --> F[Fix before merge] E -- yes --> G[PR review and normal checks] G --> H[Merge to main] H --> I[Vercel Git integration builds and deploys, independent of GitHub Actions] ``` ## Manual Validation Flow [#manual-validation-flow] Use this when editing workflows or release automation. ```mermaid flowchart TD A[Edit workflow or release scripts] --> B[Run local workflow validation] B --> C[actionlint] B --> D[act dry-run fixtures] C --> E{Local validation green?} D --> E E -- no --> F[Fix before opening or updating PR] E -- yes --> G[Push PR] G --> H[workflow-validation job reruns same validation in CI] ``` Recommended local command: ```bash bash scripts/validate-workflows.sh ``` For release dry-runs: ```bash act workflow_dispatch -W .github/workflows/release.yml -e .github/act/dry-run.json ``` ## Gate Summary [#gate-summary] | Flow | Trigger | Main gates | Success condition | |------|---------|------------|-------------------| | PR governance | `pull_request_target`, schedule, manual | Template, readiness labels, merge state, check labels | PR has no governance blockers | | CI | PR, push to `main`, manual | Path filter, lint, mypy, wheel build, model prefetch, test shards, package smoke | Required jobs green or path-skipped | | Rust | Rust paths, schedule | fmt, clippy, cargo test, wheel build, audit | Rust checks green; nightly parity is allowed to fail in Phase 0 | | E2E | CLI, install, wrap, Docker, package paths | Docker init/wrap, native init/wrap/install, platform smoke | Relevant lifecycle checks green | | Release dry-run | PRs touching release-critical paths | Version detection, wheel matrix, smoke imports | Publish path can build before merge | | Release publish | GitHub Release published by release-please | Version sync, changelog, wheels, smoke import, PyPI gate, npm, GPR, Docker | Public packages and GitHub Release assets are consistent | | Docs validate | PR touching `docs/**`, manual | Next.js/Fumadocs build | Build succeeds; deploy itself is done by Vercel's Git integration, not this workflow | ## Workflow Ownership [#workflow-ownership] | Workflow | Owns | |----------|------| | `.github/workflows/pr-health.yml` | PR body governance, readiness labels, rebase/conflict/failing-check labels | | `.github/workflows/ci.yml` | Python lint, type checks, wheel build, test shards, package smoke, workflow validation | | `.github/workflows/rust.yml` | Rust workspace quality gates and native wheel smoke artifacts | | `.github/workflows/init-e2e.yml` | Dockerized `headroom init` behavior | | `.github/workflows/wrap-e2e.yml` | Dockerized `headroom wrap` behavior | | `.github/workflows/init-native-e2e.yml` | Host-specific `headroom init -g` smoke tests | | `.github/workflows/install-native-e2e.yml` | Host-specific install CLI smoke tests | | `.github/workflows/wrap-native-e2e.yml` | Host-specific wrap prepare-only smoke tests | | `.github/workflows/devcontainers.yml` | Devcontainer startup and linked worktree compatibility | | `.github/workflows/release-please.yml` | Release PR aggregation from conventional commits | | `.github/workflows/release.yml` | Release build, wheel smoke-import gates, registry publishing, GitHub Release assets | | `.github/workflows/docker.yml` | GHCR multi-architecture image builds and manifests | | `.github/workflows/docs.yml` | Validates the Next.js/Fumadocs build on docs PRs; deploys nothing -- Vercel's own Git integration owns the actual deploy | If your Claude models live on **Azure AI Foundry**, you can still get Headroom's prompt compression. Headroom sits between Claude Code and Azure: it shrinks the big stuff in each request (file reads, logs, tool output) and forwards the rest to Azure using **your own Azure credentials**. You keep your Azure setup; Headroom just makes each call cheaper. ## What you get [#what-you-get] - **Fewer input tokens** on every Claude Code request to Azure AI Foundry — large file reads, logs, and tool output are compressed before forwarding — so you pay for less. - **Same answers** — compression is reversible and content-aware. - **No new secrets** — Headroom never holds your Azure credentials. Claude Code keeps authenticating to Azure AI Foundry with its own `api-key` or Entra Bearer token; Headroom passes it through. ## Before you start [#before-you-start] You should already have Claude Code working against Azure AI Foundry **without** Headroom. That means these are set in your shell (or your `~/.claude/settings.json` `env` block): ```bash export CLAUDE_CODE_USE_FOUNDRY=1 export ANTHROPIC_FOUNDRY_RESOURCE= # e.g. ANTHROPIC_FOUNDRY_RESOURCE=my-org-claude ``` No `ANTHROPIC_API_KEY` is needed — Foundry mode uses your Azure credentials. ## Run it (one command) [#run-it-one-command] ```bash pip install "headroom-ai[proxy]" headroom wrap claude ``` That's it. Because `CLAUDE_CODE_USE_FOUNDRY=1` is set, `headroom wrap claude` automatically: 1. derives your Azure AI Foundry endpoint from `ANTHROPIC_FOUNDRY_RESOURCE`, 2. starts the Headroom proxy with that endpoint as the upstream, 3. points Claude Code's Foundry endpoint at the proxy (`ANTHROPIC_FOUNDRY_BASE_URL`), 4. leaves your Azure resource, model, and credentials untouched. You'll see a line like: ``` Foundry mode: ANTHROPIC_FOUNDRY_BASE_URL=http://127.0.0.1:8787/anthropic → upstream https://my-org-claude.services.ai.azure.com/anthropic ``` Use Claude Code exactly as you normally would. ## How it works [#how-it-works] ``` Claude Code ──(Foundry request)──▶ Headroom ──(compressed)──▶ Azure AI Foundry (Claude) in Foundry mode compresses your resource (your api-key / Entra) ──────── passed through ───────────▶ authenticates you ``` Claude Code sends its Foundry request (Anthropic API format) to Headroom. Headroom compresses the messages, then forwards to your Azure AI Foundry resource endpoint — `https://{ANTHROPIC_FOUNDRY_RESOURCE}.services.ai.azure.com/anthropic` — with your auth headers passed through unchanged. ## If you have ANTHROPIC_FOUNDRY_BASE_URL set explicitly [#if-you-have-anthropic_foundry_base_url-set-explicitly] If your environment already has `ANTHROPIC_FOUNDRY_BASE_URL` set to the full Azure endpoint URL, Headroom uses it directly and `ANTHROPIC_FOUNDRY_RESOURCE` is not needed. Either configuration works. ## Check that compression is working [#check-that-compression-is-working] 1. Open the dashboard: [http://localhost:8787/dashboard](http://localhost:8787/dashboard). "Tokens saved" should climb as you use Claude Code. 2. Or check response headers: `x-headroom-tokens-before`, `x-headroom-tokens-after`, `x-headroom-tokens-saved`. ## Troubleshooting [#troubleshooting] **Headroom says "ANTHROPIC_BASE_URL" instead of "Foundry mode"** `CLAUDE_CODE_USE_FOUNDRY` is not set in the shell where you ran `headroom wrap claude`. Make sure to export it before running, or set it in your shell profile. **Claude Code fails with a 401 / auth error** Your Azure credentials are not being forwarded correctly. Verify that Claude Code works against Azure AI Foundry directly (without Headroom) before wrapping. If it works direct but not through Headroom, open an issue with the proxy log. **"tokens saved" is always 0** Check the [dashboard](http://localhost:8787/dashboard) — if requests are flowing but savings are 0, content may be below the compression threshold or the Rust extension may not be installed (`pip install "headroom-ai[proxy]"` includes it). If your Claude models live on **Google Vertex AI**, you can still get Headroom's prompt compression. Headroom sits between Claude Code and Vertex: it shrinks the big stuff in each request (file reads, logs, tool output) and forwards the rest to Vertex using **your own Google credentials**. You keep your GCP setup; Headroom just makes each call cheaper. ## What you get [#what-you-get] - **Fewer input tokens** on every Claude Code request to Vertex — large file reads, logs, and tool output are compressed before forwarding — so you pay Vertex for less. - **Same answers** — compression is reversible and content-aware. - **No new secrets** — Headroom never holds your Google credentials. Claude Code keeps authenticating to Vertex with its own ADC token; Headroom passes it through. ## Before you start [#before-you-start] You should already have Claude Code working against Vertex **without** Headroom. That means these are set in your shell: ```bash export CLAUDE_CODE_USE_VERTEX=1 export ANTHROPIC_VERTEX_PROJECT_ID= export CLOUD_ML_REGION=us-east5 # your Vertex region (or "global") gcloud auth application-default login # or set GOOGLE_APPLICATION_CREDENTIALS ``` No `ANTHROPIC_API_KEY` is needed — Vertex mode uses your Google login. ## Run it (one command) [#run-it-one-command] ```bash pip install "headroom-ai[proxy]" headroom wrap claude ``` That's it. Because `CLAUDE_CODE_USE_VERTEX=1` is set, `headroom wrap claude` automatically: 1. starts the Headroom proxy, 2. points Claude Code's Vertex endpoint at it (`ANTHROPIC_VERTEX_BASE_URL`), 3. leaves your project, region, and Google login untouched. You'll see a line like: ``` Vertex mode: ANTHROPIC_VERTEX_BASE_URL=http://127.0.0.1:8787 → compress, then forward to Vertex with your GCP ADC token ``` Use Claude Code exactly as you normally would. ## How it works [#how-it-works] ``` Claude Code ──(Vertex request)──▶ Headroom ──(compressed)──▶ Vertex AI (Claude) in Vertex mode compresses your project + region (your ADC token) ───────────── passed through ───────────▶ authenticates you ``` Claude Code sends its normal Vertex `…:rawPredict` / `:streamRawPredict` request to Headroom. Headroom compresses the messages (keeping the Vertex request shape intact), then forwards to the correct regional Vertex host — derived from the request itself, so multi-region and `global` both work — using the Google token Claude Code already attached. ## Check that compression is working [#check-that-compression-is-working] 1. Open the dashboard: [http://localhost:8787/dashboard](http://localhost:8787/dashboard). "Tokens saved" should climb as you use Claude Code. 2. Or look at the response headers on a request: `x-headroom-tokens-before`, `x-headroom-tokens-after`, `x-headroom-tokens-saved`. If "tokens saved" stays at 0 on large prompts, see Troubleshooting below. ## Troubleshooting [#troubleshooting] - **It still goes straight to Google (no savings).** Make sure `CLAUDE_CODE_USE_VERTEX=1` is exported *in the same shell* before `headroom wrap claude`. The wrapper only switches to Vertex mode when it sees that variable. - **Wrong region / 404 from Vertex.** Confirm `CLOUD_ML_REGION` matches a region where your Claude model is enabled. `global` is supported and maps to the non-regional host. - **Auth errors.** Headroom forwards your token as-is — if `gcloud auth application-default login` (or `GOOGLE_APPLICATION_CREDENTIALS`) works for Claude Code without Headroom, it works with it. ## Alternative: let Headroom talk to Vertex for you [#alternative-let-headroom-talk-to-vertex-for-you] If you'd rather **not** run Claude Code in Vertex mode, you can have Headroom be the translator instead: Claude Code speaks plain Anthropic to Headroom, and Headroom calls Vertex on your behalf. ```bash export HEADROOM_BACKEND=litellm-vertex_ai # note the _ai suffix export HEADROOM_REGION=us-east5 export VERTEXAI_PROJECT= export GOOGLE_APPLICATION_CREDENTIALS=/path/sa.json # or gcloud ADC export ANTHROPIC_API_KEY=placeholder # Claude Code needs *a* key to start headroom wrap claude --backend litellm-vertex_ai --region us-east5 ``` The native Vertex-mode flow above is recommended — it keeps your existing GCP auth and has the smallest moving parts. Use this alternative only if you can't set `CLAUDE_CODE_USE_VERTEX`. ## Notes [#notes] - Pick a Claude model that is enabled in your Vertex project/region (e.g. `claude-sonnet-4-6`, `claude-haiku-4-5`). - Streaming, tool use, and prompt caching all work through Headroom. - Claude Code's server-side auto-mode classifier is supported only by the native Anthropic-compatible wire path. The Vertex translation path remains fail-open but may use Claude Code's billable client-side classifier fallback until a provider-native safeguard fixture is verified. - Want to point at a private Vertex gateway instead of Google's host? Start the proxy with `--vertex-api-url https://your-gateway` and Headroom will forward there. Headroom's CodeAwareCompressor uses tree-sitter to parse source code into an AST, then selectively compresses function bodies while preserving the structural elements that LLMs need -- imports, signatures, type annotations, and error handlers. ## Why AST-Aware Compression? [#why-ast-aware-compression] Naive truncation breaks code. Cutting a function in half leaves invalid syntax that confuses the LLM. CodeAwareCompressor guarantees: - **Syntax validity** -- output always parses correctly - **Structural preservation** -- imports, signatures, types, decorators are kept intact - **Lightweight** -- ~50MB of tree-sitter parsers, loaded lazily and cached ## Supported Languages [#supported-languages] | Tier | Languages | Support Level | |---|---|---| | Tier 1 | Python, JavaScript, TypeScript | Full AST analysis | | Tier 2 | Go, Rust, Java, C, C++, C#, PHP | Function body compression | ## What Gets Preserved vs Compressed [#what-gets-preserved-vs-compressed] **Always preserved:** - Import statements - Function and method signatures - Class definitions - Type annotations - Decorators **Compressed:** - Function bodies (implementations) - Comments (unless configured to preserve) - Verbose docstrings (configurable: full, first line, or removed) ## Example [#example] ```python from headroom.transforms import CodeAwareCompressor, CodeCompressorConfig # min_tokens_for_compression defaults to 100; a snippet this small needs an # explicit override or it passes through unmodified (see "Skip small content" below). compressor = CodeAwareCompressor(CodeCompressorConfig(min_tokens_for_compression=10)) code = ''' import os from typing import List def process_items(items: List[str]) -> List[str]: """Process a list of items.""" results = [] for item in items: if not item: continue processed = item.strip().lower() results.append(processed) return results ''' result = compressor.compress(code, language="python") print(result.compressed) # import os # from typing import List # # def process_items(items: List[str]) -> List[str]: # """Process a list of items.""" # results = [] # # [6 lines omitted] # pass # # [32 tokens compressed. Retrieve more: hash=. Expires in 5m.] print(f"Compression: {result.compression_ratio:.0%}") # ~57% print(f"Syntax valid: {result.syntax_valid}") # True ``` ## Configuration [#configuration] ```python from headroom.transforms import CodeAwareCompressor, CodeCompressorConfig, DocstringMode config = CodeCompressorConfig( preserve_imports=True, # Always keep imports preserve_signatures=True, # Always keep function signatures preserve_type_annotations=True, # Keep type hints preserve_decorators=True, # Keep decorators docstring_mode=DocstringMode.FIRST_LINE, # FULL, FIRST_LINE, REMOVE target_compression_rate=0.2, # Keep 20% of tokens max_body_lines=5, # Lines to keep per function body min_tokens_for_compression=100, # Skip small content language_hint=None, # Auto-detect if None fallback_to_kompress=True, # Use Kompress for unknown langs ) compressor = CodeAwareCompressor(config) result = compressor.compress(code) ``` ### Configuration Options [#configuration-options] | Option | Default | Description | |---|---|---| | `preserve_imports` | `True` | Keep all import statements | | `preserve_signatures` | `True` | Keep function/method signatures | | `preserve_type_annotations` | `True` | Keep type hints | | `preserve_decorators` | `True` | Keep decorators | | `docstring_mode` | `FIRST_LINE` | How to handle docstrings: `FULL`, `FIRST_LINE`, `REMOVE` | | `target_compression_rate` | `0.2` | Fraction of tokens to keep (0.2 = keep 20%) | | `max_body_lines` | `5` | Max lines to keep per function body | | `min_tokens_for_compression` | `100` | Skip files smaller than this | | `language_hint` | `None` | Override language detection | | `fallback_to_kompress` | `True` | Use Kompress for unsupported languages | ## Before and After [#before-and-after] ```python # Before (full source file) def process_data(items: List[str]) -> Dict[str, int]: """Process items and count occurrences.""" result = {} for item in items: item = item.strip().lower() if item in result: result[item] += 1 else: result[item] = 1 return result # After (signature preserved, body compressed) def process_data(items: List[str]) -> Dict[str, int]: """Process items and count occurrences.""" result = {} for item in items: # ... (5 lines compressed) pass ``` The LLM sees the function's purpose, its input/output types, and the general approach -- enough to reason about the code without needing every implementation line. ## Installation [#installation] ```bash # Install tree-sitter language pack pip install "headroom-ai[code]" ``` ## Memory Management [#memory-management] Tree-sitter parsers are lazy-loaded and cached. You can free memory when done: ```python from headroom.transforms import is_tree_sitter_available from headroom.transforms.code_compressor import unload_tree_sitter # Check if tree-sitter is installed print(is_tree_sitter_available()) # True # Free memory when done unload_tree_sitter() ``` ## Performance [#performance] | Metric | Value | |---|---| | Compression | 40-70% token reduction | | Speed | ~10-50ms per file | | Memory | ~50MB (tree-sitter parsers) | | Syntax validity | Guaranteed | ContentRouter always *detects* source code automatically, but whether it routes that code to CodeAwareCompressor depends on the entry point. `headroom proxy` enables code-aware routing by default (`HEADROOM_CODE_AWARE_ENABLED` defaults to on; disable with `--no-code-aware` or `HEADROOM_CODE_AWARE_ENABLED=0`). The one-function `compress()` API and `HeadroomClient`, however, build `ContentRouterConfig` with its own default, `enable_code_aware=False` -- there, detected code falls back to Kompress unless you construct your own pipeline with `ContentRouterConfig(enable_code_aware=True)`. Direct usage of `CodeAwareCompressor`, as shown on this page, is unaffected either way. See [How Compression Works](/docs/how-compression-works#content-type-detection). Older versions of `headroom wrap codex` could run Codex with a temporary `CODEX_HOME` named `headroom-codex-home-*`. Chats and configuration created during that wrapped session stayed in the temporary home, so they disappeared from the normal Codex history after the wrapper exited. This recovery migrates the retained state back into the durable Codex home without deleting either source. This procedure can recover temporary homes that still exist and pinned sources retained by an interrupted recovery. A temporary home that was already deleted cannot be reconstructed unless one of those retained copies exists. Headroom reports deleted temporary homes still referenced by the Codex thread database. It does not treat paths pasted into prompts or error messages as recovery sources. See [issue #2159](https://github.com/headroomlabs-ai/headroom/issues/2159) for the regression details. ## Before you recover [#before-you-recover] Close Codex and any process using the temporary or durable Codex homes. Recovery detects changes made while creating each backup and aborts, but quiet homes are required for a consistent migration. The target defaults to `$CODEX_HOME` when it is set, otherwise `~/.codex`. Check that this is the durable home you normally use before confirming the migration. ## Automatic recovery [#automatic-recovery] The first interactive `headroom wrap codex` with the fixed version searches Python's temporary directory, `$TMPDIR`, `/tmp`, `/private/tmp`, and the macOS `/private/var/folders/*/*/T` roots for non-empty `headroom-codex-home-*` directories. If it finds any, it lists them and offers to back up and recover them before Codex starts. Declining the prompt changes nothing. You can run the manual command later. ## Manual recovery [#manual-recovery] Let Headroom discover retained temporary homes and `source-pinned/` copies left by interrupted or failed recovery attempts, then preview the migration: ```bash headroom recover codex ``` If no recoverable copy remains, the command audits the durable Codex thread database, rollout files, and `history.jsonl`. It reports indexed active and archived chats, surviving rollouts missing from the thread index, and history-only records whose rollouts no longer exist. History-only prompt text cannot reconstruct a full chat transcript. Codex filters the default resume picker by the current working directory. Run `codex resume --all` yourself to display indexed chats from every working directory. Headroom does not launch Codex during recovery. To select a known temporary home and an explicit durable target: ```bash headroom recover codex \ --source /path/to/headroom-codex-home-12345 \ --target "${CODEX_HOME:-$HOME/.codex}" ``` Repeat `--source` to merge multiple homes. Headroom shows the target and every source before asking for confirmation. Use `--yes` only in automation where those paths have already been reviewed. ## What gets migrated [#what-gets-migrated] When a source used the localhost `headroom` model provider injected by the old wrapper, recovery rewrites that provider in both SQLite thread rows and rollout `session_meta` records to the active target provider. This also repairs newer target records left by an earlier broken recovery. A user-defined remote provider named `headroom` is preserved. - `history.jsonl` and other JSONL indexes are combined without duplicate records. Malformed input is excluded from the result and copied to the backup quarantine. - Session rollouts under `sessions/` and `archived_sessions/` keep the newer file when the same path exists in both homes. - SQLite databases are merged only when their tables, indexes, triggers, views, and migration checksums are compatible. Primary-key conflicts keep rows from the newer database. Recovered thread rows are rewritten to the durable rollout path, including rows restored from a pinned source after the original temporary home was deleted. - `config.toml` tables are merged recursively. Values from the newer config win, and localhost Headroom routing injected by the old wrapper is removed. A user-defined remote provider is preserved. - Other files, including credentials and user settings, keep the newer copy. The durable target wins equal modification-time ties, and missing source files never delete target files. Sockets, lock files, SQLite journals, FIFOs, and other runtime-only artifacts are recorded but not copied. ## Backups and rollback [#backups-and-rollback] Every source is pinned before migration. The existing durable home, including its current Codex configuration, is backed up before any merge begins. Backups are owner-only and retained next to the target: ```text /.headroom-codex-recovery// ├── source-pinned/ ├── target-before/ ├── target-failed/ # only when a merge is rolled back ├── manifest.json └── quarantine/ # only when malformed input is found ``` `target-before/` is absent when the target did not exist before recovery. `manifest.json` records copied, merged, quarantined, and skipped paths. If configuration parsing, SQLite schema validation, integrity checks, foreign-key checks, or filesystem writes fail, Headroom atomically renames the failed target to `target-failed/` before restoring `target-before/`. This avoids recursive-deletion races with SQLite runtime files and preserves the failed merge for inspection. When the target did not exist before recovery, the partial target is retained only as `target-failed/`. The pinned source and recovery backup remain available for inspection. Recovery also refuses overlapping source and target paths, target symlink traversal, and a source that changes while it is being pinned. ## After recovery [#after-recovery] The command prints the retained backup path after each successful source merge. Review its `manifest.json`, then start Codex normally and confirm that the recovered chats and settings appear. Keep the backup until you have verified the durable history. Fixed versions of `headroom wrap codex` run Codex against the durable home and apply proxy routing only to the launched process, so new wrapped sessions remain visible after Headroom exits. Headroom can be configured via the SDK constructor, proxy command line, environment variables, or per-request overrides. ## Runtime Rollout Channels [#runtime-rollout-channels] Headroom uses rollout channels to control which behaviors an already-installed artifact may expose. They do not select a package or released version. | Variable | Default | Purpose | |----------|---------|---------| | `HEADROOM_ROLLOUT_CHANNEL` | `stable` | Selects `stable`, `beta`, `canary`, or `dev`. | | `HEADROOM_FEATURES` | unset | Comma-separated feature names to request explicitly. | | `HEADROOM_DISABLE_FEATURES` | unset | Comma-separated feature names to force off. Disable wins over every enable path. | | `HEADROOM_UNSAFE_ALLOW_UNSTABLE_FEATURES` | unset | Break-glass override for emergency mitigation only. | See [Runtime Rollouts](/docs/runtime-rollouts) for policy, provenance, and contributor rules. If Codex history disappeared after using an older wrapper, see [Recover Codex State](/docs/codex-recovery) before wrapping Codex again. ## Proxy request-body admission [#proxy-request-body-admission] | Variable | Default | Purpose | |----------|---------|---------| | `HEADROOM_REQUEST_BODY_MAX_BYTES` | `104857600` (100 MiB) | Hard whole-request ceiling, resolved at process startup. Must be a positive integer. | The ceiling applies to the complete history, not each message or text chunk. Declared oversized HTTP bodies are rejected before reading them; chunked bodies are metered as they arrive. Oversized wire bodies return HTTP 413 by default, before JSON parsing, image classification, tokenization, or compression. Decompressed gzip, deflate, Brotli, and Zstandard bodies obey the same ceiling; `/v1/compress` returns the same size refusal for oversized decoded bodies, including requests with `x-headroom-bypass: true`. The previous 100 MiB admission default is retained. Anthropic Messages, OpenAI Chat/Responses, Gemini generateContent, and Bedrock InvokeModel HTTP routes have a separate **16 MiB compression threshold**, measured on the decoded body. Larger admitted histories skip body normalization, image processing, memory injection, tokenization, and compression. Native upstreams receive the original decoded JSON bytes; obsolete compression headers are removed. Streaming and non-streaming responses retain their normal protocol. Required auth-specific wire settings still apply: ChatGPT Responses receives `store: false` even when the client omitted it or set it to true. This reserializes the body only when that setting needs rewriting; an already-valid body remains byte-faithful. Vertex route-forced streaming is retained even without a `stream` body field. Envelope/model routing currently requires one JSON parse. Configured or per-request translating backends still perform the necessary wire conversion, but do not run request-side compression. Passthrough outcomes report zero compression savings and use provider-reported usage when available; missing usage is not presented as a local token measurement. The soft threshold applies to HTTP forwarding, not explicit `/v1/compress` calls or WebSocket messages. Auth, routing, rate and budget gates remain in force. Operators can choose a different hard ceiling and restart the proxy: ```bash HEADROOM_REQUEST_BODY_MAX_BYTES=33554432 headroom proxy ``` Neither threshold is a process-memory limit or a mechanism for cancelling workers already running. JSON decoding, transport buffers, protocol translation, and third-party ASGI middleware still require memory; bound concurrency separately on small hosts. ## SDK Modes (`default_mode` / `headroom_mode`) [#sdk-modes-default_mode--headroom_mode] These modes apply to SDK usage via `HeadroomClient(default_mode=...)` or per-request `headroom_mode=...`. They are **not** the same as the proxy `--mode` flag. | Mode | Behavior | Use Case | |------|----------|----------| | `audit` | Observes and logs, no modifications | Production monitoring, baseline measurement | | `optimize` | Applies safe, deterministic transforms | Production optimization | | `simulate` | Returns plan without API call | Testing, cost estimation | > **Proxy `--mode` is a separate axis**: `--mode cache` (the default) freezes prior turns so the provider's prefix cache is never busted, which is where the savings come from on a real agent workload. `--mode token` maximizes visible per-request compression at the cost of cache stability; prefer `cache` unless you are specifically diagnosing compression numbers. The proxy does not accept `audit`, `optimize`, or `simulate`. ## SDK Configuration [#sdk-configuration] ```ts twoslash import { HeadroomClient } from 'headroom-ai'; // Reads from HEADROOM_BASE_URL and HEADROOM_API_KEY automatically const client = new HeadroomClient(); // Or configure explicitly const explicit = new HeadroomClient({ baseUrl: 'http://localhost:8787', apiKey: 'your-api-key', timeout: 30_000, fallback: true, retries: 2, }); ``` ```python from headroom import HeadroomClient, OpenAIProvider from openai import OpenAI client = HeadroomClient( original_client=OpenAI(), provider=OpenAIProvider(), # Mode: "audit" (observe only) or "optimize" (apply transforms) default_mode="optimize", # Enable provider-specific cache optimization enable_cache_optimizer=True, # Enable query-level semantic caching enable_semantic_cache=False, # Override default context limits per model model_context_limits={ "gpt-4o": 128000, "gpt-4o-mini": 128000, }, # Database location (defaults to temp directory) # store_url="sqlite:////absolute/path/to/headroom.db", ) ``` ## Per-Request Overrides [#per-request-overrides] Override configuration for individual requests: ```ts twoslash import { compress } from 'headroom-ai'; const result = await compress(messages, { model: 'gpt-4o', tokenBudget: 100_000, timeout: 15_000, }); ``` ```python response = client.chat.completions.create( model="gpt-4o", messages=[...], # Override mode for this request headroom_mode="audit", # Reserve more tokens for output headroom_output_buffer_tokens=8000, # Keep last N turns (don't compress) headroom_keep_turns=5, # Skip compression for specific tools headroom_tool_profiles={ "important_tool": {"skip_compression": True} }, ) ``` ### Proxy upstream override (`x-headroom-base-url`) [#proxy-upstream-override-x-headroom-base-url] When using the proxy, send the `x-headroom-base-url` request header to route a single request to a different upstream instead of the configured provider URL. This lets a client that speaks a provider's wire format authenticate against a compatible gateway (for example an OpenAI-compatible endpoint, or an Anthropic-Messages gateway such as OpenCode Zen) without changing the proxy configuration. The header is honored by the OpenAI-compatible routes, the Anthropic Messages route (`POST /v1/messages`), and the generic passthrough route. The proxy forwards the request to `` + the original request path (e.g. `/v1/messages`). An empty or whitespace-only value is ignored and the configured upstream is used. ```bash curl http://127.0.0.1:8787/v1/messages \ -H "content-type: application/json" \ -H "anthropic-version: 2023-06-01" \ -H "x-headroom-base-url: https://opencode.ai/zen/go" \ -H "x-api-key: " \ -d '{"model":"glm-5.2","max_tokens":16,"messages":[{"role":"user","content":"hi"}]}' ``` When `HEADROOM_STRIP_INTERNAL_HEADERS` is `enabled` (the default), the proxy reads this header for routing and then strips it before forwarding upstream. #### Configured secret headers are not sent to arbitrary upstreams [#configured-secret-headers-are-not-sent-to-arbitrary-upstreams] `ANTHROPIC_TARGET_API_HEADERS` / `OPENAI_TARGET_API_HEADERS` hold operator secrets. Because `x-headroom-base-url` is chosen by the *client*, those headers are only attached when the destination is one the operator designated: - a host in the configured provider targets (`ANTHROPIC_TARGET_API_URL`, `OPENAI_TARGET_API_URL`, and the Gemini/Vertex/Cloud Code equivalents), or - a host listed in `HEADROOM_UPSTREAM_ALLOWED_HOSTS` (comma-separated). A request to any other upstream is **still proxied** — it just does not carry your configured headers, and the proxy logs `upstream_extra_headers_withheld host=` once per host. If you route to a gateway via this header and need your configured headers to reach it, add its host to `HEADROOM_UPSTREAM_ALLOWED_HOSTS`: ```bash export HEADROOM_UPSTREAM_ALLOWED_HOSTS="gateway.internal,api.example-gateway.ai" ``` Matching is on the parsed hostname and is exact — no wildcards — so `api.anthropic.com.evil.example` and `https://api.anthropic.com@evil.example` do not match `api.anthropic.com`. #### `x-headroom-base-url` is refused when an outbound proxy is in the way [#x-headroom-base-url-is-refused-when-an-outbound-proxy-is-in-the-way] Because the destination is chosen by the client, the proxy validates it by resolving the hostname and rejecting non-public addresses — and then dials the address it validated, rather than resolving the name a second time at connect time. Without that second half, a hostname whose authoritative DNS is attacker-controlled can answer the check with a public address and the connection with `169.254.169.254` or an RFC1918 host (DNS rebinding). An outbound HTTP or SOCKS proxy (`HTTP_PROXY`, `HTTPS_PROXY`, `ALL_PROXY`) is handed the target *by name* — in the request line, in `CONNECT`, in the SOCKS5 address — and resolves it itself, from its own network, after the check ran. Headroom cannot enforce the validated address there, so a request carrying `x-headroom-base-url` that would route through the proxy is **refused** rather than forwarded on a verdict nothing enforces. This affects only client-supplied upstreams. Every operator-configured provider URL goes through the proxy exactly as before. If you need a specific endpoint reachable via `x-headroom-base-url` through an outbound proxy, name it in `HEADROOM_ALLOWED_BASE_URLS` — an explicit operator decision to admit that destination by name, which also turns off the resolve-and-pin check for it. ## SmartCrusher Configuration [#smartcrusher-configuration] Fine-tune JSON compression behavior: ```python from headroom.transforms import SmartCrusherConfig config = SmartCrusherConfig( # Maximum items to keep after compression max_items_after_crush=15, # Minimum tokens before applying compression min_tokens_to_crush=200, # Fraction of items always kept from the start/end first_fraction=0.3, last_fraction=0.15, # Variance threshold for statistical analysis variance_threshold=2.0, ) ``` ## CacheAligner Configuration [#cachealigner-configuration] Control prefix stabilization for provider cache hit rates: ```python from headroom import CacheAlignerConfig config = CacheAlignerConfig( # Enable/disable cache alignment enabled=True, # Use custom regex patterns instead of the built-in dynamic-content # detector (use_dynamic_detector=False activates date_patterns) use_dynamic_detector=False, date_patterns=[ r"Today is \w+ \d+, \d{4}", r"Current time: .*", ], ) ``` ## Context Window Management [#context-window-management] Context management is now automatic. Use per-request overrides to control behavior: ```python response = client.chat.completions.create( model="gpt-4o", messages=messages, # Reserve tokens for model output headroom_output_buffer_tokens=4000, # Keep last N turns uncompressed headroom_keep_turns=3, ) ``` The `RollingWindowConfig`, `IntelligentContextConfig`, and `ScoringWeights` classes are no longer part of Headroom. Context management now happens automatically inside the pipeline (live-zone-only compression). ### Claude 1M context window (`headroom wrap claude --1m`) [#claude-1m-context-window-headroom-wrap-claude---1m] `headroom wrap claude --1m` opts a Claude Code session into Anthropic's 1M-token context window by selecting a `[1m]`-suffixed model id, which makes Claude Code send the `context-1m` beta header. The model that `--1m` targets is resolved in this order: 1. an explicit `--model` / `ANTHROPIC_MODEL` value (used as-is, with a `[1m]` suffix appended when missing), 2. otherwise `HEADROOM_1M_MODEL`, when set, 3. otherwise the built-in default (currently `claude-opus-5`). Set `HEADROOM_1M_MODEL` to point `--1m` at a specific model without pinning `ANTHROPIC_MODEL` globally, so the default can follow a new Opus generation without a code change: ```bash # Route --1m at a specific model for this shell / session export HEADROOM_1M_MODEL=claude-opus-5 headroom wrap claude --1m ``` `HEADROOM_1M_MODEL` is a fallback only: an explicit `--model` or `ANTHROPIC_MODEL` always wins. The value may be given with or without the `[1m]` suffix; both `claude-opus-5` and `claude-opus-5[1m]` are accepted, and the suffix is added when absent. For Claude Code's official VS Code extension, use the client-owned persisted setting instead of `ANTHROPIC_MODEL`: ```bash headroom wrap vscode-claude --1m ``` This writes the resolved model with exactly one `[1m]` suffix to the top-level `model` setting in Claude Code's user settings and records the prior presence and value in Headroom's reversible sidecar. Running the command without `--1m` restores a model previously managed by Headroom; `headroom unwrap vscode-claude` restores the model and proxy environment values together. Use `--no-configure --1m` to print the setting without writing either file. Claude Code and Anthropic own model support and account eligibility. Local tests prove settings persistence and restoration, not a live entitled VS Code session or a resulting 1M context window. ## Pipeline Extensions [#pipeline-extensions] Use a `headroom.pipeline_extension` entry point when you need to normalize or annotate requests before they leave Headroom. The `PRE_SEND` stage is the right place for provider-specific request cleanup, such as turning `content: null` into `content: ""` for upstreams that reject OpenAI-spec tool-call messages. ```python from headroom.pipeline import PipelineEvent, PipelineStage class NormalizeNullContent: def on_pipeline_event(self, event: PipelineEvent) -> PipelineEvent: if event.stage is not PipelineStage.PRE_SEND or not event.messages: return event for message in event.messages: if ( message.get("role") == "assistant" and message.get("content") is None and message.get("tool_calls") ): message["content"] = "" return event ``` Register it in `pyproject.toml`: ```toml [project.entry-points."headroom.pipeline_extension"] normalize_null_content = "my_pkg.normalize:NormalizeNullContent" ``` If the upstream base URL itself must vary per request, use the `x-headroom-base-url` override header in addition to the normalization hook. ## Proxy Configuration [#proxy-configuration] ### Command Line Options [#command-line-options] ```bash headroom proxy \ --port 8787 \ # Port to listen on --host 127.0.0.1 \ # Host to bind to --mode cache \ # default; freezes prior turns for prefix-cache stability --budget 10.00 \ # Daily budget limit in USD --log-file headroom.jsonl # Log file path ``` ### Feature Flags [#feature-flags] ```bash # Disable optimization (passthrough mode) headroom proxy --no-optimize # Disable semantic caching headroom proxy --no-cache # Preserve provider prefix-cache stability instead of maximizing token removal headroom proxy --mode cache # Enable memory and live learning headroom proxy --memory headroom proxy --learn --min-evidence 3 ``` ## Environment Variables [#environment-variables] | Variable | Description | Default | |----------|-------------|---------| | `HEADROOM_HOST` | Proxy bind host | `127.0.0.1` | | `HEADROOM_PORT` | Proxy bind port | `8787` | | `HEADROOM_MODE` | Proxy optimization mode: `token` or `cache` | `cache` | | `HEADROOM_WORKERS` | Uvicorn worker count | `1` | | `HEADROOM_LIMIT_CONCURRENCY` | Maximum concurrent connections before 503 | `1000` | | `HEADROOM_MAX_CONNECTIONS` | Maximum upstream HTTP connections | `500` | | `HEADROOM_MAX_KEEPALIVE` | Maximum upstream keep-alive connections | `100` | | `HEADROOM_KEEPALIVE_EXPIRY` | Seconds an idle upstream keep-alive connection is kept open | `90` | | `HEADROOM_HTTP_PROXY` | HTTP proxy URL for upstream provider requests only; HTTPS provider APIs use CONNECT | -- | | `HEADROOM_BUDGET` | Daily budget limit in USD | -- | | `HEADROOM_TELEMETRY` | Set to `on` for **local-only** usage stats (powers your own `/stats` and dashboard; nothing is sent externally) | `off` | | `HEADROOM_STATELESS` | Set to `true` to disable filesystem writes | `false` | | `HEADROOM_MODEL_LIMITS` | Custom model config (JSON string or file path) | -- | | `HEADROOM_LOSSLESS_COMPACTION` | Set to `0` to disable the byte-reversible lossless folds (grep heading, log run-collapse, diff/config folds) while leaving the rest of the compression pipeline active. Read per request, so the proxy's `/admin/runtime-env` hot-sync applies without a restart. | `1` | | `HEADROOM_BASE_URL` | Base URL of the Headroom proxy (TypeScript SDK) | `http://localhost:8787` | | `HEADROOM_API_KEY` | Optional API key for authenticated Headroom endpoints (TypeScript SDK) | -- | | `HEADROOM_CONFIG_DIR` | Canonical config (read-mostly) root. Derives `models.json` and per-plugin config paths when set. | `~/.headroom/config` | | `HEADROOM_WORKSPACE_DIR` | Canonical workspace (read-write state) root. Derives savings, memory DB, logs, TOIN, subscription state, and more when set. | `~/.headroom` | | `HEADROOM_SAVINGS_PATH` | Override persistent savings file location. Always wins when set. | derived from `${HEADROOM_WORKSPACE_DIR}` | | `HEADROOM_TOIN_PATH` | Override TOIN telemetry file location. Always wins when set. | derived from `${HEADROOM_WORKSPACE_DIR}` | | `HEADROOM_SUBSCRIPTION_STATE_PATH` | Override subscription tracker state file. Always wins when set. | derived from `${HEADROOM_WORKSPACE_DIR}` | | `HEADROOM_PERIODIC_TOIN_STATS` | Controls periodic TOIN stats logging in long-lived proxy workers. Set to `0`, `false`, `off`, or `no` to disable the 5-minute stats loop without disabling TOIN learning or request-time feedback. | `true` | | `HEADROOM_MEMORY_INJECTION_MODE` | Memory-context routing mode: `live_zone_tail` (default), `disabled`, or `tool` (auto-injection off; the agent calls memory tools explicitly). The legacy `system_prompt` mode was retired by PR-A2; supplying it raises. | `live_zone_tail` | | `HEADROOM_PROXY_PYTHON_FORWARDER_MODE` | Python forwarder serialization mode. `byte_faithful` (default) forwards original request bytes verbatim when no transform mutated the body and re-serializes canonically only when needed — keeps Anthropic prompt-cache hit-rate intact. `legacy_json_kwarg` is an explicit operator opt-in for emergency rollback to the historical `httpx ... json=body` behavior. NOT a fallback — only flip on explicit operator decision. | `byte_faithful` | | `HEADROOM_STRIP_INTERNAL_HEADERS` | Python proxy: whether to strip internal `x-headroom-*` request headers (e.g. `x-headroom-bypass`, `x-headroom-mode`, `x-headroom-user-id`, `x-headroom-stack`, `x-headroom-base-url`) before every upstream forwarder call (PR-A5, fixes P5-49). `enabled` (default) stops fingerprinting / leakage. `disabled` is an explicit operator opt-in for diagnostic shadow tracing — NOT a fallback. Inbound reads of these headers (bypass gating, memory user-id resolution) are unaffected because they read `request.headers` directly. | `enabled` | | `HEADROOM_PROXY_STRIP_INTERNAL_HEADERS` | Rust proxy: same policy as `HEADROOM_STRIP_INTERNAL_HEADERS` but for the Rust transparent proxy. Stripping happens inside `build_forward_request_headers` so both HTTP and WebSocket upstream calls are gated by one flag. `enabled` default; `disabled` operator opt-in for diagnostic shadow tracing. Response-side `X-Headroom-*` injection (e.g. `x-headroom-tokens-saved`) is unrelated and stays. | `enabled` | | `HEADROOM_EMBEDDER_RUNTIME` | Set to `pytorch_mps` to run the memory embedder via the torch sentence-transformers backend on the Apple GPU (MPS). Only engages when Apple MPS is actually available; otherwise it logs a warning and uses the existing default embedder selection path. `pytorch_mps` is the only accepted value. Requires the `[pytorch-mps]` extra. See [Memory](/docs/memory#embedding-runtime--gpu-offload-apple-silicon). | default embedder selection | | `HEADROOM_KOMPRESS_BACKEND` | Selects the Kompress (ModernBERT) inference backend, separate from `HEADROOM_EMBEDDER_RUNTIME` above. `auto` tries ONNX CPU then falls back to PyTorch; `onnx`/`onnx_cpu` and `onnx_coreml` force ONNX Runtime (CPU or CoreML); `pytorch` and `pytorch_mps` force PyTorch (`pytorch_mps` targets Apple's GPU, with `mps`/`torch_mps` accepted as aliases). | `auto` | | `ORT_DYLIB_PATH` | Path to the ONNX Runtime shared library loaded by the Rust core (magika detection, fastembed embeddings), which loads ORT dynamically on every platform. Auto-pinned at `import headroom` to the library inside the `onnxruntime` pip package (`onnxruntime.dll` / `libonnxruntime.so*` / `libonnxruntime*.dylib`); set it yourself to override. Without a pin, ML detection degrades to the non-ONNX tiers — and on Windows the bare DLL search can resolve to the Windows ML System32 build (1.17.x on Win11 24H2+), which deadlocks ONNX session init — see [Troubleshooting](/docs/troubleshooting#windows-ml-content-detection-hangs-or-silently-falls-back). | auto-pinned | | `HEADROOM_MAGIKA_INIT_TIMEOUT_SECS` | Upper bound (integer seconds, > 0) on magika's one-time ONNX session init in the Rust detection chain. On timeout the init error is cached and detection uses the non-ML fallback tiers for the rest of the process; a warning is logged. Safety net for environments where the dylib pin above does not apply. | `5` | | `HEADROOM_REQUEST_TIMEOUT` | Request timeout in seconds | `300` | | `HEADROOM_BETA_HEADER_STICKY` | Controls per-session `anthropic-beta` / `OpenAI-Beta` re-echo. `enabled` (default): the proxy unions beta tokens across turns within a session — if the client sends a token in turn N and omits it in turn N+1, the proxy re-injects it to preserve prefix-cache stability. `disabled`: the client's value is forwarded verbatim with no accumulation. Any other value raises at request time. See [Session Beta Header Tracking](/docs/configuration#session-beta-header-tracking). | `enabled` | | `HEADROOM_BETA_TRACKER_MAX_SESSIONS` | LRU capacity of the in-memory session beta tracker. Once full, the oldest session entry is evicted. | `1000` | | `HEADROOM_PROXY_BETA_HEADER_STICKY` | Rust proxy: same per-conversation beta-token union as `HEADROOM_BETA_HEADER_STICKY`, applied to `anthropic-beta` / `openai-beta` on the intercepted `/v1/messages`, `/v1/chat/completions`, and `/v1/responses` routes. Requires the compression interceptor (`HEADROOM_PROXY_COMPRESSION=1`) — with it off the Rust proxy is a strict byte-pipe and this flag has no effect (startup warns). Unlike the Python tracker (keyed on model + system prompt), sessions are keyed per conversation, shared with the cache-drift detector — parallel conversations never inherit each other's tokens. `enabled` default; `disabled` forwards the client value verbatim and keeps no state. Tracker capacity is fixed at 1000 sessions. | `enabled` | | `HEADROOM_LIVE_ZONE_DISABLE_ARMS` | Rust proxy live-zone dispatcher: comma-separated content types whose compression arm is forced to a no-op, e.g. `source_code,plain_text`. Names are accepted in either the log-tag spelling (`json_array`, `source_code`, `search`, `build`, `diff`, `html`, `text`) or the natural spelling (`search_results`, `build_output`, `git_diff`, `plain_text`). Read once, on the first dispatch of the process, so a running proxy's behaviour cannot change mid-flight; unknown names are logged and ignored rather than failing the request. Shipped as the rollback switch for the `SourceCode` → CodeAwareCompressor and `PlainText` → Kompress arms, but applies to every arm. | -- | | `HEADROOM_MODEL_ROUTER_ENABLED` | Enable cost-aware model routing. `1`/`true`/`yes`/`on`/`enabled` turns it on and requires `HEADROOM_MODEL_ROUTES`. See [Cost-aware model routing](/docs/configuration#cost-aware-model-routing). | `off` | | `HEADROOM_MODEL_ROUTES` | JSON array of ordered routing rules for cost-aware model routing (schema below). | -- | | `HEADROOM_THINKING_COMPACT` | Compact plain-text reasoning that models re-send every turn (Kimi/GLM/DeepSeek `reasoning_content` / inline ``): Kompress it on warm turns, drop it on cold turns. No-op for Claude/Codex/OpenAI (encrypted reasoning). See [Cold-prefix hook](#cold-prefix-hook--reasoning-compaction). | `off` | | `HEADROOM_THINKING_COMPACT_KEEP_LAST` | Most-recent assistant turns whose reasoning is left intact (the active reasoning the model still uses). Only applies with `HEADROOM_THINKING_COMPACT`. | `1` | | `HEADROOM_COLD_RECOMPACT` | On a confirmed-cold turn (idle past the real cache TTL), recompact the whole prefix — cross-turn dedupe + superseded-read drop + lossless folds — instead of forwarding a byte-identical prefix to a dead cache. Warm turns unchanged. Pair with `HEADROOM_DEDUPE`. See [Cold-prefix hook](#cold-prefix-hook--reasoning-compaction). | `off` | | `HEADROOM_PREFIX_STATE_FILE` | Path where the proxy saves its prefix-cache trackers as soon as it gets SIGINT or SIGTERM (so a stop that kills it before in-flight requests drain still keeps them), saves them again at the end of a graceful shutdown, and restores them on the next start (then deletes the file), so warm conversations replay the exact prefix the provider cached instead of re-writing it. The file holds conversation content: it is written `0600`, capped at the 256 newest conversations and 64 MB, and only trackers still inside their cache lifetime are restored, with the retrieval (CCR) entries their markers point at, so those markers resolve even with `HEADROOM_CCR_BACKEND=memory`. Not supported with multiple workers; ignored in stateless mode (neither read nor written). | -- | | `HEADROOM_DEDUPE` | Whole-conversation verbatim cross-turn dedup in the router (cache-safe, information-preserving via retrieval markers). Superseded-read drop + lossless folds run without it; this adds verbatim dedup. | `off` | | `HEADROOM_CACHE_TTL_LEARN` | Append per-turn cache-outcome observations (provider, model, idle, hit/miss) to `cache_ttl_observations.jsonl` for the offline `headroom-cache-ttl` learner. Observation-only (no request-behavior change); respects `HEADROOM_STATELESS`; the log is size-bounded. | `off` | | `HEADROOM_KOMPRESS_ENDPOINT` / `HEADROOM_KOMPRESS_ENDPOINT_TOKEN` | Offload ML compression (Kompress) to a remote endpoint instead of the local ONNX model — used by reasoning compaction and the router when set. | -- | | `HEADROOM_1M_MODEL` | Fallback model that `headroom wrap claude --1m` targets when neither `--model` nor `ANTHROPIC_MODEL` is set. Accepts the id with or without the `[1m]` suffix (added when absent); an explicit `--model` / `ANTHROPIC_MODEL` always wins. See [Claude 1M context window](#claude-1m-context-window-headroom-wrap-claude---1m). | `claude-opus-5` | For provider-only proxying, prefer `HEADROOM_HTTP_PROXY` over process-wide variables such as `HTTP_PROXY`, `HTTPS_PROXY`, `ALL_PROXY`, or `NO_PROXY`. HTTPX reads those global variables, but Headroom also passes them through to tool executions. ### Cold-prefix hook & reasoning compaction [#cold-prefix-hook--reasoning-compaction] Two related optimizations target tokens that agent harnesses re-send every turn: model **reasoning** (re-billed as input on Kimi/GLM/DeepSeek and on Claude 4.6+), and the **prefix** itself once its prompt cache has lapsed. All flags below are **off by default** — the base proxy is unchanged until you opt in. **What to set for what:** | Goal | Set | Notes | |------|-----|-------| | Shrink Kimi/GLM/DeepSeek reasoning re-sent each turn | `HEADROOM_THINKING_COMPACT=1` (+ `HEADROOM_KOMPRESS_ENDPOINT` for the best ratio) | Warm turns Kompress the reasoning; cold turns drop it. No-op for Claude/Codex/OpenAI (their reasoning is an encrypted handle, already cheap on resend). | | Recompact a dead-cache prefix instead of re-sending it whole | `HEADROOM_COLD_RECOMPACT=1` (+ `HEADROOM_DEDUPE=1`) | Fires only when the cache has genuinely lapsed. Works in both `cache` and `token` mode. | | Learn each provider's real cache TTL | `HEADROOM_CACHE_TTL_LEARN=1`, then run the `headroom-cache-ttl` estimator periodically | Sharpens cold detection for providers that don't expose a TTL (Kimi/OpenAI). | **How cold detection knows the TTL.** A wrong "cold" call would recompact a *warm* prefix and bust the cache, so the TTL must be right: - **Claude Code:** read exactly, from the request's `cache_control.ttl` plus CC's own controls — `ENABLE_PROMPT_CACHING_1H` (1h), `FORCE_PROMPT_CACHING_5M` (5m), and `DISABLE_PROMPT_CACHING` (+ per-model `DISABLE_PROMPT_CACHING_`), which turns caching off and makes every turn a free recompaction candidate. - **Kimi / OpenAI / Codex:** they don't expose a TTL, so detection uses a conservative default until the learner (`HEADROOM_CACHE_TTL_LEARN` + the `headroom-cache-ttl` plugin) fills in the real value from observed hits/misses. **Running the estimator.** With `HEADROOM_CACHE_TTL_LEARN=1` set on the proxy, run the batch estimator periodically (cron / launchd — it is deliberately out-of-process so it can never touch the request path): ```bash headroom-cache-ttl # reads cache_ttl_observations.jsonl, writes cache_ttl_learned.json headroom-cache-ttl --dry-run # print the estimate table without writing ``` It emits a TTL for a `provider/model` only when the observations bound it on both sides — hits at some idle prove the cache was still alive (lower bound), `ttl_expiry` misses at a larger idle prove it was dead (upper bound) — and it writes the upper end of that interval: overestimating a TTL only skips a recompaction, while underestimating one would bust a warm cache. Estimates are never pooled across models: one model's expiry says nothing about another's TTL, so a cross-model aggregate could close one model's interval with another's death evidence. Keys without death-evidence beyond their last observed hit are skipped, as is any model with no estimate of its own; both keep the conservative static default. The proxy picks up a rewritten table by mtime; no restart needed. **Can these be on by default?** - `HEADROOM_THINKING_COMPACT` — **stays opt-in.** It rewrites model inputs (reasoning the model actively uses) and depends on Kompress, so its quality / latency trade should be a deliberate choice. - `HEADROOM_COLD_RECOMPACT` — **opt-in today; a candidate to default for Claude Code** once TTL detection is field-validated. It only fires on confirmed-cold turns and recompacts losslessly, but a mis-read TTL would bust a warm cache (expensive), so it waits for confidence. For Kimi/OpenAI it should stay opt-in until the learner has data. - `HEADROOM_CACHE_TTL_LEARN` — **the safest to default on:** observation-only, a size-bounded local log, and it respects `HEADROOM_STATELESS`. Kept opt-in for now so nothing is written to disk unasked. ### Cost-aware model routing [#cost-aware-model-routing] Complementary to content compression, Headroom can rewrite the upstream model per request to stretch quota and control spend, for example by sending small, tool-free requests to a cheaper model. Routing is opt-in and disabled by default, so behavior is unchanged unless you configure it. Enable it with `HEADROOM_MODEL_ROUTER_ENABLED=1` and declare ordered rules in `HEADROOM_MODEL_ROUTES` (a JSON array). The router evaluates rules top to bottom and the first rule whose conditions all match wins; every decision is logged with a reason so routing stays observable. Each rule object supports: | Field | Type | Meaning | |-------|------|---------| | `to_model` | string (required) | Model to route to when the rule matches. | | `max_input_tokens` | integer | Match only when the estimated input size is at or below this. | | `min_input_tokens` | integer | Match only when the estimated input size is at or above this. | | `max_output_tokens` | integer | Match only when the request's own `max_tokens` is at or below this. A request without `max_tokens` does not match. | | `require_no_tools` | boolean | Match only when the request declares no tools (a proxy for low-risk work). | | `from_models` | list of strings | Restrict the rule to these source models. Omit for any source model. | | `name` | string | Label surfaced in the decision log. | ```bash export HEADROOM_MODEL_ROUTER_ENABLED=1 export HEADROOM_MODEL_ROUTES='[ {"name": "small-no-tools", "max_input_tokens": 4000, "require_no_tools": true, "from_models": ["claude-sonnet-4-6"], "to_model": "claude-haiku-4-5"} ]' ``` Notes: - Combine `max_input_tokens` with `max_output_tokens` to downshift only turns that are cheap on both sides: a tiny prompt that asks for a long answer (a large `max_tokens`) stays on the original model. - Input size is a fast, tokenizer-free estimate over the messages, tools, and top-level `system` prompt, meant for tier selection rather than exact accounting. - A malformed rule fails open: it is skipped (never silently widened), and the rest of the rules still apply. - Routing is skipped for byte-faithful passthrough requests (`x-headroom-bypass: true` or `x-headroom-mode: passthrough`), so those are never model-rewritten. - Routing currently applies on the Anthropic `/v1/messages` path. ### Session Beta Header Tracking [#session-beta-header-tracking] When running as a proxy, Headroom maintains a per-session union of `anthropic-beta` (and `OpenAI-Beta`) tokens via `SessionBetaTracker`. The session key is derived from the `x-headroom-session-id` header if present, otherwise from `md5(model + system_prompt[:500])[:16]` — stable across turns of the same conversation. **Why:** clients such as Claude Code and Codex CLI may drop a beta token between consecutive turns. Because `anthropic-beta` is part of the request bytes that determine the upstream prefix-cache key, a dropped token would bust the cache mid-conversation. The tracker re-injects any token seen earlier in the session so the cache key stays stable. **Trade-off:** once the proxy has seen a beta token in a session it will continue re-sending it for the rest of that session, even if the client stops including it. Stopping the token on the client side alone is not sufficient — the proxy re-injects it. Set `HEADROOM_BETA_HEADER_STICKY=disabled` to pass the client's `anthropic-beta` value verbatim and bypass this accumulation. ```bash # Disable sticky beta re-echo export HEADROOM_BETA_HEADER_STICKY=disabled headroom proxy ... ``` Note: disabling sticky mode may reduce prefix-cache hit rates for clients that legitimately drop-and-re-add beta tokens across turns. ### Filesystem Contract [#filesystem-contract] Headroom resolves every on-disk resource through a two-root model (`HEADROOM_CONFIG_DIR` + `HEADROOM_WORKSPACE_DIR`) with additive precedence rules: explicit argument > per-resource env var > derived from canonical root > default. Every legacy env var continues to work unchanged. See the **[Filesystem Contract](/docs/filesystem-contract)** page for the full bucket table, plugin-author guidance, and the Docker naming overlap note (`HEADROOM_WORKSPACE` is *not* the same as `HEADROOM_WORKSPACE_DIR`). ## Custom Model Configuration [#custom-model-configuration] Configure context limits and pricing for new or custom models: ```json { "anthropic": { "context_limits": { "claude-4-opus-20250301": 200000, "claude-custom-finetune": 128000 }, "pricing": { "claude-4-opus-20250301": { "input": 15.00, "output": 75.00, "cached_input": 1.50 } } }, "openai": { "context_limits": { "ft:gpt-4o:my-org": 128000, "my-private-deployment": 200000 }, "pricing": { "my-private-deployment": [5.00, 15.00] } } } ``` Save as `${HEADROOM_CONFIG_DIR}/models.json` (defaults to `~/.headroom/config/models.json`), or set `HEADROOM_MODEL_LIMITS` to a JSON string or file path. Installs that still have `~/.headroom/models.json` (the legacy location) continue to work. Configure **models Headroom can't already look up** — fine-tunes, private deployments, gateway aliases. Public models are resolved from LiteLLM (see below), so pinning one here means maintaining a number yourself that would otherwise stay current on its own. ### Where limits and prices come from [#where-limits-and-prices-come-from] Headroom consults the **installed LiteLLM model database** (`litellm.model_cost`) for public model metadata. That data ships with the LiteLLM package rather than being fetched at runtime, so it is as current as your installed LiteLLM version and refreshes when you upgrade it. Nothing here makes a network call. Limits and prices do **not** use the same precedence. Both are first-match-wins: **Context limits** 1. **Explicit configuration and the built-in table**, checked together — they are merged into one mapping, so an exact match in either returns immediately, then partial/prefix matches. 2. **LiteLLM** (`max_input_tokens`) — reached only for models the previous step didn't match. 3. **Pattern inference**, then a generic default. **Pricing** 1. **Explicit configuration** — a value you configured is a decision, so it beats everything below. 2. **LiteLLM.** 3. **Built-in table**, then pattern inference, then a generic default. The asymmetry is deliberate, not an oversight. For limits, a built-in entry outranks LiteLLM because LiteLLM reports a model's maximum *capability* while Headroom needs its *effective default*. `claude-sonnet-4-20250514` is the concrete case: LiteLLM reports 1,000,000, but that window is an opt-in beta, so the built-in 200,000 is the safe assumption for a client that has not enabled it. Pricing has no equivalent capability-vs-default split, so there LiteLLM wins outright. Limits are an **input** budget (`max_input_tokens`), not the total window: `gpt-5` resolves to 272K input, not the 400K total (272K in + 128K out). LiteLLM also resolves gateway-routed names — `azure/...`, `bedrock/...`, `vertex_ai/...`, `groq/...` — which the built-in tables never covered. The built-in tables additionally cover installs where LiteLLM is unavailable: the dependency is gated `python_version < '3.14'`. ### Pattern-Based Inference [#pattern-based-inference] Models that reach step 3 and match no table entry are inferred from naming patterns: | Pattern | Inferred Settings | |---------|-------------------| | `*opus*` | 200K context, Opus-tier pricing | | `*sonnet*` | 200K context, Sonnet-tier pricing | | `*haiku*` | 200K context, Haiku-tier pricing | | `gpt-4o*` | 128K context, GPT-4o pricing | | `gpt-4.1*` | ~1M context, GPT-4.1 pricing | | `gpt-5*` | 272K input, GPT-5 pricing | | `o1*`, `o3*`, `o4*` | 200K context, reasoning model pricing | Matching prefers the **most specific** prefix, so `gpt-4.1` is not treated as `gpt-4` (which would give it an 8K limit and legacy `gpt-4` pricing). ## Provider-Specific Settings [#provider-specific-settings] Provider classes configure token counting and context/pricing overrides, not cache toggles -- provider-specific cache optimization (Anthropic `cache_control` breakpoints, OpenAI prefix stabilization, Google `CachedContent`) is controlled by `enable_cache_optimizer` on `HeadroomClient` (see [SDK Configuration](#sdk-configuration) above), which auto-detects the right strategy from the provider you pass in. ```python from headroom import OpenAIProvider provider = OpenAIProvider( context_limits={"gpt-4o": 128000}, ) ``` ```python from headroom import AnthropicProvider from anthropic import Anthropic provider = AnthropicProvider( # Optional: enables accurate token counting via Anthropic's Token Count API client=Anthropic(), ) ``` ```python from headroom.providers import GoogleProvider # Optional: pass a configured google.generativeai module for API-based # token counting via countTokens; omit for estimation-based counting. provider = GoogleProvider() ``` ## Tool Profiles [#tool-profiles] Skip or customize compression for specific tools: ```python response = client.chat.completions.create( model="gpt-4o", messages=messages, headroom_tool_profiles={ "important_tool": {"skip_compression": True}, "search_tool": {"max_items_after_crush": 25}, }, ) ``` ## Configuration Precedence [#configuration-precedence] Settings are applied in this order (later overrides earlier): 1. Default values 2. Environment variables 3. SDK constructor arguments 4. Per-request overrides ## Validation [#validation] Validate your configuration at startup: ```python result = client.validate_setup() if not result["valid"]: print("Configuration issues:") for check in ("provider", "storage", "config", "cache_optimizer"): if result[check]["error"]: print(f" - {check}: {result[check]['error']}") ``` Context management is now handled automatically inside the pipeline (live-zone-only compression). Headroom **never drops messages** from the conversation history and does not do position-based or score-based context management. ## How It Works [#how-it-works] Headroom compresses only the **newest content blocks** — the latest user message and the latest tool result / tool output. Compression is type-aware and reversible via [CCR](/docs/ccr), so the LLM can retrieve the original content on demand. The **cache hot zone** — the system prompt, tool definitions, and older turns — is never mutated. Leaving the prefix untouched preserves provider prompt caching, so cache hit rates stay stable across turns. ``` Conversation with a large latest tool result -> Identify the live zone (newest user message + latest tool output) -> Compress the live zone type-aware, cache original in CCR (hash=def456) -> Insert marker: "compressed, retrieve: def456" -> Older turns, tools, and system prompt are forwarded byte-for-byte ``` ## Protection rules [#protection-rules] Headroom enforces several protections to ensure model output quality: ### Output buffer reservation [#output-buffer-reservation] A configurable number of tokens is reserved for the model's response. The context budget is calculated as: ``` context_budget = model_context_limit - output_buffer_tokens ``` This prevents the input from consuming the entire context window and leaving no room for the model to respond. ### System message protection [#system-message-protection] System messages are never dropped. They contain critical instructions, persona definitions, and tool descriptions that the model needs throughout the conversation. ### Turn protection [#turn-protection] The last N messages are always preserved, ensuring the model has immediate conversational context. The one-function `compress()` API exposes this as `CompressConfig.protect_recent`, which defaults to `4` messages -- see `headroom/compress.py`. It guards recent **code** only: tool payloads detected as JSON, logs or text are not shielded by it. File reads are held back by read protection instead, which protects by default and releases only confidently non-code data (JSON, CSV/tabular, build/test logs, git diffs, HTML, search output) -- see `_read_output_should_be_protected`. ## Configuration [#configuration] Context management is now automatic. Use per-request overrides to control behavior: ```ts twoslash import { compress } from "headroom-ai"; const result = await compress(messages, { model: "gpt-4o", tokenBudget: 32000, }); console.log(`Compressed: ${result.tokensBefore} -> ${result.tokensAfter}`); ``` ```python from headroom import HeadroomClient, OpenAIProvider from openai import OpenAI client = HeadroomClient( original_client=OpenAI(), provider=OpenAIProvider(), default_mode="optimize", ) # Per-request overrides response = client.chat.completions.create( model="gpt-4o", messages=messages, headroom_output_buffer_tokens=8000, # More room for long responses ) ``` `HeadroomClient.chat.completions.create()` also accepts a `headroom_keep_turns` parameter, but as of this package version it is accepted and threaded through the wrapper methods without being forwarded into the transform pipeline (see `headroom/client.py` -- `TransformPipeline.apply()`'s accepted kwargs, listed in its docstring, don't include a turns/keep-turns option). Passing it does not raise an error, but it currently has no effect; use `compress()`'s `protect_recent` (see [Turn protection](#turn-protection) above) if you need this control today. > **Note:** The `IntelligentContextConfig`, `ScoringWeights`, and `RollingWindowConfig` classes are no longer part of Headroom. Context management is now handled automatically inside the pipeline (live-zone-only compression). Many companies route laptop traffic through a gateway that **inspects TLS**: it opens each HTTPS connection, checks it, and re-signs it with the company's own root certificate. IT installs that root in the operating system's certificate store, which is why browsers, `curl` and Claude Code keep working. Headroom verifies upstream certificates against the **same OS certificate store** (macOS Keychain, Windows certificate store, the system bundle on Linux), plus the public roots bundled with Python. On a managed machine this usually needs no configuration. ## Quick check [#quick-check] ```bash headroom doctor --network ``` For each endpoint Headroom uses, this prints who signed the certificate the network presented (for example `Zscaler Intermediate Root CA`), whether Headroom trusts it, and whether a gateway block page was returned instead of the service. If anything fails, the last row is a short request you can send to IT as-is. Add `--network-url https://your-gateway.example.com/v1/models` to test a custom upstream too. ## If a certificate is still not trusted [#if-a-certificate-is-still-not-trusted] The error Claude Code (or Codex) shows will read like: > Headroom could not verify the TLS certificate for api.anthropic.com (unable to get local issuer certificate). The certificate was issued by 'O=Zscaler Inc., CN=Zscaler Intermediate Root CA': Zscaler is inspecting this connection. ... Pick one fix: 1. **Ask IT to deploy the root to the OS certificate store.** This is the normal setup for Zscaler Client Connector ("Install Zscaler SSL Certificate") and most MDM profiles. Nothing else to change. 2. **Point Headroom at the root as a file.** Export it (your browser's certificate viewer, or IT) as PEM and set: ```bash export HEADROOM_CA_BUNDLE=/path/to/company-root.pem ``` `HEADROOM_CA_BUNDLE` and `NODE_EXTRA_CA_CERTS` are **added** to the trusted set. If you already set `NODE_EXTRA_CA_CERTS` for Claude Code, Headroom picks it up. 3. **Ask IT to exempt the provider domains from TLS inspection** (see the list below). Never turn certificate checking off to get past this. Headroom has no switch for that on purpose. ## Trust settings [#trust-settings] | Env var | Default | Effect | |---|---|---| | `HEADROOM_CERT_STORE` | `system,bundled` | Where trusted roots come from: `system` = OS certificate store, `bundled` = certifi's public roots. Set `bundled` to go back to the previous behavior (public roots only). Mirrors Claude Code's `CLAUDE_CODE_CERT_STORE`. | | `HEADROOM_CA_BUNDLE` | unset | Extra PEM roots, added to the above. | | `NODE_EXTRA_CA_CERTS` | unset | Same as `HEADROOM_CA_BUNDLE` (shared with Claude Code). | | `SSL_CERT_FILE` / `REQUESTS_CA_BUNDLE` | unset | **Replace** every other source: only this bundle is trusted. Use only if the bundle is complete. | | `HEADROOM_TLS_STRICT` | strict | `0` relaxes OpenSSL's RFC 5280 strict checks on the bundled path. Not needed with the OS store; kept for `HEADROOM_CERT_STORE=bundled`. | The same policy covers the proxy's HTTP and WebSocket upstreams, GitHub Copilot sign-in, and model and tokenizer downloads made from Python. The Rust proxy binary (`headroom-proxy`, used in the Docker image) trusts Mozilla's roots plus the OS store for all upstreams, and adds `HEADROOM_CA_BUNDLE` / `NODE_EXTRA_CA_CERTS` for HTTP upstreams. One gap remains: the embedding model that the Rust core fetches for relevance scoring still trusts Mozilla's roots only. Behind inspection that download fails and Headroom falls back to BM25 scoring; use `HF_ENDPOINT` (below) or pre-populate the cache to keep embeddings. In a container there is usually no corporate root in the OS store: mount it and set `HEADROOM_CA_BUNDLE`. ## HTTP proxies [#http-proxies] If your company uses an explicit proxy (`HTTPS_PROXY` set, or a PAC file), Headroom's upstream calls go through it. The agent's calls **to** Headroom must not: they target `127.0.0.1`, which the corporate proxy cannot reach. `headroom wrap` adds `127.0.0.1,localhost,::1` to `NO_PROXY` for the processes it launches; if you start the agent yourself, set: ```bash export NO_PROXY="$NO_PROXY,127.0.0.1,localhost,::1" ``` PAC files are not evaluated by Python. Export the proxy the PAC file resolves to as `HTTPS_PROXY`. ## What to ask IT for [#what-to-ask-it-for] Headroom runs on the developer's machine and talks to the same services the AI tools already use, plus two download hosts: | Domain | Used for | Required | |---|---|---| | `api.anthropic.com` | Claude / Claude Code | yes, for Anthropic | | `api.openai.com`, `chatgpt.com` | OpenAI API, Codex | yes, for OpenAI / Codex | | `api.githubcopilot.com` (or `api.business.githubcopilot.com` / `api.enterprise.githubcopilot.com` for those plans), `api.github.com` | GitHub Copilot | for Copilot | | `huggingface.co` and the storage hosts it redirects to (`cdn-lfs.hf.co`, `cas-bridge.xethub.hf.co`) | Compression models (downloaded once) | recommended | | `openaipublic.blob.core.windows.net` | Tokenizer vocabulary (downloaded once) | recommended | Things that help: - **Deploy the inspection root to the OS store.** This is enough for Headroom; no per-app certificate work is needed. - **Do not route these domains to "Caution" or "Isolate".** An API client cannot click through an interstitial page; it sees HTML where it expected JSON. - **Streaming:** some gateways buffer server-sent events while inspecting them, so responses arrive in bursts or time out. If users see stalled streams, exempt the provider domains from inspection or DLP buffering. - **Please don't exempt `python` as a process** (for example Zscaler's process-based bypass). Headroom does not need it once the root is trusted, and it would exempt every Python program on the machine. If your company uses Anthropic **tenant restrictions**, those require inspection of `api.anthropic.com` to stay on. That works with Headroom as long as the root is trusted. ## Model downloads when huggingface.co is blocked [#model-downloads-when-huggingfaceco-is-blocked] Headroom degrades instead of failing: without the compression model it falls back to rule-based compression, and without the tokenizer vocabulary it estimates token counts. To keep ML compression on a locked-down network, point Headroom at an internal Hugging Face mirror (Artifactory, Nexus): ```bash export HF_ENDPOINT=https://artifactory.example.com/api/huggingface/huggingface ``` Or pre-populate `~/.cache/huggingface` from a machine that has access. Headroom integrates with [CrewAI](https://github.com/crewAIInc/crewAI) to compress tool outputs before they enter the agent's LLM context. Tool-heavy agents that return large JSON arrays, database results, or verbose logs see the largest token reductions. ## Installation [#installation] ```bash pip install headroom-ai crewai ``` ## Quick start [#quick-start] Wrap tools in one line: ```python from crewai import Agent, Crew, Task from crewai.tools.base_tool import tool from headroom.integrations.crewai import wrap_tools_with_headroom @tool def search_database(query: str) -> str: """Search the database and return results.""" return json.dumps({"results": [...], "total": 1000}) wrapped = wrap_tools_with_headroom([search_database]) agent = Agent( role="Researcher", goal="Answer questions using data", backstory="You research things.", tools=wrapped, ) task = Task(description="Find all active users", agent=agent, expected_output="Summary") crew = Crew(agents=[agent], tasks=[task]) crew.kickoff() ``` ## Per-tool metrics [#per-tool-metrics] Track compression stats across all tool invocations: ```python from headroom.integrations.crewai import get_tool_metrics metrics = get_tool_metrics() print(metrics.get_summary()) # { # 'total_invocations': 25, # 'total_compressions': 18, # 'total_chars_saved': 450000, # 'average_compression_ratio': 0.35, # 'by_tool': { # 'search_database': {'invocations': 15, 'compressions': 12, 'chars_saved': 320000}, # 'fetch_logs': {'invocations': 10, 'compressions': 6, 'chars_saved': 130000}, # } # } ``` Reset between sessions: ```python from headroom.integrations.crewai import reset_tool_metrics reset_tool_metrics() ``` ## Custom configuration [#custom-configuration] Control the compression threshold: ```python wrapped = wrap_tools_with_headroom( [search_database, fetch_logs], min_chars_to_compress=500, # Default: 1000 ) ``` Use a dedicated metrics collector instead of the global one: ```python from headroom.integrations.crewai import ToolMetricsCollector, wrap_tools_with_headroom collector = ToolMetricsCollector() wrapped = wrap_tools_with_headroom( [search_database], metrics_collector=collector, ) # After crew run print(collector.get_summary()) ``` ## Wrapping individual tools [#wrapping-individual-tools] For finer control, wrap tools individually: ```python from headroom.integrations.crewai import HeadroomToolWrapper wrapper = HeadroomToolWrapper( search_database, min_chars_to_compress=500, ) # Use wrapper directly — it's a BaseTool agent = Agent(role="Researcher", tools=[wrapper], ...) ``` ## How it works [#how-it-works] CrewAI tools extend `BaseTool` with a `run()` → `_run()` execution flow. `HeadroomToolWrapper` subclasses `BaseTool` and overrides `_run()` to: 1. Call the original tool's `run()` method 2. Check if the output exceeds `min_chars_to_compress` 3. If so, compress via Headroom's `compress_tool_result()` 4. Record metrics and return the compressed output The wrapper preserves the original tool's name, description, and argument schema, so it works as a drop-in replacement anywhere CrewAI expects a tool. Run Headroom without installing Python or Node.js on the host. The install scripts add a native `headroom` wrapper that keeps **Headroom itself** in Docker while orchestrating the rest of your workflow on the host OS. ## One-line install [#one-line-install] ### Linux [#linux] ```bash curl -fsSL https://raw.githubusercontent.com/headroomlabs-ai/headroom/main/scripts/install.sh | bash ``` ### macOS (bash 4.3+) [#macos-bash-43] ```bash curl -fsSL https://raw.githubusercontent.com/headroomlabs-ai/headroom/main/scripts/install.sh | "$(brew --prefix bash)/bin/bash" ``` Stock `/bin/bash` on macOS is 3.2, so install a newer bash first (for example via Homebrew) and run the installer with that shell. The installed wrapper pins that same bash interpreter so later invocations stay on the supported runtime. ### Windows PowerShell [#windows-powershell] ```powershell irm https://raw.githubusercontent.com/headroomlabs-ai/headroom/main/scripts/install.ps1 | iex ``` ## What the installer does [#what-the-installer-does] 1. Verifies Docker is installed and available. 2. Pulls `ghcr.io/headroomlabs-ai/headroom:latest` by default, or reuses / pulls `HEADROOM_DOCKER_IMAGE` when you set a custom image override. 3. Installs a `headroom` wrapper into `~/.local/bin` or `~/bin`. 4. Updates shell startup files so the wrapper directory is on `PATH`. The wrapper keeps Headroom inside Docker and mounts host state back into the container so native behavior stays consistent: - project workspace → `/workspace` - `~/.headroom` - `~/.claude` - `~/.codex` - `~/.gemini` Port `8787` stays the default, so `http://localhost:8787` works the same way as a native install. Published releases also push versioned GHCR tags such as `ghcr.io/headroomlabs-ai/headroom:0.37.0`, and those images are built with the same synced package version used for the matching PyPI and npm release. ## How the wrapper behaves [#how-the-wrapper-behaves] ### Native Headroom commands [#native-headroom-commands] These run directly inside the container: ```bash headroom proxy headroom learn headroom mcp install headroom memory list ``` For `proxy`, the maintained wrapper always publishes the selected host port on loopback. It passes `--host 0.0.0.0` inside the container so that the loopback publication can reach the container, and it has no public-publication override. For local-only use no token is needed, but the proxy refuses to start on a non-loopback bind without one unless you acknowledge that the runtime already restricts who can reach the port (here: the `127.0.0.1` publication): ```bash docker run --rm -it \ -p 127.0.0.1:8787:8787 \ -e HEADROOM_ALLOW_UNAUTHENTICATED_BIND=1 \ -v "$PWD:/workspace" \ -w /workspace \ ghcr.io/headroomlabs-ai/headroom:latest \ --host 0.0.0.0 --port 8787 ``` The maintained wrappers (`headroom install`, the `install.sh` / `install.ps1` native wrappers, and `docker/docker-compose.native.yml`) set that acknowledgement themselves, and only together with the `127.0.0.1` publication it depends on. For deliberate public access, publish on an explicit public address and set `HEADROOM_PROXY_TOKEN`: ```bash docker run --rm -p 0.0.0.0:8787:8787 \ -e HEADROOM_PROXY_TOKEN='replace-with-a-secret' \ ghcr.io/headroomlabs-ai/headroom:latest --host 0.0.0.0 --port 8787 ``` ### `wrap` commands [#wrap-commands] `wrap` is host-oriented in Docker-native mode: - the wrapper starts the Headroom proxy in Docker - container-side prep writes Headroom config and memory into mounted host files - the target CLI itself is launched on the host by the wrapper Supported host wrap flows: - `headroom wrap claude` - `headroom wrap codex` - `headroom wrap aider` - `headroom wrap cursor` - `headroom wrap openclaw` - `headroom unwrap openclaw` OpenClaw remains host-native in Docker-native mode: - the host must already have the `openclaw` CLI installed - `headroom wrap openclaw` installs/configures the Headroom plugin through the host `openclaw` CLI - plugin auto-start still launches the installed host `headroom` wrapper from `PATH`, which then runs Headroom in Docker - local plugin source mode (`--plugin-path`) is also supported, but it may require host `npm` when build steps are needed ## Persistent Docker lifecycle from the native wrapper [#persistent-docker-lifecycle-from-the-native-wrapper] The Docker-native `headroom` wrapper exposes the persistent Docker lifecycle directly: ```bash headroom install apply --profile default --preset persistent-docker headroom install status headroom install restart headroom install remove ``` In Docker-native mode this surface is intentionally scoped to **persistent-docker**: - supported: `apply`, `status`, `start`, `stop`, `restart`, `remove` - supported flags: `--profile`, `--port`, `--backend`, `--anyllm-provider`, `--region`, `--mode`, `--memory`, `--no-telemetry`, `--image` - not supported: `persistent-service`, `persistent-task`, or provider/user/system mutation flags such as `--scope`, `--providers`, and `--target` Those broader lifecycle and config-mutation flows still belong to the Python-native `headroom install ...` command. Persistent Docker deployments launched by the wrapper also tag the proxy process with deployment metadata, so `/health` reports the active `profile`, `preset`, `runtime`, `supervisor`, and `scope` the same way the Python install subsystem does. ## Docker Compose support [#docker-compose-support] Use `docker/docker-compose.native.yml` when you want an explicit compose-managed proxy or CLI shell, or when you prefer compose over the native wrapper's `headroom install ...` surface. ### Persistent Docker runtime [#persistent-docker-runtime] The `proxy` service uses `restart: unless-stopped`, so compose can act as the always-on Docker runtime for Headroom: ```bash export HEADROOM_HOST_HOME="$HOME" export HEADROOM_WORKSPACE="$PWD" docker compose -f docker/docker-compose.native.yml up -d proxy ``` ```powershell $env:HEADROOM_HOST_HOME = $HOME $env:HEADROOM_WORKSPACE = (Get-Location).Path docker compose -f docker/docker-compose.native.yml up -d proxy ``` This is a supported persistent-Docker path when you want the proxy managed explicitly through Compose instead of the installed wrapper. These are two different variables — both are set by the compose file, and both are retained for backward compatibility: - **`HEADROOM_WORKSPACE`** (host-side) is the directory the compose file bind-mounts into the container as `/workspace`. It behaves like CWD in a native (non-Docker) run. - **`HEADROOM_WORKSPACE_DIR`** (inside the container) is the canonical Headroom state root from the [filesystem contract](/docs/filesystem-contract). The compose file sets it to `/tmp/headroom-home/.headroom` so the proxy resolves savings, logs, TOIN, and memory under the bind-mounted `${HOME}/.headroom`. You do not need to set `HEADROOM_WORKSPACE_DIR` manually when using the shipped compose file — it is already in the `environment:` block. ### macOS / Linux [#macos--linux] ```bash export HEADROOM_HOST_HOME="$HOME" export HEADROOM_WORKSPACE="$PWD" docker compose -f docker/docker-compose.native.yml up proxy ``` ### Windows PowerShell [#windows-powershell-1] ```powershell $env:HEADROOM_HOST_HOME = $HOME $env:HEADROOM_WORKSPACE = (Get-Location).Path docker compose -f docker/docker-compose.native.yml up proxy ``` You can also run one-off CLI commands through compose: ```bash docker compose -f docker/docker-compose.native.yml run --rm cli learn docker compose -f docker/docker-compose.native.yml run --rm cli mcp install ``` ## Environment passthrough [#environment-passthrough] The wrapper forwards Headroom and provider environment variables into the container, including common prefixes such as: - `HEADROOM_` - `ANTHROPIC_` - `OPENAI_` - `GEMINI_` - `AWS_` - `GOOGLE_` / `GOOGLE_CLOUD_` - `AZURE_` - `OTEL_` That keeps provider auth and runtime config working without maintaining a separate env file for the container. ## Notes [#notes] - Docker is the only required Headroom runtime dependency on the host. - Wrapped tools like Claude Code, Codex CLI, Aider, and Cursor still run on the host when you use `headroom wrap ...`. - The install scripts are idempotent: rerunning them refreshes the wrapper and image without duplicating shell profile blocks. - For persistent service and task installs, use the Python-native `headroom install ...` workflow — see [Persistent Installs](/docs/persistent-installs). - For Docker-native `headroom install ...`, the wrapper persists its profile manifest under `~/.headroom/deploy//`. ## Next steps [#next-steps] Use `headroom wrap droid` to run [Factory Droid](https://docs.factory.ai) through the local Headroom proxy. Droid keeps talking to Factory with its own login; Headroom sits in between and compresses the Anthropic-shaped inference requests. ## Quick Start [#quick-start] ```bash headroom wrap droid ``` Pass Droid arguments after `--`: ```bash headroom wrap droid -- exec "fix the failing test" ``` There is nothing to undo. The wrapper writes no Droid config and no `customModels`; it only sets an environment variable on the `droid` process it launches. ## What `wrap droid` Does [#what-wrap-droid-does] | Step | What happens | |---|---| | Upstream | Resolves the real Factory upstream (see [Upstream precedence](#upstream-precedence)) | | Proxy | Starts or reuses a Headroom proxy started with `--factory-api-url ` | | Launch | Runs `droid` with `FACTORY_API_BASE_URL=http://127.0.0.1:` | If the requested port is busy, or a proxy with different routing is running there, the wrapper picks a free port and points `FACTORY_API_BASE_URL` at it. You do not need to pass `--port` yourself. ## What Is Compressed [#what-is-compressed] | Path | Behaviour | |---|---| | `POST /api/llm/a/v1/messages` | Compressed with the same pipeline as `/v1/messages`, then forwarded to `/api/llm/a/v1/messages` | | Any other `/api/*` path | Forwarded verbatim to the upstream (method, query string, headers and body unchanged) | | Anything outside `/api/` | Never sent to Factory; keeps the proxy's normal routing | OpenAI-shaped traffic under `/api/llm/o/` is **not compressed yet**. It still reaches Factory, but is forwarded verbatim and buffered rather than streamed incrementally. Models Droid serves over that route see no savings. A few `/api/*` requests keep the proxy's normal behaviour instead of going to Factory: requests that set `x-headroom-base-url`, and Hermes Studio's `/api/codex-proxy/` and `/api/claude-code-proxy/` paths. On the compressed route, Headroom ignores a client-sent `x-headroom-base-url` and strips every `x-headroom-*` header before forwarding. Your Factory `Authorization` header is forwarded unchanged. Operator-configured Anthropic extra headers (`ANTHROPIC_TARGET_API_HEADERS`) are **not** sent to Factory. ## Upstream Precedence [#upstream-precedence] 1. `--factory-api-url ` 2. An existing `FACTORY_API_BASE_URL` in your shell (for example an enterprise or regional gateway) 3. `https://api.factory.ai` The upstream must be an `http(s)` URL with no credentials, query string or fragment. Loopback hosts (`localhost`, `127.x`, `[::1]`) are rejected, which stops a leftover `FACTORY_API_BASE_URL` from an earlier wrapped session from pointing the proxy at itself. If you see that error, unset `FACTORY_API_BASE_URL` and run the command again. ## Using an Existing Proxy [#using-an-existing-proxy] With `--no-proxy`, the wrapper does not start a proxy. It refuses to launch Droid unless the port answers `/health` as a Headroom proxy that serves the same Factory upstream: ```bash headroom proxy --port 8787 --factory-api-url https://api.factory.ai headroom wrap droid --no-proxy --port 8787 ``` A non-Headroom listener, a proxy without Factory routing, or a proxy for a different Factory upstream is an error, so Droid's credentials never go to a listener that was not set up for them. ## Options [#options] | Option | Description | |---|---| | `--port`, `-p` | Proxy port (default: `$HEADROOM_PORT`, else 8787) | | `--factory-api-url` | Real Factory upstream (default: `$FACTORY_API_BASE_URL`, else `https://api.factory.ai`) | | `--no-proxy` | Use an existing Factory-mode proxy instead of starting one | | `--learn` | Enable live traffic learning | | `--memory` | Enable persistent cross-session memory | Headroom provides explicit exceptions for debugging, with a core safety guarantee: **compression failures never break your LLM calls**. If compression fails, the original content passes through unchanged. ## Error Hierarchy [#error-hierarchy] ``` HeadroomError (base class) +-- HeadroomConnectionError # Cannot reach proxy +-- HeadroomAuthError # 401 from proxy +-- HeadroomCompressError # Compression failed (with statusCode) +-- ConfigurationError # Invalid configuration +-- ProviderError # Provider issues +-- StorageError # Storage failures +-- TokenizationError # Token counting failed +-- CacheError # Cache operations failed +-- ValidationError # Validation failures +-- TransformError # Transform execution failed ``` ```ts twoslash import { HeadroomError, HeadroomConnectionError, HeadroomAuthError, HeadroomCompressError, ConfigurationError, ProviderError, mapProxyError, } from 'headroom-ai'; ``` ``` HeadroomError (base class) +-- ConfigurationError # Invalid configuration +-- ProviderError # Provider issues (unknown model, etc.) +-- StorageError # Database/storage failures +-- CompressionError # Compression failures (rare) +-- TokenizationError # Token counting failed +-- CacheError # Cache operations failed +-- ValidationError # Setup validation failures +-- TransformError # Transform execution failed ``` ```python from headroom import ( HeadroomError, ConfigurationError, ProviderError, StorageError, CompressionError, TokenizationError, CacheError, ValidationError, TransformError, ) ``` ## Catching Errors [#catching-errors] ```ts twoslash import { compress, HeadroomConnectionError, HeadroomAuthError, HeadroomCompressError, HeadroomError } from 'headroom-ai'; try { const result = await compress(messages, { model: 'gpt-4o' }); } catch (e) { if (e instanceof HeadroomConnectionError) { console.error('Cannot reach proxy:', e.message); } else if (e instanceof HeadroomAuthError) { console.error('Auth failed:', e.message); } else if (e instanceof HeadroomCompressError) { console.error(`Compress failed (${e.statusCode}):`, e.message); } else if (e instanceof HeadroomError) { console.error('Headroom error:', e.message, e.details); } } ``` ```python from headroom import ( HeadroomClient, HeadroomError, ConfigurationError, StorageError, ) try: client = HeadroomClient(...) response = client.chat.completions.create(...) except ConfigurationError as e: print(f"Config issue: {e}") print(f"Details: {e.details}") except StorageError as e: print(f"Storage issue: {e}") # Headroom continues to work, just without metrics persistence except HeadroomError as e: print(f"Headroom error: {e}") ``` ## Error Types in Detail [#error-types-in-detail] ### ConfigurationError [#configurationerror] Raised when configuration is invalid. In the Python SDK (0.37.0), `ConfigurationError` is importable and exported from `headroom`, but `HeadroomClient` itself does not currently raise it: an invalid `default_mode` string is validated by the `HeadroomMode` enum and raises a plain **`ValueError`**, not `ConfigurationError`. ```ts twoslash import { ConfigurationError } from 'headroom-ai'; // ConfigurationError is thrown when the proxy returns // a configuration_error type in its error response ``` ```python try: client = HeadroomClient( original_client=OpenAI(), provider=OpenAIProvider(), default_mode="invalid_mode", ) except ValueError as e: print(f"Invalid mode: {e}") # 'invalid_mode' is not a valid HeadroomMode ``` ### ProviderError [#providererror] Reserved for provider-specific issues, but not currently raised anywhere in the SDK. In particular, an unknown model name does **not** raise `ProviderError`: `Provider.get_context_limit()` is documented to "never raise an exception -- uses sensible defaults for unknown models," and only logs a one-time `WARNING` naming the fallback it used. ```python import logging logging.basicConfig(level=logging.WARNING) # Does NOT raise ProviderError -- falls back to a default context limit # and logs: WARNING:headroom.providers.openai:Unknown OpenAI model # 'unknown-model-xyz': using default limit (...). To configure explicitly, # set HEADROOM_MODEL_LIMITS env var or add to ~/.headroom/models.json response = client.chat.completions.create( model="unknown-model-xyz", messages=[...], ) ``` ### StorageError [#storageerror] Reserved for database/storage failures. `HeadroomClient.get_metrics()` and `get_summary()` currently delegate straight to the storage backend (`headroom/client.py`) without wrapping failures in `StorageError`, so a broken database today surfaces as the underlying driver's own exception (for example `sqlite3.OperationalError`) rather than `StorageError`. Catch the base `HeadroomError` -- or `Exception` around storage calls specifically -- until that wrapping lands. ```python try: metrics = client.get_metrics() except Exception as e: metrics = [] # Continue without historical metrics ``` ### CompressionError [#compressionerror] Reserved for compression failures. As of 0.37.0 this class is exported but not raised anywhere in the codebase -- there is no "strict mode" that triggers it. Compression failures are instead caught internally by broad `except Exception` handlers throughout `headroom/transforms/`, which fail open and return the original, uncompressed content. This is what backs the safety guarantee below. ### HeadroomConnectionError (TypeScript) [#headroomconnectionerror-typescript] Raised when the TypeScript SDK cannot connect to the Headroom proxy. ```ts twoslash import { compress, HeadroomConnectionError } from 'headroom-ai'; try { await compress(messages, { model: 'gpt-4o' }); } catch (e) { if (e instanceof HeadroomConnectionError) { console.error('Is the proxy running? Start with: headroom proxy'); } } ``` ## Proxy Error Mapping [#proxy-error-mapping] The TypeScript SDK automatically maps proxy error responses to the correct error class: | HTTP Status | Proxy Error Type | TypeScript Class | |-------------|-----------------|-----------------| | 401 | -- | `HeadroomAuthError` | | 4xx/5xx | `configuration_error` | `ConfigurationError` | | 4xx/5xx | `provider_error` | `ProviderError` | | 4xx/5xx | `storage_error` | `StorageError` | | 4xx/5xx | `tokenization_error` | `TokenizationError` | | 4xx/5xx | `cache_error` | `CacheError` | | 4xx/5xx | `validation_error` | `ValidationError` | | 4xx/5xx | `transform_error` | `TransformError` | | 4xx/5xx | (other) | `HeadroomCompressError` | The `mapProxyError()` function handles this mapping: ```ts twoslash import { mapProxyError } from 'headroom-ai'; const error = mapProxyError(400, 'configuration_error', 'Invalid mode'); // Returns a ConfigurationError instance ``` ## Error Details [#error-details] All Headroom exceptions include a `details` dict/object with additional context: ```ts twoslash import { HeadroomError } from 'headroom-ai'; // HeadroomError.details is Record | undefined // HeadroomCompressError also has .statusCode and .errorType ``` ```python try: client = HeadroomClient(...) except HeadroomError as e: print(f"Error: {e}") print(f"Type: {type(e).__name__}") print(f"Details: {e.details}") # Details might include: # - field: which config field caused the error # - provider: which provider was involved # - model: which model was requested # - original_error: underlying exception ``` ## Safety Guarantee [#safety-guarantee] If compression fails, the original content passes through unchanged. Your LLM calls never fail due to Headroom: ```python messages = [ {"role": "tool", "content": "malformed json {{{"} ] # This will NOT raise an exception # The malformed content passes through unchanged response = client.chat.completions.create( model="gpt-4o", messages=messages, ) ``` ## Best Practices [#best-practices] 1. **Catch specific exceptions** rather than broad `Exception` to avoid hiding real bugs 2. **Don't rely on storage failures raising** -- as of 0.37.0 they surface as the underlying driver's own exception, not `StorageError` (see above) 3. **Validate on startup** with `client.validate_setup()` to catch configuration issues early 4. **Enable logging** at WARNING level to see when compression falls back safely ```python import logging logging.basicConfig(level=logging.WARNING) # WARNING:headroom.transforms.smart_crusher:SmartCrusher audit_safe: 1 protected # row(s) could not be preserved through compression (...). Failing closed: # returning original uncompressed. ``` `headroom learn` analyzes past coding agent sessions, finds what went wrong, correlates each failure with what eventually worked, and writes specific project-level learnings that prevent the same mistakes next session. ## Quick Start [#quick-start] ```bash # See recommendations for current project (dry-run, no changes) headroom learn # Write recommendations to CLAUDE.local.md and MEMORY.md headroom learn --apply # Analyze a specific project headroom learn --project ~/my-project --apply # Analyze all projects headroom learn --all --apply # Write to the team-shared CLAUDE.md instead of CLAUDE.local.md headroom learn --apply --target CLAUDE.md ``` ## Success Correlation [#success-correlation] The core innovation. Instead of cataloging failures ("Read failed 5 times"), Headroom finds what the model did to **fix** each failure: - **Failed**: `Read axion-formats/src/main/java/.../FirstClassEntity.java` - **Then succeeded**: `Read axion-scala-common/src/main/scala/.../FirstClassEntity.scala` - **Learning**: "`FirstClassEntity` is at `axion-scala-common/`, not `axion-formats/`" This produces specific, actionable corrections -- not generic advice. ## What It Learns [#what-it-learns] ### Environment Facts [#environment-facts] Which runtime commands work vs fail. ```markdown ### Environment - **Python**: use `uv run python` (not `python3` -- modules not available outside venv) ``` ### File Path Corrections [#file-path-corrections] Wrong paths the model keeps guessing, with the correct locations. ```markdown ### File Path Corrections - `axion-common/src/.../AxionSparkConstants.scala` -> actually at `axion-spark-common/src/.../AxionSparkConstants.scala` ``` ### Search Scope [#search-scope] Which directories to search in (narrow paths fail, broader ones work). ```markdown ### Search Scope - Don't search `axion-model/` -> use `axion/` (the repo root) ``` ### Command Patterns [#command-patterns] How commands should (and should not) be run. ```markdown ### Command Patterns - **user_prefers_manual**: User rejected gradle 18 times -- show the command, don't execute - **python_runtime**: Use `uv run python` not `python3` (ModuleNotFoundError) ``` ### Known Large Files [#known-large-files] Files that need `offset`/`limit` with Read. ```markdown ### Known Large Files - `proxy/server.py` (~8000 lines) -- always use offset/limit ``` ## Where Learnings Go [#where-learnings-go] | Pattern | Destination | Why | |---------|-------------|-----| | Environment, paths, search scope, commands, large files | **CLAUDE.local.md** | Personal facts (machine-specific paths), gitignored by default | | Missing paths, retry patterns, permissions | **MEMORY.md** | May change, agent-specific | `CLAUDE.local.md` lives in your project directory. It is the **personal** local memory file in Claude Code's [memory convention](https://docs.claude.com/en/docs/claude-code/memory) — meant to be gitignored — so machine-specific learnings (absolute paths, tool-discovery byproducts) don't pollute the team-shared `CLAUDE.md`. Make sure `CLAUDE.local.md` is listed in your `.gitignore`. Pass `--target CLAUDE.md` to opt into the shared file instead, or `--target ` for any custom location. MEMORY.md lives in `~/.claude/projects/*/memory/`. If an older Headroom version already wrote a learned-patterns block into your team-shared `CLAUDE.md`, the next `headroom learn --apply` moves it into `CLAUDE.local.md` and prints a warning so you can review the diff before committing. If `CLAUDE.md` contained nothing but the Headroom block, it is removed entirely. ## Marker-Based Updates [#marker-based-updates] Headroom manages a clearly-delimited section in each file: ```markdown ## Headroom Learned Patterns *Auto-generated by `headroom learn` -- do not edit manually* ... ``` On re-run, only the content between markers is replaced. Your existing file content is preserved. ## Architecture [#architecture] The system is built with an adapter pattern so it can support multiple agent systems: - **Scanners** read tool-specific log formats (e.g., `~/.claude/projects/*.jsonl`) and produce normalized `ToolCall` sequences - **Analyzers** work on `ToolCall` data -- same analysis logic for any agent system - **Writers** output to tool-specific context injection mechanisms (e.g., CLAUDE.md) To add support for a new agent (e.g., Cursor), you write a Scanner that reads its log format and a Writer that outputs to `.cursorrules`. The analyzers stay the same. ## CLI Reference [#cli-reference] ```bash headroom learn [OPTIONS] Options: --project PATH Project directory to analyze (default: current directory) --all Analyze all discovered projects --apply Write recommendations (default: dry-run) --target TEXT Context file to write to, Claude Code only (default: CLAUDE.local.md). Relative to project root, or absolute. --agent TEXT Agent to analyze (e.g. claude, codex, gemini, opencode) --model TEXT LLM model to use for analysis --workers INT Number of parallel workers ``` ## Real-World Results [#real-world-results] A prior version of this page cited specific counts (tool calls analyzed, failure rate, corrections extracted) for a real run. We could not trace those numbers to a committed benchmark, test, or dataset in this repo, and they appear to have been copied from `wiki/learn.md` rather than measured independently -- so we removed them rather than publish an unverifiable figure. Run `headroom learn` (dry-run by default) on your own session history to see real counts for your project. Headroom's `compress()` is a function over your message list, so in FastAPI it is one call before your model call. This page builds a `POST /chat` endpoint that returns the compressed messages and token counts, then shows the one-line change that forwards them to a provider. ## Install [#install] ```bash pip install fastapi "uvicorn[standard]" headroom-ai ``` No provider key, no model download. The base package is enough for `compress()`; `[ml]` adds the Kompress text model later if you want more savings on prose-heavy tool output. ## Endpoint [#endpoint] ```python # main.py from typing import Any from fastapi import FastAPI from pydantic import BaseModel from headroom import compress app = FastAPI() class ChatIn(BaseModel): messages: list[dict[str, Any]] # OpenAI or Anthropic message dicts model: str = "gpt-4o" # used for token counting and the context limit @app.post("/chat") def chat(body: ChatIn) -> dict[str, Any]: result = compress(body.messages, model=body.model) return { "messages": result.messages, "tokens_before": result.tokens_before, "tokens_after": result.tokens_after, "tokens_saved": result.tokens_saved, "transforms": result.transforms_applied, } ``` Save it as `main.py` and run it with no network at all: ```bash HEADROOM_OFFLINE=1 uvicorn main:app --port 8000 ``` Then post a 3,000-line tool output with one error in it: ```bash python - <<'EOF_PY' import json, urllib.request log = "\n".join([f"INFO step {i} ok" for i in range(3000)] + ["FATAL disk full"]) msgs = [ {"role": "user", "content": "What failed?"}, {"role": "assistant", "content": None, "tool_calls": [{"id": "c1", "type": "function", "function": {"name": "logs", "arguments": "{}"}}]}, {"role": "tool", "tool_call_id": "c1", "content": log}, ] req = urllib.request.Request("http://127.0.0.1:8000/chat", data=json.dumps({"messages": msgs}).encode(), headers={"content-type": "application/json"}) out = json.load(urllib.request.urlopen(req)) print(out["tokens_before"], "->", out["tokens_after"], "FATAL kept:", "FATAL" in json.dumps(out["messages"])) EOF_PY ``` Expected output is on the order of `20000 -> 95 FATAL kept: True`: the repeated `INFO` lines collapse, the `FATAL` line survives. ## Forward to the provider [#forward-to-the-provider] Replace `messages=body.messages` with `messages=result.messages` in your existing call. Nothing else changes: ```python from openai import OpenAI client = OpenAI() @app.post("/answer") def answer(body: ChatIn) -> dict[str, Any]: result = compress(body.messages, model=body.model) completion = client.chat.completions.create(model=body.model, messages=result.messages) return { "answer": completion.choices[0].message.content, "tokens_saved": result.tokens_saved, } ``` For Anthropic, pass `result.messages` to `client.messages.create` the same way. ## What it keeps [#what-it-keeps] Error lines, stack traces, first and last items, and statistical outliers in JSON arrays are kept. Repeated and low-information lines are dropped. Originals are cached locally; the model can ask for them back when it has the retrieval tool, which the [proxy](/docs/proxy) and the [MCP server](/docs/mcp) provide ([CCR](/docs/ccr)). The plain `compress()` call above does not expose it. Compression applies to tool and assistant messages; user messages are left alone unless you pass `compress_user_messages=True`. Short prompts and prose compress very little; see [Limitations](/docs/limitations). ## Not this page [#not-this-page] `headroom.integrations.asgi.CompressionMiddleware` is for FastAPI apps that *serve* an LLM-shaped endpoint (`/v1/messages`, `/v1/chat/completions`), such as a proxy you built. For an app that *calls* a provider, use `compress()` as above. ## Settings you will reach for [#settings-you-will-reach-for] - `HEADROOM_OFFLINE=1`: no update check, no model or pricing-table downloads, no beacon. Use it in CI and sandboxes. See [Security model](/docs/security-model). - `compress(..., model_limit=N)`: tell Headroom the model's window is N tokens, so it compresses harder as the prompt approaches it. It never truncates history. - `compress(..., protect_recent=K)`: leave source code in the last K messages uncompressed. - [Configuration](/docs/configuration) has the rest. Headroom writes configuration, runtime state, logs, and caches to a small set of well-known paths under the user's home directory. This page is the source of truth for where those paths live, how to override them, and how they behave inside Docker containers. ## Two-root model [#two-root-model] | Variable | Default | Purpose | Typical access | |---|---|---|---| | `HEADROOM_CONFIG_DIR` | `~/.headroom/config` | User/admin-authored configuration (model catalogs, plugin settings, etc.) | Read-mostly | | `HEADROOM_WORKSPACE_DIR` | `~/.headroom` | Runtime state written by the proxy and CLI (savings, logs, memory DB, telemetry, caches) | Read-write | Both variables are recognized by the Python proxy / CLI and the npm SDK. They are **additive** — every pre-existing per-resource env var (`HEADROOM_SAVINGS_PATH`, `HEADROOM_TOIN_PATH`, `HEADROOM_SUBSCRIPTION_STATE_PATH`, `HEADROOM_MODEL_LIMITS`, ...) continues to work with identical semantics. ## Precedence [#precedence] For every per-resource helper, resolution follows this order: ``` explicit argument │ falls through when None/"" ▼ per-resource env var (e.g. HEADROOM_SAVINGS_PATH) │ falls through when unset/blank ▼ derived from canonical root │ e.g. ${HEADROOM_WORKSPACE_DIR}/proxy_savings.json ▼ default (e.g. ~/.headroom/proxy_savings.json) ``` Examples: - `HEADROOM_WORKSPACE_DIR=/mnt/state` → savings land at `/mnt/state/proxy_savings.json` unless `HEADROOM_SAVINGS_PATH` overrides. - `HEADROOM_SAVINGS_PATH=/custom/savings.json` always wins, even when `HEADROOM_WORKSPACE_DIR` is set. - Unset both and the default is `~/.headroom/proxy_savings.json`. ## Bucket assignments [#bucket-assignments] ### Workspace bucket (`HEADROOM_WORKSPACE_DIR`) [#workspace-bucket-headroom_workspace_dir] | Resource | Default path | Legacy env var | |---|---|---| | Proxy savings ledger | `${WORKSPACE_DIR}/proxy_savings.json` | `HEADROOM_SAVINGS_PATH` | | TOIN telemetry JSON | `${WORKSPACE_DIR}/toin.json` | `HEADROOM_TOIN_PATH` | | Subscription tracker state | `${WORKSPACE_DIR}/subscription_state.json` | `HEADROOM_SUBSCRIPTION_STATE_PATH` | | Memory SQLite | `${WORKSPACE_DIR}/memory.db` | CLI `--memory-db-path` | | Native memory directory | `${WORKSPACE_DIR}/memories/` | `MemoryConfig.native_memory_dir` | | License cache | `${WORKSPACE_DIR}/license_cache.json` | — | | Session stats JSONL | `${WORKSPACE_DIR}/session_stats.jsonl` | — | | Memory sync state | `${WORKSPACE_DIR}/sync_state.json` | — | | Memory bridge state | `${WORKSPACE_DIR}/bridge_state.json` | — | | Proxy log directory | `${WORKSPACE_DIR}/logs/` | — | | HTTP 400 debug dumps | `${WORKSPACE_DIR}/logs/debug_400/` | — | | Deployment profiles | `${WORKSPACE_DIR}/deploy/` | — | | Beacon lock file | `${WORKSPACE_DIR}/.beacon_lock_` | — | ### Config bucket (`HEADROOM_CONFIG_DIR`) [#config-bucket-headroom_config_dir] | Resource | Default path | Legacy env var | |---|---|---| | Models catalog | `${CONFIG_DIR}/models.json` | `HEADROOM_MODEL_LIMITS` (content override) | | Plugin settings | `${CONFIG_DIR}/plugins//...` | — | ### Backward compatibility — models.json [#backward-compatibility--modelsjson] `models.json` historically lived at `~/.headroom/models.json` (i.e. in the workspace root, not in `config/`). For a seamless migration the Python providers check **both** locations in this order: 1. `${HEADROOM_CONFIG_DIR}/models.json` (new canonical location) 2. `${HEADROOM_WORKSPACE_DIR}/models.json` (legacy fallback) Existing installs continue to work unchanged. New installs are encouraged to put `models.json` in the config bucket. ## Plugin authors [#plugin-authors] Two helpers give plugins isolated, per-plugin directories under both roots: ### Python [#python] ```python from headroom import paths cfg_dir = paths.plugin_config_dir("my-plugin") # → ~/.headroom/config/plugins/my-plugin state_dir = paths.plugin_workspace_dir("my-plugin") # → ~/.headroom/plugins/my-plugin cfg_dir.mkdir(parents=True, exist_ok=True) (cfg_dir / "settings.json").write_text("{}") ``` ### npm SDK [#npm-sdk] ```typescript import { pluginConfigDir, pluginWorkspaceDir } from "@headroom/sdk"; const cfgDir = pluginConfigDir("my-plugin"); const stateDir = pluginWorkspaceDir("my-plugin"); ``` Plugin-author helpers reject names containing `/` or `\` to keep the namespace flat. ## Docker naming overlap: `HEADROOM_WORKSPACE` vs `HEADROOM_WORKSPACE_DIR` [#docker-naming-overlap-headroom_workspace-vs-headroom_workspace_dir] These are **two different variables** with different semantics, both retained for backward compatibility: | Variable | Scope | Meaning | |---|---|---| | `HEADROOM_WORKSPACE` | Host-side (Docker) | Directory to bind-mount into the container as `/workspace` (equivalent to CWD in native runs). Used by `docker-compose.native.yml`. | | `HEADROOM_WORKSPACE_DIR` | Inside the container | Canonical Headroom state root. Resolves to `/tmp/headroom-home/.headroom` inside the official container image, which in turn bind-mounts to `${HOME}/.headroom` on the host. | The official Docker bootstrap (compose file, `scripts/install.sh`, and the Python `install` command) sets `HEADROOM_WORKSPACE_DIR` and `HEADROOM_CONFIG_DIR` inside the container so the proxy resolves state to the bind-mounted path without any user action. ## Project-scoped `.headroom/` directories [#project-scoped-headroom-directories] A few code paths deliberately use **project-local** `.headroom/` paths resolved relative to the current working directory rather than the canonical workspace root: - `headroom/proxy/server.py` — project-scoped memory DB default - `headroom/memory/mcp_server.py` — project-scoped memory DB default - `headroom/cli/wrap.py` — project-scoped memory and hook artifacts These **do not obey** `HEADROOM_WORKSPACE_DIR`. This is intentional: it preserves the "project memory lives in the project directory" invariant. Users who want a single centrally located memory store can pass `--memory-db-path ` explicitly or set the path via the plugin API. ## Legacy per-resource env vars [#legacy-per-resource-env-vars] Every legacy env var continues to work with its original semantics (raw string in, raw string out — no tilde expansion, no path-separator normalization), ensuring byte-for-byte backward compatibility. Full list: - `HEADROOM_SAVINGS_PATH` - `HEADROOM_TOIN_PATH` - `HEADROOM_SUBSCRIPTION_STATE_PATH` - `HEADROOM_MODEL_LIMITS` (content override — JSON string or file path) ## See also [#see-also] Use `headroom wrap grok-build` to route Grok Build LLM traffic through the local Headroom proxy. The wrapper starts or reuses the proxy, injects a reversible `[model.grok-build]` override into `~/.grok/config.toml` (or `$GROK_HOME/config.toml`), and prints next steps for launching `grok`. ## Quick Start [#quick-start] ```bash headroom wrap grok-build ``` In another terminal, from the same project directory: ```bash grok ``` When you are done: ```bash headroom unwrap grok-build ``` ## What `wrap grok-build` Does [#what-wrap-grok-build-does] | Step | What happens | |---|---| | Proxy | Starts the Headroom proxy unless `--no-proxy` is set | | Model config | Writes or updates `[model.grok-build] base_url` in Grok's `config.toml`, pointing at `http://127.0.0.1:/v1` (with optional `/p/` prefix for savings attribution) | | Existing config | If you already have a `[model.grok-build]` table, Headroom rewrites `base_url` in place instead of appending a duplicate table (invalid TOML) | | MCP install | `headroom mcp install` can register Headroom MCP via `GrokRegistrar` | | Backup | Snapshots `config.toml` to `config.toml.headroom-backup` before the first injection | ## Options [#options] ```bash headroom wrap grok-build \ --port 8787 \ # Proxy port (default: 8787) --no-proxy \ # Use an existing proxy instead of starting one --learn \ # Enable live traffic learning --memory # Enable persistent memory ``` ## Environment Variables [#environment-variables] | Variable | Description | |---|---| | `GROK_HOME` | Override Grok config directory (default: `~/.grok`) | | `XAI_API_KEY` | Grok API key (also accepts `GROK_CODE_XAI_API_KEY`) | ## Persistent Install [#persistent-install] `grok_build` is an install target for `headroom install apply --providers manual --target grok_build`. The install manifest records proxy env values for Grok Build alongside other wrapped agents. ## Unwrap [#unwrap] `headroom unwrap grok-build` restores the pre-wrap `config.toml` from backup when available, or strips Headroom marker blocks and in-place `base_url` rewrites when no backup exists. Hermes Agent registers its own tools, so unlike Claude Code it does not automatically receive the `headroom_retrieve` MCP tool. The bundled [`plugins/hermes`](https://github.com/headroomlabs-ai/headroom/tree/main/plugins/hermes) plugin closes that loop: it gives Hermes a native `headroom_retrieve` tool that calls the proxy's `POST /v1/retrieve` endpoint directly, so compression markers like `<>` are no longer a black box. Without it, the model tends to re-run the original command (wasting tokens and time) or treat `ccr:abc123` as a file path and try to `cat` it. ## Install [#install] 1. Copy the plugin into Hermes's user plugin directory: ```bash mkdir -p ~/.hermes/plugins cp -r plugins/hermes/headroom_retrieve ~/.hermes/plugins/ ``` 2. Enable it in `~/.hermes/config.yaml`: ```yaml toolsets: - hermes-cli - web - headroom # add this plugins: enabled: - headroom_retrieve ``` > Once the `plugins.enabled` key exists it acts as an explicit allowlist — list any other user plugins you already rely on. 3. Restart the Hermes gateway / TUI (plugin discovery is cached per process). ## Proxy configuration [#proxy-configuration] No configuration is required to prevent the retrieval loop on current headroom releases: `headroom_retrieve` is a protected built-in — it ships in `DEFAULT_EXCLUDE_TOOLS` and `DEFAULT_VERBATIM_EXCLUDE_TOOLS` (`headroom/config.py`), and the content router has dedicated guards that never recompress retrieved originals (issue #1077). Exclusion matching also unwraps Hermes's deferred `tool_call` bridge (`tool_search` / `tool_call` indirection), so the real tool name is matched even when Hermes invokes the plugin through the bridge. The one Hermes-specific exclusion worth considering is `read_file`: Hermes's tool names don't match Claude Code's `Read` / `Grep` / `Edit` that the built-in defaults protect, and file reads are reference data the agent needs verbatim: ```bash HEADROOM_EXCLUDE_TOOLS=read_file ``` Configured exclusions merge with (rather than replace) the built-in defaults, so this leaves every built-in protection — including the `headroom_retrieve` guards — intact. ## Behavior [#behavior] - Accepts the bare hash or the whole marker — `<>`, `ccr:abc123`, and `hash=abc123` all normalize to `abc123`. - Retrieval is by hash and always returns the full original content. - Expired-hash (TTL) and proxy-unreachable errors tell the model to re-run the original command instead of retrying blindly. ## Requirements [#requirements] - headroom proxy running on `127.0.0.1:8787` (edit `_PROXY_URL` in the plugin's `__init__.py` otherwise) - `httpx` (already a Hermes dependency) Tested against headroom 0.22.4 and 0.23.0 with Hermes Agent on macOS and Linux. Headroom automatically detects what kind of content you're sending and routes it to the right compressor. You don't need to configure anything -- just call `compress()` and the pipeline handles the rest. ## The Pipeline [#the-pipeline] Every request flows through a short pipeline: ``` ┌──────────────┐ ┌────────────────┐ │ CacheAligner │────>│ ContentRouter │ │ (off by │ │ │ │ default) │ │ Detect type & │ │ report drift │ │ route to best │ │ for cache │ │ compressor │ └──────────────┘ └────────────────┘ ``` 1. **CacheAligner** *(detector-only, off by default)* reports dynamic-prefix drift (dates, session context) so callers can keep the static prefix cacheable. It never rewrites your messages. 2. **ContentRouter** inspects each content block and routes it to one compressor -- SmartCrusher for JSON arrays, CodeAwareCompressor for source code, LogCompressor for build output, and so on. This is where essentially all compression happens. ## Content Type Detection [#content-type-detection] The router auto-detects content type by analyzing structure and patterns. No manual hints required. | Content Type | Detection Signal | Compressor | Typical Savings | |---|---|---|---| | JSON arrays | Valid JSON with array elements | SmartCrusher | Varies with redundancy -- measured 26-54% on realistic (non-duplicate) tool-output arrays of 100-10,000 items; highly repetitive arrays compress far more. See `benchmarks/compression_benchmark.py`. | | Source code | Syntax patterns, indentation, keywords | Kompress via the library `compress()`/`HeadroomClient` path (`enable_code_aware` defaults to `False` there); CodeAwareCompressor via `headroom proxy` (`HEADROOM_CODE_AWARE_ENABLED` defaults to on) | workload-dependent | | Search results | `file:line:content` format | SearchCompressor | 80-95% (measured ~91% on a synthetic 40-file ripgrep-style corpus) | | Build/test logs | Timestamps, log levels, pytest/npm markers | LogCompressor | 85-95% (measured ~93% on a synthetic 1,000-line log with clustered errors) | | Diffs | Unified diff format | DiffCompressor | 60-80% (measured ~70% on a multi-file diff with default `max_context_lines`/`max_hunks_per_file` limits) | | HTML | Tag structure | HTMLExtractor | 70-90% (module docstring; measured ~81% on a synthetic nav/ads/script-heavy page) | | Tabular (CSV/TSV/markdown tables) | Delimited rows / table syntax | TabularCompressor | Varies with redundancy -- TabularCompressor bridges to SmartCrusher, so it inherits the same range (measured 26-28% on non-duplicate rows) | | Human-readable Git status | Branch/detached-HEAD header plus status sections and entries | Lossless passthrough (Python and Rust) | None: staged, unstaged, and untracked paths must remain intact | | Structured config (YAML/TOML/INI) | Config syntax | ConfigCompressor | 40-70% (measured ~50% on a commented/blank-line-heavy YAML file) | | Plain text | Text with no stronger signal | Kompress | workload-dependent | | Anything else | Fallback chain, then passthrough if no safe saving is found | Kompress / passthrough | varies | Human-readable Git status is structured output, not prose: it is preserved byte-for-byte, including section labels and paths containing spaces. The router checks its structure before model classification and before selected external or lossy compressors, so CSV and wildcard plugins cannot trim status entries. This protection does not exclude all `Bash` results; ordinary prose and build logs still follow their normal routes. The recognizer covers the standard English status format (`LC_ALL=C`); localized formats are not covered. This protection also takes precedence over mixed-content splitting when a recognized status is followed by fenced code or JSON. The combined tool result is preserved rather than splitting status entries into prose sections. ## Quick Start [#quick-start] ```ts twoslash import { compress } from "headroom-ai"; const messages = [ { role: "system" as const, content: "You are a helpful assistant." }, { role: "user" as const, content: "Summarize this data" }, { role: "tool" as const, content: '{"results": [...]}', tool_call_id: "call_1" }, ]; const result = await compress(messages); console.log(`Tokens saved: ${result.tokensSaved}`); console.log(`Compression ratio: ${result.compressionRatio}`); ``` ```python from headroom import compress messages = [ {"role": "user", "content": "Summarize the tool output."}, {"role": "tool", "tool_call_id": "call_1", "content": large_output}, ] result = compress(messages, model="gpt-4o") print(result.messages) print(f"Saved {result.tokens_saved} tokens ({result.compression_ratio:.0%})") ``` ## Configuring the Compressor [#configuring-the-compressor] ```ts twoslash import { compress } from "headroom-ai"; const result = await compress(messages, { model: "gpt-4o", tokenBudget: 50000, }); console.log(`Before: ${result.tokensBefore} tokens`); console.log(`After: ${result.tokensAfter} tokens`); console.log(`Transforms: ${result.transformsApplied.join(", ")}`); ``` ```python from headroom import CompressConfig, compress config = CompressConfig( compress_user_messages=True, protect_recent=0, target_ratio=0.5, # Kompress keep ratio; structural compressors use their own rules min_tokens_to_compress=250, ) result = compress(messages, model="gpt-4o", config=config) print(f"Saved: {result.tokens_saved} tokens") print(result.transforms_applied) ``` ## Structure Preservation [#structure-preservation] Headroom doesn't blindly truncate. It identifies what matters in each content type and preserves it: | Content Type | What's Preserved | What's Compressed | |---|---|---| | **JSON** | Keys, brackets, booleans, nulls, short values, UUIDs | Long string values, whitespace | | **Code** | Imports, function signatures, class definitions, types | Function bodies, comments | | **Logs** | Timestamps, log levels, error messages, stack traces | Repeated patterns, verbose details | | **Text** | High-entropy tokens (IDs, hashes), headers | Low-information content | ## Real Compression Ratios [#real-compression-ratios] Compression ratio and latency both depend heavily on the shape of the input (how redundant the data is, how large the payload is, and -- for Kompress -- what `target_ratio` is configured). There's no single number that holds across workloads, so treat the ranges in the table above as directional and reproduce them on your own traffic with the harnesses in [`benchmarks/`](https://github.com/headroomlabs-ai/headroom/tree/main/benchmarks) (for example `compression_benchmark.py`, `bench_transforms.py`, `text_crusher_quality_eval.py`). What stays constant across content types is *what's preserved*: | Content Type | What's Preserved | |---|---| | JSON (large arrays) | All keys, structure | | Source code (Python) | Signatures, imports | | Search results | Relevant matches | | Build logs | Errors, stack traces | | Plain text | High-entropy tokens | ## Compressing multiple documents [#compressing-multiple-documents] The public `compress()` API accepts an LLM message list. Put documents in separate messages or call it once per independent request; this preserves the same message shape you will send to the provider: ```python from headroom import compress messages = [ {"role": "user", "content": "Compare these documents."}, {"role": "tool", "tool_call_id": "doc_1", "content": first_document}, {"role": "tool", "tool_call_id": "doc_2", "content": second_document}, ] result = compress(messages, model="gpt-4o") ``` ## What Happens Under the Hood [#what-happens-under-the-hood] When you call `compress()`, here is the full sequence: 1. **Safety gates** -- protected tools, recent turns, frozen cache prefixes, and minimum-size rules decide what may change. 2. **Content detection** -- Magika when available plus deterministic pattern matching identifies the content type. 3. **Structural compression** -- format-specific handlers compact JSON, logs, search results, tables, configuration, HTML, and other recognized structures. 4. **Fallback compression** -- Kompress handles eligible text when its runtime is available; a no-saving or failed transform passes the original through. 5. **CCR storage in proxy flows** -- when CCR is enabled, originals for marker-bearing compression are retained for later retrieval. `--lossless` and `--no-ccr` intentionally use different contracts; see [Reversible Compression](/docs/ccr). The pipeline works out of the box with no configuration. All detection, routing, and compression happens automatically. Configuration is available when you need fine-grained control. Vision models charge by the token, and images are expensive. A single 1024x1024 image costs ~765 tokens on OpenAI. Headroom's image compression uses a trained ML router to analyze your query and automatically select the optimal compression technique, saving 40-90% of image tokens. ## How It Works [#how-it-works] ``` User uploads image + asks question | [Query Analysis] TrainedRouter (MiniLM from HuggingFace) Classifies: "What animal is this?" -> full_low | [Image Analysis] SigLIP analyzes image properties (has text? complex? fine details?) | [Apply Compression] OpenAI: detail="low" Anthropic: Resize to 512px Google: Resize to 768px | Compressed request to LLM ``` The router is a fine-tuned MiniLM classifier (`chopratejas/technique-router` on HuggingFace) with 93.7% accuracy across 1,157 training examples. ## Compression Techniques [#compression-techniques] | Technique | Savings | When Used | Example Query | |---|---|---|---| | `full_low` | ~87% | General understanding | "What is this?", "Describe the scene" | | `preserve` | 0% | Fine details needed | "Count the whiskers", "Read the serial number" | | `crop` | 50-90% | Region-specific queries | "What's in the corner?", "Focus on the background" | | `transcode` | ~99% | Text extraction | "Read the sign", "Transcribe the document" | ## Quick Start [#quick-start] ### With Headroom Proxy (Zero Code Changes) [#with-headroom-proxy-zero-code-changes] ```bash # Start the proxy headroom proxy --port 8787 # Connect your client -- images are compressed automatically ANTHROPIC_BASE_URL=http://localhost:8787 claude ``` ### With HeadroomClient [#with-headroomclient] ```python from headroom import HeadroomClient, OpenAIProvider from openai import OpenAI client = HeadroomClient( original_client=OpenAI(), provider=OpenAIProvider(), default_mode="optimize", ) response = client.chat.completions.create( model="gpt-4o", messages=[{ "role": "user", "content": [ {"type": "text", "text": "What animal is this?"}, {"type": "image_url", "image_url": {"url": "data:image/jpeg;base64,..."}} ] }] ) # Image automatically compressed with detail="low" (87% savings) ``` ### Direct API [#direct-api] ```python from headroom.image import ImageCompressor compressor = ImageCompressor() # Compress images in messages compressed_messages = compressor.compress(messages, provider="openai") # Check savings print(f"Saved {compressor.last_savings:.0f}% tokens") print(f"Technique: {compressor.last_result.technique.value}") ``` ## Provider Support [#provider-support] The compressor adapts its strategy per provider: | Provider | Compression Method | Details | |---|---|---| | **OpenAI** | Sets `detail="low"` | Native detail parameter | | **Anthropic** | Resizes to 512px | PIL-based resize | | **Google Gemini** | Resizes to 768px | Optimized for Gemini's 768x768 tile system | ### Token Savings by Provider [#token-savings-by-provider] **OpenAI** (1024x1024 image): | Technique | Before | After | Savings | |---|---|---|---| | `full_low` | 765 tokens | 85 tokens | 89% | | `preserve` | 765 tokens | 765 tokens | 0% | **Anthropic** (1024x1024 image): | Before | After | Savings | |---|---|---| | ~1,398 tokens | ~349 tokens | 75% | **Google Gemini** (1536x1536 image): | Before | After | Savings | |---|---|---| | 1,032 tokens (4 tiles) | 258 tokens (1 tile) | 75% | Unlike the OpenAI and Anthropic figures above (computed from the formulas in `headroom/image/tile_optimizer.py` and verified to match this page's numbers exactly), Headroom has no Gemini token-counting formula in the codebase -- these figures describe Google's publicly documented tile pricing, not something this repo computes or tests. ## Configuration [#configuration] ```python from headroom.image import ImageCompressor compressor = ImageCompressor( model_id="chopratejas/technique-router", # HuggingFace model use_siglip=True, # Enable image analysis device="cuda", # Use GPU if available (auto, cuda, cpu, mps) ) ``` ### Proxy Configuration [#proxy-configuration] ```bash # Image optimization is part of the ContentRouter path when the optional image # dependencies are installed. There are no public proxy CLI image toggles in # the current release. headroom proxy ``` ## Performance [#performance] | Metric | Value | |---|---| | Image resize | ~5-20ms (measured ~9ms median for a 1024px -> 512px PIL/LANCZOS resize) | | First request | Model download, cached after (duration depends on network) | | Router accuracy | 93.7% (fine-tuned MiniLM classifier, 1,157 training examples -- see `headroom/image/trained_router.py`) | | Model size | ~100MB (PyTorch MiniLM router) or ~32MB (ONNX INT8 router, preferred in production) | | GPU memory (SigLIP) | ~400MB | When using the Headroom proxy, image compression happens automatically on every request that contains images. No code changes needed. Headroom compresses everything your AI agent reads -- tool outputs, database results, file reads, RAG retrievals, API responses -- before it reaches the LLM. The model sees less noise, responds faster, and costs less. ## Quick preview [#quick-preview] ```ts twoslash import { compress } from 'headroom-ai'; const messages = [ { role: 'user' as const, content: 'Analyze these results' }, ]; const result = await compress(messages, { model: 'gpt-4o' }); // compressionRatio is tokensAfter / tokensBefore, so savings is 1 - ratio. console.log(`Saved ${result.tokensSaved} tokens (${((1 - result.compressionRatio) * 100).toFixed(0)}%)`); ``` ```python from headroom import compress from openai import OpenAI messages = [{"role": "user", "content": "Analyze these results"}] result = compress(messages, model="gpt-4o") client = OpenAI() response = client.chat.completions.create( model="gpt-4o", messages=result.messages, ) print(f"Saved {result.tokens_saved} tokens ({result.compression_ratio:.0%})") ``` ## What gets compressed [#what-gets-compressed] | Content type | What happens | Typical savings | |---|---|---| | JSON arrays (tool outputs) | Statistical analysis keeps errors, anomalies, boundaries | varies with array size and redundancy | | Source code | AST-aware compression preserves signatures, collapses bodies | opt-in; disabled by default | | Build/test logs | Keeps failures and errors, drops passing noise | varies with log verbosity | | Search results | Ranks by relevance, keeps top matches | varies with result set size | | Plain text | ModernBERT token classification removes redundancy | varies with redundancy | | Git diffs | Preserves change hunks, drops unchanged context | varies with diff size | | Images | ML router selects optimal resize/quality tradeoff | varies with image content | Savings depend heavily on how repetitive the content is -- see [Benchmarks](/docs/benchmarks) and the reproducible scenario table below for measured numbers on real workloads. ## Where Headroom fits [#where-headroom-fits] ``` Your Agent / App | | tool outputs, logs, DB reads, RAG results, file reads, API responses v Headroom <-- proxy, Python library, TS SDK, or framework integration | v LLM Provider (OpenAI, Anthropic, Google, Bedrock, 100+ via LiteLLM) ``` Headroom works as a **transparent proxy** (zero code changes), a **Python function** (`compress()`), a **TypeScript function** (`compress()`), or a **framework integration** (LangChain, Agno, Strands, LiteLLM, Vercel AI SDK, MCP). ## Real-world results [#real-world-results] Headroom is built for the scenario that matters most in agent workflows: a single critical error or fact buried inside a large, mostly-routine tool output. SmartCrusher preserves error items, anomalies (values outside normal statistical range), and first/last boundaries by construction -- not by keyword matching, but by statistical analysis of field variance -- so the signal survives even when the surrounding noise is compressed away aggressively. Measured on local, seeded test data built from real MCP server output formats, run through the default `compress()` configuration (`protect_recent=4`). These are not production telemetry, and they are reproducible: | Scenario | Before | After | Savings | |---|---|---|---| | Code search (100 results) | 17,199 | 13,597 | **21%** | | SRE incident debugging | 55,957 | 24,340 | **57%** | | Codebase exploration | 58,801 | 33,895 | **42%** | | GitHub issue triage | 46,067 | 32,429 | **30%** | ```bash uv run python benchmarks/index_proof_table.py --seed 20260902 ``` Token counts are from the `gpt-5.6` tokenizer. The corpus is generated, so the exact before/after counts move slightly with the seed; the savings hold across seeds (Code search 21%, SRE 56--57%, triage 29--30%, exploration 41--50% over five seeds). Savings depend on how repetitive your tool output is, so treat these as a shape, not a promise. See [Benchmarks](/docs/benchmarks) for the wider suite. ## Key Features [#key-features] ## Framework Integrations [#framework-integrations] ## Nothing is lost [#nothing-is-lost] Compressed content goes into the CCR store (Compress-Cache-Retrieve). The LLM gets a `headroom_retrieve` tool and can fetch full originals when it needs more detail. Compression is aggressive but reversible. ## Next steps [#next-steps] --- ## Install options [#install-options] **uv tool** - you want the `headroom` CLI (`deploy`, `proxy`, `wrap`, `doctor`, `mcp`, `learn`, and the other CLI commands) installed once on your machine in an isolated app environment. **pip** - you're writing Python, or you need the CLI, regardless of what language your app is in. **npm** - you're writing TypeScript/Node and want inline `compress()`, SDK wrapping (`withHeadroom`), or Vercel AI SDK middleware. ## Python [#python] Headroom requires **Python 3.10+** and is published as `headroom-ai` on PyPI. Release wheels are built for CPython **3.10 through 3.13** on Linux (manylinux_2_28 x86_64 / aarch64), macOS (Apple Silicon and Intel), and Windows x86_64. The Rust extension uses the CPython stable ABI, so those wheels are forward-compatible with newer supported CPython versions; optional dependencies may impose their own Python/platform limits. Platforms outside that matrix fall back to the source distribution and need a Rust/native toolchain. Use the distribution name `headroom-ai` for every install, including extras such as `"headroom-ai[langchain]"`. The Python import and CLI command remain `headroom`; the PyPI distribution named `headroom` is a different project. On Intel macOS, ONNX Runtime wheels are available through Python 3.13, using the pre-1.24 runtime. Python 3.14 and newer omit optional ONNX backends from `proxy`, `voice`, `relevance`, and `image` extras because compatible wheels are unavailable. Core compression and the proxy remain available; ML content detection, local ONNX Kompress, embedding relevance, and ONNX OCR are unavailable on that combination. Use Python 3.13 if you need those Python ONNX features. Rust ML detection requires ONNX Runtime 1.24 or newer and therefore remains disabled on Intel macOS even with the older Python runtime. ### CLI install with uv [#cli-install-with-uv] For a host-level `headroom` command on macOS Apple Silicon or Linux, prefer `uv tool install`. It keeps Headroom in a dedicated app environment instead of tying it to the current project or shell Python. ```bash uv tool install --python 3.13 "headroom-ai[all]" headroom --version ``` On macOS with Homebrew, `python3` may point at a newer interpreter than the current Headroom wheel set. Passing `--python 3.13` keeps installation on a wheel-supported interpreter. If Python 3.13 is missing, install it with Homebrew or let uv download a managed interpreter: ```bash brew install python@3.13 uv tool install --python 3.13 "headroom-ai[all]" ``` If `headroom` is installed but your shell cannot find it, add uv's tool directory to `PATH`: ```bash uv tool update-shell ``` For MCP clients such as Codex that do not inherit your interactive shell `PATH`, configure the absolute executable path returned by `command -v headroom`: ```toml [mcp_servers.headroom] command = "/Users/you/.local/bin/headroom" args = ["mcp", "serve"] ``` ### Core package [#core-package] ```bash pip install headroom-ai ``` The core package includes the `compress()` function, SmartCrusher, CacheAligner, and live-zone ContentRouter compression. No heavy dependencies. > **Note:** IntelligentContext / RollingWindow (score-based history dropping) were retired in PR-B1. Headroom compresses fresh tool output and new turns only — it does not drop conversation history. ### Extras [#extras] Install only what you need, or use `[all]` for the default CLI/proxy feature set: ```bash pip install "headroom-ai[all]" ``` | Extra | What it adds | Install command | |---|---|---| | `proxy` | Proxy server, HTTP API, MCP runtime, local ONNX Kompress path | `pip install "headroom-ai[proxy]"` | | `proxy-prod` | `proxy` plus gunicorn on Unix production hosts | `pip install "headroom-ai[proxy,proxy-prod]"` | | `ml` | PyTorch/Hugging Face Kompress backend and model tooling; opt-in, not in `[all]` | `pip install "headroom-ai[ml]"` | | `code` | CodeCompressor (tree-sitter AST parsing) | `pip install "headroom-ai[code]"` | | `memory` | Persistent memory (sqlite-vec, sentence-transformers); opt-in, not in `[all]` | `pip install "headroom-ai[memory]"` | | `vector` | Optional HNSW vector backend (hnswlib) — needs a C++ toolchain; **not in `[all]`** | `pip install "headroom-ai[vector]"` | | `memory-stack` | Optional Qdrant + Neo4j memory backend helpers; **not in `[all]`**. Headroom turns off Mem0's PostHog telemetry by default; set `MEM0_TELEMETRY=True` to allow it | `pip install "headroom-ai[memory-stack]"` | | `relevance` | fastembed-based relevance scoring (BAAI/bge-small-en-v1.5, ONNX) | `pip install "headroom-ai[relevance]"` | | `image` | Image compression (Pillow, ONNX runtime, OCR) | `pip install "headroom-ai[image]"` | | `reports` | HTML/Markdown report generation (Jinja2) | `pip install "headroom-ai[reports]"` | | `otel` | OpenTelemetry exporter (OTLP) | `pip install "headroom-ai[otel]"` | | `voice` | Voice/audio support; opt-in, not in `[all]` | `pip install "headroom-ai[voice]"` | | `voice-train` | Voice training dependencies; **not in `[all]`** | `pip install "headroom-ai[voice-train]"` | | `mcp` | MCP server tools (`headroom_compress`, `headroom_retrieve`, `headroom_stats`) | `pip install "headroom-ai[mcp]"` | | `langchain` | LangChain `HeadroomChatModel` wrapper; **not in `[all]`** | `pip install "headroom-ai[langchain]"` | | `agno` | Agno `HeadroomAgnoModel` wrapper; **not in `[all]`** | `pip install "headroom-ai[agno]"` | | `strands` | AWS Strands Agents integration; **not in `[all]`** | `pip install "headroom-ai[strands]"` | | *(none)* | CrewAI integration: no extra, install CrewAI alongside Headroom | `pip install headroom-ai crewai` | | `autogen` | AutoGen AgentChat integration; **not in `[all]`** | `pip install "headroom-ai[autogen]"` | | `anyllm` | any-llm multi-provider backend (Python 3.11+); **not in `[all]`** | `pip install "headroom-ai[anyllm]"` | | `bedrock` | Native AWS Bedrock credentials/backend support; **not in `[all]`** | `pip install "headroom-ai[bedrock]"` | | `redis` | Redis client for state shared across replicas (`HEADROOM_CCR_BACKEND=redis`, `HEADROOM_COMPRESS_SESSION_STORE`); **not in `[all]`**, included in every Docker image | `pip install "headroom-ai[redis]"` | | `evals` | Evaluation framework (GSM8K, SQuAD, BFCL benchmarks); opt-in, not in `[all]` | `pip install "headroom-ai[evals]"` | | `pytorch-mps` | Apple-GPU (MPS) memory-embedder offload — **macOS only**, not in `[all]` (torch + sentence-transformers); opt in with `HEADROOM_EMBEDDER_RUNTIME=pytorch_mps` | `pip install "headroom-ai[pytorch-mps]"` | | `html` | HTML main-content extraction with trafilatura | `pip install "headroom-ai[html]"` | | `spreadsheet` | `.xlsx` / `.xls` ingestion with openpyxl and xlrd | `pip install "headroom-ai[spreadsheet]"` | | `lean` | Torch-free proxy bundle for constrained environments (a dependency profile; it provides no isolation). `sandbox` is a deprecated alias | `pip install "headroom-ai[lean]"` | | `all` | CLI/proxy feature set without PyTorch-backed dependencies | `pip install "headroom-ai[all]"` | `[all]` is the built-in CLI/proxy feature set, not every integration or PyTorch-backed feature. The rows marked “not in `[all]`” must be requested explicitly. PyPI's Linux PyTorch wheels can include CUDA runtime packages. You can combine extras: ```bash pip install "headroom-ai[proxy,langchain,ml]" ``` ### Source builds and unsupported targets [#source-builds-and-unsupported-targets] Windows x86_64, Intel/Apple Silicon macOS, and Linux x86_64/aarch64 have release wheels. If pip selects the source distribution on another target (or because a matching wheel is unavailable), the build needs Rust plus the platform C/C++ toolchain. On Windows without MSVC on `PATH`, for example, you'll see: ```text error: linker `link.exe` not found note: please ensure that Visual Studio 2017 or later, or Build Tools for Visual Studio were installed with the Visual C++ option ``` To install the prerequisites: 1. **MSVC toolchain** — install **[Build Tools for Visual Studio](https://visualstudio.microsoft.com/visual-cpp-build-tools/)** and select the *"Desktop development with C++"* workload (this gives you `link.exe`). VS Code on its own is not enough. 2. **Rust** — install via [rustup](https://rustup.rs/). Choose the `stable-x86_64-pc-windows-msvc` toolchain so Cargo uses the MSVC linker you just installed. 3. **Open a fresh PowerShell** so the installer's PATH updates take effect, then run the install: ```powershell uv tool install --python 3.13 "headroom-ai[all]" # or pip install "headroom-ai[all]" ``` If you'd rather avoid a source build, use a supported interpreter/platform or run Headroom through Docker — see the [Docker](#docker) section below. ### pipx [#pipx] `pipx` creates one virtual environment per app. If that environment uses an unsupported Python version, `pipx` may resolve an older compatible Headroom release instead of the newest one. Use Python 3.13 explicitly. If you already use uv, prefer the [`uv tool`](#cli-install-with-uv) path above. ```bash pipx install --python python3.13 "headroom-ai[all]" ``` For a pinned release, replace `` with the version you require: ```bash pipx install --python python3.13 "headroom-ai[all]==" ``` Check which Python an existing `pipx` environment uses: ```bash pipx list ``` ### Verify the install [#verify-the-install] ```bash python -c "import headroom; print(headroom.__version__)" ``` ## TypeScript / Node.js [#typescript--nodejs] The TypeScript SDK is published as `headroom-ai` on npm. It requires **Node.js 18+**. It is a library you import — it does **not** install the `headroom` CLI (`headroom wrap`, `headroom proxy`, etc.), which ships only with the Python package above. ```bash npm install headroom-ai ``` Or with other package managers: ```bash pnpm add headroom-ai yarn add headroom-ai ``` The TypeScript SDK sends messages to the Headroom proxy over HTTP for compression. The proxy runs the full compression pipeline (Python). Start it before using the SDK: ```bash pip install "headroom-ai[proxy]" headroom proxy --port 8787 ``` Then point the SDK at it: ```ts import { compress } from 'headroom-ai'; const result = await compress(messages, { baseUrl: 'http://localhost:8787', }); ``` ### Verify the install [#verify-the-install-1] ```bash node -e "const h = require('headroom-ai'); console.log('headroom-ai loaded')" ``` ## Docker [#docker] Pre-built images are published to GitHub Container Registry on every release. ```bash docker pull ghcr.io/headroomlabs-ai/headroom:latest docker run --rm -p 127.0.0.1:8787:8787 \ ghcr.io/headroomlabs-ai/headroom:latest --host 0.0.0.0 ``` If you want a host `headroom` CLI that keeps Headroom itself inside a container — with mounted state, a one-line installer, and a persistent Docker lifecycle — see [Docker-Native Install](/docs/docker-install). ### Image tags [#image-tags] | Tag | Extras | Base image | Description | |---|---|---|---| | `latest` | `proxy,bedrock,redis` | Debian slim | Default image, runs as non-root | | `` | `proxy,bedrock,redis` | Debian slim | Pinned release, same default variant | | `nonroot` | `proxy,bedrock,redis` | Debian slim | Explicit non-root variant | | `code` | `proxy,code,bedrock,redis` | Debian slim | Includes tree-sitter for code compression | | `code-nonroot` | `proxy,code,bedrock,redis` | Debian slim | Code compression, non-root | | `slim` | `proxy,bedrock,redis` | Distroless | Minimal image, no shell | | `slim-nonroot` | `proxy,bedrock,redis` | Distroless | Minimal, non-root | | `code-slim` | `proxy,code,bedrock,redis` | Distroless | Code compression, minimal | | `code-slim-nonroot` | `proxy,code,bedrock,redis` | Distroless | Code compression, minimal, non-root | ### Build from source [#build-from-source] Use Docker Bake for multi-variant builds: ```bash # List all targets docker buildx bake --list targets # Build the default runtime image docker buildx bake runtime-default # Build a specific variant with custom registry docker buildx bake runtime-code-slim-nonroot \ --set '*.tags=my-registry/headroom:code-slim-nonroot' ``` ## Environment variables [#environment-variables] These variables configure Headroom at runtime. Set them in your shell, `.env` file, or container environment. ### LLM provider keys [#llm-provider-keys] | Variable | Description | |---|---| | `OPENAI_API_KEY` | OpenAI API key (used when proxying to OpenAI) | | `ANTHROPIC_API_KEY` | Anthropic API key (used when proxying to Anthropic) | | `AWS_ACCESS_KEY_ID` / `AWS_SECRET_ACCESS_KEY` | AWS credentials for Bedrock backend | | `GOOGLE_APPLICATION_CREDENTIALS` | Google Cloud credentials for Vertex AI backend | | `VERTEXAI_PROJECT` | GCP project id for the Vertex AI backend (LiteLLM-specific — distinct from `GOOGLE_CLOUD_PROJECT`; set it explicitly to avoid silently billing your ADC default quota project) | | `VERTEXAI_LOCATION` | GCP region for the Vertex AI backend (LiteLLM-specific — distinct from `GOOGLE_CLOUD_LOCATION`) | The Vertex AI backend also requires `google-cloud-aiplatform>=1.38`, which ships in the `[vertex]` extra (`pip install "headroom-ai[proxy,vertex]"`) rather than `[proxy]`, since it adds roughly 175 MB and only that backend needs it. The proxy refuses to start if the backend is selected without it. Vertex's native passthrough routes need neither the extra nor the backend flag — see [Google Vertex AI](/docs/proxy#google-vertex-ai) for setup details. ### Proxy configuration [#proxy-configuration] | Variable | Default | Description | |---|---|---| | `HEADROOM_PORT` | `8787` | Port the proxy listens on | | `HEADROOM_HOST` | `127.0.0.1` | Host the proxy binds to | | `HEADROOM_MODE` | `cache` | Default optimization mode: `token` or `cache` | | `HEADROOM_TELEMETRY` | `off` | Set to `on` for local-only usage stats (nothing is sent externally) | | `HEADROOM_REQUEST_TIMEOUT` | `300` | Request timeout in seconds | ### TypeScript SDK [#typescript-sdk] | Variable | Default | Description | |---|---|---| | `HEADROOM_BASE_URL` | `http://localhost:8787` | Proxy URL for the TypeScript SDK | | `HEADROOM_API_KEY` | _(none)_ | API key if the proxy requires auth | ## Troubleshooting [#troubleshooting] These are common issues faced during initial setup and how to resolve them. ### Python version error [#python-version-error] This project requires **Python 3.10+**. Check your version: ```bash python3 --version ``` If needed (Mac with Homebrew): ```bash brew install python@3.13 ``` ### Editable install fails (`pip install -e`) [#editable-install-fails-pip-install--e] Upgrade pip to the latest version: ```bash python3 -m pip install --upgrade pip ``` ### Missing `cargo` (Rust error) [#missing-cargo-rust-error] Some tests require Rust tooling. The recommended way to install rust is using `rustup`. You can find the official installation instructions [here](https://rust-lang.org/tools/install/). ## Dashboard [#dashboard] Headroom serves a live savings dashboard while the proxy is running. Open it with: ```bash headroom dashboard # opens the running proxy's dashboard in your browser headroom dashboard --no-open # just print the URL ``` With no `--port`, `headroom dashboard`, `headroom inspect` and the runtime probe used by `headroom learn` auto-detect the port of the most recently started live wrap session before falling back to `8787`. This matters because `headroom wrap ...` does not always land on `8787` — a Copilot-subscription session takes a dedicated port, and any wrap falls forward to the next free port when `8787` is busy, so a session can be serving on `8788` while `8787` is stale or empty. Pass `--port` (or set `HEADROOM_PORT`) to target a specific proxy explicitly; an explicit value always wins over auto-detection. `headroom doctor` keeps the requested port and reports a live proxy found elsewhere, including one associated with a wrap session marker, so you can rerun it with the suggested `--port`. If nothing is listening on the resolved port, the command prints a warning instead of silently opening a dead page. ## Next steps [#next-steps] Headroom integrates with [Kong AI Gateway](https://developer.konghq.com/ai-gateway/) as its compression service. Kong sends each request to Headroom, Headroom compresses it, and Kong forwards the compressed request to the LLM provider. Applications that call the gateway get compressed requests without installing Headroom themselves. Kong AI Gateway routes requests from applications to LLM providers. You configure it in Kong Konnect, and data plane nodes that you run handle the traffic. Headroom connects to it through Kong's [AI Prompt Compressor](https://developer.konghq.com/ai-gateway/policies/ai-prompt-compressor/#headroom-compressor-service) policy. ## How it works [#how-it-works] ``` Your app ──▶ Kong AI Gateway ──▶ LLM provider │ ▲ ▼ │ Headroom ``` 1. Your app sends a chat request to a model on Kong AI Gateway. 2. Kong sends the request to Headroom. Headroom compresses the large parts, such as tool results, logs, and JSON, and sends the request back. 3. Kong forwards the compressed request to the LLM provider and returns the response to your app. Kong also sends a session ID with each request, built from a header such as `x-session-id` when the request carries one. Headroom uses it to return the earlier turns of a conversation unchanged, so the provider's prompt cache keeps matching them. See [Session mode](/docs/proxy#session-mode). ## Installation [#installation] Install Headroom 0.37.0 or later with the proxy extra, on a host the Kong data plane can reach: ```bash pip install "headroom-ai[proxy]" ``` Start the proxy so the data plane can call it: ```bash export HEADROOM_PROXY_TOKEN="$(openssl rand -hex 32)" HEADROOM_COMPRESS_ALLOW_REMOTE=1 headroom proxy --host 0.0.0.0 --port 8787 ``` By default, Headroom accepts compression requests only from the machine it runs on. `HEADROOM_COMPRESS_ALLOW_REMOTE=1` accepts them from the data plane, and `HEADROOM_PROXY_TOKEN` is the token Kong sends with each one. Keep the token for the next step. To run Headroom in a container instead, see the manual `docker run` instructions in [Docker install](/docs/docker-install), pass both variables to the container, and publish its port on an interface the data plane can reach. The Docker-native wrapper publishes on loopback only; setting these variables does not change that binding. Point Kong at a single Headroom process, such as one next to each data plane node or one that all nodes share. A conversation's session lives in the process that served it, so don't put several instances behind a load balancer. ## Quick start [#quick-start] You need an AI Gateway (version 2.2 or later) with at least one AI Model, and kongctl, Kong's command-line tool for Konnect. If you don't have them yet, follow Kong's [AI Gateway getting started guide](https://developer.konghq.com/ai-gateway/get-started/). It creates a gateway with a local data plane, exports the gateway's ID as `AI_GATEWAY_ID`, and adds a model. To use a gateway you already have, run `kongctl get ai-gateways -o yaml` and export the `id` of your gateway as `AI_GATEWAY_ID`. On Kubernetes, Kong's [Kong Operator guide](https://developer.konghq.com/operator/konnect/how-to/ai-gateway-headroom/) deploys Headroom and the policy together instead. The policy below tells Kong where Headroom runs. Save it as `headroom-policy.yaml`: ```yaml ai_gateway_policies: - ref: headroom-compressor ai_gateway: !lookup {id: !env AI_GATEWAY_ID} name: headroom-compressor display_name: Headroom compressor type: ai-prompt-compressor enabled: true global: true config: provider: headroom compressor_url: http://host.docker.internal:8787/v1/compress timeout: 45000 stop_on_error: true headroom: proxy_token: !env HEADROOM_PROXY_TOKEN ``` `compressor_url` is the address of Headroom's [`/v1/compress`](/docs/proxy#post-v1compress) endpoint as the data plane sees it. `host.docker.internal` works when the data plane runs in Docker Desktop on the same machine as Headroom, for example the local data plane from Kong's getting started guide. For a data plane on a separate host, use an HTTPS endpoint with a trusted certificate, such as `https://headroom.example.com/v1/compress`, through a TLS reverse proxy in front of Headroom. The connection carries both the proxy token and prompt content. Restrict access to the Headroom listener to the data plane or TLS reverse proxy; do not expose the HTTP listener directly on a public network. kongctl reads `AI_GATEWAY_ID` and `HEADROOM_PROXY_TOKEN` from the environment when it applies the file. In the terminal where you run kongctl, set the token to the value Headroom runs with, then apply the file: ```bash export HEADROOM_PROXY_TOKEN='' kongctl apply -f headroom-policy.yaml ``` kongctl shows the change it's about to make and asks you to type `yes` to confirm. With `global: true`, the policy applies to every AI Model on the gateway. To see it work, send a request that carries a large tool result or log output, since short prompts aren't compressed. Headroom logs a line like this for each request it compresses: ``` Pipeline complete: 10275 -> 5148 tokens (saved 5127, 49.9% reduction) ``` `curl http://localhost:8787/stats` on the Headroom host shows the totals. ## Options [#options] These fields go under `config` in the policy. See Kong's [policy reference](https://developer.konghq.com/ai-gateway/policies/ai-prompt-compressor/#configure-headroom-connection) for the rest of the configuration. | Field | Description | | --- | --- | | `compressor_url` | The address of Headroom's `/v1/compress` endpoint as the data plane sees it. Include the path: Kong uses the URL exactly as written. | | `headroom.proxy_token` | The token Headroom runs with (`HEADROOM_PROXY_TOKEN`). Kong requires it whenever `compressor_url` isn't a loopback address. | | `headroom.session_id_headers` | The request headers Kong builds the session ID from. The default list includes `x-session-id`. | | `stop_on_error` | `true` (default) returns an error to your app when the call to Headroom fails. `false` sends the original, uncompressed request to the provider instead. | | `timeout` | How long Kong waits for Headroom, in milliseconds. The default is `10000`. | `global` sits next to `config`. To compress only some models, set `global: false` and add `headroom-compressor` to the `policies` list of each AI Model that should use it. Headroom plugs into LangChain at four points: the chat model, tool output, retrieved documents, and conversation history. In LangGraph it also works as a graph node, compressing `ToolMessage` content between the tool step and the agent step. ## Install [#install] ```bash pip install "headroom-ai[langchain]" # langchain-core + langchain-openai pip install "headroom-ai[langgraph]" # the above, plus langgraph ``` Provider packages are separate, as they are in LangChain itself: ```bash pip install langchain-anthropic # ChatAnthropic pip install langchain # create_agent, init_chat_model pip install langchain-classic # ContextualCompressionRetriever ``` LangChain 1.0 removed several modules that older Headroom docs referenced. `langchain.memory` and `langchain.retrievers` no longer exist, and `create_openai_tools_agent` and `AgentExecutor` are gone from `langchain.agents`. The [migration table](#migrating-from-langchain-0x) at the bottom of this page maps each one to its replacement. Verified against langchain-core 1.6.1, langchain 1.3.18 and langgraph 1.2.11. ## Chat model [#chat-model] `HeadroomChatModel` wraps any LangChain chat model. Messages are compressed on the way out; everything else about the model is unchanged. ```python from langchain_openai import ChatOpenAI from headroom.integrations import HeadroomChatModel llm = HeadroomChatModel(ChatOpenAI(model="gpt-4o")) response = llm.invoke("Hello!") print(llm.get_savings_summary()) # {'total_requests': 1, 'total_tokens_saved': 0, 'average_savings_percent': 0.0, # 'total_tokens_before': 9, 'total_tokens_after': 9} ``` Short prompts compress to nothing, which is the intended behaviour — savings appear once tool output and history are in the context. Any provider works: ```python from langchain_anthropic import ChatAnthropic from headroom.integrations import HeadroomChatModel llm = HeadroomChatModel(ChatAnthropic(model="claude-sonnet-4-20250514")) ``` Tool binding is forwarded to the underlying model, so `llm.bind_tools(...)` and the agent runtimes built on it behave the same as the unwrapped model. ## Agents [#agents] `wrap_tools_with_headroom` compresses tool output before it re-enters the agent's context. The wrapped tool keeps the original argument schema, so the model sees the same parameters. ```python import json from langchain_core.tools import tool from langchain_openai import ChatOpenAI from langchain.agents import create_agent from headroom.integrations import HeadroomChatModel, wrap_tools_with_headroom @tool def query_database(query: str) -> str: """Query the users database. Returns JSON rows.""" return json.dumps({"results": [...], "total": 300}) llm = HeadroomChatModel(ChatOpenAI(model="gpt-4o-mini")) tools = wrap_tools_with_headroom([query_database], min_chars_to_compress=1000) agent = create_agent(llm, tools) result = agent.invoke({ "messages": [("user", "How many users signed up last week?")] }) ``` `create_agent` comes from the `langchain` package. LangGraph's `create_react_agent` still works and is a drop-in substitute, but it is deprecated as of LangGraph 1.0 and slated for removal in 2.0. Per-tool metrics are collected globally: ```python from headroom.integrations import get_tool_metrics print(get_tool_metrics().get_summary()) # {'total_invocations': 1, 'total_compressions': 1, 'total_chars_saved': 4202, # 'average_compression_ratio': 0.901, # 'by_tool': {'query_database': {'invocations': 1, 'compressions': 1, 'chars_saved': 4202}}} ``` Async agents work too — the wrapped tool exposes a coroutine, so `await agent.ainvoke(...)` compresses on the async path as well. ## LangGraph compression node [#langgraph-compression-node] Wrapping tools and compressing in the graph are not equivalent, and the difference is large. On the same 300-row JSON result: | Path | Result | Saved | |---|---:|---:| | `wrap_tools_with_headroom` | 38,395 chars | 10% | | `create_compress_tool_messages_node` | 18,390 chars | 57% | The tool wrapper routes through the MCP compressor, which deliberately keeps its output parseable as JSON for downstream tool consumers. That rules out the schema-hoisting rewrite — the one that turns 300 repeated objects into a single schema line plus CSV rows — so on an array of similar records it mostly removes whitespace. The graph node has no such constraint, because the value only has to be read by the model. Use the graph node when the output is going to the model and nothing else parses it. Use tool wrapping when something downstream still needs JSON. ```python from langgraph.graph import StateGraph, MessagesState, START, END from headroom.integrations.langchain import create_compress_tool_messages_node graph = StateGraph(MessagesState) graph.add_node("agent", agent_node) graph.add_node("tools", tools_node) graph.add_node("compress", create_compress_tool_messages_node( min_tokens_to_compress=100, )) graph.add_edge(START, "agent") graph.add_edge("tools", "compress") graph.add_edge("compress", "agent") app = graph.compile() ``` On a 300-row JSON tool result — 42,597 characters — the node returns 18,390, a 57% reduction, with `tool_call_id` preserved so the graph stays valid. To call it directly rather than as a node: ```python from headroom.integrations.langchain import compress_tool_messages result = compress_tool_messages(state["messages"], min_tokens_to_compress=100) result.messages # the rewritten message list result.messages_compressed # 1 result.total_tokens_saved # 6052 ``` Messages below the threshold are returned unchanged. ## Memory [#memory] `HeadroomChatMessageHistory` wraps any `BaseChatMessageHistory` and compresses older turns once the history crosses a token budget. ```python from langchain_core.chat_history import InMemoryChatMessageHistory from headroom.integrations import HeadroomChatMessageHistory history = HeadroomChatMessageHistory( InMemoryChatMessageHistory(), compress_threshold_tokens=4000, # start compressing above 4K tokens keep_recent_turns=5, # never touch the last 5 turns ) history.add_user_message("...") history.add_ai_message("...") print(history.get_compression_stats()) # {'compression_count': 0, 'total_tokens_saved': 0, # 'threshold_tokens': 4000, 'keep_recent_turns': 5} ``` Use it anywhere a chat history is accepted: ```python from langchain_core.runnables.history import RunnableWithMessageHistory chain = RunnableWithMessageHistory(llm, lambda session_id: history) ``` `ConversationBufferMemory` was removed in LangChain 1.0. `RunnableWithMessageHistory`, or a LangGraph checkpointer, replaces it. ## Retriever [#retriever] `HeadroomDocumentCompressor` scores retrieved documents against the query and keeps the best. Retrieve widely for recall, then narrow for precision. ```python from headroom.integrations import HeadroomDocumentCompressor compressor = HeadroomDocumentCompressor( max_documents=10, min_relevance=0.3, prefer_diverse=True, # MMR-style diversity ) docs = compressor.compress_documents(retrieved_docs, "What is Python?") ``` It is a real `langchain_core.documents.compressor.BaseDocumentCompressor`, so it also drops into the classic retriever pattern: ```python from langchain_classic.retrievers import ContextualCompressionRetriever retriever = ContextualCompressionRetriever( base_compressor=compressor, base_retriever=vectorstore.as_retriever(search_kwargs={"k": 50}), ) docs = retriever.invoke("What is Python?") # retrieves 50, returns 10 ``` `ContextualCompressionRetriever` moved out of `langchain.retrievers` in 1.0 and now ships in `langchain-classic`. ## Streaming [#streaming] ```python for chunk in llm.stream("Tell me a story"): print(chunk.content, end="", flush=True) response = await llm.ainvoke("Hello!") async for chunk in llm.astream("Tell me a story"): print(chunk.content, end="", flush=True) ``` ## Configuration [#configuration] ```python from headroom import HeadroomConfig, HeadroomMode from headroom.integrations import HeadroomChatModel from langchain_openai import ChatOpenAI config = HeadroomConfig(default_mode=HeadroomMode.OPTIMIZE) llm = HeadroomChatModel(ChatOpenAI(model="gpt-4o"), config=config) ``` ## Migrating from LangChain 0.x [#migrating-from-langchain-0x] | Removed in LangChain 1.0 | Use instead | Package | |---|---|---| | `langchain.memory.ConversationBufferMemory` | `RunnableWithMessageHistory`, or a LangGraph checkpointer | `langchain-core` | | `langchain_community.chat_message_histories.ChatMessageHistory` | `langchain_core.chat_history.InMemoryChatMessageHistory` | `langchain-core` | | `langchain.retrievers.ContextualCompressionRetriever` | same class, moved | `langchain-classic` | | `langchain.agents.create_openai_tools_agent` + `AgentExecutor` | `langchain.agents.create_agent` | `langchain` | | `langgraph.prebuilt.create_react_agent` | `langchain.agents.create_agent` (the old name still works, deprecated in LangGraph 1.0) | `langchain` | | `langchain_community.vectorstores.*` | provider packages, or `langchain_core.vectorstores.InMemoryVectorStore` for tests | varies | Headroom's own API did not change across the LangChain 1.0 boundary. `HeadroomChatModel`, `HeadroomChatMessageHistory`, `HeadroomDocumentCompressor` and `wrap_tools_with_headroom` take the same arguments they always did. Headroom is designed to compress LLM context without losing accuracy. This page documents when it helps, when it does not, and the safety gates that prevent harmful compression. ## When Headroom Helps vs. Does Not [#when-headroom-helps-vs-does-not] The compression figures below are not from a published, repeatable benchmark suite -- we could not trace a prior version of this table to one. Instead of an unsourced number, each row is a directional single-sample measurement taken with `compress()` against a synthetic payload of that shape. For a seeded, committed harness that reproduces figures of this kind exactly, run `uv run python benchmarks/index_proof_table.py --seed 20260902`. Your own traffic will vary; measure it with `client.chat.completions.simulate()` (see [Troubleshooting](/docs/troubleshooting#use-simulation-to-inspect-transforms)) rather than treating these as guarantees. | Content Type | Compression (single-sample) | Latency Impact | Best For | |---|---|---|---| | **JSON: Arrays of dicts** (search results, API responses, DB rows) | ~52% (200-item sample) | Net latency win on Sonnet/Opus | Primary use case | | **JSON: Arrays of strings** (file paths, log lines, tags) | ~95% (300-item sample) | Net latency win | String dedup + sampling | | **JSON: Arrays of numbers** (metrics, time series) | ~97% (500-item sample) | Net latency win | Statistical summary | | **JSON: Mixed-type arrays** | ~45% (200-item sample) | Net latency win | Group-by-type compression | | **Structured logs** (as JSON) | Varies widely with duplication -- see methodology note | Net latency win | Log entries in tool outputs | | **Agentic conversations** (multi-turn) | Not independently measured for this page | Break-even to net win | Multi-tool agent sessions | | **Plain text** (documentation, articles) | Requires the optional `kompress`/ML text path; not exercised in this measurement pass | Adds latency (cost savings only) | Cost optimization | | **Code** | Passthrough | Minimal overhead | See below | | **RAG document contexts** | Passthrough | Minimal overhead | Not compressed | ### Where Headroom Adds the Most Value [#where-headroom-adds-the-most-value] - Long agent sessions with accumulated tool outputs - JSON-heavy workflows -- API responses, database queries - Build and test output - Multi-tool agents (repeated tool results compound the per-call savings above) ### Where Headroom Adds Little Value [#where-headroom-adds-little-value] - Short conversational exchanges -- overhead can exceed savings on small payloads - Code-only sessions (reading/writing files) -- code passes through - Single-turn requests with no accumulated context ## What Headroom Does NOT Compress [#what-headroom-does-not-compress] - **Short messages** (below `min_tokens_to_compress`, 50 tokens by default -- `headroom/transforms/content_router.py:4806`) -- overhead exceeds savings - **Source code** -- passes through unchanged to preserve correctness - **grep/search results** -- compact structured format, already minimal - **Images** -- counted at fixed token cost (~1,600 tokens), not compressed - **System prompts** -- preserved for prefix cache compatibility ## Code Compression [#code-compression] Headroom includes an AST-aware CodeCompressor (tree-sitter, 11 languages: Python, JavaScript, TypeScript, Go, Rust, Java, C, C++, Perl, C#, PHP) but it is gated behind safety protections that prevent it from firing in most real-world scenarios. This is intentional. **Why code mostly passes through:** 1. **Size gates, not a word count**: a message under `min_tokens` (50 tokens by default; savings profiles set this anywhere from 10-250) is skipped, and a block under `min_chars_for_block_compression` (500 characters) is skipped -- both measured in tokens/characters, never words (`headroom/transforms/content_router.py:4806,1596`) 2. **Recent code protection** (`protect_recent_code=4`): Code in the last 4 messages is never compressed 3. **Analysis intent protection** (`protect_analysis_context=True`): If the most recent user message contains keywords like "analyze", "review", "explain", "fix", "debug" -- ALL code in the conversation is protected **Why this is the right default**: Code is almost always fetched because the user wants to work with it. Compressing function bodies would remove exactly what they need. **Where code savings come from**: Headroom compresses code in the live zone — the newest tool outputs and content blocks — with the AST-aware CodeCompressor, while keeping recent and analysis-context code fully intact. It never drops messages from the conversation history or strips function bodies. **Override**: Set `protect_analysis_context=False` in `ContentRouterConfig` for aggressive code compression. Requires `headroom-ai[code]` for tree-sitter. ## JSON Compression Constraints [#json-compression-constraints] ### What Gets Compressed [#what-gets-compressed] - Arrays of **dicts**: Full statistical analysis with adaptive K (Kneedle algorithm) - Arrays of **strings**: Dedup + adaptive sampling + error preservation - Arrays of **numbers**: Statistical summary + outlier/change-point preservation - **Mixed-type** arrays: Grouped by type, each group compressed independently - **Nested** objects: Recursed into, arrays within are compressed (up to depth 5) ### What Passes Through [#what-passes-through] - Arrays below 5 items (`min_items_to_analyze`) - Content below 200 tokens (`min_tokens_to_crush`) - Bool-only arrays - JSON objects without array values - Malformed JSON (silently passes through, no error) ### Edge Cases [#edge-cases] - **NaN/Infinity** in numeric fields: Filtered out before statistics are computed - **Nesting depth > 5**: Inner arrays not examined for compression - **Mixed-type arrays with small groups**: Groups below `min_items_to_analyze` are kept as-is ## Safety Gates [#safety-gates] All compressors follow the same principle: **fail gracefully, return original content unchanged**. - Invalid JSON passes through (no error raised) - AST parse failure falls back to the original content, unchanged - Compression that makes output larger returns the original - A missing optional dependency (tree-sitter) causes a passthrough with a warning log - Errors are logged at WARNING level and never propagated to callers LLMLingua is not part of Headroom 0.37.0. The `[llmlingua]` extra was removed in 0.9.x with no live code path using it (`pyproject.toml:115`); text compression today goes through the Kompress (ModernBERT ONNX) path instead, which fails open the same as every other compressor. ## Adaptive K: How Item Retention Works [#adaptive-k-how-item-retention-works] SmartCrusher does not use fixed K values. It uses information-theoretic sizing: 1. **Kneedle algorithm** on bigram coverage curves finds the point where adding more items stops providing new information 2. **SimHash** fingerprinting detects near-duplicate items 3. **zlib validation** ensures the subset captures the full set's diversity The resulting K is split: 30% from array start, 15% from end, 55% for importance-scored items. **Safety guarantees (additive, never dropped):** - Error items (containing "error", "exception", "failed", "critical") -- across ALL array types - Numeric anomalies (> 2 standard deviations from mean) - String length anomalies (> 2 standard deviations from mean length) - Change points (sudden shifts in running values) These are kept even if they exceed the K budget. ## Configuration Tuning [#configuration-tuning] | Parameter | Default | Effect | |---|---|---| | `min_items_to_analyze` | 5 | Arrays below this pass through | | `min_tokens_to_crush` | 200 | Content below this passes through | | `max_items_after_crush` | 15 | Upper bound on retained items | | `variance_threshold` | 2.0 | Std devs for anomaly detection (lower = more preserved) | | `protect_analysis_context` | True | Protect code when user asks about it | | `protect_recent_code` | 4 | Messages from end to protect code in | | `skip_user_messages` | True | Never compress user messages | | `toin_confidence_threshold` | 0.3 | Minimum TOIN confidence to apply hints | ## Provider Interactions [#provider-interactions] - CacheAligner maximizes Anthropic/OpenAI prefix cache hit rates - Token counting uses model-specific tokenizers (tiktoken for OpenAI, calibrated estimation for Anthropic) - Compression works with all providers -- no provider-specific limitations - Compressed content is valid JSON -- downstream tools and parsers work unchanged ## TOIN Cold Start [#toin-cold-start] The Tool Output Intelligence Network (TOIN) learns compression patterns from usage. For new tool types: - No learned patterns exist -- falls back to statistical heuristics - Confidence below `toin_confidence_threshold` (default 0.3) -- TOIN hints ignored - Patterns build up over time as tools are used repeatedly - Cross-session learning requires persistence (`TelemetryConfig.storage_path`) ## Telemetry and the Upload Beacon [#telemetry-and-the-upload-beacon] Headroom ships two independent, separately-defaulted switches (`headroom/telemetry/beacon.py:1-92`) -- worth knowing about before you treat this page's "nothing leaves the machine" framing as universal: - **`HEADROOM_TELEMETRY`** -- off by default, opt-in. Aggregates compression stats locally for the in-process collector and the `/stats` endpoint. Never leaves the machine. - **`HEADROOM_BEACON`** -- **on by default, opt-out.** Uploads an anonymous summary of how compression behaved, so we can see when a release regresses a ratio or starts skipping a content type across real workloads instead of only our own test corpus. It carries counters and identifiers only: token totals, compression ratios, skip reasons, provider and model ids, failure counts, plus OS and architecture. Never prompts, completions, code, or file paths -- the collector allowlists fields server-side and drops the rest. Disable it with `HEADROOM_BEACON=off`, `HEADROOM_TELEMETRY=off`, the cross-tool `DO_NOT_TRACK=1` convention, or by running offline. `is_beacon_enabled()` is fail-open: an unrecognized value uploads rather than staying silent. See [Proxy](/docs/proxy) for the full field list. These are deliberately separate so that enabling local `/stats` does not silently start an upload on the next release. The one crossing only ever turns the upload off: an explicit `HEADROOM_TELEMETRY=off` also stops the beacon (unless `HEADROOM_BEACON=on`). Headroom integrates with [LiteLLM](https://github.com/BerriAI/litellm) as a callback that compresses messages before they reach any provider. One line to enable, works with all 100+ LiteLLM-supported providers. Looking for the proxy's `--backend vertex_ai` / `bedrock` options instead? Those make the Headroom **proxy** call cloud providers through LiteLLM — a different mechanism from the callback documented here. See [Cloud providers](/docs/proxy#cloud-providers). ## Installation [#installation] ```bash pip install headroom-ai litellm ``` ## Quick start [#quick-start] ```python import litellm from headroom.integrations.litellm_callback import HeadroomCallback litellm.callbacks = [HeadroomCallback()] # All calls now compressed automatically response = litellm.completion(model="gpt-4o", messages=[...]) response = litellm.completion(model="bedrock/claude-sonnet", messages=[...]) response = litellm.completion(model="azure/gpt-4o", messages=[...]) ``` The callback compresses messages in LiteLLM's `async_pre_call_hook` before they reach the provider. ## How it works [#how-it-works] 1. You call `litellm.completion()` with your messages 2. `HeadroomCallback.async_pre_call_hook` compresses the messages 3. LiteLLM sends the compressed messages to the provider 4. The response comes back unchanged This works with every provider LiteLLM supports: OpenAI, Anthropic, Bedrock, Azure, Vertex AI, Cohere, Groq, Mistral, Together, Ollama, and more. LiteLLM and other schema-translating routes are not claimed to preserve Claude Code's server-side auto-mode classifier protocol. Requests continue to fail open as ordinary model traffic, but Claude Code may use its billable client-side classifier fallback. Native Anthropic transport is the supported path until an adapter has an end-to-end safeguard fixture. ## With LiteLLM Proxy [#with-litellm-proxy] If you run LiteLLM as a proxy server, use the ASGI middleware: ```python from litellm.proxy.proxy_server import app from headroom.integrations.asgi import CompressionMiddleware app.add_middleware(CompressionMiddleware) ``` Or configure via YAML: ```yaml # litellm_config.yaml litellm_settings: callbacks: ["headroom.integrations.litellm_callback.HeadroomCallback"] ``` ## Direct compress() with LiteLLM [#direct-compress-with-litellm] You can also use `compress()` directly instead of the callback: ```python import litellm from headroom import compress messages = [{"role": "user", "content": large_content}] compressed = compress(messages, model="bedrock/claude-sonnet") response = litellm.completion( model="bedrock/claude-sonnet", messages=compressed.messages, ) print(f"Saved {compressed.tokens_saved} tokens") ``` ## ASGI middleware [#asgi-middleware] Drop-in middleware for any ASGI application. Intercepts `/v1/messages`, `/v1/chat/completions`, `/v1/responses`, and `/chat/completions`: ```python from fastapi import FastAPI from headroom.integrations.asgi import CompressionMiddleware app = FastAPI() app.add_middleware(CompressionMiddleware) ``` Response headers include `x-headroom-compressed: true` and `x-headroom-tokens-saved: 1234`. ## Over HTTP (guardrail / gateway) [#over-http-guardrail--gateway] The options above run Headroom **in** the LiteLLM process. If instead LiteLLM runs as its own proxy and you want it to call Headroom over the network — the guardrail deployment — point it at [`POST /v1/compress`](/docs/proxy#post-v1compress). LiteLLM swaps `messages` for the compressed result and forwards to the provider. Two things this deployment needs — the proxy extras, and the proxy itself running with remote access opted in: ```bash pip install "headroom-ai[proxy]" # Headroom is loopback-only by default and answers remote callers with 404. HEADROOM_COMPRESS_ALLOW_REMOTE=1 headroom proxy ``` Without `HEADROOM_COMPRESS_ALLOW_REMOTE=1` a remote caller gets `404`, not `403` — so a misconfigured guardrail looks exactly like a wrong URL. If you also set `HEADROOM_PROXY_TOKEN`, send it as `X-Headroom-Proxy-Token` or you get `401`. Leave `config.mode` unset. The default pipeline is marker-free, which is what a forward-only caller wants: `mode: "ccr"` emits retrieval markers that are a dangling pointer unless you also inject the `headroom_retrieve` tool and can reach `/v1/retrieve`. Because LiteLLM passes model names through, send the real one — `claude-sonnet-4-6`, `bedrock/anthropic.claude-3-5-sonnet`, `gemini-2.5-pro` — so Headroom resolves the right tokenizer and context limit. Anthropic-shaped messages need no conversion; see [Message format](/docs/proxy#message-format). For multi-turn agent loops, set `config.frozen_message_count` to the number of messages the provider has already cached, **and send back the messages you previously forwarded rather than the pristine originals**. Getting this wrong silently destroys the provider's prefix cache — see [Multi-turn usage](/docs/proxy#multi-turn-usage-keeping-the-prefix-cache) for the loop. Local models do not charge per token, but they still pay for every prompt token during prefill. On Apple Silicon and other local inference setups, long coding-agent sessions often bottleneck on prompt processing rather than generation speed. Headroom can help by sending fewer prompt tokens to the local server. This workflow measures that effect with the proxy dashboard: run the same task once with optimization disabled, reset the agent state, run it again with optimization enabled, and compare token counts. Joe Maddalone demonstrated this workflow in [Cut Local LLM Prompt Processing 30% on a Mac with Headroom](https://www.youtube.com/watch?v=j6U_kKiMXgo). His June 2026 demo used an OpenAI-compatible local server, a coding-agent refactor task, `--no-optimize` for the baseline, and the dashboard to compare sessions. ## Setup [#setup] Start your local OpenAI-compatible model server first. Examples include MLX/OMLX, vLLM, LM Studio, Ollama's OpenAI-compatible endpoint, or another server that accepts `/v1/chat/completions` or `/v1/responses`. For this guide, assume the local server is listening on `http://127.0.0.1:8000`. ```bash pip install "headroom-ai[proxy]" ``` ## 1. Baseline passthrough run [#1-baseline-passthrough-run] Start Headroom as a transparent proxy with optimization disabled: ```bash headroom proxy \ --port 8787 \ --openai-api-url http://127.0.0.1:8000 \ --no-optimize ``` Point your agent or app at Headroom, not directly at the local server: ```bash export OPENAI_BASE_URL=http://127.0.0.1:8787/v1 export OPENAI_API_KEY=local ``` Run a realistic task. Coding-agent refactors are good benchmark candidates because they produce repeated file reads, tool results, lint/test output, and a growing conversation context. Open the dashboard while the run is active: ```bash headroom dashboard --port 8787 --no-open # or open http://127.0.0.1:8787/dashboard ``` Record the baseline session totals. With `--no-optimize`, before and after token counts should match because Headroom is only forwarding traffic. ## 2. Reset the task [#2-reset-the-task] Before the optimized run, reset the benchmark state so the second run is comparable: - revert the code or data changes made by the first run - start a fresh agent session - use the same model and local server - use the same prompt - avoid changing unrelated flags or server settings For coding-agent tests, a clean git worktree is the simplest reset point. ## 3. Optimized run [#3-optimized-run] Restart Headroom without `--no-optimize`: ```bash headroom proxy \ --port 8787 \ --openai-api-url http://127.0.0.1:8000 ``` Run the same task again with the same `OPENAI_BASE_URL` and prompt. Watch the dashboard's before/after token counts for the session. The savings percentage is the prompt-token reduction sent upstream to the local model. That does not make the model's prefill kernel faster; it reduces how much prompt the kernel has to process. ## Optional: traffic learning [#optional-traffic-learning] After you have a baseline, you can test learning-enabled runs: ```bash headroom proxy \ --port 8787 \ --openai-api-url http://127.0.0.1:8000 \ --learn ``` `--learn` implies memory and lets Headroom learn recurring traffic patterns from proxy sessions. Treat this as a separate benchmark condition: compare passthrough, optimized, and optimized-with-learning runs independently. ## What to report [#what-to-report] For a useful local prefill benchmark, include: | Field | Example | |---|---| | Local server | MLX, vLLM, LM Studio, Ollama-compatible endpoint | | Model | local model name and quantization, if relevant | | Hardware | Mac model, RAM, or GPU/CPU target | | Agent/client | coding agent or app name | | Task | short description of the repeated task | | Baseline tokens | dashboard before/after total with `--no-optimize` | | Optimized tokens | dashboard before/after total without `--no-optimize` | | Savings | dashboard percentage | | Notes | whether `--learn`, `--memory`, or other flags were enabled | ## Interpreting results [#interpreting-results] Local inference changes the value proposition: - Hosted APIs: fewer input tokens usually means lower cost and lower latency. - Local models: fewer input tokens primarily means less prefill work and lower memory pressure. Long-running agent sessions tend to show larger gains than short chat turns because repeated file reads, tool outputs, and logs create more compressible context. If a task is mostly short natural-language turns, expect smaller savings. Do not compare a cold first run against a warmed second run and attribute all improvement to compression. Keep the server, model, prompt, and agent task stable, and use the dashboard token counts as the primary measurement. Headroom's MCP server exposes compression, retrieval, and observability as tools that any MCP-compatible AI coding tool can call -- Claude Code, Cursor, Codex, and more. No proxy required. ## Installation [#installation] ```bash # MCP tools only (lightweight) pip install "headroom-ai[mcp]" # Or with the proxy pip install "headroom-ai[proxy]" ``` ## Setup for Claude Code [#setup-for-claude-code] ```bash # Register with Claude Code (one-time) headroom mcp install # Start Claude Code — it now has headroom tools claude ``` Claude Code can now compress content on demand, retrieve originals, and check session stats. For automatic compression of **all** traffic, also run the proxy: ```bash # Terminal 1 headroom proxy # Terminal 2 ANTHROPIC_BASE_URL=http://127.0.0.1:8787 claude ``` ## Tools [#tools] ### headroom_compress [#headroom_compress] Compress content on demand. The LLM calls this when it wants to shrink large content before reasoning over it. **Parameters:** - `content` (required) -- text to compress (files, JSON, logs, search results) **Returns:** - `compressed` -- compressed text - `hash` -- key for retrieving the original later - `original_tokens` / `compressed_tokens` / `savings_percent` - `transforms` -- which compression algorithms were applied Example flow: ``` Claude: Let me compress this large output to save context space. -> headroom_compress(content="[5000 lines of grep results...]") <- { "compressed": "[key matches with context...]", "hash": "a1b2c3d4e5f6...", "original_tokens": 12000, "compressed_tokens": 3200, "savings_percent": 73.3, "transforms": ["router:search:0.27"] } ``` The original is stored locally for 1 hour. If the LLM needs the full content later, it calls `headroom_retrieve`. ### headroom_retrieve [#headroom_retrieve] Retrieve original uncompressed content by hash. **Parameters:** - `hash` (required) -- hash key from a previous compression - `query` (optional) -- search within the original to return only matching items **Returns:** - `original_content` (full retrieval) or `results` (filtered search) - `source` -- `"local"` or `"proxy"` Retrieval checks the local store first, then falls back to the proxy's store. Hashes from either source work transparently. ### headroom_stats [#headroom_stats] Session compression statistics. **Returns:** - `compressions`, `retrievals`, `tokens_saved`, `savings_percent` - `estimated_cost_saved_usd` - `recent_events` -- last 10 compression/retrieval events - `sub_agents` -- stats from sub-agent MCP instances - `combined` -- main + sub-agent totals - `proxy` -- request count, cache hits, cost saved (if proxy is running) Sub-agent stats are aggregated via a shared stats file at `~/.headroom/session_stats.jsonl`. ## CLI commands [#cli-commands] ```bash # Install (registers with Claude Code) headroom mcp install headroom mcp install --proxy-url http://host:9000 # Custom proxy URL headroom mcp install --force # Overwrite existing # Check status headroom mcp status # Uninstall headroom mcp uninstall # Debug mode headroom mcp serve --debug # Streamable HTTP mode headroom mcp serve --transport http --host 127.0.0.1 --port 8788 --path /mcp ``` ## MCP host configuration [#mcp-host-configuration] For MCP hosts that let you configure a local stdio server, point them at `headroom mcp serve`. If you also run the proxy, pass the proxy URL explicitly so retrieval and stats come from the intended proxy instance. If you are publishing or consuming Headroom through an MCP registry, use the canonical descriptor at `https://github.com/headroomlabs-ai/headroom/blob/main/server.json`. It captures the package form for `headroom-ai[mcp]` and the current `headroom mcp serve` launch contract in one file. ```json { "mcpServers": { "headroom": { "type": "stdio", "command": "headroom", "args": ["mcp", "serve", "--proxy-url", "http://127.0.0.1:8787"] } } } ``` For multiple proxy instances, register one stdio MCP server per proxy URL: ```json { "mcpServers": { "headroom": { "type": "stdio", "command": "headroom", "args": ["mcp", "serve", "--proxy-url", "http://127.0.0.1:8787"] }, "headroom-azure": { "type": "stdio", "command": "headroom", "args": ["mcp", "serve", "--proxy-url", "http://127.0.0.1:8788"] } } } ``` If you need Streamable HTTP instead of stdio, run `headroom mcp serve --transport http` and point your MCP host at that endpoint. The default path is `/mcp`, and the default port is `8788`. Do not assume that a running proxy exposes an HTTP MCP endpoint at `/mcp`. The proxy serves its own API; it does not automatically provide the MCP HTTP transport. ### `command: "headroom"` fails to start [#command-headroom-fails-to-start] The configurations above use `"command": "headroom"`, which only works if the `headroom` executable is on the PATH your MCP host (Codex, etc.) sees at startup. If you installed Headroom into a project virtualenv — for example with `uv add headroom-ai` — the CLI lives only inside that venv, and the host fails at launch with: ```text MCP client for `headroom` failed to start: MCP startup failed: No such file or directory (os error 2) ``` Install Headroom so it's globally on PATH — `uv tool install "headroom-ai[mcp]"` (or `pipx install "headroom-ai[mcp]"`) — or replace `"headroom"` with the absolute path to the binary (`command -v headroom`, or `where headroom` on Windows). ## Cross-tool compatibility [#cross-tool-compatibility] | Tool | MCP Support | Setup | |------|-------------|-------| | Claude Code | Native | `headroom mcp install` | | Cursor | Supported | Add to Cursor MCP settings | | Codex | If supported | Configure MCP server | | Any MCP host | Yes | Point to `headroom mcp serve` | ## Architecture [#architecture] For user-managed Serena drift, run `headroom mcp reconcile` to inspect the current recommendation. Add `--adopt` only when you want Headroom to replace the Serena entry. If an enabled Claude Code plugin (such as `serena` from the `claude-plugins-official` marketplace) also bundles a Serena server, `reconcile` reports it and prints the `claude plugin disable` command; Headroom never edits third-party plugins. ### MCP only (no proxy) [#mcp-only-no-proxy] The LLM calls `headroom_compress` on demand. Compression happens locally in the MCP process. Originals are stored in a local `CompressionStore` with 1-hour TTL. ### MCP + Proxy (full setup) [#mcp--proxy-full-setup] The proxy compresses all traffic at the HTTP level (before the LLM sees content). MCP tools operate after the LLM receives content. They handle different data and do not double-compress. `headroom_retrieve` checks the local store first, then falls back to the proxy's store. ## Troubleshooting [#troubleshooting] **"MCP SDK not installed"** -- Run `pip install "headroom-ai[mcp]"`. **"Proxy not running"** -- Start the proxy with `headroom proxy` in another terminal. Only needed for proxy-backed retrieval. **"Entry not found or expired"** -- Local content expires after 1 hour, proxy content after 30 minutes (configurable via `HEADROOM_CCR_TTL_SECONDS`). **Claude doesn't see headroom tools** -- Run `headroom mcp status`, restart Claude Code, and verify with `/mcp` inside Claude Code. ### Claude Code `/usage` attributes a large share to `headroom` MCP [#claude-code-usage-attributes-a-large-share-to-headroom-mcp] Claude Code counts MCP tool calls and MCP tool results as session context. If a long-running workflow or a subagent-heavy command calls `headroom_compress`, `headroom_retrieve`, or `headroom_stats` many times, `/usage` can show a visible share under the `headroom` MCP server even when Headroom is saving tokens inside individual tool results. That number is not a direct "Headroom overhead" bill. It means Claude Code kept Headroom MCP interactions in the conversation context. Deep research workflows can amplify this because each subagent has its own requests and may keep its own MCP results in context. Use these checks when the MCP share looks high: - Run `headroom_stats` and compare `tokens_saved` with the number of MCP calls. - Use `/compact` after large MCP-backed investigation steps so old MCP tool results stop occupying the active context window. - Prefer the proxy path for automatic compression of normal Claude Code traffic: `headroom proxy` plus `ANTHROPIC_BASE_URL=http://127.0.0.1:8787 claude`. - Disable the MCP server for sessions where you only want proxy-level compression and do not need on-demand `headroom_compress` or `headroom_retrieve`. - For deep research or custom subagent workflows, reduce unnecessary subagent fan-out first; subagent traffic usually dominates the usage picture before MCP overhead does. LLMs have two fundamental limitations: context windows overflow with too much history, and every conversation starts from zero. Persistent Memory solves both by extracting key facts, persisting them, and injecting them when relevant. This is **temporal compression** -- instead of carrying 10,000 tokens of conversation history, carry 100 tokens of extracted memories. ## Quick Start [#quick-start] ```python from openai import OpenAI from headroom import with_memory # One line -- that's it client = with_memory(OpenAI(), user_id="alice") # Use exactly like normal response = client.chat.completions.create( model="gpt-4o", messages=[{"role": "user", "content": "I prefer Python for backend work"}] ) # Memory extracted INLINE -- zero extra latency # Later, in a new conversation... response = client.chat.completions.create( model="gpt-4o", messages=[{"role": "user", "content": "What language should I use?"}] ) # Response uses the Python preference from memory ``` ## How It Works [#how-it-works] The `with_memory()` wrapper intercepts every chat completion call: 1. **Inject** -- Semantic search finds relevant memories and prepends them to the user message 2. **Instruct** -- Adds a memory extraction instruction to the system prompt 3. **Call** -- Forwards the request to the LLM 4. **Parse** -- Extracts the `` block from the response 5. **Store** -- Saves with embeddings, vector index, and full-text search index 6. **Return** -- Cleans the response (strips the memory block before returning) Memory extraction happens **inline** as part of the LLM response. No extra API calls, no extra latency. ## Proxy Project Scope [#proxy-project-scope] With proxy memory enabled, local `project` mode gives each resolved workspace its own SQLite database at `/memories/projects//memory.db`. Use `--memory-db-path` to choose the base database path. Resolution checks `x-headroom-project-id`, then `x-headroom-cwd`, then `--memory-project-root`, then the client's system-prompt working directory. Claude Code / Codex cwd lines and pi's `...` block are supported. The same directory produces the same project key across these formats; explicit request headers still win. A pi cwd block in ordinary user content does not select a project. With the default `empty` fallback, Anthropic requests with an unresolved project receive neither memory context nor Headroom memory tools, even when the session previously injected those tools. Client-owned tools are left alone. Once scope is restored, the session can replay its memory definitions. Explicit `user` / `global` storage and configured global fallback keep their existing behavior. ## Hierarchical Scoping [#hierarchical-scoping] Memories exist at four scope levels, from broadest to narrowest: | Scope | Persists Across | Use Case | |-------|-----------------|----------| | **User** | All sessions, all time | Long-term preferences, identity | | **Session** | Current session only | Current task context | | **Agent** | Current agent in session | Agent-specific context | | **Turn** | Single turn only | Ephemeral working memory | ```python from openai import OpenAI from headroom import with_memory # Session 1: Morning client1 = with_memory( OpenAI(), user_id="bob", session_id="morning-session", ) response = client1.chat.completions.create( model="gpt-4o", messages=[{"role": "user", "content": "I prefer Go for performance-critical code"}] ) # Memory stored at USER level (persists across sessions) # Session 2: Afternoon (different session, same user) client2 = with_memory( OpenAI(), user_id="bob", session_id="afternoon-session", ) response = client2.chat.completions.create( model="gpt-4o", messages=[{"role": "user", "content": "What language for my new microservice?"}] ) # Recalls Go preference from morning session ``` ## Memory Categories [#memory-categories] There is no dedicated `category` field on `Memory` or parameter on `add()` (`headroom/memory/models.py`, `headroom/memory/core.py`) -- store a free-form category string in the `metadata` dict instead, e.g. `metadata={"category": "fact"}`. These are the conventional category values used in Headroom's own examples for organization and retrieval: | Category | Description | Examples | |----------|-------------|----------| | `"preference"` | Likes, dislikes, preferred approaches | "Prefers Python", "Likes dark mode" | | `"fact"` | Identity, role, constraints | "Works at fintech startup", "Senior engineer" | | `"context"` | Current goals, ongoing tasks | "Migrating to microservices", "Working on auth" | | `"entity"` | Information about entities | "Project Apollo uses React", "Team lead is Sarah" | | `"decision"` | Decisions made | "Chose PostgreSQL over MySQL" | | `"insight"` | Derived insights | "User tends to prefer typed languages" | ## Memory API [#memory-api] The `with_memory()` wrapper exposes a `.memory` attribute for direct access: ```python client = with_memory(OpenAI(), user_id="alice") # Search memories (semantic) results = client.memory.search("python preferences", top_k=5) for memory in results: print(f"{memory.content}") # Add a memory manually client.memory.add( "User is a senior engineer", importance=0.9, ) # Get all memories for this user all_memories = client.memory.get_all() # Clear all memories client.memory.clear() # Get stats # `stats()` currently returns only {"total": N} -- there is no # per-category breakdown (`headroom/memory/wrapper.py:342-348`). stats = client.memory.stats() print(f"Total memories: {stats['total']}") ``` ## Temporal Versioning [#temporal-versioning] When facts change, Headroom creates a **supersession chain** that preserves history: ```python from headroom.memory import HierarchicalMemory, MemoryFilter memory = await HierarchicalMemory.create() # Original fact # `category` is a convention, not a keyword argument -- `add()` has no # `category` param (`headroom/memory/core.py`); store it in `metadata`. orig = await memory.add( content="User works at Google", user_id="alice", metadata={"category": "fact"}, ) # User changes jobs -- supersede the old memory new = await memory.supersede( old_memory_id=orig.id, new_content="User now works at Anthropic", ) # Query current state (excludes superseded by default) current = await memory.query(MemoryFilter( user_id="alice", include_superseded=False, )) # Returns only "User now works at Anthropic" # Get the full chain chain = await memory.get_history(new.id) # [ # Memory(content="User works at Google", is_current=False), # Memory(content="User now works at Anthropic", is_current=True), # ] ``` This gives you an audit trail, the ability to debug why the LLM made certain decisions, and rollback if needed. ## Backends [#backends] ### Embedder Backends [#embedder-backends] ```python from headroom.memory import MemoryConfig, EmbedderBackend # ONNX embeddings (recommended -- fast, free, private; ~30 MB int8-quantized) # Note: EmbedderBackend.LOCAL requires PyTorch (~2 GB); use ONNX instead. config = MemoryConfig( embedder_backend=EmbedderBackend.ONNX, embedder_model="BAAI/bge-small-en-v1.5", ) # OpenAI embeddings (higher quality, costs money) config = MemoryConfig( embedder_backend=EmbedderBackend.OPENAI, openai_api_key="sk-...", embedder_model="text-embedding-3-small", ) # Ollama embeddings (local server, many models) config = MemoryConfig( embedder_backend=EmbedderBackend.OLLAMA, ollama_base_url="http://localhost:11434", embedder_model="nomic-embed-text", ) ``` ### Embedding Runtime / GPU Offload (Apple Silicon) [#embedding-runtime--gpu-offload-apple-silicon] By default the proxy's memory embedder runs on the **ONNX CPU** backend -- fast and dependency-light, but CPU-only. Under sustained load the embedding step can saturate the CPU and make the proxy less responsive. On Apple Silicon you can opt in to running the embedder on the **Apple GPU (MPS)** instead, which offloads that work off the CPU and keeps the proxy responsive. Install the extra and set the env var: ```bash pip install "headroom-ai[pytorch-mps]" # also works as [pytorch_mps] export HEADROOM_EMBEDDER_RUNTIME=pytorch_mps ``` When set, the embedder runs via the torch sentence-transformers backend on the Apple GPU instead of the default ONNX CPU embedder. Notes: - **Strictly opt-in.** `pytorch_mps` is the only accepted value; anything else (or unset) keeps the default ONNX CPU embedder. Default behavior is unchanged, and there is no CLI flag -- it is env-var only. - **Auto-fallback.** It only activates when Apple MPS is actually available (Apple Silicon + torch). If MPS is unavailable or torch/sentence-transformers is not installed, it logs a warning and uses the existing default embedder selection path: ONNX when available, then the pre-existing local sentence-transformers fallback. - **MPS serialization.** torch-MPS is not thread-safe, so the embedder serializes MPS encode calls internally via a single-worker executor. This is automatic -- there is nothing to configure. ### Storage [#storage] Storage uses **SQLite** for CRUD and filtering, **HNSW** for vector similarity search, and **FTS5** for full-text keyword search. All embedded -- no external services required. ```python config = MemoryConfig( db_path="memory.db", vector_dimension=384, hnsw_ef_construction=200, hnsw_m=16, hnsw_ef_search=50, cache_enabled=True, cache_max_size=1000, ) ``` ### Direct Mem0 / Qdrant payload (mem0 2.x) [#direct-mem0--qdrant-payload-mem0-2x] The `qdrant-neo4j` backend (`DirectMem0Adapter`) writes facts straight into Qdrant so Mem0 can skip its extra LLM extraction. mem0ai 2.x search (`Memory.search` → `_search_vector_store`) **skips any point whose payload has no `data` field**. New writes store the fact under both: - `data` — required for mem0 2.x search - `memory` — legacy alias for older payload readers Existing collections written before this contract only have `memory`. Those points still sit in Qdrant, but `search_memories` returns nothing for them until you copy `memory` onto `data`: ```python from qdrant_client import QdrantClient client = QdrantClient(...) # same URL/host as Headroom collection = "headroom_memories" # Mem0Config.collection_name offset = None while True: points, offset = client.scroll( collection_name=collection, with_payload=True, limit=100, offset=offset, ) updates = [] for point in points: payload = point.payload or {} if payload.get("data") or not payload.get("memory"): continue updates.append(point.id) client.set_payload( collection_name=collection, payload={"data": payload["memory"]}, points=[point.id], ) if offset is None: break ``` Run this once per collection after upgrading. New saves already write `data` and do not need a second pass. ## Provider Compatibility [#provider-compatibility] Memory works with any OpenAI-compatible client: ```python from openai import OpenAI from headroom import with_memory # OpenAI client = with_memory(OpenAI(), user_id="alice") # Azure OpenAI client = with_memory( OpenAI(base_url="https://your-resource.openai.azure.com/..."), user_id="alice", ) # Groq from groq import Groq client = with_memory(Groq(), user_id="alice") ``` ## Performance [#performance] | Operation | Latency | Notes | |-----------|---------|-------| | Memory injection | <50ms | Local embeddings + HNSW search | | Memory extraction | +50-100 tokens | Part of LLM response (inline) | | Memory storage | <10ms | SQLite + HNSW + FTS5 indexing | | Cache hit | <1ms | LRU cache lookup | Headroom provides comprehensive metrics for monitoring compression performance, cost savings, and system health through both the proxy server and the SDK. ## Proxy Endpoints [#proxy-endpoints] ### Stats Endpoint [#stats-endpoint] ```bash curl http://localhost:8787/stats ``` ```json { "persistent_savings": { "lifetime": { "tokens_saved": 12500, "compression_savings_usd": 0.04 } }, "requests": { "total": 42, "cached": 5, "rate_limited": 0, "rate_limited_by_source": { "headroom": 0, "upstream": 0 }, "failed": 0, "failed_by_provider": {} }, "tokens": { "input": 50000, "output": 8000, "saved": 12500, "savings_percent": 25.0 }, "cost": { "total_cost_usd": 0.15, "total_savings_usd": 0.04 }, "cache": { "entries": 10, "total_hits": 5 } } ``` `requests.total` is the **completed**-request count, not total traffic: a request that ended in a 4xx, 5xx or 429 is counted under `requests.failed` or `requests.rate_limited` instead and never reaches `total`. `rate_limited_by_source` splits 429s into `headroom` (Headroom's own rate limiter refused the request) and `upstream` (the provider refused it) — different problems, different fixes. `failed_by_provider` attributes failures to the upstream that produced them. The unlabelled `rate_limited` / `failed` totals are unchanged. Persistent savings are stored at `~/.headroom/proxy_savings.json` and survive proxy restarts. Override the path with `HEADROOM_SAVINGS_PATH`. ### Historical Savings [#historical-savings] ```bash curl http://localhost:8787/stats-history ``` Returns durable compression history with hourly, daily, weekly, and monthly rollups. Supports CSV export: ```bash curl "http://localhost:8787/stats-history?format=csv&series=daily" curl "http://localhost:8787/stats-history?format=csv&series=monthly" ``` ### Prometheus Metrics [#prometheus-metrics] ```bash curl http://localhost:8787/metrics ``` ``` # HELP headroom_requests_total Total requests processed headroom_requests_total 1234 # HELP headroom_tokens_saved_total Total tokens saved headroom_tokens_saved_total 5678900 # HELP headroom_persistent_savings_tokens_saved_total Durable lifetime input tokens saved by proxy compression headroom_persistent_savings_tokens_saved_total 5678900 # HELP headroom_latency_ms_sum Sum of request latency in milliseconds headroom_latency_ms_sum 152300 # HELP headroom_latency_ms_count Count of latency samples headroom_latency_ms_count 1234 # HELP headroom_provider_cache_hit_requests_total Requests that read from the provider's prompt cache headroom_provider_cache_hit_requests_total{provider="anthropic"} 456 # HELP headroom_requests_failed_total Requests that failed upstream (4xx and 5xx, excluding 429), by provider headroom_requests_failed_total{provider="anthropic"} 3 # HELP headroom_requests_rate_limited_total Requests rejected with 429, by who rejected them (headroom=our own limiter, upstream=the provider) headroom_requests_rate_limited_total{source="headroom"} 0 headroom_requests_rate_limited_total{source="upstream"} 12 ``` `headroom_requests_total` counts requests that **completed**. A request that ended in a 4xx, 5xx or 429 lands in `headroom_requests_failed_total` / `headroom_requests_rate_limited_total` and is not in `requests_total` — so a failure rate must add `failed` back into its own denominator. `headroom_inbound_requests_total` is the all-traffic counter. `headroom_requests_failed_total` gained a `provider` label and `headroom_requests_rate_limited_total` a `source` label, so unaggregated queries on them now return several series. `sum without (provider) (...)` and `sum without (source) (...)` reproduce the previous single-series behaviour. There are no Prometheus histogram buckets for compression ratio or latency — build percentiles or averages from `_sum` / `_count` pairs (`headroom_latency_ms_sum`, `headroom_latency_ms_count`, and so on), or use the `headroom perf` CLI for real p95/p99. ### OpenTelemetry (OTLP) Export [#opentelemetry-otlp-export] The proxy can also push its counters to any OTLP/HTTP endpoint. Install the extra and set four variables: ```bash pip install "headroom-ai[proxy,otel]" ``` ```bash HEADROOM_OTEL_METRICS_ENABLED=1 HEADROOM_OTEL_METRICS_ENDPOINT=http://127.0.0.1:4318/v1/metrics HEADROOM_OTEL_SERVICE_NAME=headroom-proxy HEADROOM_OTEL_RESOURCE_ATTRIBUTES=deployment.environment=prod ``` | Variable | Default | Purpose | | --- | --- | --- | | `HEADROOM_OTEL_METRICS_ENABLED` | `0` | Enable Headroom-managed OTLP metric export | | `HEADROOM_OTEL_METRICS_EXPORTER` | `otlp_http` | `otlp_http` or `console` (local debugging) | | `HEADROOM_OTEL_METRICS_ENDPOINT` | unset | Full OTLP metrics URL — Headroom does **not** append `/v1/metrics` for you | | `HEADROOM_OTEL_METRICS_HEADERS` | unset | Comma-separated `key=value` auth headers | | `HEADROOM_OTEL_METRICS_EXPORT_INTERVAL_MS` | `10000` | Export interval | | `HEADROOM_OTEL_SERVICE_NAME` | `headroom-proxy` | OTEL `service.name` | | `HEADROOM_OTEL_RESOURCE_ATTRIBUTES` | unset | Comma-separated resource attributes | Exported counters include `headroom.proxy.requests`, `headroom.proxy.tokens.input`, and `headroom.proxy.tokens.output`. `headroom.proxy.tokens.saved` is the all-layer total: message/compression savings plus tool-schema deferral savings. The component counter `headroom.proxy.tokens.tool_schema_saved` exposes the deferral portion separately; `headroom.compression.tokens.saved` remains the compression-pipeline component. Confirm the exporter is live with `curl -s http://localhost:8787/stats | jq .otel`. If your application already configures a global OTEL meter provider, leave `HEADROOM_OTEL_*` unset — Headroom records into the ambient provider automatically. ### Dynatrace [#dynatrace] Point the exporter at your environment's OTLP API and add the API token as a header. The token needs the `metrics.ingest` scope. ```bash HEADROOM_OTEL_METRICS_ENABLED=1 HEADROOM_OTEL_METRICS_ENDPOINT="https://.live.dynatrace.com/api/v2/otlp/v1/metrics" HEADROOM_OTEL_METRICS_HEADERS="Authorization=Api-Token dt0c01.XXXX" OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE=DELTA HEADROOM_OTEL_SERVICE_NAME=headroom-proxy ``` `OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE=DELTA` is required. Dynatrace [only ingests delta counters](https://docs.dynatrace.com/docs/ingest-from/opentelemetry/getting-started/metrics/limitations) and rejects cumulative ones with `UNSUPPORTED_METRIC_TYPE_MONOTONIC_CUMULATIVE_SUM`, while the OTEL SDK default is cumulative. Without this line, every Headroom metric is dropped at ingest and the proxy logs no error. Restart the proxy, then search the Dynatrace metric explorer for `headroom.proxy.tokens.saved` — data appears within ~30s. For an ActiveGate deployment, swap the base URL for `https://:9999/e//api/v2/otlp/v1/metrics`. If you already run an OpenTelemetry Collector, send Headroom to it instead and add the `cumulativetodelta` processor — then the temporality variable is unnecessary and the collector holds the token. Trace export is separate: Headroom's self-configured tracing targets Langfuse only. To land its spans in Dynatrace, leave `HEADROOM_LANGFUSE_*` unset and run the proxy under `opentelemetry-instrument` with the standard `OTEL_EXPORTER_OTLP_ENDPOINT` / `OTEL_EXPORTER_OTLP_HEADERS` / `OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf` variables; Headroom records into the ambient tracer provider. ### Health Check [#health-check] ```bash curl http://localhost:8787/health ``` ```json { "status": "healthy", "version": "0.37.0", "uptime_seconds": 3600 } ``` ## SDK Metrics [#sdk-metrics] ### Proxy Stats [#proxy-stats] The TypeScript SDK queries the proxy for stats: ```ts twoslash import { HeadroomClient } from 'headroom-ai'; const client = new HeadroomClient(); // Get proxy stats const stats = await client.proxyStats(); console.log(`Tokens saved: ${stats.tokens.saved}`); console.log(`Savings: ${stats.tokens.savingsPercent}%`); ``` ### Compression Result Metrics [#compression-result-metrics] Every `compress()` call returns metrics: ```ts twoslash import { compress } from 'headroom-ai'; const result = await compress(messages, { model: 'gpt-4o' }); console.log(`Tokens: ${result.tokensBefore} -> ${result.tokensAfter}`); console.log(`Saved: ${result.tokensSaved} (${(result.compressionRatio * 100).toFixed(1)}%)`); console.log(`Transforms: ${result.transformsApplied.join(', ')}`); ``` ### Session Stats [#session-stats] Quick stats for the current session (no database query): ```python stats = client.get_stats() print(f"Mode: {stats['config']['mode']}") print(f"Tokens saved: {stats['session']['tokens_saved_total']}") print(f"Requests optimized: {stats['session']['requests_optimized']}") ``` Returns: ```python { "session": { "requests_total": 10, "requests_optimized": 8, "requests_audit": 2, "tokens_saved_total": 15000, "cache_hits": 3, }, "config": { "mode": "optimize", "provider": "openai", "cache_optimizer": "openai-prefix-stabilizer", "semantic_cache": False, }, "transforms": { "smart_crusher_enabled": True, "cache_aligner_enabled": True, }, } ``` ### Historical Metrics [#historical-metrics] Query stored metrics from the database: ```python from datetime import datetime, timedelta metrics = client.get_metrics( start_time=datetime.utcnow() - timedelta(hours=1), limit=100, ) for m in metrics: print(f"{m.timestamp}: {m.tokens_input_before} -> {m.tokens_input_after}") ``` ### Summary Statistics [#summary-statistics] Aggregate statistics across all stored metrics: ```python summary = client.get_summary() print(f"Total requests: {summary['total_requests']}") print(f"Total tokens saved: {summary['total_tokens_saved']}") print(f"Avg tokens saved per request: {summary['avg_tokens_saved']:.0f}") ``` ## Logging [#logging] ```python import logging # INFO level shows compression summaries logging.basicConfig(level=logging.INFO) # DEBUG level shows detailed transform decisions logging.basicConfig(level=logging.DEBUG) ``` Example output: ``` INFO:headroom.transforms.pipeline:Pipeline complete: 45000 -> 4500 tokens (saved 40500, 90.0% reduction) INFO:headroom.transforms.smart_crusher:SmartCrusher applied top_n strategy: kept 15 of 1000 items DEBUG:headroom.transforms.smart_crusher:Kept items: [0,1,2,42,77,97,98,99] (errors at 42, warnings at 77) ``` ```bash # Log to file headroom proxy --log-file headroom.jsonl # Increase verbosity (no --log-level flag; use the env var) HEADROOM_LOG_LEVEL=debug headroom proxy ``` ## Cost Tracking [#cost-tracking] ### Budget Alerts [#budget-alerts] Set a budget limit in the proxy: ```bash headroom proxy --budget 10.00 ``` When the budget is exceeded, requests return a budget exceeded error, the `/stats` endpoint shows budget status, and logs indicate the budget state. ### Measured vs Estimated Spend [#measured-vs-estimated-spend] Every cost record carries a *basis* — where its input-token count came from. When a provider response includes a usage breakdown, the basis is `measured`. When it doesn't, Headroom substitutes its own `tokens_sent` count so input cost isn't dropped from the budget, and the record's basis is `estimated`. Headroom logs one warning per model the first time this happens. `/stats` keeps the two separable under `cost.budget_basis`: ```json { "cost": { "budget_limit_usd": 10.0, "budget_period": "daily", "budget_estimated_basis": "count", "budget_basis": { "total_usd": 3.1400, "measured_usd": 2.9000, "estimated_usd": 0.2400, "estimated_pct": 7.6, "records": 412, "estimated_records": 31 } } } ``` An estimate can drift in either direction, so you choose what it does to the hard limit: ```bash headroom proxy --budget 10.00 --budget-estimated-basis count # default ``` | Value | Effect | |-------|--------| | `count` | Estimated spend consumes the budget like measured spend. The default; matches historical behavior. | | `ignore` | Estimated spend is still booked and reported, but only provider-reported spend consumes the budget. | | `block` | Refuse requests once the period holds any estimated spend, rather than enforcing a hard limit against a guess. | Env: `HEADROOM_BUDGET_ESTIMATED_BASIS`. `headroom doctor` reports the estimated share alongside the budget check. ## Key Metrics to Monitor [#key-metrics-to-monitor] | Metric | What It Tells You | Target | |--------|-------------------|--------| | `headroom_tokens_saved_total` | Runtime tokens saved since this proxy process started | Higher is better | | `headroom_persistent_savings_tokens_saved_total` | Durable lifetime tokens saved from `/stats.persistent_savings` | Higher is better | | `headroom_overhead_ms_sum` / `headroom_overhead_ms_count` | Mean latency Headroom itself adds (there are no percentile buckets — see the callout above) | Low is better | | `headroom_provider_cache_hit_requests_total` / `headroom_provider_cache_requests_total` | Cache effectiveness, by provider | >20% is good | | `headroom_requests_failed_total{provider}` | Reliability — upstream **4xx and 5xx** (429s are counted separately), attributed to the provider that failed | 0 | | `headroom_requests_rate_limited_total{source}` | Throttling — `source="headroom"` is Headroom's own rate limiter, `source="upstream"` is the provider's | 0 | ## Grafana Dashboard [#grafana-dashboard] A dashboard template ships in [`examples/grafana/headroom-dashboard.json`](https://github.com/headroomlabs-ai/headroom/blob/main/examples/grafana/headroom-dashboard.json). Its metric names match `/metrics`, but every panel query filters on a `pool` label (e.g. `headroom_tokens_saved_total{pool=~"..."}`) that no metric in `headroom/proxy/prometheus_metrics.py` actually emits, so the panel dropdowns come up empty on import. Edit the queries to drop the `pool=~"..."` filter, or use the ad-hoc queries below instead. Example ad-hoc queries against the same metric family: | Panel | PromQL | |-------|--------| | Runtime Tokens Saved | `headroom_tokens_saved_total` | | Lifetime Tokens Saved | `headroom_persistent_savings_tokens_saved_total` | | Mean Headroom Overhead | `rate(headroom_overhead_ms_sum[5m]) / rate(headroom_overhead_ms_count[5m])` | | Cache Hit Rate (by provider) | `sum by (provider) (rate(headroom_provider_cache_hit_requests_total[5m])) / sum by (provider) (rate(headroom_provider_cache_requests_total[5m]))` | Enterprise AI gateways (Azure AD / Entra, Okta, Auth0, Keycloak, Cognito, …) often protect their OpenAI-compatible endpoints with an OAuth2 client-credentials flow. The [`headroom-oauth2`](https://github.com/headroomlabs-ai/headroom/tree/main/plugins/headroom-oauth2) extension mints a bearer token from a configurable token endpoint, caches and refreshes it (single-flight), and injects `Authorization: Bearer ` on each upstream request. It is fully vendor-neutral — no provider is hard-coded. It plugs into Headroom's public `headroom.proxy_extension` entry-point seam, so it is fully out-of-tree and opt-in. ## Install & enable [#install--enable] ```bash pip install headroom-oauth2 headroom proxy --backend litellm-openai --proxy-extension oauth2 ``` ## Configuration [#configuration] Environment variables (no-op unless `HEADROOM_OAUTH2_TOKEN_URL` is set): | Env | Meaning | |-----|---------| | `HEADROOM_OAUTH2_TOKEN_URL` | token endpoint (client_credentials grant) | | `HEADROOM_OAUTH2_CLIENT_ID` / `_CLIENT_SECRET` | credentials | | `HEADROOM_OAUTH2_SCOPES` | space/comma-separated scopes | | `HEADROOM_OAUTH2_AUDIENCE` | optional audience | | `HEADROOM_OAUTH2_GRANT_TYPE` | default `client_credentials` | | `HEADROOM_OAUTH2_AUTH_STYLE` | `post` (form creds) or `basic` (HTTP Basic) | | `HEADROOM_OAUTH2_HEADERS` | static upstream headers, `K=V,K2=V2` | ## Notes [#notes] - **Effective backends**: the injected bearer reaches the upstream only for OpenAI-compatible / passthrough litellm providers. `bedrock` / `vertex` / `sagemaker` authenticate from env and ignore it — the extension is a no-op there (it logs a warning at startup). - **Transport**: `token_url` must be `https` (loopback `http` allowed for tests; set `HEADROOM_OAUTH2_ALLOW_INSECURE=1` to override). - **Startup**: an invalid config makes the proxy refuse to start rather than run without the middleware and relay the client's own `Authorization` upstream. - **TLS**: tokens are minted with the standard library (`urllib`, system cert store), so a corporate-injected CA is trusted without bundling roots — this works behind corporate SSL-inspection where bundled-root TLS stacks fail. Headroom wraps the OpenAI Node.js SDK to automatically compress messages before every `chat.completions.create()` call. All other methods (embeddings, images, audio) pass through unchanged. ## Installation [#installation] ```bash npm install headroom-ai openai ``` The TypeScript SDK sends messages to a local Headroom proxy for compression. Start the proxy before using the SDK: ```bash pip install "headroom-ai[proxy]" headroom proxy ``` ## Quick start [#quick-start] ```ts twoslash import { withHeadroom } from 'headroom-ai/openai'; import OpenAI from 'openai'; const client = withHeadroom(new OpenAI()); // Messages are compressed automatically before sending const response = await client.chat.completions.create({ model: 'gpt-4o', messages: longConversation, }); ``` That's it. Every call to `client.chat.completions.create()` compresses the messages first. The response format is identical to the unwrapped client. ## How it works [#how-it-works] `withHeadroom()` returns a proxy around your OpenAI client that intercepts `chat.completions.create()`: 1. Extracts `messages` from the request params 2. Sends them to the Headroom proxy's [`POST /v1/compress`](/docs/proxy#post-v1compress) endpoint 3. Replaces the original messages with the compressed result 4. Forwards the request to OpenAI as normal The SDK talks to a **local** proxy, which is why no extra configuration is needed: `/v1/compress` is loopback-only by default (in container environments, host gateway traffic is admitted automatically). If you move the proxy to another host, set `HEADROOM_COMPRESS_ALLOW_REMOTE=1` on it or requests come back `404`. All other client methods are untouched: ```ts twoslash import { withHeadroom } from 'headroom-ai/openai'; import OpenAI from 'openai'; const client = withHeadroom(new OpenAI()); // These pass through unchanged const embedding = await client.embeddings.create({ model: 'text-embedding-3-small', input: 'Hello world', }); ``` ## Options [#options] Pass compression options as the second argument: ```ts twoslash import { withHeadroom } from 'headroom-ai/openai'; import OpenAI from 'openai'; const client = withHeadroom(new OpenAI(), { model: 'gpt-4o', baseUrl: 'http://localhost:8787', }); ``` ## Streaming [#streaming] Streaming works normally. Compression happens before the request is sent: ```ts twoslash import { withHeadroom } from 'headroom-ai/openai'; import OpenAI from 'openai'; const client = withHeadroom(new OpenAI()); const stream = await client.chat.completions.create({ model: 'gpt-4o', messages: longConversation, stream: true, }); for await (const chunk of stream) { process.stdout.write(chunk.choices[0]?.delta?.content ?? ''); } ``` ## Tool calling [#tool-calling] Tool call messages and tool results are compressed like any other message content. Large tool outputs (JSON arrays, logs) see the biggest savings: ```ts twoslash import { withHeadroom } from 'headroom-ai/openai'; import OpenAI from 'openai'; const client = withHeadroom(new OpenAI()); const response = await client.chat.completions.create({ model: 'gpt-4o', messages: [ { role: 'user', content: 'Search for recent errors' }, { role: 'assistant', content: null, tool_calls: [{ id: 'call_1', type: 'function', function: { name: 'search', arguments: '{"q":"errors"}' } }], }, { role: 'tool', tool_call_id: 'call_1', content: hugeJsonResult, // Compressed automatically }, ], tools: [{ type: 'function', function: { name: 'search', parameters: {} } }], }); ``` The bundled [`plugins/openclaw`](https://github.com/headroomlabs-ai/headroom/tree/main/plugins/openclaw) plugin (`@headroom-ai/openclaw`) brings Headroom compression to [OpenClaw](https://github.com/openclaw/openclaw): tool outputs, code, logs, and structured data — 70-90% token savings with zero LLM calls. ## Install [#install] Recommended one-command setup: ```bash headroom wrap openclaw ``` Manual install: ```bash pip install "headroom-ai[proxy]" openclaw plugins install --dangerously-force-unsafe-install headroom-ai/openclaw ``` > The plugin can auto-start a local `headroom proxy` when needed. Auto-start is opt-in via `autoStart` (default `false` — see [Configure](#configure)). OpenClaw treats process-launching plugins as unsafe by default, so `--dangerously-force-unsafe-install` is required even if you plan to use a remote proxy — the capability is declared at install time. ## Configure [#configure] Install automatically selects the `contextEngine` slot for `headroom` on current OpenClaw releases. To switch back manually, set `plugins.slots.contextEngine` to `"legacy"` or another engine id. Key options (exposed as plugin UI hints): | Option | Default | Meaning | |--------|---------|---------| | `proxyUrl` | auto-detected | Optional proxy URL (e.g. `http://127.0.0.1:8787` or remote). Probe-gated before provider routing. Remote URLs are connect-only: probed at startup, no subprocess spawned. | | `proxyPort` | `8787` | Default port for auto-detect/auto-start when `proxyUrl` is unset. | | `pythonPath` | auto-detected | Optional explicit python executable for the python fallback launcher. | | `autoStart` | `false` | Opt-in auto-start of a local `headroom proxy` if none is detected (local URLs only; ignored for remote proxies). Keep `false` when systemd or another supervisor owns the proxy. | | `startupTimeoutMs` | `20000` | Time to wait for an auto-started proxy to become healthy. | | `retryMaxAttempts` | unset | Max upstream retry attempts (connection/read/5xx) with an auto-started local proxy. | | `connectTimeoutSeconds` | unset | Upstream connection timeout for the auto-started proxy. | | `requestTimeoutMs` | `30000` | Max wait for one compression request; on timeout the call is cancelled and the original uncompressed messages are used as a fallback. | | `circuitBreakerThreshold` | `3` | Consecutive `assemble()` errors before the circuit breaker opens and all requests bypass the proxy. | | `circuitBreakerCooldownMs` | `60000` | How long the circuit breaker stays open; after the cooldown it resets and the next request re-probes the proxy via `/health`. | | `routeCodexViaProxy` | `true` | Rewrite OpenClaw's built-in `openai-codex` provider base URL to the active proxy in memory (see below). | | `gatewayProviderIds` | `[]` | Explicit list of OpenClaw provider ids to route through the proxy. Aliases `codex`, `claude`, `copilot`, `gemini` are accepted; when set, it overrides the default `openai-codex` routing list. | ### Upstream gateway routing [#upstream-gateway-routing] By default (`routeCodexViaProxy: true`) the plugin rewrites the built-in `openai-codex` provider base URL to a verified active proxy at runtime, in memory — so Codex provider traffic flows through Headroom and `/stats` can observe real upstream request and cache activity instead of only local context compression. This does not replace the proxy's own Codex routing rules (it still decides between `api.openai.com` and `chatgpt.com/backend-api/codex/responses` based on ChatGPT auth); the plugin only points the provider config at the active proxy and preserves the rest. To route additional providers through the same proxy, set `gatewayProviderIds`: ```json { "plugins": { "entries": { "headroom": { "enabled": true, "config": { "gatewayProviderIds": ["openai-codex", "anthropic", "github-copilot", "google", "openrouter"] } } } } } ``` The rewrite is intentionally lightweight and reversible: provider `baseUrl` changes are never persisted back to `openclaw.json`, and disabling the plugin, clearing `gatewayProviderIds`, or restarting without Headroom restores normal provider resolution. Set `routeCodexViaProxy: false` to disable the default Codex routing; for durable provider rewrites use `headroom wrap openclaw` instead of relying on plugin install side effects. ## Local development install [#local-development-install] When testing from the repo, build from the plugin directory so launcher detection aligns with runtime paths: ```bash cd plugins/openclaw npm install npm run build openclaw plugins install --dangerously-force-unsafe-install --link . ``` The plugin checks launchers in this order: **PATH → local npm bin → global npm → python**, where "local npm bin" means `plugins/openclaw/node_modules/.bin/headroom` relative to the source checkout. `plugins/openclaw` also carries a no-op hook shim so OpenClaw's hook-pack fallback treats the path as valid instead of emitting a misleading `package.json missing openclaw.hooks` warning. Cut DeepSeek API costs with Headroom's context compression proxy: tool outputs, logs, and search results are compressed before they reach the model, and responses can be shaped to be more concise. ## How it works [#how-it-works] ``` OpenCode → Headroom Proxy (:8787) → DeepSeek API ↑ compresses input + shapes output ``` The proxy sits between OpenCode and DeepSeek. It compresses tool outputs, logs, and search results before they reach the model, then shapes responses to be concise. DeepSeek's API is OpenAI-compatible — one flag and you're running. --- ## 1. Install Headroom [#1-install-headroom] ```bash pip install "headroom-ai[proxy]" # or via uv: uv tool install "headroom-ai[proxy]" ``` You get SmartCrusher (structural compression), the proxy, output shaping, and the MCP server — everything you need. --- ## 2. Get your DeepSeek API key [#2-get-your-deepseek-api-key] Sign up at [platform.deepseek.com](https://platform.deepseek.com) and generate an API key. Store it somewhere safe: ```bash export DEEPSEEK_API_KEY="sk-your-deepseek-key-here" ``` --- ## 3. Start the proxy [#3-start-the-proxy] ```bash headroom proxy \ --port 8787 \ --openai-api-url https://api.deepseek.com/v1 ``` The proxy auto-detects `api.deepseek.com` and labels itself "DeepSeek" on the dashboard. Verify it's running: ```bash curl http://127.0.0.1:8787/health # → "status": "healthy" ``` To see which models the proxy exposes: ```bash curl -s http://127.0.0.1:8787/v1/models \ -H "Authorization: Bearer sk-your-key" | jq '.data[].id' ``` ### With output shaping (optional) [#with-output-shaping-optional] Output shaping makes the model's responses shorter — fewer tokens, lower cost: ```bash HEADROOM_OUTPUT_SHAPER=1 HEADROOM_VERBOSITY_LEVEL=2 \ headroom proxy --port 8787 --openai-api-url https://api.deepseek.com/v1 ``` Verbosity levels: | Level | Behavior | |---|---| | `1` | Skip preambles/postambles | | `2` | + Don't restate code/file content already in context (**recommended**) | | `3` | + Omit rationale unless asked | | `4` | Maximum — fragments, zero fluff | --- ## 4. Configure OpenCode [#4-configure-opencode] **Note:** If you have an existing `~/.config/opencode/opencode.json` (for MCP servers, etc.), merge the provider section into that file. Having both `.json` and `.jsonc` in the same directory can cause conflicts. Edit `~/.config/opencode/opencode.json`: ```jsonc { "$schema": "https://opencode.ai/config.json", "model": "headroom/deepseek-v4-pro", "provider": { "headroom": { "npm": "@ai-sdk/openai-compatible", "name": "Headroom Proxy", "options": { "baseURL": "http://127.0.0.1:8787/v1", "apiKey": "sk-your-deepseek-key" }, "models": { "deepseek-v4-pro": { "name": "DeepSeek V4 Pro", "limit": { "context": 1000000, "output": 384000 } }, "deepseek-v4-flash": { "name": "DeepSeek V4 Flash", "limit": { "context": 1000000, "output": 384000 } } } } }, "mcp": { "headroom": { "type": "local", "command": ["headroom", "mcp", "serve"], "enabled": true } } } ``` **Important:** Only include model IDs that appear in the proxy's `/v1/models` response. OpenCode validates config models against the proxy's model list. The current DeepSeek model names are `deepseek-flash` and `deepseek-v4-pro`. The retired `deepseek-v4-flash` and `deepseek-v4-flash-vision-exp` are still accepted and billed at the Flash price; `deepseek-chat` and `deepseek-reasoner` are deprecated compatibility aliases. ### Model comparison [#model-comparison] All prices are USD per 1M tokens, at DeepSeek's **off-peak** tier. Peak hours — Beijing 09:00-12:00 and 14:00-18:00, i.e. 01:00-04:00 and 06:00-10:00 UTC — cost exactly twice these rates. | Model | Input / Output (per 1M, off-peak) | Context | Max Output | Vision | |---|---|---|---|---| | `deepseek-flash` (V4.1-Flash) | $0.15 / $0.60 | 1M | 384K | yes | | `deepseek-v4-pro` | $0.66 / $1.98 | 1M | 384K | no | Both models support **thinking mode** for step-by-step reasoning (see below). Switch models at any time with `/model` in OpenCode. --- ## 5. Start OpenCode [#5-start-opencode] ```bash opencode ``` Run `/models` to confirm both DeepSeek models appear under "Headroom Proxy". Select one with `/model deepseek-flash` or `/model deepseek-v4-pro`. --- ## 6. Check savings [#6-check-savings] ```bash curl http://127.0.0.1:8787/stats | python3 -m json.tool | grep -A5 compression ``` Or open the dashboard at [http://127.0.0.1:8787/dashboard](http://127.0.0.1:8787/dashboard). --- ## Thinking mode (reasoning) [#thinking-mode-reasoning] Both models support thinking mode natively, and DeepSeek enables it by default. This replaces the deprecated `deepseek-reasoner` (R1) model. See [DeepSeek's thinking mode docs](https://api-docs.deepseek.com/guides/thinking_mode) for details on switching between thinking and non-thinking modes. --- ## Common issues [#common-issues] ### "Authentication Fails" / Unauthorized [#authentication-fails--unauthorized] The `apiKey` in OpenCode's config is missing or wrong. OpenCode must send the API key to the proxy, and the proxy forwards it to DeepSeek. Make sure `"apiKey": "sk-..."` is set under `options`. ### Models don't appear under "Headroom Proxy" [#models-dont-appear-under-headroom-proxy] 1. Verify the proxy is running: `curl http://127.0.0.1:8787/health` 2. Check which models the proxy exposes: `curl -s http://127.0.0.1:8787/v1/models -H "Authorization: Bearer sk-your-key"` 3. Make sure your config model IDs match **exactly** what the proxy returns 4. Don't use both `opencode.json` and `opencode.jsonc` in the same config directory — use one file ### Models appear but requests fail [#models-appear-but-requests-fail] You ran `headroom wrap opencode` without telling it about DeepSeek. Without an explicit upstream the proxy it starts forwards OpenAI-compatible traffic to OpenAI, so wrap leaves the `apiKey` and `models` entries from step 4 out of the `headroom` provider rather than send your DeepSeek key there. Pass `--openai-api-url` as shown in the next section, or launch OpenCode directly with `opencode` against the proxy you started in step 3. ### I used `headroom wrap opencode` and get "Incorrect API key provided" [#i-used-headroom-wrap-opencode-and-get-incorrect-api-key-provided] ``` Incorrect API key provided: sk-13005***********************898d. You can find your API key at https://platform.openai.com/account/api-keys. ``` That 401 comes from **OpenAI**, not DeepSeek. `headroom wrap opencode` starts the proxy for you, and unless it is told otherwise the proxy forwards OpenAI-compatible traffic to its default upstream, `https://api.openai.com/v1`. Your DeepSeek key is then presented to OpenAI, which rejects it and points you at a dashboard for a key you never had. Pass the upstream to wrap, exactly like `headroom proxy --openai-api-url`: ```bash headroom wrap opencode --openai-api-url https://api.deepseek.com/v1 ``` The equivalent environment variable also works, and is honored by both commands: ```bash OPENAI_TARGET_API_URL=https://api.deepseek.com/v1 headroom wrap opencode ``` If a proxy is already running on the port with a different upstream, stop it first (`headroom unwrap opencode`) — wrap will not silently re-point a proxy that other clients may be sharing. ### When is `headroom wrap opencode` safe? [#when-is-headroom-wrap-opencode-safe] - **Safe as-is** when your traffic goes to the default upstreams — OpenAI (`api.openai.com`) or Anthropic (`api.anthropic.com`). - **Needs `--openai-api-url`** (or `OPENAI_TARGET_API_URL`) for any third-party OpenAI-compatible provider: DeepSeek, Together, OpenRouter, Groq, a self-hosted gateway, and so on. - **Keep the step 4 provider entries.** OpenCode only offers the `headroom/` models listed under `provider.headroom.models`, and DeepSeek needs your key in `options.apiKey` (for example `"{env:DEEPSEEK_API_KEY}"`). With `--openai-api-url`, wrap keeps both. It rewrites the provider's `npm`, `name` and `baseURL` and adds its default `gpt-4o` and `gpt-4.1` ids next to your models. Without the flag it leaves your key and models out, because the proxy would forward them to OpenAI. ### "headroom" command not found [#headroom-command-not-found] `uv tool install` puts binaries in `~/.local/bin/`. Add it to your PATH: ```bash export PATH="$HOME/.local/bin:$PATH" ``` ### Output shaping shows no savings [#output-shaping-shows-no-savings] Output savings are measured against a learned baseline (it compares "what the model actually emitted" vs "what it would have emitted unshaped"). After a few sessions, run: ```bash headroom learn --verbosity --apply ``` This builds the baseline, and `/stats` will show output savings numbers. The shaper is active immediately — the numbers just need calibration. --- ## What's NOT in this guide [#whats-not-in-this-guide] - **Claude or GPT models** — this setup uses DeepSeek exclusively - **Deprecated model names** — `deepseek-chat` and `deepseek-reasoner` are compatibility aliases slated for removal; use `deepseek-v4-pro` and `deepseek-v4-flash` instead - **Kompress (ML compression)** — requires extra dependencies; SmartCrusher handles the majority of use cases - **Any code changes** — headroom ships full DeepSeek support natively (model tables, pricing, tokenizers, domain detection) Use `headroom wrap opencode` to route OpenCode LLM traffic through the Headroom proxy with a single command. The wrapper starts or reuses the proxy, writes OpenCode config, injects Headroom MCP tools, and launches OpenCode with the generated config. The `headroom-opencode` npm package also exports a native OpenCode plugin. The plugin can be used directly from OpenCode config when you want in-process transport interception plus the Headroom retrieve tool. ## Quick Start [#quick-start] ```bash headroom wrap opencode ``` When you are done: ```bash headroom unwrap opencode ``` ## What `wrap opencode` Does [#what-wrap-opencode-does] | Step | What happens | |---|---| | Proxy | Starts the Headroom proxy unless `--no-proxy` is set | | Provider injection | Writes a `headroom` provider using `@ai-sdk/openai-compatible` into `opencode.json`, pointing at `http://127.0.0.1:/v1` | | Runtime env | Sets `OPENCODE_CONFIG_CONTENT` with provider, plugin, and optional local MCP config so OpenCode picks up Headroom at launch | | Provider compatibility | Leaves `OPENAI_BASE_URL` and `ANTHROPIC_BASE_URL` untouched so OpenCode `/connect` providers keep their own routing | | MCP setup | Registers the Headroom MCP server (`headroom_compress`, `headroom_retrieve`, `headroom_stats`) | | Serena MCP | Off by default; `--code-memory serena` registers Serena code navigation tools | | Backup | Snapshots `opencode.json` to `opencode.json.headroom-backup` before making any changes | | Launch | Starts the `opencode` binary through the proxy | ## Options [#options] ```bash headroom wrap opencode \ --port 8787 \ --no-mcp \ --code-memory serena \ --code-graph \ --no-proxy \ --openai-api-url https://api.deepseek.com/v1 \ --learn \ --memory \ --backend anthropic \ --anyllm-provider ... \ --region ... \ -- ``` Pass `--openai-api-url` when OpenCode should route through a third-party OpenAI-compatible provider instead of OpenAI itself — DeepSeek, Together, OpenRouter, or a self-hosted gateway. Without it, OpenAI-compatible traffic is forwarded to the default upstream (`https://api.openai.com/v1`), so a non-OpenAI key is rejected with `401 Incorrect API key provided`. Setting `OPENAI_TARGET_API_URL` before wrapping has the same effect. See [OpenCode + DeepSeek](/docs/opencode-deepseek) for a full walkthrough. The flag means the same thing whether or not wrap starts the proxy: ```bash # wrap starts the proxy and points it at DeepSeek headroom wrap opencode --openai-api-url https://api.deepseek.com/v1 # reuse a proxy you started yourself — it must already target that upstream headroom proxy --port 8787 --openai-api-url https://api.deepseek.com/v1 headroom wrap opencode --no-proxy --openai-api-url https://api.deepseek.com/v1 ``` With `--no-proxy`, wrap never starts or re-points a proxy. It checks the one listening on `--port` and exits with an error — before it rewrites `opencode.json` — if nothing is listening, the listener is not a Headroom proxy, or it forwards OpenAI-compatible traffic to a different upstream. Start the proxy with the upstream you want, or drop `--no-proxy` and let wrap start it. ## Provider Model Mapping [#provider-model-mapping] The generated `headroom` provider exposes these models through the proxy: | Provider model | Upstream model | |---|---| | `headroom/claude-sonnet-4-6` | Claude Sonnet 4.6, 200K context, 16K output | | `headroom/claude-opus-4-6` | Claude Opus 4.6, 200K context, 16K output | | `headroom/claude-haiku-4-5-20251001` | Claude Haiku 4.5, 200K context, 8K output | | `headroom/gpt-4o` | GPT-4o, 128K context, 16K output | | `headroom/gpt-4.1` | GPT-4.1, 1M context, 32K output | The default model is `headroom/claude-sonnet-4-6`. Change it in `opencode.json` or in the generated `OPENCODE_CONFIG_CONTENT` payload. ## Environment Variables [#environment-variables] | Variable | Description | |---|---| | `OPENCODE_CONFIG_CONTENT` | JSON payload with provider, plugin, and optional local MCP config injected by `wrap` | | `HEADROOM_PROXY_URL` | Proxy URL passed to Headroom MCP when a non-default port is used, and to the native plugin when configured | | `OPENAI_TARGET_API_URL` | Upstream for OpenAI-compatible traffic when `--openai-api-url` is not passed; defaults to `https://api.openai.com/v1` | | `HEADROOM_OPENCODE_EXCLUDE_HOSTS` | Comma-separated hosts (and their subdomains) the native plugin sends straight upstream instead of through the proxy, for example `opencode.ai,.corp.internal` | ## Failure Learning [#failure-learning] `headroom learn` supports OpenCode as a scan target. It reads past sessions from the newer of `~/.local/share/opencode/opencode-local.db` and `~/.local/share/opencode/opencode.db`, or from `HEADROOM_OPENCODE_DB` when you set an explicit override, and writes corrections to your project's `AGENTS.md`. ```bash headroom learn --agent opencode --apply ``` See [Failure Learning](/docs/failure-learning) for details on the learn system. ## Persistent Installs [#persistent-installs] `headroom install` supports OpenCode as a target for persistent provider wiring. Use provider scope when you want Headroom to edit `opencode.json` directly: ```bash headroom install apply --preset persistent-service --scope provider --providers manual --target opencode ``` This writes the Headroom provider into `~/.config/opencode/opencode.json` and keeps the proxy running on port 8787. The default user scope only writes shell environment configuration. For OpenCode, direct provider config requires `--scope provider`. ## Native OpenCode Plugin [#native-opencode-plugin] The `headroom-opencode` package's default export is a native OpenCode plugin that works with both OpenCode 1.x and 2.x. It installs Headroom transport interception inside OpenCode, exposes the `headroom_retrieve` tool, and adds Headroom metadata (`HEADROOM_ACTIVE`, `HEADROOM_PROXY_URL`, `HEADROOM_PROJECT`) to the environment of shell commands OpenCode runs. Register it in `opencode.json`: ```json { "plugin": [ ["headroom-opencode", { "proxyUrl": "http://127.0.0.1:8787" }] ] } ``` OpenCode 2.x migrates the 1.x `plugin` key automatically. To configure it the 2.x way, use `"plugins": [{ "package": "headroom-opencode", "options": { "proxyUrl": "http://127.0.0.1:8787" } }]`. Without `proxyUrl`, the plugin reads `HEADROOM_PROXY_URL` and falls back to `http://127.0.0.1:8787`. The default export has `server` for OpenCode 1.x and `setup` for OpenCode 2.x. Each version ignores the other's field. `HeadroomPlugin` (1.x) and `headroomSetup` (2.x) are also exported by name for custom wrappers. Use this plugin when OpenCode should intercept provider traffic in-process. Use `headroom wrap opencode` when you want the CLI to manage the proxy, config injection, MCP registration, backups, and unwrap behavior. The plugin routes every external HTTP(S) request through the proxy. To keep a provider or gateway off the proxy while leaving the plugin loaded, pass `excludeHosts` in the plugin options or set `HEADROOM_OPENCODE_EXCLUDE_HOSTS` before launching OpenCode. An entry matches the host and all of its subdomains; excluded requests are sent as-is, with no compression or telemetry. ```jsonc // opencode.json { "plugin": [["/path/to/headroom/providers/opencode/_dist/entry.opencode.js", { "excludeHosts": ["opencode.ai"] }]] } ``` ## Programmatic Config Helpers [#programmatic-config-helpers] The package also exports helpers for custom integrations: ```ts import { buildOpencodeConfigContent, createHeadroomProvider, createHeadroomRetrieveTool, } from "headroom-opencode"; const provider = createHeadroomProvider({ proxyPort: 8787 }); const config = buildOpencodeConfigContent({ proxyPort: 8787, defaultModel: "claude-sonnet-4-6", }); const retrieve = createHeadroomRetrieveTool({ proxyBaseUrl: "http://127.0.0.1:8787", }); ``` ## How It Works Under The Hood [#how-it-works-under-the-hood] 1. **Config injection**. The wrapper writes a `provider.headroom` block into `opencode.json`. The provider uses `@ai-sdk/openai-compatible`, which OpenCode supports natively. Model mappings route requests through `http://127.0.0.1:/v1`. 2. **Runtime config**. `OPENCODE_CONFIG_CONTENT` is set as an env var containing provider, plugin, and optional local MCP JSON. OpenCode reads it at startup and merges it with on-disk config. 3. **MCP tools**. Headroom registers `headroom_compress`, `headroom_retrieve`, and `headroom_stats` through `headroom mcp serve` unless `--no-mcp` is set. 4. **Native plugin path**. `wrap` loads the transport plugin from a local directory (`plugins/opencode/dist/` in a checkout, `headroom/providers/opencode/_dist/` in the wheel), because OpenCode 2.x only accepts local plugins as directories. The plugin uses `HEADROOM_PROXY_URL` or `http://127.0.0.1:8787` to reach the proxy. 5. **Unwrap**. `headroom unwrap opencode` restores `opencode.json` from the pre-wrap backup when present, strips Headroom marker blocks when no backup exists, and unregisters Headroom MCP servers. ## Troubleshooting [#troubleshooting] **OpenCode does not use the headroom provider.** Check that `OPENCODE_CONFIG_CONTENT` is set and contains the `provider.headroom` block. The wrap command prints the env vars it sets. **OpenCode 2.x traffic does not show up in Headroom.** OpenCode 2.x connects to a shared background service by default. A service that was already running when you wrapped never sees Headroom's config. `headroom wrap opencode` adds `--standalone` on 2.x so OpenCode runs a private server with that config instead. If you pass `--server `, that server must be started with Headroom's config too. Check what OpenCode actually loaded with `opencode api plugin.list --param directory="$PWD"`, which should list `headroom`. **The native plugin cannot reach Headroom.** Set `HEADROOM_PROXY_URL` to the running proxy URL, for example `http://127.0.0.1:8787`. **One provider rejects requests only while the plugin is loaded.** Some gateways validate the client on the wire and reject the proxy hop. Exclude that host with `HEADROOM_OPENCODE_EXCLUDE_HOSTS=` (or the `excludeHosts` plugin option); Headroom keeps routing every other provider. **Provider not found after unwrap.** If unwrap left the provider configured, run `headroom unwrap opencode` again, or manually restore from `~/.config/opencode/opencode.json.headroom-backup`. **Proxy port conflict.** Use `--port` to select a specific port, or let the proxy auto-select an available one. Headroom can be installed as a durable local runtime instead of only being started ad hoc with `headroom proxy` or `headroom wrap ...`. Use `headroom deploy` when you want the one-line path: Headroom chooses the best local runtime it can run on the current host, configures detected tools, starts the proxy at `http://127.0.0.1:8787`, and stores the deployment profile for later lifecycle commands. Use the lower-level Python-native `headroom install` CLI when you need to force a specific service, task, Docker, scope, or provider target. Both surfaces create profiles that `wrap` can reuse or recover instead of starting a second ephemeral proxy. ## Runtime matrix [#runtime-matrix] | Mode | What stays running | Primary entrypoint | |---|---|---| | Turnkey Deploy | Docker, scheduled watchdog, or detached runtime selected for this host | `headroom deploy` | | Persistent Service | Native background service | `headroom install apply --preset persistent-service` | | Persistent Task | Scheduled watchdog + on-demand runner | `headroom install apply --preset persistent-task` | | Persistent Docker | Restartable Docker container | `headroom install apply --preset persistent-docker` | | On-Demand CLI (Python) | Nothing after command exits | `headroom proxy` | | On-Demand CLI (Docker) | Nothing after container exits | Docker-native wrapper / compose CLI | | Wrapped (Python) | Proxy lasts for wrapped session | `headroom wrap ...` | | Wrapped (Docker) | Containerized proxy + host tool session | Docker-native wrapper | ## Quick examples [#quick-examples] ### Turnkey local deployment [#turnkey-local-deployment] ```bash headroom deploy headroom install status ``` `deploy` prefers the most capable restartable path it can prove is available. On NVIDIA workstations with Docker GPU support, such as an RTX 4090 host with the NVIDIA container runtime installed, it uses Docker GPU passthrough. Otherwise it prefers plain Docker, then the native scheduled watchdog for the host when available (`launchd`, Task Scheduler, or cron). If no supported supervisor is present, it still starts a managed detached Python runtime instead of failing on missing platform services. ### Persistent service on the local machine [#persistent-service-on-the-local-machine] ```bash headroom install apply --preset persistent-service --providers auto headroom install status ``` This installs a background service on the current machine, applies persistent tool wiring, and keeps the proxy healthy on port `8787`. ### Persistent watchdog task [#persistent-watchdog-task] ```bash headroom install apply --preset persistent-task --providers manual --target claude --target codex ``` This installs a scheduled recovery path instead of a traditional always-running service. ### Persistent Docker [#persistent-docker] ```bash headroom install apply --preset persistent-docker --scope user --providers auto ``` This uses Docker's restart policy instead of an OS supervisor. If you are using the Docker-native host wrapper instead of a Python install, you can use `headroom install apply|status|start|stop|restart|remove` for the `persistent-docker` preset directly from the installed wrapper. Service/task installs and provider/user/system mutation flows still belong to the Python-native CLI. ### Rootless Podman file ownership [#rootless-podman-file-ownership] For the Python-native `persistent-docker` preset, if `docker` is a Podman wrapper script rather than a symlink, set `HEADROOM_CONTAINER_RUNTIME=podman` in the installer/runtime environment so it selects the Podman path. On Linux the Podman command combines `--userns=keep-id` with an explicit `--user` using the invoking user's UID and GID. Both are needed on Podman versions that honor the image's `USER root` even with `keep-id`: the namespace preserves the host mapping, and the process user keeps newly created files owned by the host user. The configured image is not changed. Restart the deployment with the updated installer to use these flags. This prevents new ownership mismatches; it does not repair files created by an older container. Inspect and restore ownership of affected files separately before restarting if the host user can no longer read them. ## Command surface [#command-surface] ```text headroom install apply headroom install status headroom install start headroom install stop headroom install restart headroom install remove ``` `apply` creates or updates a named deployment profile, stores its manifest under `~/.headroom/deploy//manifest.json`, applies reversible configuration changes, and starts the selected runtime. `status` distinguishes the default backend from the Anthropic and OpenAI protocol targets. When the health response includes a target's configuration, that target is shown as `configured` or `default`. If configuration is unavailable or omits a target, that target is shown as `unknown`; this does not mean it is unset. For example, a Docker bridge peer may not pass the loopback health-config gate. Target URLs and credentials are not printed. An OpenAI-compatible deployment can therefore have `Default backend: anthropic` and `OpenAI target: configured` at the same time. ## Presets and runtime kinds [#presets-and-runtime-kinds] ### Presets [#presets] - `persistent-service` → native service supervisor - `persistent-task` → scheduled watchdog / recovery supervisor - `persistent-docker` → Docker restart policy with no extra OS supervisor ### Runtime kinds [#runtime-kinds] - `--runtime python` runs `headroom proxy` directly - `--runtime docker` runs Headroom inside Docker while keeping the deployment managed locally For `persistent-docker`, the runtime is always Docker. ## Configuration scopes [#configuration-scopes] | Scope | What changes | |---|---| | `provider` | Tool-specific config surfaces where Headroom can make a precise reversible edit | | `user` | User-level shell or environment surfaces | | `system` | Machine-wide shell or environment surfaces | ### Provider scope today [#provider-scope-today] Provider scope is intentionally conservative. The current direct adapters are: - Claude Code → `~/.claude/settings.json` `env` - Codex → managed block in `~/.codex/config.toml` - OpenClaw → existing `wrap openclaw` / `unwrap openclaw` flow - OpenCode → managed block in `~/.config/opencode/opencode.json` For OpenCode provider scope, reapplying the deployment, restarting it, or recovering a stopped proxy removes and reinjects only the managed `headroom` provider. Later edits to themes, other providers, and MCP configuration stay intact. JSONC input may contain comments and trailing commas. Active configuration is serialized as JSON; original formatting and comments are preserved in the snapshot. The original `.headroom-backup` is kept across these transitions for both `.json` and `.jsonc` configurations. Final `headroom install remove` restores that original snapshot and deletes the backup, including after a stop and manifest reload. Existing JSONC snapshots using the older `.json.headroom-backup` filename are migrated only when the canonical backup is absent. When a sibling `.json` config or a canonical snapshot already exists, Headroom preserves the ambiguous legacy backup in place and creates a `.json.headroom-backup.jsonc-migration-blocked` marker. This marker survives removal and prevents later installs from claiming the legacy backup, even after the conflicting config or snapshot disappears. The legacy bytes remain available for manual recovery; copy them to the intended config only after inspecting them. Keep the marker to prevent automatic migration. If the marker cannot be created, the operation fails before changing the config or consuming its canonical snapshot. Backup ownership follows the config path recorded in the deployment, not a later `OPENCODE_CONFIG` value. A successful replacement restores snapshots for retired config paths and transfers any pending restoration to the new manifest. If restoration fails, the replacement remains active; resolve the filesystem error and retry `headroom install apply` to complete it. Failed replacement cleanup consumes snapshots newly created by that attempt, but preserves snapshots owned by the previous deployment for recovery. For Copilot, Aider, Cursor, and broader env-driven setups, prefer `--scope user` or `--scope system`. ## Provider selection [#provider-selection] | Option | Meaning | |---|---| | `--providers auto` | Detect supported tools on the host and configure the best available defaults | | `--providers all` | Configure all known targets | | `--providers manual --target ...` | Configure only the named tools | Examples: ```bash headroom install apply --providers auto headroom install apply --providers all --scope user headroom install apply --providers manual --target claude --target copilot ``` ## Health and wrap behavior [#health-and-wrap-behavior] Persistent deployments publish the same `readyz` and `health` endpoints as ad hoc proxy runs. `/health` also exposes deployment metadata when the proxy was launched through the install subsystem: ```json { "deployment": { "profile": "default", "preset": "persistent-service", "runtime": "python", "supervisor": "service", "scope": "user" } } ``` The Python-native `headroom wrap ...` flow checks for a matching persistent deployment on the requested port before it starts a new ephemeral proxy. If an installed deployment exists but is stopped or unhealthy, it attempts to recover it first. The Docker-native host wrapper does **not** yet reuse or recover persistent profiles automatically; it still starts a fresh proxy container unless you opt into `--no-proxy`. ## Claude Code VSCode extension caveat [#claude-code-vscode-extension-caveat] Persistent Claude deployments default to `ENABLE_TOOL_SEARCH=true` because the standalone Claude CLI benefits from deferred tool schemas. Anthropic's VSCode extension currently does not render those deferred-tool content blocks correctly through Headroom and can show `unsupported content type` in the webview. If your persistent install targets Claude Code inside VSCode, edit `~/.headroom/deploy//manifest.json`, set `tool_envs.claude.ENABLE_TOOL_SEARCH` to `"false"`, then restart the deployment. Keep `ENABLE_TOOL_SEARCH=true` for the standalone `claude` CLI unless you hit the same renderer limitation there. ## Docker-native relationship [#docker-native-relationship] The Docker-native host wrapper and the Python install CLI solve different layers of the runtime story: - [Docker-Native Install](/docs/docker-install) → containerized on-demand CLI, wrapped host-tool flows, and Docker-native `persistent-docker` lifecycle commands - `headroom install ...` → full persistent service, task, and Docker lifecycle management, including provider/user/system mutation For a no-Python persistent Docker workflow, use the compose-managed proxy path from `docker/docker-compose.native.yml`: ```bash export HEADROOM_HOST_HOME="$HOME" export HEADROOM_WORKSPACE="$PWD" docker compose -f docker/docker-compose.native.yml up -d proxy ``` That keeps `localhost:8787` stable and restarts the proxy automatically. `HEADROOM_WORKSPACE` (the host-side bind-mount source used by the compose file) is **not** the same variable as `HEADROOM_WORKSPACE_DIR` (the canonical Headroom state root inside the container). Both are retained; the compose file sets the latter automatically. See [Filesystem Contract](/docs/filesystem-contract) for the full bucket model. ## Related guides [#related-guides] Headroom emits lifecycle events at every stage of the canonical request pipeline. Third-party packages can hook these events — without forking Headroom — by registering a **pipeline extension** under the `headroom.pipeline_extension` entry-point group. Extensions can mutate `messages`, `tools`, `headers`, or `metadata` in place before the request is forwarded upstream. Both the SDK client and the proxy dispatch the same events, so one extension covers both deployments. ## Lifecycle stages [#lifecycle-stages] Extensions receive a `PipelineEvent` for each stage in `headroom.pipeline.PipelineStage`: | Stage | When | |-------|------| | `SETUP`, `PRE_START`, `POST_START` | Process/pipeline startup | | `INPUT_RECEIVED` | Raw request accepted | | `INPUT_CACHED`, `INPUT_ROUTED`, `INPUT_COMPRESSED`, `INPUT_REMEMBERED` | Cache, routing, compression, memory stages | | `PRE_SEND` | Last hook before the request is forwarded upstream | | `POST_SEND`, `RESPONSE_RECEIVED` | After forwarding / on response | | `OUTCOME_OBSERVED` | Emits a read-only `OutcomeSnapshot` (tokens, stop reason, transforms applied) once the response is fully processed | `PRE_SEND` is the right stage for normalizing requests to fit a quirky upstream: compression and caching are done, and whatever you write into `event.messages` is exactly what the provider receives. ## Recipe: normalize requests for a quirky upstream provider [#recipe-normalize-requests-for-a-quirky-upstream-provider] Some OpenAI-compatible gateways reject valid OpenAI-spec payloads. A real example: an upstream returns `400 "Message content is null"` for assistant messages that carry `content: null` alongside `tool_calls` — a combination the OpenAI spec explicitly produces when the model returns only tool calls. The provider-recommended workaround is to send `content: ""` instead. An extension that rewrites those messages at `PRE_SEND`: ```python # my_headroom_ext/normalize.py from headroom.pipeline import PipelineEvent, PipelineStage class NullContentNormalizer: """Rewrite `content: null` + tool_calls to `content: ""` before send.""" def on_pipeline_event(self, event: PipelineEvent) -> PipelineEvent | None: if event.stage is not PipelineStage.PRE_SEND or not event.messages: return None for message in event.messages: if ( message.get("role") == "assistant" and message.get("content") is None and message.get("tool_calls") ): message["content"] = "" return None # mutated in place; returning None keeps the event ``` Register it as an entry point in your extension package: ```toml # pyproject.toml of your extension package [project.entry-points."headroom.pipeline_extension"] null-content-normalizer = "my_headroom_ext.normalize:NullContentNormalizer" ``` Install the package into the same environment as Headroom (`pip install my-headroom-ext`) and it is discovered automatically — entry points are loaded on startup, and a failing extension is isolated and logged rather than breaking the pipeline. Notes on the contract: - An extension is either an object with an `on_pipeline_event(event)` method or a class Headroom instantiates with no arguments. - Return `None` (mutate in place) or return a replacement `PipelineEvent`. - Exceptions raised by an extension are caught and logged (`fail-open`); the request proceeds unmodified. - Discovery can be disabled with the SDK config flag `discover_pipeline_extensions=False`, and explicit instances can be passed via `pipeline_extensions=[...]` (SDK `HeadroomConfig` and proxy `ProxyConfig` both expose these fields). ## Per-request upstream routing with `x-headroom-base-url` [#per-request-upstream-routing-with-x-headroom-base-url] To route different models through one Headroom instance to different OpenAI-compatible upstream bases — instead of one global `OPENAI_API_URL` / `OPENAI_TARGET_API_URL` per proxy process — send the `x-headroom-base-url` request header. The dedicated OpenAI handlers (`/v1/chat/completions`, `/v1/responses`) and the generic passthrough route all honor it, falling back to the configured upstream when absent: ```bash curl http://localhost:8787/v1/chat/completions \ -H "content-type: application/json" \ -H "x-headroom-base-url: https://api.example-gateway.ai/gemini-3-flash" \ -d '{"model": "gemini-3-flash", "messages": [{"role": "user", "content": "hi"}]}' ``` Internal `x-headroom-*` headers (including this one) are stripped before the request is forwarded upstream by default — see `HEADROOM_STRIP_INTERNAL_HEADERS` in [Configuration](/docs/configuration). Because this header is client-driven, operator-configured secret headers (`OPENAI_TARGET_API_HEADERS` / `ANTHROPIC_TARGET_API_HEADERS`) are only attached when the resolved upstream host is one you designated — a configured provider target, or a host in `HEADROOM_UPSTREAM_ALLOWED_HOSTS`. Other upstreams are still routed to, just without those headers. See [Configuration](/docs/configuration) for details. ## Per-request model routing with `request.state.headroom_route` [#per-request-model-routing-with-requeststateheadroom_route] `x-headroom-base-url` is client-driven and points at one OpenAI-compatible base. When the choice of model belongs to an extension instead of the caller — a router that picks a cheaper model per turn, say — publish it on the request state and Headroom serves that one request from a backend that speaks the target provider: ```python # middleware or an extension holding the request request.state.headroom_route = SimpleNamespace( model="moonshot/kimi-k2", # required provider="moonshot", # optional; inferred from the model id if absent reason="cheaper at this prefix length", ) ``` The contract, in `headroom/proxy/route_advice.py`: - **Absent means unchanged.** No advice — or advice that is malformed, names an unknown provider, or fails to build a backend — and the request takes exactly the path it took before. A routing preference can never take traffic down. - **Duck-typed**, so an extension does not import Headroom to publish one. - A **native** provider (`anthropic`) needs no backend switch — rewrite `body["model"]` yourself. A foreign one is translated by a `LiteLLMBackend` built for it, and Headroom writes the model id. - Backends are **built once per provider** and cached; a provider that fails to build is not retried per request. - Honored on `/v1/messages` and `/v1/chat/completions`, streaming and non-streaming alike. (Not the Responses API, which does not use the backend abstraction.) `routemegood` is the reference consumer of this seam: it decides, Headroom routes. The Headroom proxy is a standalone HTTP server that compresses all LLM traffic passing through it. Point any client at the proxy and get automatic context optimization. Running a local OpenAI-compatible model? See [Local LLM prefill benchmarking](/docs/local-llm-prefill) for a baseline-vs-optimized workflow that measures prompt-processing savings with the dashboard. ## Starting the proxy [#starting-the-proxy] ```bash # Basic usage headroom proxy # Deliberate public access, with the existing token protocol HEADROOM_PROXY_TOKEN='replace-with-a-secret' headroom proxy --host 0.0.0.0 --port 8080 # Send `Authorization: Bearer replace-with-a-secret` or # `X-Headroom-Proxy-Token: replace-with-a-secret` from the caller. # With logging and budget headroom proxy \ --log-file /var/log/headroom.jsonl \ --budget 100.0 ``` Two telemetry switches exist. `HEADROOM_TELEMETRY=on` (or `--telemetry`) turns on **local-only** in-process usage stats that power your own `/stats`, `/metrics`, and dashboard — off by default, and nothing under this switch leaves the machine. Separately, an **anonymous usage beacon is on by default** (opt-out). Once a session goes idle it POSTs a small summary of how compression behaved to a Headroom Labs collector. **Why it exists.** Compression quality is the product. The beacon tells us when a release changes it — a ratio that regresses, a content type that starts getting skipped, a provider path that begins failing — across real workloads rather than only our own test corpus. That is the whole purpose; there is no other use. **What it sends.** Counters and identifiers, never free text. The collector applies an allowlist server-side and drops everything else before storage ([`deploy/beacon/worker.js`](https://github.com/headroomlabs-ai/headroom/blob/main/deploy/beacon/worker.js)): token totals, compression ratios and rates, skip reasons, source and provider names, model ids, failure counts and status codes, and a routing summary. Alongside those, seven resource attributes: service name and version, install id, install mode, stack, OS type and CPU architecture. A typical event is about 2KB. **What it never sends.** Your prompts, your completions, your code, file paths, file contents, environment variables, or anything else derived from request or response bodies. None of these are on the allowlist, so even if a future client sent one the collector would discard it. The install id is a random UUID generated on first run and stored in your config directory. It is deliberately not derived from hostname, MAC address, or any hardware property — delete the file and you get a new one. Disable it with `HEADROOM_BEACON=off`, `HEADROOM_TELEMETRY=off` (or `--no-telemetry`), the `DO_NOT_TRACK=1` convention, or `HEADROOM_OFFLINE=1` (which also disables update checks and license reporting). `HEADROOM_BEACON=on` keeps the beacon on even with local stats off. `headroom telemetry` names the setting that turned it off. ## CLI options [#cli-options] ### Core [#core] | Option | Default | Description | |--------|---------|-------------| | `--host` | `127.0.0.1` | Host to bind to | | `--port` | `8787` | Port to bind to | | `--uds` | None | Serve on a Unix domain socket at this path instead of `--host`/`--port`. POSIX only — Windows has no `AF_UNIX` support in Python or asyncio. See [Serving on a Unix socket](#serving-on-a-unix-socket) | | `--workers` | `1` | Number of Uvicorn worker processes | | `--limit-concurrency` | `1000` | Maximum concurrent connections before Uvicorn returns 503 | | `--max-connections` | `500` | Maximum upstream HTTP connections | | `--max-keepalive` | `100` | Maximum upstream keep-alive connections | | `--http-proxy` | None | HTTP proxy URL for upstream provider requests only; HTTPS provider APIs use CONNECT | | `--mode` | `cache` | Optimization mode: `token` prioritizes compression, `cache` preserves provider prefix-cache stability. Default is `cache` (see [Savings profiles](#savings-profiles)) | | `--no-optimize` | `false` | Disable optimization (passthrough mode) | | `--no-cache` | `false` | Disable semantic caching | | `--no-rate-limit` | `false` | Disable rate limiting | | `--log-file` | None | Path to JSONL log file | | `--log-messages` | `false` | Store full request/response content for the live feed | | `--budget` | None | Daily budget limit in USD | | `--openai-api-url` | `https://api.openai.com` | Custom OpenAI API URL | | `--provider-name` | Detected from `--openai-api-url` | Display name for the OpenAI-compatible upstream on the dashboard (e.g. `OpenRouter`). Well-known hosts (OpenRouter, Groq, Together, Azure OpenAI, …) are detected automatically; this overrides them. Routing and pricing are unaffected. | | `--anthropic-api-url` | Anthropic default | Custom Anthropic API URL | | `--gemini-api-url` | Gemini default | Custom Gemini API URL | | `--backend` | `anthropic` | Backend: `anthropic`, `bedrock`, `openrouter`, `anyllm`, or `litellm-` | | `--bedrock-api-url` | None | Bedrock InvokeModel upstream for the `/model/{id}/invoke` passthrough routes (see [Bedrock via a local gateway](#bedrock-via-a-local-gateway)) | | `--factory-api-url` | None | Factory Droid upstream (env: `FACTORY_TARGET_API_URL`). Registers `/api/*` routes: `/api/llm/a/v1/messages` is compressed, other `/api/*` paths pass through verbatim. Paths outside `/api/` are never sent to Factory. Set automatically by `headroom wrap droid` (see [Factory Droid](/docs/droid)) | | `--telemetry` | `false` | Enable local, in-process usage stats (for your own `/stats` and dashboard; nothing leaves the machine) | | `--no-telemetry` | `false` | Force local telemetry off (already the default) | | `--stateless` | `false` | Disable filesystem writes and keep runtime state in memory | Use `--http-proxy` or `HEADROOM_HTTP_PROXY` when only provider API traffic should go through a proxy: ```bash headroom proxy --http-proxy http://proxy.internal:8080 ``` Avoid setting process-wide variables such as `HTTP_PROXY`, `HTTPS_PROXY`, `ALL_PROXY`, or `NO_PROXY` for this use case. HTTPX reads those variables too, but Headroom also inherits them into tool executions, so they can proxy unrelated tool traffic. ### Context management [#context-management] | Option | Default | Description | |--------|---------|-------------| | `--mode token` | | Prioritize token compression; prior turns may be rewritten for maximum savings. | | `--mode cache` | default | Freeze prior turns to maximize provider prefix-cache hit rate. This is the effective default (see [Savings profiles](#savings-profiles)). | | `--intercept-tool-results` | `false` | Opt into canary tool-result interceptors such as ast-grep Read outlining. Requires `HEADROOM_ROLLOUT_CHANNEL=canary` (or `dev`). | | `--no-read-lifecycle` | `false` | Disable stale/superseded Read-output compression. | | `--code-aware` / `--no-code-aware` | disabled | Enable or disable AST-based code compression. Requires `headroom-ai[code]`. | | `--code-graph` | `false` | Enable the proxy's live code-graph file watcher for the current project. | #### Code-memory MCP (Serena) [#code-memory-mcp-serena] `headroom wrap` registers no code-memory MCP by default. Pass `--code-memory serena` (or set `HEADROOM_CODE_MEMORY=serena`) to register **[Serena](https://github.com/oraios/serena)** for semantic, symbol-level code navigation. Serena runs on demand via `uvx` — Headroom downloads and executes no binary of its own — and indexes the current project locally. It is opt-in because its ~20 tool definitions add about 6K tokens to every request where the agent does not defer tools, and indexing runs at each launch. > **Upgrading?** Earlier releases registered Serena by default. On your next `headroom wrap`, Headroom removes the Serena entry it installed and says so; pass `--code-memory serena` to keep it. For Claude Code the entry is per project, so it is removed the next time you run `headroom wrap claude` (or `headroom unwrap claude`) in that directory. A Serena you registered yourself is left alone. For Claude Code, the entry is written at `local` scope, which means it applies to the directory you wrapped and nothing else. That keeps a Serena that cannot start from breaking Claude Code sessions in unrelated projects. Pass `--code-memory-scope user` (or set `HEADROOM_CODE_MEMORY_SCOPE=user`) to register it machine-wide instead, which is how releases before #2787 behaved. If an earlier version already registered Serena machine-wide, the next `headroom wrap claude --code-memory serena` moves it into the project scope for you — but only when Headroom installed that entry itself; a Serena you registered by hand is left alone. > **Upgrading from tokensave?** Earlier releases registered a `tokensave` MCP server (a downloaded Rust binary). tokensave has been retired in favour of Serena. On your next `headroom wrap` / `headroom unwrap`, Headroom removes the `tokensave` MCP entry it installed — nothing to migrate, since it was just an index rebuilt from your source. The leftover `tokensave` binary in `~/.local/bin` and any `.tokensave/` folders are unused and safe to delete. By default, the proxy uses the shared **ContentRouter** pipeline. It routes text, logs, JSON, code, images, and tool outputs through the currently enabled compressors and preserves reversible CCR markers where applicable. ```bash # Maximize compression headroom proxy --mode token # Preserve provider prefix cache stability headroom proxy --mode cache ``` ### Savings profiles [#savings-profiles] `HEADROOM_SAVINGS_PROFILE` selects a named profile that seeds Headroom's whole compression posture — proxy mode, keep-ratio, which messages are compressed, and `force_kompress` — at proxy startup. It is read by `headroom proxy` and by the `headroom wrap` subprocesses. When unset, the default profile is `coding`. | Profile | Target savings | Mode | Notes | |---------|----------------|------|-------| | `coding` | ~50% of new input tokens (emergent) | `cache` | **Default.** Delta-only compression at ~0 prefix-cache busts; never lossy-compresses file reads. Whole-request and lifetime ratios are much lower in cache mode. | | `balanced` | ~70% of new input tokens | `token` | Moderate compression with structural compaction. | | `agent-90` | ~90% of new input tokens | `token` | Aggressive; pins a `0.10` keep-ratio and forces Kompress. | | `general` | ~60% of new input tokens (emergent) | `token` | Non-coding workloads. | An unrecognized `HEADROOM_SAVINGS_PROFILE` value logs a warning and falls back to `coding` (the same default an unset variable resolves to) — the proxy never fails to start over a bad profile name. `balanced` is used only as a last-resort fallback when the runtime has no `coding` entry at all (old-runtime/new-client version skew). See `headroom/agent_savings.py` for each profile's full set of knobs. Because the default `coding` profile uses **cache** mode (and the proxy's own default mode is also `cache`), Headroom runs in cache mode out of the box. For a direct `headroom proxy` invocation, mode precedence is `--mode` > `HEADROOM_MODE` > the `cache` default. The selected `HEADROOM_SAVINGS_PROFILE` does not provide a third-level mode fallback after startup; wrapped launches may seed `HEADROOM_MODE` before starting the proxy. To run token mode, pass `--mode token` or set `HEADROOM_MODE=token`: ```bash # Aggressive ~90% token-savings profile HEADROOM_SAVINGS_PROFILE=agent-90 headroom proxy --port 8787 ``` ### Optional features [#optional-features] | Option | Default | Description | |--------|---------|-------------| | `--memory` | `false` | Enable persistent user memory and provider-appropriate memory tools. In `--mode cache`, automatic context injection is skipped only for Anthropic `/v1/messages` to preserve prefix stability; OpenAI chat/responses and Gemini append memory to the live-zone tail. Use `--mode token` for automatic memory context on the Anthropic path. | | `--memory-db-path` | `{cwd}/.headroom/memory.db` | Override the memory SQLite path | | `--no-memory-tools` | `false` | Disable automatic memory tool injection | | `--no-memory-context` | `false` | Disable automatic memory context injection | | `--memory-top-k` | `10` | Number of memories to inject as context | | `--learn` | `false` | Enable live traffic learning; implies `--memory` | | `--no-learn` | `false` | Explicitly disable traffic learning | | `--min-evidence` | `5` | Minimum observations before a learned pattern is persisted; `1` saves the first sighting | | `--codex-wire-debug` | `false` | Write local Codex wire snapshots and matching proxy log traces | | `HEADROOM_COMPRESS_PASSTHROUGH` | `0` | Also compress custom proxy paths that fall through to the catch-all handler (OpenAI Responses-shaped bodies, path ends in `/responses`). No `--compress-passthrough` flag on `headroom proxy`; the direct module entry point (`python -m headroom.proxy.server`) does accept one | ```bash headroom proxy --memory headroom proxy --learn --min-evidence 3 headroom proxy --codex-wire-debug HEADROOM_COMPRESS_PASSTHROUGH=1 headroom proxy ``` Live learning includes observations received while a save is queued or in flight. A pattern is marked as saved only after its evidence total is persisted. A full queue, failed save or cancellation retains the accumulated evidence for the next sighting and the shutdown snapshot. With LocalBackend, recovery reuses a row that committed before indexing failed instead of inserting another memory, and refreshes its indexes. Reconciliation is idempotent even when a cancelled SQLite update finishes late. Automatic memory lookup renders a separate, bounded embedding query: | Input | Maximum characters | | --- | ---: | | Entire query, including delimiters | 4096 | | Latest user message tail | 2048 | | Each recent tool-output tail | 512 | | Each recent assistant-turn tail | 512 | These are source-size limits, not token estimates. The local ONNX and sentence-transformers embedders then use their actual tokenizer and configured input window, including special tokens. When both sources are present, they reserve room for the question and context separately; unused space can go to the other source. Newest tool evidence is at the end of the context so retaining its tail does not silently discard it behind a long question. Remote embedders that do not expose a local tokenizer retain the character limits; these do not guarantee an arbitrary remote model's token window. The raw `MemoryQuery` fields stay unchanged, and SDK callers that render without a budget still receive the full sources. Anthropic's pre-upstream lookup keeps its existing 2.0-second deadline, configured by `ProxyConfig.anthropic_pre_upstream_memory_context_timeout_seconds`. Bounding the input limits embedding work; it is not a guarantee against cold model initialization or a slow embedding server. Memory failures include the exception representation, so an otherwise empty timeout is identifiable as `TimeoutError()`. The old LLMLingua proxy toggles are no longer part of the CLI. Headroom's proxy compression path uses ContentRouter plus the current built-in compressors, including Kompress where applicable. ## Savings profiles [#savings-profiles-1] The proxy uses a **savings profile** to control compression behavior — which messages get compressed, how aggressively, and whether to prioritize provider prefix-cache stability or raw savings. Only the env var survives across related tools (`headroom wrap` passes it to the proxy it launches). ```bash # Switch to a different profile HEADROOM_SAVINGS_PROFILE=agent-90 headroom proxy ``` ### Built-in profiles [#built-in-profiles] | Profile | Target savings | proxy_mode | force_kompress | Best for | |---------|---------------|------------|---------------|----------| | `coding` (default) | ~50% of new input tokens (emergent) | `cache` | No | Coding agents — preserves Anthropic prefix-cache stability. Whole-request and lifetime ratios are much lower in cache mode. | | `agent-90` | ~90% of new input tokens | `token` | Yes | Non-coding, cost-sensitive, or high-volume workloads | | `balanced` | ~70% of new input tokens | `token` | No | General-purpose moderate compression | | `general` | ~60% of new input tokens (emergent) | `token` | No | Non-coding chat, little code in context | **`coding` (default)** — Optimizes for coding-agent workloads with Anthropic. Uses **cache mode** (`proxy_mode="cache"`): compresses only the newest delta in each turn so the provider's prefix-cache is never busted. User messages are compressed, system prompts preserved (hottest cache). `protect_recent` is `0` — in cache mode the delta already *is* the newest turn, so a turn-count guard would suppress the only thing cache mode compresses; byte-exact fidelity instead comes from `protect_reads=True` (file reads are never lossy-compressed). Lossless-first with lossy fallback; tool search and cross-turn dedup enabled. This is the profile that `headroom wrap` uses. **`agent-90`** — Forces ML-based (Kompress) compression with a 10% keep-ratio, ignoring the lossless path. Compresses both user and system messages. Designed for non-coding or cost-sensitive workloads where maximum compression is the goal. **`balanced`** — Token-mode compression with a 30% keep-ratio. Uses the standard lossless pipeline (does not force Kompress). Protects 4 recent turns. A safe general-purpose profile. **`general`** — Token-mode compression for non-coding conversations. No turn protection (`protect_recent=0` — nothing code-positional to preserve) and does not compress user or system messages. Uses the standard lossless pipeline. ### Profile defaults and overrides [#profile-defaults-and-overrides] Profiles seed compression settings. For a direct `headroom proxy` invocation, explicit mode settings take precedence in this order: `--mode` (CLI) > `HEADROOM_MODE` (environment) > the `cache` default. The selected profile does not replace that final fallback, but wrapped launches may pre-seed `HEADROOM_MODE`. The `coding` profile defaults to `cache`, and an explicit `--mode token` still takes effect: ```bash # Coding's default mode is cache headroom proxy # An explicit CLI mode overrides the profile default headroom proxy --mode token # An explicit environment mode also overrides the profile default HEADROOM_MODE=token HEADROOM_SAVINGS_PROFILE=coding headroom proxy ``` You can also choose a profile whose default is token mode: ```bash HEADROOM_SAVINGS_PROFILE=agent-90 headroom proxy --mode token ``` ### Extending a profile with env overrides [#extending-a-profile-with-env-overrides] Profile defaults are applied only when the corresponding env var is not already set. You can start from a named profile and override individual settings: ```bash # Start from coding but force Kompress on HEADROOM_SAVINGS_PROFILE=coding HEADROOM_FORCE_KOMPRESS=1 headroom proxy # Start from balanced but lower the keep-ratio HEADROOM_SAVINGS_PROFILE=balanced HEADROOM_TARGET_RATIO=0.15 headroom proxy ``` ### Custom profiles [#custom-profiles] For permanent custom profiles, see the profile definitions in `headroom/agent_savings.py`. Each profile is an `AgentSavingsProfile` dataclass with fields for compression mode, target ratio, turn protection, and pipeline toggles. ## Serving on a Unix socket [#serving-on-a-unix-socket] `--uds` binds an `AF_UNIX` socket instead of a TCP port. Request handling is identical — only the transport changes. ```bash headroom proxy --uds ~/.headroom/run/proxy.sock ``` Two reasons to prefer it over a loopback port: - **No port to collide with, and no port exposed.** Access is governed by filesystem permissions rather than by anything listening on the network. - **Deployments where a port is awkward.** Containers and systemd units can share a socket through a bind mount or a runtime directory without publishing a port. A socket does **not** re-enable Claude Code's Remote Control. Setting `ANTHROPIC_UNIX_SOCKET` passes that feature's host check but makes Claude Code treat the session as API-key auth, which fails a separate subscription-auth requirement — see [Why the Unix socket route does not help](/docs/troubleshooting#why-the-unix-socket-route-does-not-help). Point any Unix-socket-capable client at the path. With curl: ```bash curl --unix-socket ~/.headroom/run/proxy.sock http://localhost/health ``` ### Permissions and lifecycle [#permissions-and-lifecycle] A Unix socket carries no credentials of its own, so **the mode of the directory holding it is the access-control boundary**. If the parent directory does not exist, Headroom creates it `0700`. If it already exists, Headroom does not touch its permissions — something else owns that directory's policy, and silently tightening it could lock out whatever put it there. Instead it refuses to start when an existing parent is writable by other users and not sticky, since anyone on the host could then replace the socket. (A sticky directory such as `/tmp` is accepted: others may create their own entries there but cannot unlink or rename yours.) On startup Headroom removes a stale socket left behind by a crashed process, but it refuses to start when: - something is still listening on the path (two proxies would silently split traffic), - the path exists and is not a socket (far more likely a typo'd argument pointing at real data than a leftover), or - the path exceeds the platform's `sun_path` limit — 108 bytes on Linux, 104 on macOS and the BSDs. Keep it short; a long path fails inside `bind()` with an error that names nothing. `--uds` is POSIX-only. On Windows the command exits immediately with an error telling you to use `--port`; Python exposes no `socket.AF_UNIX` there and asyncio has no Windows UDS transport. ## Configuration in depth [#configuration-in-depth] Proxy behavior is set by three layers, **each overriding the one before**: 1. **Savings profile** (`HEADROOM_SAVINGS_PROFILE`) — seeds a whole posture (mode, keep-ratio, which roles get compressed, Kompress on/off). Default `coding`. See [Savings profiles](#savings-profiles). 2. **Environment variables** — nearly every CLI flag has an `HEADROOM_*` twin, which is what you'll use in Docker, systemd, or CI. 3. **CLI flags** — the most explicit; they win over env and profile. The tables below group the knobs by what they control. They aren't exhaustive (`headroom proxy --help` prints the full list), but they cover what real deployments actually touch. Unless noted, every option is off/unset by default and safe to ignore. ### Compression tuning [#compression-tuning] Fine-grained control over what gets compressed and how hard. Most users pick a [profile](#savings-profiles) instead and never touch these. | Flag / env | Default | Effect | |---|---|---| | `--mode` / `HEADROOM_MODE` | `cache` | `cache` compresses only the newest delta (prefix-cache safe); `token` maximizes removal. | | `--target-ratio` / `HEADROOM_TARGET_RATIO` | unset | Keep-ratio for ML text compression; lower = more aggressive (e.g. `0.10`). | | `HEADROOM_MIN_TOKENS` | `500` | Minimum block size before a tool output is compressed. | | `HEADROOM_SHELL_RESULT_MIN_CHARS` | `2000` | Shell tool results shorter than this many characters bypass lossy compression. For top-level text blocks, the threshold uses their combined text length. Byte-lossless folds such as search grouping may still apply. Applies to `bash`, `shell`, `local_shell`, and aliases such as `run_shell_command`/`execute_command`; set `0` to restore lossy compression. | | `HEADROOM_COMPRESS_USER_MESSAGES` | profile | Compress text inside user-role messages. The `coding` profile turns this on; set `0` to keep user text verbatim under it. Anthropic `tool_result` blocks compress either way. No `--compress-user-messages` flag on `headroom proxy`; the direct module entry point (`python -m headroom.proxy.server`) does accept one. | | `HEADROOM_COMPRESS_SYSTEM_MESSAGES` | unset | Compress system prompts. Off by default to keep the hottest cache prefix stable. | | `HEADROOM_PROTECT_RECENT` | profile | Never compress the N most recent turns. | | `--protect-tool-results` / `HEADROOM_PROTECT_TOOL_RESULTS` | empty | Comma-separated tool names whose output is never lossy-compressed. | | `--compressor` (repeatable) / `HEADROOM_COMPRESSORS` | all | Restrict to specific compressors: `smart_crusher,kompress,code_aware,search,log,tabular,config,html,image`. Leaving out `image` also turns off the proxy's image stage. | | `--code-aware` / `--no-code-aware` | off | AST-based [code compression](/docs/code-compression). Requires `headroom-ai[code]`. | ### Kompress (ML compression) [#kompress-ml-compression] Kompress is the ModernBERT/ONNX compressor that ContentRouter falls back to for prose and unstructured text. It can run in-process or be offloaded to a hosted endpoint. | Flag / env | Default | Effect | |---|---|---| | `--disable-kompress` / `HEADROOM_DISABLE_KOMPRESS` | `false` | Turn off ML compression; keep the structural compressors. | | `--disable-kompress-anthropic` / `--disable-kompress-openai` | inherit | Per-provider override. | | `HEADROOM_FORCE_KOMPRESS_ALL` | `false` | Route *all* content through Kompress, bypassing per-type selection. No `--force-kompress-all` flag on `headroom proxy`; the direct module entry point (`python -m headroom.proxy.server`) does accept one. | | `HEADROOM_KOMPRESS_ENDPOINT` | none | Offload ML compression to a remote `/compress` endpoint (e.g. a Modal deployment) instead of running the model locally. | | `HEADROOM_KOMPRESS_ENDPOINT_TOKEN` | none | Bearer token for the remote endpoint. | | `HEADROOM_KOMPRESS_BACKEND` | `auto` | Compute backend: `auto`, `onnx_cpu`, `onnx_coreml`, `pytorch`, `pytorch_mps`. | ### Reversible compression (CCR) and lossless mode [#reversible-compression-ccr-and-lossless-mode] By default Headroom stores originals so the model can recover them via `headroom_retrieve`. See [Reversible Compression](/docs/ccr). | Flag / env | Default | Effect | |---|---|---| | `--no-ccr` / `HEADROOM_NO_CCR` | CCR on | Disable retrieval markers **and** the injected `headroom_retrieve` tool. | | `--lossless` / `HEADROOM_LOSSLESS` | `false` | Format-native lossless compaction only — no CCR marker, no retrieval tool. | | `--ccr-buffered-chat-streaming` / `HEADROOM_CCR_BUFFERED_CHAT_STREAMING` | `false` | Answer `headroom_retrieve` on streaming `/v1/chat/completions`. A turn carrying a redeemable marker runs as `stream: false` upstream and is re-emitted as `chat.completion.chunk` frames, so its text arrives all at once; other turns keep their real stream | | `--no-ccr-proactive-expansion` | expansion on | Stop proactively re-expanding compressed content when the model appears to need it. | ### File-read handling [#file-read-handling] Coding agents re-read the same files repeatedly; these control how stale reads are handled without busting the prefix cache. | Flag / env | Default | Effect | |---|---|---| | `--no-read-lifecycle` | lifecycle on | Stop replacing stale/superseded file reads with CCR markers. | | `--read-maturation` / `HEADROOM_READ_MATURATION` | `false` | *(Beta)* Keep freshly-read files verbatim while in use, then compress them once the file quiesces, unless the provider cache already holds them. Requires `HEADROOM_ROLLOUT_CHANNEL=beta` (or `dev`). | | `--read-maturation-quiesce-turns` | `5` | Turns of no change before a held read is admitted. | ### Reliability: timeouts, retries, limits [#reliability-timeouts-retries-limits] | Flag / env | Default | Effect | |---|---|---| | `--request-timeout-seconds` / `HEADROOM_REQUEST_TIMEOUT` | `300` | Upstream request timeout (seconds). | | `--connect-timeout-seconds` | `10` | Upstream connect timeout (seconds). | | `--retry-max-attempts` | `3` | Upstream retries on transient failure. | | `--limit-concurrency` | `1000` | Concurrent connections before returning 503. | | `--rpm` / `--tpm` | `60` / unlimited | Requests- and tokens-per-minute rate limits (disable with `--no-rate-limit`). | | `--budget` / `--budget-period` | none / `daily` | Spend cap in USD per period; over-budget requests get 429. | | `--workers` / `HEADROOM_WORKERS` | `1` | Uvicorn worker processes. | ### Tool search and MCP [#tool-search-and-mcp] Defers large tool schemas so they don't sit in every request. See [MCP](/docs/mcp). | Env | Scope | Effect | |---|---|---| | `HEADROOM_TOOL_SEARCH` | proxy (server-side) | Defer MCP/system tool schemas behind a `search_tools` tool. **On by default** for Anthropic requests carrying enough tools to be worth it; set `HEADROOM_TOOL_SEARCH=0` to opt out. | | `ENABLE_TOOL_SEARCH` | client (Claude Code) | Keep Claude Code's own deferred tool-loading active behind a custom base URL ([issue #746](https://github.com/headroomlabs-ai/headroom/issues/746)). Set automatically by `headroom wrap`. | ### Cost-aware model routing [#cost-aware-model-routing] Rewrite the upstream model per request — for example, send small, tool-free calls to a cheaper model. Opt-in and off by default. Configure with `HEADROOM_MODEL_ROUTER_ENABLED` plus `HEADROOM_MODEL_ROUTES`; see [Cost-aware model routing](/docs/configuration#cost-aware-model-routing). ### Observability [#observability] | Flag / env | Default | Effect | |---|---|---| | `--telemetry` / `HEADROOM_TELEMETRY` | off | **Local-only** usage stats for your own `/stats`, `/metrics`, and dashboard. Nothing leaves the machine. | | `--log-file` / `HEADROOM_LOG_FILE` | none | JSONL request/response log. | | `--log-messages` | `false` | Include full message bodies in the log (may contain sensitive data). | | `HEADROOM_LOG_PAYLOAD_PREVIEW` | `off` | Include up to 4 KB of the retrieved [CCR](/docs/ccr) payload in each `headroom_retrieve` line of the runtime log (`~/.headroom/logs/proxy-.log`). Off by default: the payload is verbatim tool-result content and can carry source code, credentials the redactor does not recognize, or customer data; the log otherwise records byte counts only. Turn it on with `1`/`true`/`yes`/`on`. Do not share a log captured with this on. | | `HEADROOM_LOG_LEVEL` | `warning` | uvicorn's log level (`critical`, `error`, `warning`, `info`, `debug`, `trace`). Raise to `info` for the per-request access log when diagnosing a deployed proxy. An unrecognized value warns and falls back to `warning`. | | `HEADROOM_OTEL_METRICS_ENABLED` | `false` | Export OpenTelemetry metrics (`HEADROOM_OTEL_METRICS_ENDPOINT`, …). See [OTLP export](/docs/metrics#opentelemetry-otlp-export). | | `HEADROOM_LANGFUSE_ENABLED` | `false` | Emit Langfuse traces (`LANGFUSE_PUBLIC_KEY` / `LANGFUSE_SECRET_KEY`). | See [Metrics](/docs/metrics) for the Prometheus and Grafana setup. #### Who can read the logs [#who-can-read-the-logs] Headroom writes the runtime log (`~/.headroom/logs/proxy-.log`) and, when `--log-file` is set, the JSONL request log. Both can hold request and response content — `--log-messages` bodies, wire-debug dumps, and CCR payload previews when they are enabled. - **Linux and macOS:** every log file Headroom creates is `0600`, owner-read/write only, and so is every rotated backup (`proxy-8000.log.1`, …). This is not conditional on which content switch is on. If the log path already exists as a symlink, Headroom refuses to log rather than write through it, and says so. - **Windows:** Headroom restricts the log file too, through an NTFS ACL — Python has no ACL API, so this goes directly through `ctypes` rather than a third-party dependency. The DACL it sets grants access to exactly the current user, replacing (not merging with) whatever was inherited from the parent directory. This can fail for reasons unrelated to Headroom's own code — a non-NTFS volume, a missing privilege — so the proxy re-reads the file afterward to confirm, and only logs a warning at startup if that check fails. If you see that warning, keep `%USERPROFILE%\.headroom` (or your chosen `--log-file` path) off shared volumes and out of directories with broadened permissions. A log shipper or sidecar running as a different user or group cannot read the runtime log on Linux/macOS. Run the collector as the same user, or copy the file with a privileged job. ### Security and networking [#security-and-networking] | Flag / env | Default | Effect | |---|---|---| | `HEADROOM_PROXY_TOKEN` | none | Require a token from non-loopback callers, as `X-Headroom-Proxy-Token: ` or `Authorization: Bearer `. The token is removed from the request before it is forwarded upstream; clients that also send a provider credential should use `X-Headroom-Proxy-Token` and keep `Authorization` for the provider. Also gates the read-only operator routes (`/stats-history`, `/quota`, `/subscription-window`, `/metrics`), which otherwise answer loopback and trusted-CIDR callers only. A token holder also sees the per-request metadata `/stats` and `/stats-lifetime` withhold from other network callers. That metadata covers every caller of the proxy, so only share the token between clients that may see each other's traffic metadata; see [What the proxy token unlocks](/docs/security-model#what-the-proxy-token-unlocks). | | `HEADROOM_ALLOW_UNAUTHENTICATED_BIND` | `false` | Acknowledge a non-loopback bind (`--host 0.0.0.0`, `::`, a LAN address) with **no** `HEADROOM_PROXY_TOKEN`. Without a token or this flag the proxy refuses to start, because `/v1/*` would relay upstream with the operator's credentials for every peer that can reach the port. Only set it when the runtime already restricts reachability (a container published on `127.0.0.1`). | | `HEADROOM_COMPRESS_ALLOW_REMOTE` | `false` | Allow non-loopback callers to reach [`POST /v1/compress`](#post-v1compress) and `POST /v1/usage`. Required to run Headroom as a remote gateway/sidecar; without it remote callers get `404`. | | `HEADROOM_OFFLINE` | `false` | Air-gap mode: refuse every connection Headroom itself initiates — telemetry, update checks, license reporting, model/tokenizer/binary/dataset downloads, litellm's pricing-table refresh, CrewAI's install tracking, remote Kompress, OTLP and Langfuse export, Copilot auth, the subscription pollers, the OpenAI embedders, Headroom Cloud compression and the TLS diagnostics (`headroom doctor --network` reports them as skipped). Four things still work on purpose: forwarding your own requests to the upstream you configured, operator-configured local endpoints (the Ollama embedder), loopback health probes, and paths an `is_offline()` check already disables. Refusals surface as messages, not tracebacks. See the air-gap notes in `docs/metrics-technical-guide.md`. | | `HEADROOM_LICENSE` | unset | Licence token, read by core and every licensed extension. `HEADROOM_LICENSE_KEY` is a deprecated alias for one release and logs a warning. A licence on its own sends nothing to Headroom. | | `HEADROOM_USAGE_REPORTING` | `false` | Set `1` to let the proxy validate the licence and post aggregate usage counts (no content) to the Headroom cloud. Off unless set; `HEADROOM_OFFLINE` still overrides it. | | `--stateless` / `HEADROOM_STATELESS` | `false` | Keep all state in memory; no filesystem writes (disables logs, memory, TOIN). | | `HEADROOM_STRIP_INTERNAL_HEADERS` | `enabled` | Strip internal `x-headroom-*` headers before forwarding upstream. | | `HEADROOM_TLS_STRICT` | strict | Set `0` to relax CA-constraint checks behind a corporate TLS-inspection proxy. | | `HEADROOM_CERT_STORE` | `system,bundled` | Trusted roots for upstream TLS: OS certificate store and/or certifi. See [Corporate Networks](/docs/corporate-networks). | | `HEADROOM_CA_BUNDLE` | unset | Extra PEM roots (e.g. a Zscaler root) added to the trusted set. | ### Performance [#performance] | Flag / env | Default | Effect | |---|---|---| | `--embedding-server` / `HEADROOM_EMBEDDING_SERVER` | off | Share one ONNX embedder across workers (~600 MB RSS saved). | | `--compression-max-workers` / `HEADROOM_COMPRESSION_MAX_WORKERS` | CPU count | Bound the CPU-bound compression threadpool. | For programmatic deployment you can pass an entire proxy config as JSON via `HEADROOM_PROXY_CONFIG_JSON`, or point `HEADROOM_CONFIG_DIR` / `HEADROOM_WORKSPACE_DIR` at custom roots (see [Filesystem Contract](/docs/filesystem-contract)). ## API endpoints [#api-endpoints] ### `GET /health` [#get-health] ```bash curl http://localhost:8787/health ``` ```json { "status": "healthy", "optimize": true, "stats": { "total_requests": 42, "tokens_saved": 15000, "savings_percent": 45.2 } } ``` ### `GET /stats` [#get-stats] Live session statistics plus durable `persistent_savings` totals. Stored at `~/.headroom/proxy_savings.json` (override with `HEADROOM_SAVINGS_PATH`). ```bash curl http://localhost:8787/stats ``` ### `GET /stats-history` [#get-stats-history] Durable history with hourly, daily, weekly, and monthly rollups. Powers the `/dashboard` view. ```bash curl http://localhost:8787/stats-history curl "http://localhost:8787/stats-history?format=csv&series=weekly" ``` ### `GET /metrics` [#get-metrics] Prometheus-format metrics for monitoring. ```bash curl http://localhost:8787/metrics ``` ``` headroom_requests_total{mode="optimize"} 1234 headroom_tokens_saved_total 5678900 headroom_persistent_savings_tokens_saved_total 5678900 headroom_compression_ratio_bucket{le="0.5"} 890 headroom_latency_seconds_bucket{le="0.01"} 800 headroom_cache_hits_total 456 ``` `headroom_tokens_saved_total` is the runtime counter for the current proxy process. Use `headroom_persistent_savings_tokens_saved_total` for durable lifetime savings that match `/stats.persistent_savings`. ### `POST /v1/messages` [#post-v1messages] Anthropic API format. The proxy compresses messages, forwards to Anthropic, and returns the response. ### `POST /v1/chat/completions` [#post-v1chatcompletions] OpenAI API format. The proxy compresses messages, forwards to OpenAI, and returns the response. ### `POST /v1/responses` [#post-v1responses] OpenAI Responses API format. The proxy compresses `input` payloads where applicable, forwards the request, and returns the response. For Codex-compatible clients, the proxy also accepts these alias paths and routes them through the same handler: - `POST /v1/codex/responses` - `POST /backend-api/responses` - `POST /backend-api/codex/responses` Matching WebSocket and subpath aliases are also supported for Codex flows. ### Codex Live voice WebSocket [#codex-live-voice-websocket] The proxy relays Codex Live voice frames without parsing or transforming them. These paths use the same transparent transport: - `ws://localhost:8787/v1/live` - `ws://localhost:8787/v1/codex/live` - `ws://localhost:8787/backend-api/live` - `ws://localhost:8787/backend-api/codex/live` Subscription authentication uses the derived ChatGPT backend path. API-key authentication preserves the selected OpenAI-compatible base URL and inbound path. The backend Live suffix defaults to `/live` and can be corrected with `HEADROOM_CODEX_LIVE_WS_PATH` if the upstream contract changes. The exact ChatGPT backend path is not confirmed by this proxy documentation. ### `POST /v1internal:streamGenerateContent` [#post-v1internalstreamgeneratecontent] Google Cloud Code Assist / Antigravity compatibility endpoint used by Pi-style `google-gemini-cli` and `google-antigravity` providers. The proxy also accepts: - `POST /v1/v1internal:streamGenerateContent` ### Antigravity IDE [#antigravity-ide] Google's Antigravity IDE can route its model traffic through the proxy: ```bash headroom wrap antigravity # Start proxy + print Antigravity setup instructions headroom mcp install --agent antigravity # Register the Headroom MCP server in Antigravity ``` The Antigravity IDE does not support third-party model providers — its agent models are Google-managed, so there is no settings panel to point at the proxy. What the proxy does offer Antigravity users: - MCP tools: `mcp install` writes the Headroom server into Antigravity's global `mcp_config.json` under `~/.gemini/` (preferring the install's current location when one already exists) and refuses to overwrite a config file that does not parse as JSON. - Native traffic: `google-antigravity` clients are accepted on [`POST /v1internal:streamGenerateContent`](#post-v1internalstreamgeneratecontent) and routed upstream like any other Cloud Code request. The Antigravity CLI (`agy`), unlike the IDE, accepts a custom endpoint via its `GOOGLE_GEMINI_BASE_URL` setting — the one Antigravity surface where a custom model endpoint is supported. ### `POST /v1/compress` [#post-v1compress] Compression-only endpoint. Compresses messages and returns them without ever making a **completion request to an LLM provider** — no generation, no provider API key, no upstream chat call. Used by the TypeScript SDK, by LiteLLM's `headroom` guardrail, and by API gateways running Headroom as a sidecar. "No LLM call" means no *generative* request to a provider. Compression itself is ML-backed: **Kompress** is a ModernBERT encoder that scores tokens for retention (classification, not generation), and Magika classifies content types. Both run in-process by default, so budget CPU and memory for the sidecar accordingly. If `HEADROOM_KOMPRESS_ENDPOINT` is set, Kompress inference is offloaded over HTTP to that model server — **real egress from the sidecar**, which matters if you deployed it expecting none. Only inference goes remote: the CCR store and retrieval markers stay proxy-local, and original content never persists off-box. Leave the variable unset to keep everything in-process, or run with `HEADROOM_DISABLE_KOMPRESS=1` for structural compression only. This route is restricted to loopback callers and answers everyone else with **`404`**, not `403` — deliberately, so it stays invisible to external scanners. A gateway calling it from another host or pod therefore sees what looks like a missing route. Both the client IP and the inbound `Host:` header must name loopback. In container environments (Docker/Podman), requests forwarded from the container host gateway are also admitted under loopback `Host:` headers. To allow arbitrary remote callers, set `HEADROOM_COMPRESS_ALLOW_REMOTE=1`. `HEADROOM_PROXY_TOKEN` still applies if set. The same policy applies to [`POST /v1/usage`](#relaying-provider-usage-post-v1usage). #### Message format [#message-format] The endpoint does **no format conversion**. Whatever shape you send in `messages` is the shape you get back, and both wire formats are compressed natively: - **OpenAI shape** — `role: "tool"` messages with `tool_call_id`, assistant `tool_calls` - **Anthropic shape** — content-block lists with `tool_use` / `tool_result` / `thinking` blocks So an Anthropic-native caller does not need to convert to OpenAI format first. Block types, `tool_use_id`s and message order are all preserved. `model` selects the tokenizer (per-model, from Headroom's tokenizer registry) and the context limit. Send the real model name — including gateway-prefixed forms like `bedrock/anthropic.claude-3-5-sonnet` or `vertex_ai/claude-sonnet-4@20250514` — so token counts and compression aggressiveness are right. #### Request [#request] | Field | Type | Required | Description | | --- | --- | --- | --- | | `messages` | array | yes | Messages to compress, in either wire format. `400` if missing. Empty array returns immediately with zero metrics. | | `model` | string | yes | Model name. Drives tokenizer + context-limit resolution. `400` if missing. | | `token_budget` | integer | no | Overrides the model's context limit. Used by callers that need to fit a tighter budget. | | `config` | object | no | Compression options, below. A non-object value is ignored rather than rejected. | | `gateway` | object | no | Switches on [gateway mode](#gateway-mode-two-half-turn-contract). May be `{}`. Fields below. | Without a `gateway` block, only the four fields above are read. Anthropic sends `system` and `tools` **out of band**, alongside `messages` — this endpoint accepts them without complaint (you get a `200`, no warning) and returns neither, so neither is compressed. Keep carrying both yourself and send them upstream unchanged. Two consequences worth knowing: - An Anthropic system prompt is not compressed here, even though it is resent on every request. - Tool-schema compaction, tool-search deferral and output steering are not reachable this way — on tool-heavy traffic those can be the largest share of available savings. [Gateway mode](#gateway-mode-two-half-turn-contract) passes `tools` and `system` through and applies all three. `config` fields: | Field | Type | Default | Description | | --- | --- | --- | --- | | `mode` | string | unset | `ccr`, `lossy_inline`, `lossless_then_lossy`, or `lossless`. Unset selects the default marker-free pipeline. Any other value is a `400`. | | `frozen_message_count` | integer | unset | Pin a prefix: the first N messages are returned byte-for-byte unchanged while staying visible to cross-message transforms like dedup. Set it to the number of messages the provider has already cached so compression cannot rewrite the prefix and bust that cache. Must be a non-negative integer; anything else is a `400`. | | `session_id` | string | unset | Opt into [session mode](#session-mode): Headroom keeps this conversation's replay state itself, so you can resend the raw conversation every turn and get a byte-identical prefix back. Non-empty, at most 256 characters; anything else is a `400`. Cannot be combined with `compress_user_messages` (`400`). | | `compress_user_messages` | boolean | `false` | Also compress user-role messages. | | `target_ratio` | number | unset | Target compression ratio. | | `protect_recent` | integer | unset | Leave recent code uncompressed in the last N messages (non-code tool payloads are not shielded). | | `protect_analysis_context` | boolean | unset | Preserve analysis context blocks. | **`config.mode` values:** - **unset (default)** — marker-free. Emits no `<>` retrieval markers and writes nothing to the CCR store, so you can forward the returned messages straight to a provider. This is the right mode for a gateway or guardrail that just swaps `messages` and forwards. - **`ccr`** — emits CCR markers and writes to the store. Only for callers that also inject the `headroom_retrieve` tool *and* can reach `/v1/retrieve` (itself loopback-only). Markers are a dangling pointer for the model otherwise. - **`lossy_inline`** (alias `lossless_then_lossy`) — runs the lossless byte/data fold first, then compresses the folded remainder. Marker-free. - **`lossless`** — format-native folds only, with no markers and no retrieval. For a sidecar with no retrieval loop; a registered lossless provider still applies. Keep the mode stable within a `session_id`, because session mode replays earlier turns byte-for-byte. #### Response [#response] | Field | Type | Description | | --- | --- | --- | | `messages` | array | Compressed messages, in the shape you sent. | | `tokens_before` | integer | Token count before compression. | | `tokens_after` | integer | Token count after compression. | | `tokens_saved` | integer | `tokens_before - tokens_after`. | | `compression_ratio` | number | `tokens_after / tokens_before` — so **lower is better**. A ratio of `0.23` means a 77% reduction, not 23%. `1.0` when nothing was compressed. | | `transforms_applied` | array | Transform labels that ran. | | `transforms_summary` | object | Per-transform counts. | | `ccr_hashes` | array | Retrieval hashes for markers inserted (empty unless `mode: "ccr"`). | | `session` | object | [Session mode](#session-mode) only: `{ "id", "frozen_message_count", "cached_prefix_replayed" }`, described there. | ```json { "messages": [{ "role": "user", "content": "..." }], "tokens_before": 15000, "tokens_after": 3500, "tokens_saved": 11500, "compression_ratio": 0.23, "transforms_applied": ["router:smart_crusher:0.35"], "transforms_summary": { "router:smart_crusher:0.35": 1 }, "ccr_hashes": [] } ``` #### Headers [#headers] `x-headroom-bypass: true` (case-insensitive) skips compression entirely and echoes your messages back with zeroed metrics. The bypass and empty-messages responses omit `transforms_summary`. #### Errors and fail-open [#errors-and-fail-open] | Status | Body | When | | --- | --- | --- | | `400` | `error.type = "invalid_request"` | Missing `messages` or `model`, malformed JSON, invalid `config.mode`, `config.frozen_message_count` or `config.session_id`, `session_id` combined with `compress_user_messages`, or a malformed `gateway` block. | | `401` | — | `HEADROOM_PROXY_TOKEN` is set and the bearer token is missing or wrong. | | `404` | — | Non-loopback caller without `HEADROOM_COMPRESS_ALLOW_REMOTE=1`. | | `503` | `error.type = "compression_error"` | Compression failed unexpectedly. | | `503` | `error.type = "compression_timeout"` | Session mode only: the turn timed out or the session's lock was busy. Retry the turn. | Without a session, compression **fails open on timeout**: you get `200` with your original messages, zeroed metrics, plus `compression_skipped: true` and `skip_reason: "compression_timeout"`. Always check `compression_skipped` if you need to know whether compression actually ran. In session mode the same timeout is a `503` instead — see below for why. Requests are recorded under `provider="compress"` in `/stats` and `/metrics`. #### Multi-turn usage: keeping the prefix cache [#multi-turn-usage-keeping-the-prefix-cache] This is the single most important thing to get right, and the default is not safe for an agent loop. When Headroom proxies a request itself it watches the provider's cache hit rate turn over turn and freezes the already-cached prefix. Without `config.session_id`, `/v1/compress` cannot do that: it sees one isolated call and has no idea what the provider already cached. You have two options — let Headroom keep the state ([session mode](#session-mode), below) or keep it yourself with `frozen_message_count`, which the rest of this section covers. The provider caches the bytes you **forwarded**. Compression changed those bytes, so your original messages and the ones the provider cached are no longer the same thing — and it is the forwarded version you have to keep reproducing. Send the pristine originals again next turn and the provider sees a different prefix and re-reads it from scratch. On Anthropic a cache read is ~90% cheaper than fresh input, so that can easily cost more than the compression saves. Compression is also not uniform over a conversation: how hard a message is compressed depends partly on how far it now sits from the end, so an older tool result can fall outside the recent-read protection window as the conversation grows and be compressed harder than it was last turn. Another reason not to rely on re-compression reproducing earlier output. Two rules: 1. **Pass `config.frozen_message_count`** — how many leading messages the provider has already cached. 2. **Send back your own previous output, not the original messages.** `frozen_message_count` returns those leading messages *exactly as you passed them in* — it pins whatever you hand it. Hand it pristine originals and you get pristine originals back, which is precisely the prefix the provider does not have. ```python # Keep what you FORWARDED, not what you started with. forwarded: list[dict] = [] def next_turn(new_messages: list[dict]) -> list[dict]: body = { "messages": forwarded + new_messages, "model": "claude-sonnet-4-6", # Everything already forwarded is already cached upstream — pin it. "config": {"frozen_message_count": len(forwarded)}, } result = requests.post(f"{proxy}/v1/compress", json=body).json() forwarded[:] = result["messages"] # becomes next turn's frozen prefix return forwarded ``` Compressing the full original conversation on each turn looks correct — you get a `200` and a positive `tokens_saved` — but the leading messages come back different from the ones the provider cached. You pay for compression *and* for a cache miss. Nothing in the response tells you this happened; watch your provider's cache-read tokens. Also for multi-turn callers: - **Leave `config.mode` unset.** The default is marker-free, which is what a forward-only caller wants. - **Send the real model name** so the tokenizer and context limit resolve correctly — including gateway-prefixed forms. - **`protect_recent` is not a substitute.** It guards the newest messages; `frozen_message_count` guards the oldest, which is the cached end. #### Session mode [#session-mode] Pass `config.session_id` and Headroom keeps the conversation's replay state itself — the same per-session compression cache and prefix tracker the proxy path uses — so a gateway that owns routing can resend the **raw** conversation every turn and still get a byte-identical prefix back. Whatever this endpoint previously returned for the session is treated as the provider's cache contract: every already-returned message comes back unchanged, even where recompressing it would do better, and only the new tail is compressed. An explicit `config.frozen_message_count` still wins when it is larger. ```json { "messages": [{ "role": "user", "content": "..." }], "model": "claude-sonnet-4-6", "config": { "session_id": "conv-8f1c" } } ``` The response gains a `session` object: ```json "session": { "id": "conv-8f1c", "frozen_message_count": 12, "cached_prefix_replayed": false } ``` | Field | Type | Description | | --- | --- | --- | | `id` | string | The session id this turn ran under: `config.session_id` as sent, or the `x-headroom-session-id` header when `HEADROOM_COMPRESS_SESSION_FROM_HEADER=1` and `config.session_id` is absent. | | `frozen_message_count` | integer | How many leading messages the pipeline was told not to recompress this turn. Headroom counts the leading run it can replay from the session's compression cache: plain messages always count, a tool result counts only once its content is in that cache, and the last message never counts. An explicit `config.frozen_message_count` wins when larger. It is derived from Headroom's own state, not from the provider, and it can be non-zero on a first turn. | | `cached_prefix_replayed` | boolean | `true` when the pipeline's output for some leading messages differed from what this endpoint returned for the session last turn, and Headroom overwrote those messages with last turn's bytes. `false` when nothing was overwritten: when the serving process holds no previous turn for the session (a first turn, expired state, or a replica that never served or loaded it), when the pipeline already produced last turn's bytes (the usual case), or when the replay was skipped as unsafe (this turn's messages do not line up with last turn's, or the replay would be larger than the pipeline's output). So `false` does not mean the returned prefix matches last turn's. | `cached_prefix_replayed` reports Headroom's own repair step. It says nothing about whether the provider served the prefix from its prompt cache. `false` is the normal value on a healthy session, and it is also what a turn that changed the prefix returns when there was nothing to replay. Read cache hits and misses from the provider's usage (`cache_read_input_tokens`, `prompt_tokens_details.cached_tokens`). What differs from the stateless contract: - **Forward the returned messages verbatim.** They are what the next turn replays. - **`compress_user_messages` is refused** (`400`). User-message rewrites are not byte-replayable across turns, so they would bust the prefix later. - **Timeouts and busy sessions are a `503`, never fail-open.** Handing back originals would leave the caller forwarding bytes the replay state does not know about, so a compression timeout answers `503` with `error.type = "compression_timeout"`; retry the turn and it lands on consistent state. Turns for one session are serialized under a per-session lock, and a concurrent turn for the same session that waits more than 10 seconds gets the same `503`. - **State is in-process by default.** The replay cache lives for `HEADROOM_COMPRESSION_CACHE_TTL_SECONDS` of idle time (3900 by default, floor 600); the tracker's usage state expires after 10 idle minutes. Without a shared store, multi-process deployments (gunicorn workers, replicas) must pin a session to one process — hash the upstream on the session id — or replay silently misses. - **`HEADROOM_COMPRESS_SESSION_STORE=redis://...` shares sessions across replicas.** Every replica also keeps each session's tracker in Redis, so any replica can serve the session's next turn and its `POST /v1/usage`. It needs the `redis` extra, which every Docker image includes. It also selects the Redis CCR store unless `HEADROOM_CCR_BACKEND` names another, so CCR markers resolve on any replica. Two limits remain. The per-session lock is per process: two concurrent turns of one session on two replicas both run, the first write wins (compare-and-set), and the next turn continues from the winner. Gateway-mode pending turns stay in-process, so `POST /v1/compress/response` must reach the replica that answered the request half; re-drive therefore still needs real affinity (`gateway.session_affinity`). - **An unreachable Redis does not fail turns.** A Redis error is a miss: the replica serves from its own memory, logs one `WARNING` when a kind of call (read, write) starts failing and an `INFO` when it works again, and skips Redis for 5 seconds after a connection error (each command times out after 1 second). The cost is the sharing. A replica with no local state for the session recompresses the history, which is slower, and the prefix it returns can differ from last turn's, which is a provider prompt-cache miss. `cached_prefix_replayed` stays `false` on those turns. A Redis that comes back empty is refilled as each session's next turn writes it whole. Run Redis with high availability if prompt-cache hits matter. - `HEADROOM_COMPRESS_SESSION_FROM_HEADER=1` lets the `x-headroom-session-id` header stand in for `config.session_id`. Off by default: gateways commonly stamp that header on all traffic, and a value shared across conversations (Claude Code subagents do this) would blend them into one replay state. **Relaying provider usage: `POST /v1/usage`.** Session mode never sees the provider's response, so cache-hit attribution and savings accounting for the session come from this optional relay (same exposure rules as `/v1/compress`). Post the provider's `usage` block under the session id: ```json { "session_id": "conv-8f1c", "usage": { "cache_read_input_tokens": 11800, "cache_creation_input_tokens": 400 } } ``` OpenAI-shaped usage is accepted too: `prompt_tokens_details.cached_tokens` or a flat `cached_tokens` is read as the cache-read count (OpenAI reports no write counter). A block carrying none of these fields is a `400` — it has no cache signal, and reading it as zeros would wipe the tracker's cached-prefix state. The reply is `{ "session_id": "conv-8f1c", "frozen_message_count": 12, "applied": true }`. `applied: false` with `reason: "no_cache_signal"` means the only present field was `0` (a fully cold turn can only be asserted with both fields present). `404` with `error.type = "unknown_session"` means the tracker's state for that session has expired — replay may still be active; only this telemetry landed nowhere. `503` with `error.type = "session_busy"` means a compress turn for the session is in flight; retry. The relay is telemetry only. It never raises the frozen count, because a provider-confirmed count could freeze a message whose cache entry has since been evicted. Skipping it costs attribution, never correctness. #### Gateway mode (two-half turn contract) [#gateway-mode-two-half-turn-contract] An API gateway that owns routing (Kong, LiteLLM, a custom sidecar) never lets Headroom see the provider's response, which rules out every transform whose reload step needs it. Gateway mode splits one model turn into two HTTP calls: the **request half** (`POST /v1/compress` with a top-level `gateway` object) returns the complete provider body plus the *obligations* the gateway must redeem, and the **response half** (`POST /v1/compress/response`) takes the provider's answer and either finishes the turn or asks the gateway to call the provider again. The rule behind it is the proxy's own **no shrink without reload**, moved to the API boundary: a transform whose reload step needs the response only runs when the gateway has said it can re-drive. A body without a `gateway` key never touches any of this — stateless and session behaviour are unchanged. **The `gateway` block.** Presence is the switch: `"gateway": {}` is gateway mode with every default. Unknown keys are ignored (a newer plugin can talk to an older Headroom); a non-object block or a non-boolean field is a `400`. | Field | Type | Default | Description | | --- | --- | --- | --- | | `can_redrive` | boolean | `false` | The gateway can call the provider again with a request Headroom hands it and post the result back. Unlocks re-driving turn hooks and, with `config.mode: "ccr"`, in-band CCR retrieval. Send `false` for streaming requests. | | `can_relay_response` | boolean | `false` | The gateway will post the provider's status and usage to `/v1/compress/response` after every turn. | | `session_affinity` | boolean | `true` | The gateway pins this session to one Headroom replica. Pending turns are in-process state, so `false` disables re-driving for the turn even when `can_redrive` is set. `HEADROOM_COMPRESS_SESSION_STORE` shares replay state, not pending turns, so it does not lift this. | | `plugin_version` | string | unset | Diagnostics only. | | `request_headers` | object | `{}` | The client's request headers the gateway is willing to hand over, as lowercase name → string value. Only `anthropic-beta` is read today: when a turn hook asks for an `anthropic-beta` token, the client's tokens go first in the returned `headers`; every other name is ignored, so a gateway may pass its whole header map. A non-object block or a non-string value is a `400`. Never forwarded anywhere by Headroom. | **What changes on the request half.** Every top-level field other than `messages`, `model`, `token_budget`, `config` and `gateway` passes through into the returned `body`: `system`, `tools`, `temperature`, `stream`, `metadata`, anything the provider accepts. `tools` is compacted deterministically (`tool_schema_compaction` in `transforms_applied`, plus `tool_desc_compaction` when `HEADROOM_TOOL_DESC_MAX_CHARS` is set), so identical tools come back byte-identical every turn. Two more request-only, streaming-safe levers run here exactly as they do on the proxy path: - **Tool-search deferral (Anthropic) is an extension's job on this path.** OSS core does not mark tools `defer_loading` on `/v1/compress`; the [headroom-tool-search](https://github.com/headroomlabs-ai/headroom-tool-search) extension's native tier does, through the turn-hook contract, when it is installed, enabled (`HEADROOM_PROXY_EXTENSIONS=tool_search`) and licensed. It applies the same primitives as the proxy path (12 or more Anthropic-shaped tools on a first-party Claude model, the same resident set, the same client-already-searching check, `HEADROOM_TOOL_SEARCH=0` as the kill switch), is a pure function of the input tools so the tools prefix keeps prompt-caching turn over turn, and is named `tool_search:native_deferral:tools:tok` in `transforms_applied` and credited as `tool_schema_tokens_saved` in `/stats`. Without the extension the tools pass through compacted and `headers` is `{}`. Tool-search history repair stays in core regardless: once a transcript carries Anthropic's tool-search blocks, a later request whose tools cannot support them (a side request with a smaller tools array) has those blocks replaced in place by an inert placeholder text block, as on the proxy path (`router:tool_search_repair:blocks`). - **Output steering.** When the output shaper is on (`HEADROOM_OUTPUT_SHAPER=1` or the `proxy_output_shaper` rollout; off in `mode="cache"` for steering), the byte-stable verbosity block is appended to the tail of `system` (Anthropic) or the last system/developer message (OpenAI chat), the conversation-stable `HEADROOM_OUTPUT_HOLDOUT` arm is honoured, and the `output_shaper:stratum:…` / `output_shaper:control:…` label rides `transforms_applied` — it is what turns the response half's relayed `output_tokens` into output savings in `/stats`. The block is identical every turn, so the prefix still caches; it is applied to the returned `body` only, never to the session snapshot, so it is re-appended once per turn rather than accumulating. Both fail open: any error leaves the body as built and `headers` empty. Turn hooks registered by proxy extensions run here as they do on the proxy path. Request hooks run stream-safe-only unless `can_redrive` and `session_affinity` are both `true` — a hook that must see the response is skipped when the gateway cannot come back with one. Hook savings show up as `turn_hook` and `turn_hook:tools:tok`. In session mode hooks run after the replay overlay and before the snapshot, so a hook that is not a pure function of its input busts the prefix exactly as it would on the proxy. The response keeps every field above and adds six: ```json { "messages": [{ "role": "user", "content": "..." }], "tokens_before": 15000, "tokens_after": 3500, "body": { "model": "gpt-4o", "messages": [{ "role": "user", "content": "..." }], "tools": [{ "type": "function", "function": { "name": "search", "parameters": {} } }], "temperature": 0.2 }, "turn_id": "3d2f6c0e0b0a4e1a9c1b2f3d4e5f6a7b", "route": { "model": "gpt-4o", "provider": "openai", "service_tier": null, "reason": "" }, "obligations": ["relay_usage"], "gateway": { "can_redrive": false, "can_relay_response": true, "session_affinity": true }, "headers": {} } ``` | Field | Description | | --- | --- | | `body` | The complete provider request: every pass-through field, final `messages` (the same list as top-level `messages`) and final `tools`. Never contains `config`, `gateway` or `token_budget`. Forward it as-is. | | `headers` | Request headers the gateway must **set** on the provider request (name → value), on the original forward and on every `redrive`. `{}` when nothing is needed. Produced by turn hooks through `TurnContext.provider_headers`: only allow-listed names are honoured (today `anthropic-beta`), and `anthropic-beta` is merged behind the client's value from `gateway.request_headers` (deduplicated, client tokens first — the same merge the proxy path uses for every Headroom-required beta). The tool-search extension's native tier uses it for `advanced-tool-use-2025-11-20`. Fail-open answers always carry `{}`. | | `turn_id` | Handle for the response half. Always present, but a turn is only *registered* — and worth posting back — when `obligations` is non-empty. | | `route` | Advisory. `provider` is inferred from the model name (`anthropic` for names containing `claude` or `anthropic`, else `openai`) unless a routing extension published advice; when it did, its model is also written into `body.model`, so `route.model` and `body.model` never disagree. `service_tier` echoes the request. Headroom never switches the upstream itself. | | `obligations` | `redrive` — a transform ran whose reload step needs the response; post `response` to `/v1/compress/response` and honour `redrive` answers. `relay_usage` — you offered `can_relay_response`; post status and usage when the provider answers. | | `gateway` | Echo of the capabilities Headroom honoured. | Fail-open answers (bypass header, empty `messages`, stateless timeout) carry the same six keys with your original body, `obligations: []` and `headers: {}`. **Savings on a gateway answer.** The token counts follow the proxy path, so a gateway can report the same figure `/stats` does. When compaction shortened `tools`, `tokens_before` and `tokens_after` also count the tool definitions before and after it, so `tokens_saved` includes the compaction saving. Two more fields carry the tool definitions kept out of the context, which no message count sees; both are absent on fail-open answers: | Field | Description | | --- | --- | | `tool_tokens_saved` | Tool-search deferral plus tools a turn hook removed, net (`tool_search_deferred_tokens` after reconciliation + `turn_hook_tools_saved_tokens` after netting against message growth). `tokens_saved + tool_tokens_saved` is the `/stats` headline for the turn. | | `tool_tokens_deferred` | The deferral share of `tool_tokens_saved`. `/stats` books it as `estimated`: a deferred tool the model loads later is billed after all. Subtract it for a figure of measured savings only. | When `relay_usage` is owed, the turn's `/stats` record is deferred until the response half arrives with billed input and output, cache counts, provider status and latency; a turn that never comes back is recorded as-is when it expires. Pending turns live `HEADROOM_GATEWAY_TURN_TTL_SECONDS` (default `120`, refreshed on every `redrive`), at most `HEADROOM_GATEWAY_MAX_PENDING_TURNS` at a time (default `10000`, oldest evicted first), and a turn may re-drive at most `HEADROOM_GATEWAY_MAX_REDRIVES` times (default `8`). **`config.mode: "ccr"` on this contract.** When markers were inserted and re-driving is allowed, Headroom injects the `headroom_retrieve` tool into `body.tools` (`ccr_tool_injected` in `transforms_applied`), adds the `redrive` obligation, and the response half answers the model's retrieval calls itself — the gateway just keeps re-driving. Without `can_redrive`, `ccr` mode behaves as it does today: the markers are yours to resolve. #### `POST /v1/compress/response` [#post-v1compressresponse] The response half. Same exposure rules as `/v1/compress`: loopback-only unless `HEADROOM_COMPRESS_ALLOW_REMOTE=1`, and `HEADROOM_PROXY_TOKEN` applies. | Field | Type | Required | Description | | --- | --- | --- | --- | | `turn_id` | string | yes | From the request half. | | `status` | integer | no | Provider HTTP status, `100`–`599`. Default `200`. Hooks only start on a `200`. | | `latency_ms` | number | no | Gateway-measured provider latency, added to the turn's recorded latency. Must be non-negative. | | `round` | integer | no | Which provider call this response answers: `0` for the original forward, `n` for the `n`-th re-drive (the `round` this endpoint returned). Makes the call idempotent: a retried relay for a round already answered gets the same `redrive` answer again instead of being fed to the hook, and its usage is not billed twice. A round ahead of the turn is a `400`. | | `usage` | object | no | The provider's usage block, in any accepted shape (below). | | `response` | object | no | The full provider response JSON. **Required** when the turn carries `redrive` and the provider call succeeded (`2xx`). A failed call closes the turn with its `status` alone. | | `model` | string | no | Provider-reported model. Accepted, not read today. | Accepted `usage` shapes — the whole provider response body works too, since a nested `usage` key is descended into once: - Anthropic: `input_tokens`, `output_tokens`, `cache_read_input_tokens`, `cache_creation_input_tokens` - OpenAI chat: `prompt_tokens`, `completion_tokens`, `prompt_tokens_details.cached_tokens` - OpenAI Responses: `input_tokens`, `output_tokens`, `input_tokens_details.cached_tokens` - Kong's flattened `ai.proxy` log shape: `prompt_tokens`, `completion_tokens`, top-level `cached_tokens` Booleans and negative counts are a `400`. Usage with no cache field is accepted but not applied to the session tracker; the apply rule is `/v1/usage`'s (a positive count, or both cache fields present at `0`). Across re-drive rounds the billed counters are summed and the cache fields come from the latest round that carried them. There are two answers. `done` ends the turn: ```json { "action": "done", "turn_id": "3d2f6c0e0b0a4e1a9c1b2f3d4e5f6a7b", "response": null, "frozen_message_count": 12, "usage_applied": true, "rounds": 0, "billed_usage": { "input_tokens": 3120, "output_tokens": 212, "cache_read_input_tokens": 20414 } } ``` `response` is what to send to the client. On a turn with no re-drive it is the hook's replacement when there is one, otherwise `null` and you forward the provider response you already hold. After one or more re-drives it is never `null`: it is the hook's replacement or the latest provider response, with `usage` set to the whole turn's bill. `frozen_message_count` is the session's count after the usage was applied (`null` for a stateless turn). `usage_applied` is `false` when the usage carried no cache signal, the session had expired, or its lock was busy — never an error, since the turn is finishing and there is nothing to retry with. `billed_usage` is what the provider billed for the whole turn: every counter, cache reads and writes included, summed across all rounds, in Anthropic keys (`null` when no usage was relayed). That total is the re-driven `response`'s `usage`, since the client made one call. After `done` the turn is gone. `redrive` asks for another provider call: ```json { "action": "redrive", "turn_id": "3d2f6c0e0b0a4e1a9c1b2f3d4e5f6a7b", "request": { "model": "gpt-4o", "messages": [], "tools": [] }, "round": 1 } ``` Send `request` to the provider exactly as given — it is the provider body with the hook's messages and possibly reloaded tools — and post the provider's JSON back under the same `turn_id`. Past `HEADROOM_GATEWAY_MAX_REDRIVES` the turn ends with `done` and the latest provider response. Hook failures never fail the turn either: the answer is `done`, with the latest provider response after a re-drive, or `response: null` before one, and the gateway forwards what it gets. | Status | Body | When | | --- | --- | --- | | `400` | `error.type = "invalid_request"` | Malformed JSON, missing or invalid `turn_id`, `status` not an HTTP status code, negative `latency_ms`, non-object `usage` or `response`, boolean or negative usage counts. | | `400` | `error.type = "missing_response"` | The turn carries `redrive` and `response` is absent. | | `404` | `error.type = "unknown_turn"` | Never registered (the request half returned no obligations), already finished, or expired. | | `409` | `error.type = "turn_busy"` | A response-half call for this turn is already being driven. | | `400` | `error.type = "round_out_of_sequence"` | `round` is ahead of the round the turn is waiting for. | | `409` | `error.type = "round_already_answered"` | `round` names a round that was already answered with `done`. | #### Kong plugin [#kong-plugin] [kong-plugin-headroom](https://github.com/headroomlabs-ai/kong-plugin-headroom) is a Kong Gateway 3.9 plugin, in its own repository and installable with `luarocks install kong-plugin-headroom` or as a Konnect custom plugin, that implements both halves: it sends every chat request through `/v1/compress` with `config.session_id` and a `gateway` block, swaps `body` in, relays usage from the `log` phase, and runs the re-drive loop in `access` when a `redrive` obligation comes back. Install steps, configuration and limits are in its README. Headroom's docker end-to-end test (`tests/gateway/test_kong_integration.py`, `HEADROOM_KONG_E2E=1`) expects a checkout of it next to this repository or at `HEADROOM_KONG_PLUGIN_DIR`. **The contract is itself an extension.** Core exposes a compress-turn seam (`headroom.proxy.compress_turn`): an extension registers a contract with `register_compress_turn_extension`, and per request it may claim the body (`begin`), prepare executor-side work (`prepare`), rewrite the outbound messages at the one safe moment in session mode (`transform`, after the prefix replay and before the tracker records what was returned), add response fields and transform labels (`finish`), take ownership of the outcome (`commit`), and answer the fail-open cases (`fail_open_fields`). The gateway turn contract described above is installed through that seam by `headroom.proxy.gateway_extension` (on by default; `HEADROOM_GATEWAY_CONTRACT=0` leaves it out, after which a `gateway` block is ignored and the endpoint answers in legacy shape). A third-party contract's `install(app, config)` finds the `/v1/compress` exposure policy on `app.state.compress_route_dependencies` for any route it adds. `HEADROOM_KOMPRESS_ENDPOINT` points *outbound* at a remote Kompress ML model server that happens to expose a `/compress` path. It is unrelated to this inbound endpoint. ## Agent wrapping [#agent-wrapping] Use `headroom wrap` to launch supported CLI agents through the local proxy: ```bash # Claude Code headroom wrap claude # Claude Code extension in VS Code (configures settings, then starts the proxy) headroom wrap vscode-claude # OpenAI Codex headroom wrap codex # Aider headroom wrap aider # Cursor (starts the proxy and prints settings to paste into Cursor) headroom wrap cursor # Grok Build (updates ~/.grok/config.toml and starts the proxy) headroom wrap grok-build ``` Wrapping can reuse an existing proxy instead of starting a new process. Its startup-only settings do not change when you export different values in the calling shell. `wrap` warns when an explicit `HEADROOM_MODE`, `HEADROOM_MIN_TOKENS`, or `HEADROOM_EXCLUDE_TOOLS` disagrees with the settings reported by that proxy. It does not restart a shared proxy or interrupt other clients. Restart the proxy with the desired settings, or use `--port` to start a separate instance. Tool exclusions are compared case-insensitively using the effective router exclusions, including built-ins and tools protected by `HEADROOM_PROTECT_TOOL_RESULTS`. Older proxies that do not report a setting cannot be compared for that setting. `headroom wrap codex` keeps routing scoped to the Codex process it launches: it passes the proxy URL as a `--config` override and never writes it to `~/.codex/config.toml`. Codex runs any session started with command-line overrides in embedded mode, so a wrapped session does not use Codex's shared background server and Codex prints "Running without the shared background server". If you want the shared server, write the proxy routing to Codex's own config instead, then start `codex` directly: ```bash headroom init codex # this project's .codex/config.toml (trusted projects) headroom init -g codex # ~/.codex/config.toml, for every project ``` That routing is persistent: it applies to every Codex session in that scope until you remove it. `headroom unwrap codex` removes the `-g` routing; for a project, delete the `# --- Headroom init provider ---` block from `.codex/config.toml`. Unwrap removes only the routing: the Headroom `SessionStart` hook that `init` adds to Codex's `hooks.json` stays, and still starts a proxy when a Codex session begins until you delete it. On Windows, nothing starts the proxy for this routing, so Codex cannot connect while the proxy is down; use `headroom install apply` if you need a supervised proxy. Cursor reads model endpoints from its settings UI, so `headroom wrap cursor` does not rewrite Cursor configuration or launch the app. After it starts the proxy, copy the printed base URL into Cursor's model settings. Grok Build reads model endpoints from `~/.grok/config.toml`. `headroom wrap grok-build` injects or updates `[model.grok-build] base_url` to point at the local proxy, then run `grok` from the same project directory. See [Grok Build Integration](/docs/grok-build). The official Claude Code extension reads Claude Code's user settings rather than the terminal environment. Use `headroom wrap vscode-claude`, reload VS Code after the first run, and keep the wrapper running. See the [VS Code Claude Code guide](/docs/vscode-claude-code) for verification and undo steps. For environment-driven clients, you can also set the base URL manually: ```bash # Claude Code ANTHROPIC_BASE_URL=http://localhost:8787 claude # Any OpenAI-compatible CLI client that reads OPENAI_BASE_URL OPENAI_BASE_URL=http://localhost:8787/v1 your-client ``` ## Cloud providers [#cloud-providers] ```bash # AWS Bedrock headroom proxy --backend bedrock --region us-east-1 # Google Vertex AI headroom proxy --backend vertex_ai --region us-central1 # Azure OpenAI headroom proxy --backend azure # OpenRouter (400+ models) OPENROUTER_API_KEY=sk-or-... headroom proxy --backend openrouter ``` ### Google Vertex AI [#google-vertex-ai] `--backend vertex_ai` delegates to [LiteLLM](https://docs.litellm.ai/docs/providers/vertex), which brings two requirements that are easy to miss: **1. Install the Vertex SDK.** `google-cloud-aiplatform` ships in the dedicated `[vertex]` extra, kept out of `[proxy]` because it and its transitive tree add roughly 175 MB and only this backend needs it: ```bash pip install "headroom-ai[proxy,vertex]" ``` Without it the proxy **refuses to start**, naming the fix and exiting `2`: ```text Cannot start proxy: Vertex backend selected but the Vertex SDK is missing. ... ``` This is a startup check rather than a per-request failure, because the backend would otherwise construct cleanly and then fail on every call with an opaque `litellm.BadRequestError: … No module named 'vertexai'`. **2. Set the LiteLLM project/location variables.** LiteLLM reads the GCP project and region from `VERTEXAI_PROJECT` / `VERTEXAI_LOCATION` — these are **not** the standard Google Cloud variables (`GOOGLE_CLOUD_PROJECT` / `GOOGLE_CLOUD_LOCATION`) used by `gcloud` and current Google SDKs: ```bash export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json # or use ADC export VERTEXAI_PROJECT= export VERTEXAI_LOCATION=us-central1 headroom proxy --backend vertex_ai --region us-central1 ``` If `VERTEXAI_PROJECT` is unset, requests do not fail loudly — they can silently resolve against your Application Default Credentials' default quota project, billing a different GCP project than you intended. Set it explicitly even if `GOOGLE_CLOUD_PROJECT` is already exported. **Backend name aliases.** `vertex_ai`, `vertex`, `google-vertex`, `googlevertex`, `litellm-vertex`, and `litellm-vertex_ai` are all normalized to the same LiteLLM-backed `vertex_ai` backend — CLI help text and older docs use these spellings interchangeably. Running Claude Code against Claude models on Vertex? See [Claude Code on Vertex AI](/docs/claude-code-vertex) for the recommended native Vertex-mode flow that reuses Claude Code's own GCP auth. `--backend vertex_ai` runs Headroom **as a proxy that itself calls Vertex via LiteLLM**. The [LiteLLM integration page](/docs/litellm) documents the inverse: adding Headroom as a compression callback inside your own LiteLLM app. Despite the shared name, they are different mechanisms. ### Native Vertex passthrough routes [#native-vertex-passthrough-routes] Separately from `--backend vertex_ai`, the proxy always registers routes that mirror Vertex's native REST shape verbatim — no backend flag needed: ```text /{api_version}/projects/{project}/locations/{location}/publishers/{publisher}/models/{model}:generateContent /{api_version}/projects/{project}/locations/{location}/publishers/{publisher}/models/{model}:streamGenerateContent /{api_version}/projects/{project}/locations/{location}/publishers/{publisher}/models/{model}:countTokens /{api_version}/projects/{project}/locations/{location}/publishers/{publisher}/models/{model}:rawPredict /{api_version}/projects/{project}/locations/{location}/publishers/{publisher}/models/{model}:streamRawPredict ``` Requests with `publisher=google` (Gemini models) go through Headroom's full Gemini optimization handler; `publisher=anthropic` (Claude on Vertex) is forwarded to Vertex directly, preserving your bearer token. Any client that already speaks the native Vertex REST API can simply point its endpoint at the proxy — this is the mechanism `headroom wrap claude` uses in [Vertex mode](/docs/claude-code-vertex). These routes need **neither** `--backend vertex_ai` nor the `[vertex]` extra. Do not combine the two: with the backend flag set, Claude publisher requests are re-routed through LiteLLM, which derives the project, region and model from its own configuration and ignores the ones in your URL — a request that works without the flag can return a `404` for a model you never asked for. ### Reading Vertex errors [#reading-vertex-errors] When Vertex rejects a request with `401`, `403`, `404` or `429`, Headroom appends a `[headroom] hint: …` note to `error.message`, sets an `x-headroom-hint` response header, and logs a warning. The hint names the likely cause — expired ADC, a partner model not yet enabled in Model Garden, or a model that is not served in the requested location. Successful responses are never modified. Hints are fixed text: they never echo your request path, project, credentials or the underlying exception. With `--backend vertex`, a backend that cannot serve (missing Vertex SDK, no ADC) returns the standard sanitized proxy error plus the same kind of fixed hint; the raw provider detail is only in the proxy log, under the response's `request_id`. ### Bedrock via a local gateway [#bedrock-via-a-local-gateway] `--backend bedrock` accepts **Anthropic** input (`/v1/messages`) and re-signs to AWS. Some setups are the other way around: the client already speaks **Bedrock** (e.g. Claude Code with `CLAUDE_CODE_USE_BEDROCK=1`, or any AWS SDK pointed at a custom endpoint), sending `POST /model/{id}/invoke` to a local gateway that re-signs and forwards to AWS (LiteLLM, LocalStack, a corporate Bedrock proxy). `--bedrock-api-url` lets Headroom sit in that chain. It registers passthrough routes for `/model/{id}/invoke` and `/model/{id}/invoke-with-response-stream`, compresses the request body with the same pipeline as `/v1/messages`, and forwards to the gateway: ```bash headroom proxy --bedrock-api-url http://127.0.0.1:4000 # then point the client's Bedrock endpoint at Headroom: AWS_ENDPOINT_URL_BEDROCK_RUNTIME=http://127.0.0.1:8787 your-bedrock-client ``` The routes are registered **only** when `--bedrock-api-url` (or `BEDROCK_TARGET_API_URL`) is set — otherwise Bedrock requests fall through unchanged. Rewriting the request body invalidates the caller's **SigV4** signature (it covers a hash of the body). Point `--bedrock-api-url` at a gateway that re-signs or does not verify the inbound signature — **never raw AWS**, which would reject the request with 403. For direct-to-AWS compression, use `--backend bedrock` (which re-signs). The two are complementary. ## Environment variables [#environment-variables] ```bash # For deliberate public access, configure the existing token and send it from callers. export HEADROOM_HOST=0.0.0.0 export HEADROOM_PROXY_TOKEN='replace-with-a-secret' export HEADROOM_PORT=8787 export HEADROOM_BUDGET=100.0 # Route OpenAI passthrough requests to a custom endpoint export OPENAI_TARGET_API_URL=https://custom.openai.endpoint.com # Route Anthropic passthrough requests to a custom endpoint export ANTHROPIC_TARGET_API_URL=https://litellm.company.internal # Compress Bedrock InvokeModel traffic, forwarding to a re-signing gateway export BEDROCK_TARGET_API_URL=http://127.0.0.1:4000 headroom proxy ``` ## Production deployment [#production-deployment] ### gunicorn [#gunicorn] ```bash pip install gunicorn gunicorn "headroom.proxy.server:create_app()" \ --workers 4 \ --bind 0.0.0.0:8787 \ --worker-class uvicorn.workers.UvicornWorker # Callers must send `Authorization: Bearer replace-with-a-secret` or # `X-Headroom-Proxy-Token: replace-with-a-secret`. ``` `headroom.proxy.server` has no module-level `app` object — it exposes a `create_app(config: ProxyConfig | None = None) -> FastAPI` factory, so gunicorn's import string must call it. ### Docker [#docker] ```dockerfile FROM python:3.11-slim RUN apt-get update && apt-get install -y --no-install-recommends build-essential \ && pip install "headroom-ai[proxy]" \ && apt-get purge -y build-essential && apt-get autoremove -y \ && rm -rf /var/lib/apt/lists/* EXPOSE 8787 CMD ["headroom", "proxy", "--host", "0.0.0.0", "--port", "8787"] ``` Run this image with an explicit token and a deliberate publication choice: ```bash docker run --rm -p 127.0.0.1:8787:8787 \ -e HEADROOM_PROXY_TOKEN='replace-with-a-secret' \ headroom-proxy ``` Callers must send `Authorization: Bearer replace-with-a-secret` or `X-Headroom-Proxy-Token: replace-with-a-secret`. For network access, replace the host-side `127.0.0.1` with an intentional public address and keep the token configured. `build-essential` is required at install time because `headroom-ai` includes `hnswlib`, a C++ extension compiled from source. It is removed after installation to keep the image slim. This guide gets you from zero to compressed LLM calls in under 5 minutes. ## 1. Install [#1-install] ```bash npm install headroom-ai ``` ```bash # Python project or virtualenv: enough for compress(), no API key, no model download pip install headroom-ai # CLI, proxy and `headroom wrap` on your machine uv tool install --python 3.13 "headroom-ai[all]" ``` `[all]` adds the proxy, the MCP server, the Kompress model and more; `compress()` needs only the base package. The TypeScript SDK sends messages to a local Headroom proxy for compression. Start the proxy before using the TS SDK: ```bash uv tool install --python 3.13 "headroom-ai[proxy]" # or, inside a Python project: pip install "headroom-ai[proxy]" headroom proxy --port 8787 ``` The proxy runs the compression pipeline (Python) and exposes an HTTP API that the TS SDK calls. ## 2. Compress messages [#2-compress-messages] ```ts twoslash import { compress } from 'headroom-ai'; const messages = [ { role: 'system' as const, content: 'You analyze search results.' }, { role: 'user' as const, content: 'Search for Python tutorials.' }, { role: 'assistant' as const, content: null, tool_calls: [{ id: 'call_1', type: 'function' as const, function: { name: 'search', arguments: '{"q": "python"}' }, }], }, { role: 'tool' as const, tool_call_id: 'call_1', content: JSON.stringify({ results: Array.from({ length: 500 }, (_, i) => ({ title: `Result ${i}`, snippet: `Description ${i}`, score: 100 - i, })), }), }, { role: 'user' as const, content: 'What are the top 3 results?' }, ]; const result = await compress(messages, { model: 'gpt-4o', baseUrl: 'http://localhost:8787', }); ``` ```python from headroom import compress import json messages = [ {"role": "system", "content": "You analyze search results."}, {"role": "user", "content": "Search for Python tutorials."}, { "role": "assistant", "content": None, "tool_calls": [{ "id": "call_1", "type": "function", "function": {"name": "search", "arguments": '{"q": "python"}'}, }], }, { "role": "tool", "tool_call_id": "call_1", "content": json.dumps({ "results": [ {"title": f"Result {i}", "snippet": f"Description {i}", "score": 100 - i} for i in range(500) ] }), }, {"role": "user", "content": "What are the top 3 results?"}, ] result = compress(messages, model="gpt-4o") ``` The Python call runs in-process. `HEADROOM_OFFLINE=1` makes it zero-network as well (no update check, no model or pricing-table downloads): ```bash HEADROOM_OFFLINE=1 python -c "from headroom import compress; log='\n'.join(f'INFO step {i} ok' for i in range(3000))+'\nFATAL disk full'; r=compress([{'role':'user','content':'What failed?'},{'role':'assistant','content':None,'tool_calls':[{'id':'c1','type':'function','function':{'name':'logs','arguments':'{}'}}]},{'role':'tool','tool_call_id':'c1','content':log}], model='gpt-4o'); print(r.tokens_before, '->', r.tokens_after, 'FATAL kept:', 'FATAL' in str(r.messages))" ``` A [FastAPI endpoint](/docs/fastapi) built on the same call is one page away. ## 3. Send to your LLM [#3-send-to-your-llm] Use the compressed messages exactly like the originals: ```ts twoslash import OpenAI from 'openai'; const client = new OpenAI(); // result.messages from the previous step const messages: any[] = []; const response = await client.chat.completions.create({ model: 'gpt-4o', messages, }); console.log(response.choices[0].message.content); ``` ```python from openai import OpenAI client = OpenAI() response = client.chat.completions.create( model="gpt-4o", messages=result.messages, ) print(response.choices[0].message.content) ``` ## 4. Check your savings [#4-check-your-savings] ```ts twoslash const result = { tokensBefore: 45000, tokensAfter: 4500, tokensSaved: 40500, // compressionRatio is tokensAfter / tokensBefore, so savings is 1 - ratio. compressionRatio: 0.1, transformsApplied: ['smart_crusher', 'cache_aligner'], messages: [], ccrHashes: [], compressed: true, }; // ---cut--- console.log(`Tokens before: ${result.tokensBefore}`); console.log(`Tokens after: ${result.tokensAfter}`); console.log(`Tokens saved: ${result.tokensSaved}`); console.log(`Compression: ${((1 - result.compressionRatio) * 100).toFixed(0)}%`); console.log(`Transforms: ${result.transformsApplied.join(', ')}`); ``` Example output: ``` Tokens before: 45000 Tokens after: 4500 Tokens saved: 40500 Compression: 90% Transforms: smart_crusher, cache_aligner ``` ```python print(f"Tokens before: {result.tokens_before}") print(f"Tokens after: {result.tokens_after}") print(f"Tokens saved: {result.tokens_saved}") print(f"Compression: {result.compression_ratio:.0%}") print(f"Transforms: {result.transforms_applied}") ``` Example output: ``` Tokens before: 45000 Tokens after: 4500 Tokens saved: 40500 Compression: 90% Transforms: ['smart_crusher', 'cache_aligner'] ``` ## Alternative: proxy mode (zero code changes) [#alternative-proxy-mode-zero-code-changes] Run `headroom wrap vscode-claude`, reload the VS Code window once, and keep the wrapper running while you use the official Claude Code extension. See the [complete VS Code Claude Code guide](/docs/vscode-claude-code) for verification, undo steps, custom profiles, and remote development. If you do not want to change any code, run Headroom as a proxy and point your existing client at it: ```bash # Start the proxy headroom proxy --port 8787 # Point Claude Code at it ANTHROPIC_BASE_URL=http://localhost:8787 claude # Or any OpenAI-compatible client OPENAI_BASE_URL=http://localhost:8787/v1 your-app ``` All requests flow through Headroom automatically. Check savings at any time: ```bash curl http://localhost:8787/stats # {"requests_total": 42, "tokens_saved_total": 125000, ...} ``` ## What gets compressed [#what-gets-compressed] The biggest savings come from tool outputs -- search results, database rows, log files, API responses. Headroom auto-detects the content type and routes it to the best compressor. No configuration needed. | Content type | Compressor | Typical savings | |---|---|---| | JSON arrays | SmartCrusher | varies with array size and redundancy | | Source code | CodeCompressor | opt-in; disabled by default | | Build/test logs | LogCompressor | varies with log verbosity | | Search results | SearchCompressor | varies with result set size | | Plain text | Kompress | varies with redundancy | Savings depend heavily on how repetitive the content is -- see [Benchmarks](/docs/benchmarks) for measured numbers on real workloads. ## Next steps [#next-steps] ## Overview [#overview] Headroom uses `release-please` to maintain a release PR from conventional commits on `main`. Merging that release PR creates the release tag and GitHub Release, which triggers `.github/workflows/release.yml` to publish all packages, build version-matched Docker images, and attach release assets. The release workflow also calls `.github/workflows/docker.yml` as a reusable workflow so GHCR images are published in the same release run with the exact same synced version as PyPI, npm, and GitHub release assets. For the end-to-end visual flow, see [CI/CD Flow Diagrams](/docs/ci-cd-flows). ## Packages & Registries [#packages--registries] | Package | Type | Registry | Environment Variable | |---------|------|----------|----------------------| | `headroom-ai` | Python | PyPI | `PYPI_PACKAGE` | | `headroom-ai` | TypeScript SDK | npmjs.org | `NPM_SDK_PACKAGE` | | `headroom-openclaw` | TypeScript plugin | npmjs.org | `NPM_OPENCLAW_PACKAGE` | | `headroom-opencode` | TypeScript plugin | npmjs.org | `NPM_OPENCODE_PACKAGE` | | `@{owner}/headroom-ai` | TypeScript SDK | GitHub Package Registry | — | | `@{owner}/headroom-openclaw` | TypeScript plugin | GitHub Package Registry | — | | `headroom-ai-{version}.tar.gz` / `headroom_ai-{version}-py3-none-any.whl` | Python package distributions | GitHub Release (`{owner}/headroom`) | — | | `headroom-ai-{version}.tgz` / `headroom-openclaw-{version}.tgz` | Node release assets | GitHub Release (`{owner}/headroom`) | — | | `ghcr.io/{owner}/headroom` | Docker image | GitHub Container Registry | — | ## Version Strategy [#version-strategy] Release Please calculates the release version from conventional commits and the release manifest. The release workflow still computes and verifies the version it is about to publish: 1. `.release-please-config.json` defines release-please behavior. 2. `.release-please-manifest.json` tracks current package versions. 3. The release PR updates versions and changelog content. 4. Merging the release PR publishes a GitHub Release tagged `vX.Y.Z`. 5. `release.yml` uses that tag as the manual version for the publish run. ### Version Files [#version-files] - `.release-please-config.json` - release-please package configuration - `.release-please-manifest.json` - release-please version manifest - `pyproject.toml` - `[project].version` - `headroom/_version.py` - `__version__`, synced at build time - `plugins/openclaw/package.json` - `version`, synced at build time - `plugins/opencode/package.json` - `version`, synced at build time - `sdk/typescript/package.json` - `version`, synced at build time `release.yml` does not commit back to the repo. Version synchronization happens inside the release build workspace. ## Conventional Commits & Semantic Bumping [#conventional-commits--semantic-bumping] Release Please analyzes unreleased conventional commits and applies the highest required bump level: | Commit | Bump | |--------|------| | `fix:` | patch | | `feat:` | minor | | Any conventional commit with `!` or any commit with `BREAKING CHANGE` in the body | major | | `docs:`, `ci:`, `chore:`, `refactor:` | no release note by default unless configured | Commits are linted in CI via `commitlint` using `@commitlint/config-conventional`. The release PR is the place where version and changelog changes are reviewed before publishing. ## Release Workflow [#release-workflow] The `release.yml` workflow runs when a GitHub Release is published, which normally happens when the release-please PR is merged. It also supports manual `workflow_dispatch` and PR dry-runs for release-critical workflow/package changes. ``` detect-version → build → build-wheels ─┬→ collect-dist ─┐ │ └→ smoke-import-wheels ─┬→ publish-pypi ─┐ ├──────────────────────────────────────┼→ publish-docker ┤ ├→ publish-npm ──────────────────────────────────────────┤ └→ publish-github-packages ──────────────────────────────┴→ create-release ``` `build-wheels` fans out to `collect-dist` and `smoke-import-wheels` in parallel (both only need the wheel artifacts, not each other). `publish-npm` and `publish-github-packages` only need `detect-version` + `build` -- they do not wait on the wheel matrix at all. `publish-pypi` and `publish-docker` both gate on `smoke-import-wheels`. `create-release` is the only job that waits on everything (`.github/workflows/release.yml:78-1004`). The workflow never commits back to the repo. ### detect-version [#detect-version] Resolves the release version from the trigger. On `release: published`, it uses the published tag (`vX.Y.Z`) as the manual version for the run. On `workflow_dispatch`, it uses the optional `version` input when provided. PR dry-runs compute a version without publishing. ### build [#build] 1. Syncs version across package files via `scripts/version-sync.py --version {npm_version}` 2. Verifies package versions with `scripts/verify-versions.py` 3. Generates the changelog artifact 4. Builds npm release packages for the TypeScript SDK and OpenClaw plugin 5. Uploads release asset artifacts for downstream publish jobs ### build-wheels [#build-wheels] Builds the Python wheel matrix for Linux x86_64, Linux arm64, Apple Silicon macOS, Intel macOS, and Windows x86_64, plus one source distribution (`.github/workflows/release.yml:214-264`). Linux wheels are audited for glibc symbol compatibility. ### collect-dist [#collect-dist] Collects the wheel matrix, source distribution, and npm tarballs into the canonical artifacts used by publishing and GitHub Release asset upload. ### smoke-import-wheels [#smoke-import-wheels] Installs the built wheels into representative customer environments and imports `headroom._core`. This blocks publishing if a wheel builds successfully but cannot import on its promised platform floor. ### publish-pypi [#publish-pypi] Downloads the Python dist artifact and publishes to PyPI via `pypa/gh-action-pypi-publish@v1.13.0` (trusted publisher). ### publish-npm [#publish-npm] Publishes all npm packages to npmjs.org: - `sdk/typescript/` as `headroom-ai` - `plugins/openclaw/` as `headroom-openclaw` - `plugins/opencode/` as `headroom-opencode` ### publish-github-packages [#publish-github-packages] Publishes both Node packages to GitHub Package Registry (`npm.pkg.github.com`) using the current repository owner as the npm scope: - `sdk/typescript/` as `@{owner}/headroom-ai` - `plugins/openclaw/` as `@{owner}/headroom-openclaw` ### GitHub release assets [#github-release-assets] Uploads the built Python distributions and both npm tarballs to the GitHub Release created in the current repository. GitHub Packages does not provide a PyPI-compatible package registry, so the workflow publishes Python wheels and sdists to GitHub as release assets while npm packages go to GitHub Package Registry and Docker images go to GHCR. ### publish-docker [#publish-docker] Calls the reusable Docker workflow to publish GHCR images with the same semantic version and synced package metadata as the rest of the release. ### create-release [#create-release] Creates or updates the GitHub Release in the current repo and uploads the built Python distributions and npm tarballs as release assets. PyPI publish is a hard gate unless `PYPI_SKIP=true`, so release notes and assets do not advertise a version that failed to publish to PyPI. ## Configuration [#configuration] All package names, registry URLs, and environment names are defined as top-level `env` constants: ```yaml env: PYPI_PACKAGE: headroom-ai PYPI_ENVIRONMENT: pypi NPM_REGISTRY_URL: https://registry.npmjs.org NPM_SDK_PACKAGE: headroom-ai NPM_OPENCLAW_PACKAGE: headroom-openclaw NPM_OPENCODE_PACKAGE: headroom-opencode GITHUB_PACKAGES_REGISTRY_URL: https://npm.pkg.github.com ``` To rename a package, update the corresponding constant — all references throughout the workflow update automatically. ## Safety Gates [#safety-gates] Each publish job requires all of the following to be true (`.github/workflows/release.yml:768`, PyPI shown; npm/GitHub Packages mirror it with their own skip variable): ```yaml if: github.event_name != 'pull_request' && github.event.inputs.dry_run != 'true' && vars.PYPI_SKIP != 'true' ``` To skip a publish target, set the corresponding GitHub Actions variable: | Variable | Effect | |----------|--------| | `PYPI_SKIP=true` | Skip PyPI publish | | `NPM_SKIP=true` | Skip both npm publishes | | `GH_PACKAGES_SKIP=true` | Skip GitHub Package Registry publish | Set these in: **GitHub repo → Settings → Variables → Actions Variables → New repository variable**. PyPI publishing is a hard gate for GitHub Releases unless `PYPI_SKIP=true`. If the PyPI upload fails, the workflow stops before creating or updating the GitHub Release, so release notes cannot advertise a version that was not published to PyPI. Before release artifacts are built, the workflow runs: ```bash python scripts/verify-versions.py ``` That gate fails on cross-package version drift. The sdist build is also checked for a top-level `LICENSE` file before any publish job can consume it. ## Workflow Triggers [#workflow-triggers] Release Please runs on pushes to `main` and maintains the release PR: ```yaml on: push: branches: [main] ``` The publish workflow runs when a GitHub Release is published, on PR dry-runs for release-critical paths, and by manual dispatch: ```yaml on: release: types: [published] pull_request: paths: - ".github/workflows/release.yml" - ".github/workflows/docker.yml" - "crates/headroom-py/**" - "pyproject.toml" - "scripts/verify-versions.py" - "scripts/version-sync.py" - "Cargo.toml" - "Cargo.lock" workflow_dispatch: inputs: version: description: "Manual version override" required: false dry_run: description: "Skip publish" type: boolean default: false ``` - **Normal release:** merge the release-please PR; the bot publishes a GitHub Release, which triggers `release.yml`. - **PR dry-run:** release-critical PRs build and smoke-import wheels before merge, but do not publish. - **Manual dispatch:** use `version` to override the release version and `dry_run: true` to skip publish steps. ## Local Testing with `act` [#local-testing-with-act] ### Prerequisites [#prerequisites] ```bash # macOS brew install act actionlint # Windows winget install act winget install actionlint ``` ### Dry-run Test [#dry-run-test] ```bash act workflow_dispatch -W .github/workflows/release.yml -e .github/act/dry-run.json ``` This runs the full workflow end-to-end with `dry_run=true`, skipping all publish steps. ### Simulate Release Please [#simulate-release-please] ```bash act push -W .github/workflows/release-please.yml -e .github/act/push-feat.json ``` The `push-feat.json` event file simulates a `feat:` commit on `main` so the release-please workflow can be validated locally. ### Simulate a Published Release [#simulate-a-published-release] ```bash act release -W .github/workflows/release.yml -e .github/act/release-published.json -n ``` The `release-published.json` event file simulates the event emitted when the release-please PR is merged. ### Validate the Release and Docker Workflows [#validate-the-release-and-docker-workflows] ```bash bash scripts/validate-workflows.sh ``` This runs `actionlint` plus `act -n` against the release and Docker workflows using the checked-in `.github/act/*.json` event fixtures. CI runs the same script in the `workflow-validation` job so branch changes to release automation are validated before merge. ### Local Secrets [#local-secrets] ```bash cp .env.act.example .env.act # Edit .env.act and add your test tokens ``` `act` automatically reads `.env` and passes values as workflow secrets. ## Workflow Files Reference [#workflow-files-reference] | File | Purpose | |------|---------| | `.github/workflows/release.yml` | Main release pipeline | | `.github/workflows/release-please.yml` | Release PR aggregation from conventional commits | | `.github/workflows/ci.yml` | CI — lint, test, commitlint | | `.github/workflows/publish.yml` | Manual-only PyPI fallback (superseded by `release.yml`) | | `.commitlintrc.json` | Conventional commit rules | | `scripts/version-sync.py` | Sync version across all packages | | `scripts/changelog-gen.py` | Generate changelog from git log | | `scripts/verify-versions.py` | Pre-release version alignment check | | `.github/act/dry-run.json` | `act` event file for dry-run testing | | `.github/act/push-feat.json` | `act` event file for feat commit testing | | `.github/act/release-published.json` | `act` event file for release publish simulation | | `.github/act/docker-version.json` | `act` event file for Docker workflow validation | | `scripts/validate-workflows.sh` | Shared `actionlint` + `act -n` workflow validation script | | `.actrc` | Default `act` flags (Ubuntu runner, reuse, quiet) | | `.actrc.local.example` | Local `act` override template | ## Required GitHub Secrets [#required-github-secrets] | Secret | Purpose | Where to Get | |--------|---------|--------------| | `NPM_TOKEN` | Publishing to npmjs.org | npmjs.com → Account → Access Tokens | | `GITHUB_TOKEN` | GitHub Package Registry (auto-provided) | Automatically available in GitHub Actions | The PyPI publish uses trusted publisher OIDC — no secret required, only the `pypi` GitHub Environment must be configured with your PyPI project. ## Release Cadence [#release-cadence] Day to day: 1. Merge regular PRs to `main`. 2. Release Please updates the open release PR when releasable conventional commits land. 3. Review the release PR changelog and version bump. 4. Merge the release PR when ready to ship. 5. Watch `release.yml` publish PyPI, npm, GitHub Packages, Docker, and GitHub Release assets. Runtime rollout answers one question: **which behaviors may this already-built Headroom artifact expose in this process?** It is separate from the source and distribution lifecycle, which decides which commit/artifact is qualified, released, packaged, and published. ```bash HEADROOM_ROLLOUT_CHANNEL=canary headroom proxy ``` This runs the installed artifact with canary-eligible runtime features available according to that artifact's rollout policy. It does **not** install, select, or run a canary release/version of Headroom. ## Channels and feature policy [#channels-and-feature-policy] Channels are ordered `stable < beta < canary < dev`. | Channel | Purpose | |---------|---------| | `stable` | Default; behavior eligible for normal production use. | | `beta` | Opt-in behavior backed by automated and limited production evidence. | | `canary` | Early dogfood behavior still gathering evidence. | | `dev` | Local development and maintainer experiments. | Availability and default enablement are separate registry fields. A feature can be available in `canary` but remain off until explicitly requested; another can be available and default-enabled in `stable`. Request a named feature: ```bash HEADROOM_ROLLOUT_CHANNEL=canary \ HEADROOM_FEATURES=tool_result_interceptors \ headroom proxy --intercept-tool-results ``` Force it off with the kill switch: ```bash HEADROOM_DISABLE_FEATURES=tool_result_interceptors headroom proxy ``` ## Resolution and precedence [#resolution-and-precedence] CLI arguments, environment variables, and typed configuration are resolved once at configuration construction. The immutable snapshot is injected into the proxy and transform pipelines; changing the process environment afterward does not alter a running proxy. The existing loopback-only `/admin/runtime-env` endpoint is one narrow exception: hot-reloading the legacy `HEADROOM_OUTPUT_SHAPER` alias replaces the proxy's immutable snapshot with a newly resolved snapshot. Channel bounds and `HEADROOM_DISABLE_FEATURES` still win, and `/stats.rollout` changes with the effective running decision. Because these overrides are process-local, the endpoint rejects updates when the built-in server uses multiple workers; restart the proxy with the desired environment instead. Ambient environment mutation remains ignored. Precedence is deterministic: | Condition | Result | |-----------|--------| | Explicit disable | Off, even if defaulted, requested, aliased, or unsafe override is active. | | Requested below its availability channel, unsafe override active | On with `unsafe_override`. | | Requested below its availability channel | Off with `blocked_by_channel`. | | Explicit request in an allowed channel | On with `explicit`. | | Enabled legacy alias in an allowed channel | On with `legacy_alias`. | | Default-enabled in the active channel | On with `default`. | | Otherwise | Off with `not_requested`. | Legacy feature-specific variables are narrow compatibility aliases only. They obey channel bounds and explicit disable precedence. ## Unsafe override and invalid input [#unsafe-override-and-invalid-input] `HEADROOM_UNSAFE_ALLOW_UNSTABLE_FEATURES=1` is a break-glass mechanism. It can cross a channel boundary for a requested feature, but cannot beat an explicit disable. The runtime remains usable for debugging and emergency reproduction, while its snapshot reports: ```json { "unsafe_override": true, "qualification_eligible": false, "qualification_ineligible_reason": "unsafe_rollout_override_active" } ``` The Python resolver logs a warning and falls back to `stable` for an unknown channel; unknown feature names are warned and ignored (fail-closed). Explicit Python diagnostics (`headroom rollout status`) and the Rust front proxy's typed CLI/environment parser reject unknown channels/features and list valid values before startup. ## Machine-readable status and provenance [#machine-readable-status-and-provenance] Inspect a supplied configuration without starting the proxy: ```bash headroom rollout status --json ``` Inspect the actual running process through the supported black-box endpoint: ```bash curl http://127.0.0.1:8787/stats ``` The Python proxy publishes the object at `/stats.rollout`. The Rust front proxy, when deployed, publishes its own effective snapshot at `/rollout/status`; this keeps each process's distinct feature registry and decisions independently observable. The `/stats.rollout` object and CLI output contain no secrets. They include: ```json { "schema_version": 1, "policy_version": "1", "channel": "stable", "unsafe_override": false, "registry_digest": "sha256:...", "snapshot_digest": "sha256:...", "qualification_eligible": true, "features": [ { "name": "tool_result_interceptors", "available_in": "canary", "default_enabled_in": null, "requested": false, "disabled": false, "enabled": false, "decision": "not_requested" } ] } ``` `schema_version` versions the external JSON contract. `policy_version` versions the rollout rules. `registry_digest` is SHA-256 over canonical, ordered feature definitions. `snapshot_digest` identifies the complete effective runtime state. Equivalent policies/configurations produce equal digests; material policy or decision changes do not. These identities deliberately remain separate from source SHA, artifact SHA-256, runtime payload SHA-256, and future qualification-policy identities. An external benchmark can compare `/stats.rollout.registry_digest` and `snapshot_digest` between A1 passthrough and B Headroom arms without importing Headroom internals. A mismatch makes the future experiment invalid; benchmark logic itself is out of scope for runtime rollout. ## Evidence-backed graduation and rollback [#evidence-backed-graduation-and-rollback] Features progress from canary through beta toward stable only with linked deterministic, integration, and benchmark evidence. **Bake time is evidence, not qualification by itself.** Stable eligibility is followed by release qualification before behavior becomes a stable default. Every rollout-managed behavior must have a fast disable path. Operational rollback uses `HEADROOM_DISABLE_FEATURES`; source rollback reverts the defining change. The unsafe override is for diagnostics, not promotion or passing release evidence. Contributors should add named registry entries and tests for default behavior, explicit request, channel blocking, disable precedence, unsafe behavior, decision reasons, and provenance rather than reading rollout variables inside implementation components. Python and Rust registries contain features relevant to their own runtimes, but share channel ordering, precedence, decision reasons, fail-closed invalid-input semantics, and deterministic identity semantics. `headroom savings` shows how much Headroom has saved you over time — cost avoided, token counts, and breakdowns by model and client. Unlike `headroom_stats` (a single in-memory session snapshot), it reads a **durable ledger** that survives proxy and agent restarts. ## Usage [#usage] ```bash headroom savings # human-readable summary headroom savings --json # machine-readable report headroom savings --days 7 # restrict the lookback window (1-30, default 30) headroom savings --reset # delete the ledger and start fresh ``` ### Example [#example] ```text Today ███████████░░░░░ 67.9% saved 19,000 / 28,000 tokens $0.0850 Last 7 days ███████████░░░░░ 67.1% saved 47,000 / 70,000 tokens $0.2250 Last 30 days ██████████░░░░░░ 65.0% saved 78,000 / 120,000 tokens $0.2680 Cost avoided per model: claude-opus-4-8 $0.1750 gpt-5.5 $0.0350 unknown $0.0330 claude-haiku-4-5 $0.0250 Savings by client: claude-code 4 calls · 60,000 tokens saved codex 2 calls · 18,000 tokens saved ``` ## How it works [#how-it-works] Every compression appends one line to an **append-only, file-locked event ledger** at `~/.headroom/savings_events.jsonl`, and `headroom savings` aggregates it on read. This design is: - **Durable** — the ledger is on disk, so totals survive proxy and agent restarts. - **Accurate under concurrency** — Headroom's MCP server runs as multiple processes (the main agent plus each subagent), and the proxy is a separate process. An append-only, locked log lets every writer contribute without the lost-update races a single shared mutable file would suffer. - **Self-pruning** — events older than the retention window (30 days, which is also the hard maximum for `--days`) are dropped on read, and the file is compacted once it grows large. Both compression paths feed the same ledger: - **MCP tool** — each `headroom_compress` call records its client (the MCP client name) and tokens saved. - **Proxy** — each request records its real upstream model, so cost is priced accurately. ### Cost basis [#cost-basis] Cost avoided is the dollar value of the saved **input** tokens. Headroom uses [litellm](https://docs.headroomlabs.ai/docs/litellm) list pricing where the model is known (proxy traffic). MCP-tool compressions don't know the agent's upstream model, so they record `model="unknown"` and fall back to a blended per-token rate rather than reporting `$0`. ## Configuration [#configuration] | Variable | Purpose | | --- | --- | | `HEADROOM_SAVINGS_EVENTS_PATH` | Override the ledger location (default `~/.headroom/savings_events.jsonl`). | | `HEADROOM_MCP_CLIENT` | Override the client label recorded by the MCP tool path. | | `HEADROOM_MCP_MODEL` | Optional model hint so MCP-tool compressions price against a known model instead of the blended fallback. | `headroom savings` is distinct from `headroom_stats` (a per-session, in-memory snapshot) and from the proxy's live `/stats` endpoint (backed by `proxy_savings.json`). The savings ledger is the durable, cross-process source of truth. Headroom is a local MITM proxy. It sits between your coding agents and your LLM providers, and it inspects, rewrites, caches, and re-forwards the traffic that passes through it. That is the product, and it has direct consequences for where your data goes and what it is protected by. This page documents those consequences: what is written to disk, with what permissions and retention, what leaves the host, and what the local control surfaces do and do not allow. It is intended to be sufficient for a security review without reading the source. For reporting a vulnerability, see [SECURITY.md](https://github.com/headroomlabs-ai/headroom/blob/main/SECURITY.md). ## Trust model in one paragraph [#trust-model-in-one-paragraph] Headroom runs as a local process under your own user account, and it trusts that account. It sees your full agent traffic in plaintext, and it holds whatever provider credentials you give it. Its local HTTP surfaces are restricted to loopback, which protects you from remote and cross-origin attackers but **not** from other processes running as you on the same machine. Headroom is not designed for multi-tenant hosts, and it should not be treated as an isolation boundary between users. ## Data in flight [#data-in-flight] The proxy terminates the connection from your agent, decompresses and parses the request body, applies its transforms, and re-issues the request upstream using your credentials. There is no mode in which it forwards traffic it cannot read — inspection is what makes compression possible. In practice this means the proxy process sees, in plaintext: system prompts, full conversation history, source code sent as context, tool call arguments and results, retrieved chunks, and anything else your agent transmits. Transport to the provider remains TLS. See [Governed knobs](#governed-knobs) for the one setting that relaxes certificate validation. The trust store itself is configurable: `SSL_CERT_FILE` and `REQUESTS_CA_BUNDLE` have **replacement** semantics — set either and only those CAs are trusted — while `NODE_EXTRA_CA_CERTS` is **additive**, loading extra roots on top of the system store, matching Node.js. First match wins in that order. ### The upstream is partly client-selectable [#the-upstream-is-partly-client-selectable] "Upstream" is not purely operator-configured. The OpenAI-compatible handlers accept an `x-headroom-base-url` request header, so a client on the proxy port can choose the destination of a single request. That is deliberate — it is how BYOK gateways (LiteLLM, Azure, self-hosted vLLM) reach the dedicated handlers instead of generic passthrough — and two separate guards bound it. **Where it may point.** A client-supplied base URL is resolved and rejected if it lands on a private, loopback, link-local, or otherwise non-public address: the confused-deputy case where the proxy is used to reach cloud metadata (`169.254.169.254`) or RFC1918 hosts the caller cannot reach directly. NAT64-embedded IPv4 is unwrapped before the check, so it cannot be smuggled through an IPv6 literal. What happens to a rejected override depends on the route. The OpenAI Chat Completions and Responses handlers (`/v1/chat/completions`, `/v1/responses` and their unprefixed and Codex/ChatGPT-backend aliases) **ignore** it: the request proceeds to the configured upstream and a warning is logged. The other routes that read the header **reject** the request with HTTP 400 (`Rejected unsafe upstream base URL`) before contacting any upstream: Anthropic Messages and token counting, the native Gemini routes, model listing, embeddings, moderations, audio, and the generic passthrough. A client that sends an unsafe override should be ready for either outcome. `HEADROOM_ALLOWED_BASE_URLS` (comma-separated hosts or URLs) re-permits specific internal endpoints as an explicit operator choice: a bare host permits every safe scheme and port on that host, a full URL permits only its exact normalized origin. DNS resolution is bounded by `HEADROOM_UPSTREAM_RESOLVE_TIMEOUT_S` (3 seconds by default). **What may ride along with it.** The operator's own `*_extra_headers` secrets — the two credential-shaped maps in [the settings store](#the-settings-store) — travel only to a host the operator designated. Designated means one of the resolved provider API targets (`ANTHROPIC_TARGET_API_URL`, `OPENAI_TARGET_API_URL`, and the Gemini/Vertex/Bedrock/Cloud Code equivalents), the default `api.anthropic.com` / `api.openai.com`, or a host listed in `HEADROOM_UPSTREAM_ALLOWED_HOSTS`. Matching is exact equality on the *parsed* hostname, never the URL string, so neither `https://api.anthropic.com@evil.example` nor `https://api.anthropic.com.evil.example` matches. An undesignated destination is still proxied — it simply does not receive those headers — and the drop is warned once per host rather than once per request. Read the pair for what it is: it stops a client redirecting the *operator's* stored credentials to a host of its choosing, and it stops the proxy being used as an SSRF pivot into the local network. Neither guard authenticates the caller. They constrain what a request can reach and what it carries, not who is allowed to make one — the same boundary described under [Local control surfaces](#local-control-surfaces). ## Data at rest [#data-at-rest] Most state lives under the workspace directory — `~/.headroom` by default, relocatable with `$HEADROOM_WORKSPACE_DIR`. **The workspace is not exhaustive**, and an inventory that assumes it is will miss files: the memory store defaults to a *project-local* path, and both the memory store and the Copilot token can be relocated independently. The `Path` column below states where each item actually resolves. | What | Path | Contents | Retention | Encrypted | Permissions | |---|---|---|---|---|---| | Copilot OAuth token | `/copilot_auth.json`, or `$HEADROOM_COPILOT_AUTH_FILE` | GitHub Copilot OAuth **refresh token**, plaintext JSON | Until you delete it | No | `0600`, best-effort | | CCR cache | `/ccr_store.db` (SQLite) | **Pre-compression originals**: tool outputs, file contents, retrieved chunks | 30 min default (`HEADROOM_CCR_TTL_SECONDS`), lazily purged | No | `0600` | | Memory store | **Project-local by default** — see [Memory store paths](#memory-store-paths) | Extracted cross-session memories | Until deleted | No | Inherits umask | | Memory exports | `/memories/` | Markdown exports of the above | Until deleted | No | Inherits umask | | Install ID | `/install_id` | Random UUID identifying the install to the beacon | Until deleted | No | `0600`, best-effort | | Savings tracker | `/proxy_savings.json` | Aggregate token/cost counters — no prompt content | Until deleted | No | Inherits umask | | License cache | `/license_cache.json` | Cached license envelope | Per envelope | No | Inherits umask | | Settings store | `/settings.json`, or `$HEADROOM_SETTINGS_PATH` | Dashboard-managed knob values — **including two header maps that carry credentials**, see [The settings store](#the-settings-store) | Until deleted | No | `0600`, inherited from `mkstemp` | | Proxy log | `/logs/proxy.log` + 5 rotations | INFO-level operational log, always on; carries the admin audit stream | 10 MB × 5 backups, then rotated out | No | Inherits umask | | Request log | Opt-in, `--log-file` / `$HEADROOM_LOG_FILE` | JSONL per-request records; **full request and response content** when `--log-messages` is on | Until deleted | No | `0600`, owner-only | The config directory is `$HEADROOM_CONFIG_DIR`, else `$HEADROOM_WORKSPACE_DIR/config`, else `~/.headroom/config`. **Nothing Headroom writes is encrypted at rest.** Confidentiality of everything above rests on filesystem permissions and on the security of the account the proxy runs as. ### The settings store [#the-settings-store] The dashboard settings GUI persists a curated set of 64 knobs to `/settings.json`. Two of them are marked secret, and both are credential-shaped: `ANTHROPIC_TARGET_API_HEADERS` and `OPENAI_TARGET_API_HEADERS` are free-form JSON header maps merged into forwarded requests — the field's own help text gives `{"Api-Key": "..."}` as the example. Anything you put there is written to disk in plaintext. The GUI masks them on read (`GET /settings` returns a sentinel, not the value), but that is a display control, not an at-rest one. Where these two are *sent* is separately constrained: they are merged only after the destination is known, and only for operator-designated hosts. See [The upstream is partly client-selectable](#the-upstream-is-partly-client-selectable). That bounds the blast radius of the values; it does not change how they are stored. Two further properties matter for policy: - **It is a second way environment knobs get set.** `settings_store.apply_to_environ` runs at CLI startup and again when the app is constructed, using `os.environ.setdefault`. The precedence is `shell export > settings.json > code default`, so a stored value silently becomes the effective configuration on any host where the operator assumed the environment was the only input. - **The file is `0600` by construction, not by assertion.** `save()` writes atomically via `tempfile.mkstemp` + `os.replace`, and inherits mkstemp's `0600`. There is no explicit `chmod`, so treat the permission as a property of the current implementation rather than a guarantee. `HEADROOM_TLS_STRICT`, `HEADROOM_BEACON`, `HEADROOM_CCR_BACKEND`, and `HEADROOM_OFFLINE` are **not** in the registry — none of them can be set from this file or the routes that write it. ### Logs [#logs] Two log paths, with very different sensitivity: - **`/logs/proxy.log` — always on.** A `RotatingFileHandler` (10 MB, 5 backups) is attached to the `headroom` logger when the app is created; there is no flag to suppress it, and it is written before configuration is otherwise applied. It carries INFO-level operational records, and — because the audit logger is a child of `headroom` and writes no file of its own — the structured admin audit stream lands here too. Audit events record the action, method, path, source IP, and status code of `/admin/*`, `/cache/clear`, and `/stats/reset` calls. Note the permission asymmetry: unlike `copilot_auth.json`, `ccr_store.db`, and `settings.json`, this file gets no explicit mode. Under a common `0002` umask it lands at `0664` — readable by every account on the host. - **The request log — opt-in, and content-bearing when you ask for it.** `--log-file` (`$HEADROOM_LOG_FILE`) writes one JSON object per request. By default those objects hold metadata only. `--log-messages` (`$HEADROOM_LOG_MESSAGES`) adds full request and response message content to the same file *and* serves it on the live feed endpoint. Its own help text warns that it may log sensitive data; on this page's terms, it writes your prompts and completions to disk unencrypted, for as long as you keep the file. Both `HEADROOM_LOG_FILE` and `HEADROOM_LOG_MESSAGES` are in the settings registry, which means full message logging can be switched on — and its destination chosen — from the settings surface described under [Local control surfaces](#local-control-surfaces). `--stateless` suppresses the request log entirely (`log_file` is forced to `None`); it does not suppress `proxy.log`. ### Memory store paths [#memory-store-paths] Memory is the one store that does **not** default into the global workspace. With `--memory` enabled and `--memory-db-path` unset, the proxy creates `{cwd}/.headroom/memory.db` — relative to the working directory the proxy was launched from, so a fleet running agents in many repos produces many databases. Partitioning is then governed by `--memory-storage`, which defaults to `project`: | Mode | Where memories land | |---|---| | `project` (default) | One DB per resolved workspace, under `/memories/projects/-/memory.db` | | `user` | One DB per resolved identity — see the note on `x-headroom-user-id` below | | `global` | A single shared DB — the `--memory-db-path` file itself | `--memory-db-path` (env `HEADROOM_MEMORY_DB_PATH`) sets the `global`-mode file and seeds the storage root for `project` mode. Scoping can also be steered per-request with the `x-headroom-project-id` and `x-headroom-cwd` headers. `x-headroom-user-id` is treated differently, and the distinction matters for anyone reasoning about who can read whose memories. It is a partition **hint, not an authenticated identity**, and `resolve_memory_identity` honors it only for loopback callers — the single-user local model. Any other caller is bound to a hash of `HEADROOM_PROXY_TOKEN`, or failing that the server's OS user, so a network client cannot select another user's partition by asserting a header. The check fails closed: a request whose peer metadata is missing is not treated as loopback, so unusual ASGI transports cannot opt into header trust. Multi-tenant deployments replace the default resolver through `set_identity_resolver`; the OSS proxy ships only the single-user default. For an inventory or a data-deletion procedure, enumerate project-local `.headroom/` directories as well as the workspace — memories are the store most likely to sit outside it. ### The CCR cache deserves specific attention [#the-ccr-cache-deserves-specific-attention] CCR ("Compress-Cache-Retrieve") makes compression reversible by keeping the original content so the model can retrieve it on demand. That original is written to disk. By default — `HEADROOM_CCR_BACKEND` unset or `sqlite` — entries land in `~/.headroom/ccr_store.db` as one JSON row per entry, with the original in an `original_content` field, in plaintext. The store is restart-safe by design, because the 30-minute TTL assumes it can be shared across worker processes. Concretely: if a tool result contained a credential, an internal hostname, or a customer record, that value is on disk in a readable SQLite database for at least the TTL window. Two behaviors worth knowing: - **Expiry is lazy, not scheduled.** Expired rows are removed by a sweep that runs on lookup, not by a background timer. On an idle proxy, content past its TTL remains on disk until the next access. The TTL bounds *retrievability*, not *residency*. - **The backend can fall back silently.** If the SQLite backend fails to initialize, Headroom logs a warning and continues with an in-memory store. Retrieval still works; it simply stops surviving restarts. To avoid disk persistence entirely: ```bash HEADROOM_CCR_BACKEND=memory headroom proxy --port 8787 ``` This trades restart-safety and multi-worker sharing for keeping originals in process memory only. ## Local control surfaces [#local-control-surfaces] The proxy exposes HTTP routes beyond the provider-shaped ones. The sensitive ones — `/admin/*`, `/debug/*`, `/cache/clear`, `/stats/reset`, `/settings*`, `/transformations/feed`, `/v1/feedback*`, `/v1/telemetry*`, `/v1/toin/*`, and the content-bearing `/v1/retrieve*` family — are guarded by `require_loopback`, which enforces two independent conditions: 1. The client address must be a loopback IP. 2. The inbound `Host:` header must also name loopback. The second gate is what defeats DNS rebinding, where a remote page resolves an attacker-controlled hostname to `127.0.0.1` so the browser connects locally while the page origin stays remote. The IP check alone passes in that scenario; the `Host:` check does not. Guarded routes return **404**, not 403, so they are indistinguishable from absent routes to a scanner. A third gate, `require_same_origin`, sits on the mutating routes (`/admin/runtime-env`, `/cache/clear`, `/stats/reset`, `/settings`, `/settings/apply`). It defends a different attack: a plain CSRF, where a remote page's script posts a "simple" request straight at `http://127.0.0.1:`. Both loopback gates pass in that case — the browser really is connecting to loopback and really does send a loopback `Host:` — but the `Origin:` header still names the attacker's page, and that is what this gate rejects. Requests with no `Origin` at all (CLI tools, curl) pass, which is the intended behaviour for a locally-driven surface. ### The two CIDR allowlists [#the-two-cidr-allowlists] Two environment variables can extend this beyond loopback. Both default to empty, so the default posture is the one described above — but an operator who sets either has changed the boundary and should say so in their own threat model. | Variable | Effect | |---|---| | `HEADROOM_PROXY_TRUSTED_DASHBOARD_CLIENT_CIDRS` | Peers in these CIDRs reach `/settings`, `/settings/schema`, `/dashboard/settings`, and the settings *writes* **without being on loopback** — and additionally see the fields `/stats` and `/stats-lifetime` otherwise withhold from network callers (per-request ids, providers, models, errors, project names, backend config). Added so the settings UI works behind a reverse proxy. A caller holding `HEADROOM_PROXY_TOKEN` sees those `/stats` fields too, but none of the settings routes; see [What the proxy token unlocks](#what-the-proxy-token-unlocks). | | `HEADROOM_PROXY_TRUSTED_GATEWAY_CIDRS` | Peers in these CIDRs are trusted to supply `X-Forwarded-*` headers, which is what makes the client IP the *forwarded* one rather than the gateway's. | Both parse strictly: a malformed CIDR raises at startup rather than silently emptying the allowlist. Note the interaction — trusting a gateway for `X-Forwarded-For` means the dashboard CIDR check is applied to a client-supplied header, so the two must be configured together and pointed at infrastructure you control. ### What the proxy token unlocks [#what-the-proxy-token-unlocks] With `HEADROOM_PROXY_TOKEN` set, a non-loopback caller that presents the token (`X-Headroom-Proxy-Token` or `Authorization: Bearer`) can use the provider routes and the read-only operator routes (`/stats-history`, `/quota`, `/subscription-window`, `/metrics`). It also sees the per-request metadata that `/stats` and `/stats-lifetime` withhold from other network callers, the same fields a dashboard CIDR peer sees: | Endpoint | Fields a token holder also sees | |---|---| | `/stats` (and `/stats?cached=1`) | `recent_requests` and `request_logs`: request ids, timestamps, providers, models, token counts, transforms applied, tags, error text. Never prompt or response content. Plus `config`: backend and savings profile. | | `/stats-lifetime` | `projects` (per-project savings) and `persistence.error` | That is what a dashboard behind an authenticating reverse proxy needs. The reverse proxy adds the token after its own sign-in, then reaches `/stats` with a domain `Host:` from a peer outside any dashboard CIDR, which the CIDR rule never admits. The token opens nothing else in this section. `/settings`, `/settings/schema`, `/dashboard/settings` and the settings writes still need loopback or a dashboard CIDR peer. The `require_loopback` routes (`/admin/*`, `/debug/*`, `/cache/clear`, `/stats/reset`, `/transformations/feed`, `/v1/retrieve*` and the rest listed above) still need loopback. The token is one shared secret, not a per-caller identity. Every holder sees the request metadata of every caller on that proxy: which models they use, their error text, their project names. If you hand the token to several clients that should not see each other's traffic metadata, run one proxy per client. ### What `POST /admin/runtime-env` can change [#what-post-adminruntime-env-can-change] `headroom wrap` hot-syncs settings to a running proxy through this route. It does not accept arbitrary environment variables — writes are restricted to a hardcoded allowlist (`RUNTIME_ENV_KNOBS`), currently: | Knob | Purpose | |---|---| | `HEADROOM_OUTPUT_SHAPER` | Master switch for output-token shaping | | `HEADROOM_VERBOSITY_LEVEL` | Verbosity steering level | | `HEADROOM_VERBOSITY_AUTOTUNE` | AIMD verbosity controller state | | `HEADROOM_OUTPUT_HOLDOUT` | Output-shaping holdout fraction | | `HEADROOM_INTERCEPT_READ_MIN_CHARS` | ast-grep read-interception threshold | | `HEADROOM_VERIFY_TRUNCATION_ON_DISK` | Verify ast-grep Read truncation against disk when no client banner is found. Always returns `UNKNOWN` on Windows, where `dir_fd` / `O_NOFOLLOW` support is unavailable. | | `HEADROOM_VERIFY_TRUNCATION_MAX_BYTES` | Max on-disk file size (bytes) the disk-verify fallback will read | | `HEADROOM_DEFERRED_TOOL_AFFINITY` | Keep `defer_loading` tools out of the Anthropic prefix-tracker lineage key (default on; `0` counts them again) | | `HEADROOM_WEB_SEARCH_HISTORY` | Drop `web_search_call` history from a Responses request that declares no web_search tool (default on; `0` disables) | | `HEADROOM_BUFFERED_UPSTREAM_STREAM` | Send buffered CCR requests (and their continuations) upstream as `stream: true` and fold the reply (default on; `0` sends `stream: false`) | Keys outside this list are ignored, and `/admin/upstream` is read-only — there is no write route on the admin surface, specifically so a caller cannot redirect credential-bearing traffic through it. ### What `POST /settings` can change [#what-post-settings-can-change] The settings routes are the wider surface, and they should be read as part of the same threat model as `/admin/*` rather than as a separate UI concern. `POST /settings` validates against the 64-field registry described under [The settings store](#the-settings-store) and writes the result to disk; `POST /settings/apply` does the same and then **restarts the proxy** so the values take effect (self-restarting on service deployments, returning the host command under Docker). That combination has consequences worth stating plainly: - **The upstream URL *can* be changed through this surface.** `ANTHROPIC_TARGET_API_URL` and `OPENAI_TARGET_API_URL` are in the registry. `/admin/upstream` remains read-only, but a caller who reaches `/settings` can still repoint credential-bearing traffic at a destination of their choosing and restart the proxy to apply it. If you are relying on the admin surface being read-only, this is the path that gets around it. - **Full message logging can be switched on.** `HEADROOM_LOG_MESSAGES` and `HEADROOM_LOG_FILE` are both in the registry, so the same caller can start writing your prompts and completions to a file path they choose. - **Credential-shaped values can be written.** The two `*_TARGET_API_HEADERS` maps are stored here in plaintext. What is *not* reachable, verified against both the runtime-env allowlist and the settings registry: `HEADROOM_TLS_STRICT`, `HEADROOM_BEACON`, `HEADROOM_CCR_BACKEND`, and `HEADROOM_OFFLINE`. TLS verification in particular cannot be relaxed over HTTP; it is read from the process environment at startup only. All of these calls are audited. `/admin/*`, `/cache/clear`, and `/stats/reset` emit a structured event carrying action, method, path, source IP, and status; `/admin/runtime-env`, `POST /settings`, and `POST /settings/apply` add their own with the *changed keys* — keys only, deliberately, so a secret's value never reaches the log. The audit stream is a logger, not a file of its own, so in a default install it lands in `/logs/proxy.log`. ### Residual risk [#residual-risk] The loopback and same-origin gates are an effective boundary against remote and browser-based attackers. They are not a boundary against local ones. Any process that can bind a loopback socket as your user can flip the ten runtime knobs, write the 64 settings and restart the proxy to apply them, read `/v1/retrieve*`, and reach the debug routes. There is no token, no mTLS, and no per-caller authorization — the guards distinguish *where a request came from*, not *who sent it*. For single-user developer machines — the intended deployment — this is proportionate. For shared build boxes, multi-user jump hosts, or anywhere untrusted local code runs, it is not, and Headroom should not be deployed there without additional isolation. ## Network egress [#network-egress] Beyond your configured LLM providers, Headroom may contact: | Destination | Purpose | Default | Disable / pre-provision | |---|---|---|---| | `headroom-beacon.headroom-beacon.workers.dev` | Anonymous session summaries to Headroom Labs | **On** — see [The beacon](#the-beacon-is-on-by-default) | `HEADROOM_BEACON=off`, `HEADROOM_TELEMETRY=off`, `DO_NOT_TRACK=1`, or `HEADROOM_OFFLINE` | | `pypi.org` | Daily version check for update notices | On | `HEADROOM_UPDATE_CHECK=off`; also skipped in `--stateless`, in CI, and from source checkouts | | `huggingface.co` | Downloads models for the optional ML compression paths | On first use of those features | Pre-provision the model cache, or leave the features disabled | | `app.headroomlabs.ai` | License validation and usage reporting | Only with a license key configured | Omit the license key | | `api.anthropic.com` | Subscription usage poll (`/api/oauth/usage`) for the quota panel; sends your Claude OAuth token as a bearer credential | On, but it only polls when a token is found (`CLAUDE_CODE_OAUTH_TOKEN` or the Claude credentials file); every 300 s | `HEADROOM_NO_SUBSCRIPTION_TRACKING=1` or `HEADROOM_OFFLINE` | | `api.github.com` | GitHub Copilot quota poll (`/copilot_internal/user`); sends a GitHub token as a bearer credential | On, but it only polls when one of `GITHUB_COPILOT_GITHUB_TOKEN`, `GITHUB_TOKEN`, `COPILOT_GITHUB_TOKEN` or `GITHUB_COPILOT_API_TOKEN` is set; every 60 s | No dedicated switch found. Unset those variables, or use `HEADROOM_OFFLINE` | | `mobile.events.data.microsoft.com` | ONNX Runtime's own usage telemetry to Microsoft (ORT 1.31+ on Linux and macOS): hashed device id, model file and graph names, model hashes, execution providers, hardware ids | **Off**: importing `headroom` sets `ORT_DISABLE_TELEMETRY=1` unless it is already set | Leave `ORT_DISABLE_TELEMETRY` unset or `1`; setting it to `0` turns ORT's telemetry back on | ### The beacon is on by default [#the-beacon-is-on-by-default] This is the one egress path that is enabled by default whatever credentials you have, so it warrants its own treatment. It is governed by `HEADROOM_BEACON` and is **opt-out** (`BEACON_DEFAULT_ON = True`). Session summaries are POSTed to `https://headroom-beacon.headroom-beacon.workers.dev/v1/logs`, currently a Cloudflare `workers.dev` address. **Payload class: counters and identifiers, no content.** Each report is a cumulative snapshot — not a delta — carrying: - **Session**: a random id, a sequence number, duration, turn count, and whether this is the final report for the session. - **Tokens**: original, attempted, input, output, saved, tool-saved, cache read/write, and uncached counts, plus derived percentages (saved, eligible, yield, all-layers variants, cache-read, latency overhead). - **Compression**: transform and skip tallies keyed by transform slug, a per-strategy breakdown (`strategy`, event count, tokens in, tokens out), aggregate and per-turn latency/overhead in milliseconds, passthrough turns, and response-cache hits. - **Context**: request sources (`proxy` / `mcp`), the sorted provider and model names seen, a failure count, and those failures split by HTTP status code. - **Schema v2 additions** (`schema_version: 2`, additive over v1): `quality` (tokens re-read because compression removed them), `hist` (fixed-bucket distributions), `shapes` (content type and strategy counts, plus a bucketed structural descriptor per tool output: field count rounded down to a power of two, nesting depth and shape-flag letters, never field names or TOIN's structure hash), `cache` (prompt-cache hit rate by gap since the previous turn), `trajectory`, `clients` (harness name and turn count), `errors` (4xx counts by status), `strata` (output-shaper A/B arm and request features), and `config` (the values of 15 allow-listed `HEADROOM_*` variables, each reduced to a short slug; the list is `_CONFIG_ENV_KEYS` in `headroom/telemetry/session.py`). `headroom telemetry --show` prints the exact payload. Alongside each report go resource attributes: a random per-install UUID, Headroom version, OS type, CPU architecture, and — when known — install mode and detected stack. The keys in the tally dictionaries are internal slugs, not free text: transform names are truncated at the first colon before being counted, and sources are one of two literals. Prompts, completions, file contents, tool results, and file paths are **not** in the payload. The install id is a random UUID rather than a machine fingerprint, and resets if you delete `/install_id`. Four controls switch it off, checked in this order: ```bash HEADROOM_OFFLINE=1 # offline mode — disables all egress, not just the beacon DO_NOT_TRACK=1 # the cross-tool convention, honoured HEADROOM_BEACON=off # also false / 0 / no / disable / disabled; =on keeps it on HEADROOM_TELEMETRY=off # same off-values; also what --no-telemetry sets ``` `headroom telemetry` names the setting that turned it off. The destination can also be redirected with `HEADROOM_TELEMETRY_ENDPOINT`, which is useful if you would rather terminate the reports at a receiver you control than block them outright. One asymmetry is worth knowing for policy purposes: `HEADROOM_TELEMETRY` is fail-*closed* (an unrecognised value leaves it off), whereas the beacon is fail-*open* while the default stands — an unrecognised `HEADROOM_BEACON` value uploads. If you are disabling it by configuration management, assert one of the exact off-values above rather than assuming a typo fails safe. ### Local telemetry and OpenTelemetry are separate [#local-telemetry-and-opentelemetry-are-separate] `HEADROOM_TELEMETRY` is a **different switch**, off by default and opt-in. It aggregates stats locally for the in-process collector and the `/stats` and `/v1/telemetry` endpoints, and sends nothing off-host. Enabling it does not authorise beacon upload; an explicit `HEADROOM_TELEMETRY=off` does suppress the beacon, unless `HEADROOM_BEACON=on`. Operational metrics are separate again: OpenTelemetry export (`HEADROOM_OTEL_METRICS_*`) is opt-in and targets a collector you specify. For a controlled fleet, set `HEADROOM_BEACON=off` (or `DO_NOT_TRACK=1`) via configuration management, pin the package version, set `HEADROOM_UPDATE_CHECK=off`, and pre-provision model assets rather than allowing runtime fetches. `HEADROOM_OFFLINE=1` covers all four at once where fully-offline operation is acceptable. ## Governed knobs [#governed-knobs] Two further environment variables are worth an explicit policy decision. (`HEADROOM_BEACON` is the third, covered under [The beacon](#the-beacon-is-on-by-default).) A note on where "the environment" comes from: `settings.json` is applied to the process environment at startup with `setdefault`, so for any knob in the settings registry the effective value may come from that file rather than from your shell. None of the three knobs in this section are in that registry, but a fleet policy asserted purely by inspecting exported environment variables is incomplete for the ones that are — see [The settings store](#the-settings-store). **`HEADROOM_TLS_STRICT=0`** relaxes certificate validation so Headroom can operate behind a corporate TLS-inspecting proxy. It is narrowly scoped — it clears only the RFC 5280 strict flag and does not disable verification wholesale — but it is still a loosening of transport security and should be set deliberately rather than habitually. See the README for details on scope and platform behavior. **`HEADROOM_UPDATE_CHECK=off`** disables the daily PyPI version check. Recommended for managed fleets, where upgrades should be driven by your own release process. Neither is reachable through the admin endpoint or the settings routes; both are read from the process environment only. ## What `wrap` changes on your machine [#what-wrap-changes-on-your-machine] `headroom wrap` makes persistent modifications to your development environment: it injects configuration into agent config files, may edit `CLAUDE.md` / `AGENTS.md`, and can install MCP servers. `headroom unwrap` reverses the wrapping. A full teardown currently spans several commands and is tracked in [#748](https://github.com/headroomlabs-ai/headroom/issues/748), which also proposes a consolidated `headroom uninstall`. Until that lands, treat wrapping as a change to inventory on managed machines. When agents hand off to each other, context gets replayed in full. SharedContext compresses what moves between agents using Headroom's compression pipeline, typically saving **~80% of tokens** on agent handoffs. ## Quick Start [#quick-start] ```ts twoslash import { SharedContext } from "headroom-ai"; const ctx = new SharedContext(); // Agent A stores large output const entry = await ctx.put("research", bigResearchOutput, { agent: "researcher", }); // Agent B gets compressed version (~80% smaller) const summary = ctx.get("research"); // Agent B needs full details on demand const full = ctx.get("research", { full: true }); ``` ```python from headroom import SharedContext ctx = SharedContext() # Agent A stores large output ctx.put("research", big_research_output, agent="researcher") # Agent B gets compressed version (~80% smaller) summary = ctx.get("research") # Agent B needs full details on demand full = ctx.get("research", full=True) ``` ## API [#api] ### `put(key, content, agent?)` [#putkey-content-agent] Store content under a key. Compresses automatically using Headroom's full pipeline (SmartCrusher for JSON, CodeCompressor for code, Kompress for text). ```ts twoslash import { SharedContext } from "headroom-ai"; const ctx = new SharedContext(); // ---cut--- const entry = await ctx.put("findings", bigJsonOutput, { agent: "researcher", }); entry.originalTokens; // 20000 entry.compressedTokens; // 4000 entry.savingsPercent; // 80.0 entry.transforms; // ["router:json:0.20"] ``` ```python entry = ctx.put("findings", big_json_output, agent="researcher") entry.original_tokens # 20,000 entry.compressed_tokens # 4,000 entry.savings_percent # 80.0 entry.transforms # ["router:json:0.20"] ``` ### `get(key, full?)` [#getkey-full] Retrieve content. Returns the compressed version by default, or the original with `full=True`. ```ts twoslash import { SharedContext } from "headroom-ai"; const ctx = new SharedContext(); // ---cut--- const compressed = ctx.get("findings"); // 4K tokens const original = ctx.get("findings", { full: true }); // 20K tokens const missing = ctx.get("nonexistent"); // null ``` ```python compressed = ctx.get("findings") # 4K tokens original = ctx.get("findings", full=True) # 20K tokens missing = ctx.get("nonexistent") # None ``` ### `stats()` [#stats] Aggregated statistics across all entries. ```ts twoslash import { SharedContext } from "headroom-ai"; const ctx = new SharedContext(); // ---cut--- const stats = ctx.stats(); stats.entries; // 3 stats.totalOriginalTokens; // 60000 stats.totalCompressedTokens; // 12000 stats.totalTokensSaved; // 48000 stats.savingsPercent; // 80.0 ``` ```python stats = ctx.stats() stats.entries # 3 stats.total_original_tokens # 60000 stats.total_compressed_tokens # 12000 stats.total_tokens_saved # 48000 stats.savings_percent # 80.0 ``` ### `keys()` and `clear()` [#keys-and-clear] `keys()` lists all non-expired keys. `clear()` removes all entries. ## Configuration [#configuration] ```ts twoslash import { SharedContext } from "headroom-ai"; // ---cut--- const ctx = new SharedContext({ model: "claude-sonnet-4-5-20250929", // For token counting ttl: 3600, // 1 hour (default) maxEntries: 100, // Evicts oldest when full }); ``` ```python ctx = SharedContext( model="claude-sonnet-4-5-20250929", # For token counting ttl=3600, # 1 hour (default) max_entries=100, # Evicts oldest when full ) ``` Entries expire after `ttl` seconds. When `maxEntries` is reached, the oldest entry is evicted. ## Framework Examples [#framework-examples] SharedContext is framework-agnostic. It works anywhere context moves between agents. ### CrewAI [#crewai] ```python from headroom import SharedContext ctx = SharedContext() # After researcher task completes ctx.put("findings", researcher_task.output.raw) # Coder task gets compressed context coder_context = ctx.get("findings") ``` ### LangGraph [#langgraph] ```python from headroom import SharedContext ctx = SharedContext() def researcher_node(state): result = do_research() ctx.put("research", result) return {"research_summary": ctx.get("research")} def coder_node(state): # Compressed summary in state, full details on demand full = ctx.get("research", full=True) return {"code": write_code(full)} ``` ### OpenAI Agents SDK [#openai-agents-sdk] ```python from headroom import SharedContext ctx = SharedContext() def compress_handoff(messages): for msg in messages: if len(msg.content) > 1000: ctx.put(msg.id, msg.content) msg.content = ctx.get(msg.id) return messages handoff(agent=coder, input_filter=compress_handoff) ``` ## How It Works [#how-it-works] Under the hood, `put()` calls `headroom.compress()` -- the same pipeline used by the Headroom proxy -- and stores the original in memory. `get()` returns the compressed version. `get(full=True)` returns the original. The compression pipeline routes content to the best compressor: - **JSON arrays** -- SmartCrusher (70-95% compression) - **Code** -- CodeCompressor (AST-aware) - **Text** -- Kompress (ModernBERT-based) or passthrough Simulation mode lets you preview what Headroom would do to your messages without sending them to an LLM. This is useful for cost estimation, debugging compression behavior, and understanding where token waste comes from. ## Basic Usage [#basic-usage] ```ts twoslash import { compress } from 'headroom-ai'; // compress() returns the same result structure — // use it without sending to your LLM to simulate const result = await compress(messages, { model: 'gpt-4o' }); console.log(`Would save: ${result.tokensSaved} tokens`); console.log(`Compression ratio: ${(result.compressionRatio * 100).toFixed(1)}%`); console.log(`Transforms: ${result.transformsApplied.join(', ')}`); ``` ```python plan = client.chat.completions.simulate( model="gpt-4o", messages=large_conversation, ) print(f"Tokens before: {plan.tokens_before}") print(f"Tokens after: {plan.tokens_after}") print(f"Would save: {plan.tokens_saved} tokens ({plan.tokens_saved/plan.tokens_before*100:.1f}%)") print(f"Transforms: {plan.transforms}") ``` ## Waste Signals [#waste-signals] Simulation reports where token waste comes from in your messages: ```python plan = client.chat.completions.simulate( model="gpt-4o", messages=messages, ) waste = plan.waste_signals # dict[str, int] print(f"JSON bloat: {waste['json_bloat']} tokens") print(f"HTML noise: {waste['html_noise']} tokens") print(f"Whitespace: {waste['whitespace']} tokens") print(f"Dynamic dates: {waste['dynamic_date']} tokens") print(f"Repetition: {waste['repetition']} tokens") ``` Waste signals help you understand which parts of your input are contributing the most unnecessary tokens. ## Block Breakdown [#block-breakdown] The parser breaks your conversation into blocks so you can see where tokens are concentrated: ```python # Block types: system, user, assistant, tool_call, tool_result, rag # The breakdown shows token counts per block type ``` | Block Kind | Description | |-----------|-------------| | `system` | System prompt instructions | | `user` | User messages | | `assistant` | Model responses | | `tool_call` | Function call requests | | `tool_result` | Tool output (largest source of waste) | | `rag` | Retrieved document context | ## Use Cases [#use-cases] ### Cost Estimation [#cost-estimation] Run simulation on a representative sample of your workload to estimate savings before enabling `optimize` mode: ```python import json total_before = 0 total_after = 0 for messages in sample_conversations: plan = client.chat.completions.simulate( model="gpt-4o", messages=messages, ) total_before += plan.tokens_before total_after += plan.tokens_after savings_pct = (1 - total_after / total_before) * 100 print(f"Estimated savings: {savings_pct:.1f}%") print(f"Tokens saved: {total_before - total_after:,}") ``` ### Debugging Compression [#debugging-compression] Use simulation to understand why a particular conversation is or is not being compressed: ```python plan = client.chat.completions.simulate( model="gpt-4o", messages=messages, ) if plan.tokens_saved == 0: print("No compression applied. Possible reasons:") print("- Messages are too short (< 200 tokens per tool output)") print("- No tool outputs with compressible JSON arrays") print("- Content is already compact (code, grep results)") else: print(f"Transforms applied: {plan.transforms}") # See the optimized messages print(json.dumps(plan.messages_optimized, indent=2)) ``` ### Comparing Configurations [#comparing-configurations] Test different configurations to find the best settings for your workload: ```python from headroom import HeadroomClient, HeadroomConfig, OpenAIProvider from headroom.transforms import SmartCrusherConfig configs = [ SmartCrusherConfig(max_items_after_crush=10), SmartCrusherConfig(max_items_after_crush=25), SmartCrusherConfig(max_items_after_crush=50), ] for smart_crusher_config in configs: client = HeadroomClient( original_client=OpenAI(), provider=OpenAIProvider(), config=HeadroomConfig(smart_crusher=smart_crusher_config), ) plan = client.chat.completions.simulate(model="gpt-4o", messages=messages) print(f"max_items={smart_crusher_config.max_items_after_crush}: " f"{plan.tokens_saved} tokens saved ({plan.tokens_saved/plan.tokens_before*100:.1f}%)") ``` Simulation never calls the LLM API. It runs the full transform pipeline locally and returns the results, so there is no cost and no latency from the provider. SmartCrusher is Headroom's compressor for JSON tool outputs. This is the compressor that fires automatically when ContentRouter detects JSON arrays. By default SmartCrusher tries a **lossless** compaction first: if the array is cleanly tabular (a consistent set of keys across items), it re-encodes every item into a compact `csv-schema` form and keeps it *if that saves at least 30% of the bytes* -- no items are dropped, they're just represented more compactly. Only when that lossless fold doesn't clear the 30% bar (or the array isn't cleanly tabular) does SmartCrusher fall back to the scored keep/drop behavior described below. See `headroom/transforms/smart_crusher.py:415-419`. Set `with_compaction=False` on `SmartCrusher(...)` to force the pre-lossless keep/drop-only path (this is also what Headroom's retention-property tests use). ## How It Works [#how-it-works] When the lossy keep/drop path runs, SmartCrusher doesn't blindly truncate arrays -- it scores each item across five dimensions: 1. **First/Last items** -- Context for pagination and recency 2. **Error items** -- 100% preservation of error states (never dropped) 3. **Anomalies** -- Statistical outliers (> 2 standard deviations from the mean) 4. **Relevant items** -- Matches to the user's query via BM25/embeddings 5. **Change points** -- Significant transitions in data On that path, the target size is `max_items_after_crush` (15 by default): a 1,000-item array becomes ~15 items with all the information the LLM actually needs (measured: a 1,000-item array with `with_compaction=False` compressed to 16 items). ## What Gets Preserved [#what-gets-preserved] | Category | Preserved | Why | |---|---|---| | Errors | 100% | Critical for debugging | | First N | 100% | Context and pagination | | Last N | 100% | Recency | | Anomalies | All | Unusual values matter | | Relevant | Top K | Match user's query | | Others | Sampled | Statistical representation | ### JSON objects and unchanged values [#json-objects-and-unchanged-values] Object keys are never sampled or dropped: named fields are not interchangeable array rows. Removing a key without a retrieval marker would make its value unrecoverable. SmartCrusher instead recurses into each value, so nested arrays can still use lossless table compaction or CCR-backed row offload, and opaque strings can still be replaced by retrievable CCR references. A wide object containing only unchanged values therefore has **zero structural compression savings**, regardless of how many keys it contains. This is a deliberate reversibility tradeoff, not a promise that every large JSON object will shrink. Key-level offload would need its own retrieval contract before object-key sampling could safely return. When recursive processing leaves a valid JSON value unchanged, SmartCrusher returns its original bytes and sets `was_modified=False`. This applies to objects, arrays, and scalars, including whitespace, Unicode escapes, and numeric representations. Actual structural transformations still use compact JSON serialization. The routing pipeline then applies the existing Rust `JsonMinifier` as a separate lossless step when SmartCrusher leaves routed JSON unchanged. Savings are attributed to `json_minifier`, not `smart_crusher`, and do not create structural TOIN records. Minification removes only whitespace outside strings: key order, duplicate keys, string escapes, and number spellings remain byte-stable. Repeating the same input produces the same output; already-minified JSON is a no-op. The native live-zone JSON-array arm follows the same explicit step; its existing content-type and token-reduction gates still apply. Because no information is offloaded, lossless minification does not store an original or append a CCR retrieval marker: the forwarded value remains valid JSON. A mixed document still reports `mixed` when its sections take different routes, even if only its JSON section saves tokens. Minification takes precedence over lossy fallbacks when it saves tokens; already-compact input can still reach the existing fallback chain. For tool results with CCR enabled, each changed lossy mixed section must carry its own retrieval marker. Embedded JSON carries reversibility separately for each adopted span: lossless minification needs no marker, while a marker on another span or section does not authorize dropping its data. Candidates rejected by the token benefit gate do not affect the adopted spans' reversibility. Rejected mixed results do not enter the tool-result cache. ### Comparing historical TOIN data [#comparing-historical-toin-data] Unchanged values do not produce new TOIN compression records, even when the strategy contains a diagnostic about a skipped transformation. Older TOIN aggregates may include object-scoring strategies, formatting-only changes, or zero-savings `lossless_only` decisions from different implementations. Do not compare those aggregates directly with current object-key-preserving results or treat zero-savings decisions as successful compression. Compare the same corpus and configuration on both revisions, report unchanged wide objects separately from objects with transformed descendants, and identify the engine revision. Existing TOIN history is retained; this change does not rewrite or delete a user's learned state. ## Quick Start [#quick-start] ```ts twoslash import { compress } from "headroom-ai"; // SmartCrusher fires automatically for JSON tool outputs const messages = [ { role: "system" as const, content: "You are a helpful assistant." }, { role: "user" as const, content: "Find errors in the last 24 hours" }, { role: "tool" as const, content: JSON.stringify({ results: new Array(1000).fill({ status: "ok" }) }), tool_call_id: "call_1", }, ]; const result = await compress(messages); console.log(`Tokens saved: ${result.tokensSaved}`); // SmartCrusher keeps errors, anomalies, and relevant items ``` ```python import json from headroom import SmartCrusher crusher = SmartCrusher() # crush() takes a JSON string, not a dict tool_output = json.dumps({"results": ["...1000 items..."]}) # Returns a CrushResult; the compressed JSON string is `.compressed` result = crusher.crush(tool_output, query="user's question") print(result.compressed) print(result.strategy) # e.g. "lossless:table(...)" or "smart_sample(1000->15)" ``` ### Recovered tool output in the Python message transform [#recovered-tool-output-in-the-python-message-transform] `SmartCrusher.apply(messages, tokenizer)` preserves recognized `headroom_retrieve` results byte for byte when their call ID matches the originating assistant call. The originating identity is resolved with the shared wrapper parser for both OpenAI `tool_calls`/`arguments` and Anthropic `tool_use`/`input`, including Hermes `tool_call` bridges and retrieval calls inside `execute`, `exec`, or `functions.exec`. Other wrapper output remains compressible; an unrecognized or ambiguous wrapper does not gain a blanket recovery exemption. Keep the originating assistant call in the message history so the result can be identified. The context-free `crush(json_string)` API has no originating-call history and does not provide this message-level exemption. ## Configuration [#configuration] ```ts twoslash import { compress } from "headroom-ai"; // Configure via the Headroom proxy or HeadroomClient const result = await compress(messages, { model: "gpt-4o", tokenBudget: 10000, // SmartCrusher will reduce JSON to fit }); console.log(`Transforms: ${result.transformsApplied}`); // ["smart_crusher", "cache_aligner"] ``` ```python from headroom import SmartCrusher, SmartCrusherConfig config = SmartCrusherConfig( min_tokens_to_crush=200, # Only compress if > 200 tokens max_items_after_crush=15, # Keep at most 15 items (lossy path only) first_fraction=0.3, # Keep first 30% of items last_fraction=0.15, # Keep last 15% of items variance_threshold=2.0, # Statistical variance threshold preserve_change_points=True, # Keep significant transitions ) crusher = SmartCrusher(config) result = crusher.crush(tool_output, query="find payment failures") ``` ## Configuration Options [#configuration-options] | Option | Default | Description | |---|---|---| | `min_tokens_to_crush` | `200` | Only compress arrays with more than this many tokens | | `min_items_to_analyze` | `5` | Minimum items before analyzing for compression | | `max_items_after_crush` | `15` | Maximum items to keep after compression | | `variance_threshold` | `2.0` | Statistical variance threshold for analysis | | `uniqueness_threshold` | `0.1` | Uniqueness threshold for deduplication | | `similarity_threshold` | `0.8` | Similarity threshold for grouping | | `preserve_change_points` | `True` | Preserve significant transitions in data | | `first_fraction` | `0.3` | Fraction of items always kept from the start | | `last_fraction` | `0.15` | Fraction of items always kept from the end | | `dedup_identical_items` | `True` | Deduplicate identical items | | `use_feedback_hints` | `True` | Use TOIN feedback hints for scoring | ## Example: Before and After [#example-before-and-after] Consider a tool that returns 1,000 search results: ```python # Before compression: 45,000 tokens { "results": [ {"id": 1, "status": "ok", "message": "Success", "timestamp": "..."}, {"id": 2, "status": "ok", "message": "Success", "timestamp": "..."}, # ... 995 more "ok" results ... {"id": 998, "status": "error", "message": "Connection timeout", "timestamp": "..."}, {"id": 999, "status": "ok", "message": "Success", "timestamp": "..."}, {"id": 1000, "status": "ok", "message": "Success", "timestamp": "..."}, ] } # After SmartCrusher's lossy keep/drop path (with_compaction=False): # ~15-16 items kept -- first items, last items, and the error at id=998 ``` The LLM sees the structure, the error, and a representative sample -- everything it needs to answer "find errors in the last 24 hours" without wading through 1,000 identical success responses. Note this specific shape (mostly-identical items, one error) is also exactly what the lossless table fold favors (see the callout above), so with default settings this array is more likely to be re-encoded compactly with every item still present than reduced to ~15 items -- the numbers above illustrate the scored keep/drop path, not a guarantee of which path a given array takes. You don't need to call SmartCrusher directly. The ContentRouter detects JSON arrays and routes them to SmartCrusher automatically. Direct usage is available when you want fine-grained control over the configuration. Headroom integrates with [Strands Agents](https://github.com/strands-agents/sdk-python) through two patterns: wrap the model for full conversation compression, or hook into tool calls for targeted tool output compression. ## Installation [#installation] ```bash pip install headroom-ai strands-agents ``` ## Quick start [#quick-start] ```python from strands import Agent from strands.models.bedrock import BedrockModel from headroom.integrations.strands import HeadroomStrandsModel model = BedrockModel(model_id="us.anthropic.claude-sonnet-4-20250514-v1:0") optimized = HeadroomStrandsModel(wrapped_model=model) agent = Agent(model=optimized) response = agent("Investigate the production incident") print(f"Tokens saved: {optimized.total_tokens_saved}") ``` ## Model wrapping [#model-wrapping] Wraps the Strands `Model` interface. Every call to `stream()` compresses messages before they reach the provider: ```python from headroom import HeadroomConfig from headroom.integrations.strands import HeadroomStrandsModel optimized = HeadroomStrandsModel( wrapped_model=model, config=HeadroomConfig(), ) agent = Agent(model=optimized) response = agent("Analyze these logs") ``` ## Hook provider (tool output compression) [#hook-provider-tool-output-compression] Compresses tool call results via Strands' hook system. Uses SmartCrusher on JSON arrays returned by tools: ```python from strands import Agent from strands.models.bedrock import BedrockModel from headroom.integrations.strands import HeadroomHookProvider model = BedrockModel(model_id="us.anthropic.claude-sonnet-4-20250514-v1:0") hooks = HeadroomHookProvider( compress_tool_outputs=True, min_tokens_to_compress=200, preserve_errors=True, ) agent = Agent(model=model, hooks=[hooks]) response = agent("Search the database for recent failures") print(f"Tokens saved by hooks: {hooks.total_tokens_saved}") ``` The hook preserves error items, anomalous values (statistical outliers), items matching the query context, and boundary items (first/last). ## Both together [#both-together] Model wrapping compresses conversation history. Hooks compress individual tool results. Use both for maximum savings: ```python from headroom.integrations.strands import HeadroomStrandsModel, HeadroomHookProvider optimized = HeadroomStrandsModel(wrapped_model=model) hooks = HeadroomHookProvider(compress_tool_outputs=True) agent = Agent(model=optimized, hooks=[hooks]) ``` ## How it works [#how-it-works] ``` Agent decides to call tool | v Tool executes, returns result | v HeadroomHookProvider (optional) compresses tool result JSON | v Agent builds next API request | v HeadroomStrandsModel.stream() compresses full message list | v Provider API (Bedrock, etc.) ``` The model wrapper uses the full Headroom pipeline (CacheAligner, ContentRouter). The hook provider uses SmartCrusher directly for fast JSON compression. ## Structured output [#structured-output] `structured_output()` is an async generator (it delegates to the wrapped model's own `structured_output()`), so it must be consumed with `async for`: ```python from pydantic import BaseModel class Analysis(BaseModel): severity: str root_cause: str recommendation: str async for event in optimized.structured_output(Analysis, messages): result = event ``` ## Metrics [#metrics] ```python for m in optimized.metrics_history: print(f" {m.tokens_before} -> {m.tokens_after} ({m.tokens_saved} saved)") print(f"Total saved: {optimized.total_tokens_saved}") ``` ## Supported providers [#supported-providers] | Strands Model | Provider Detected | |--------------|-------------------| | `BedrockModel` | Anthropic (via Bedrock) | | `OllamaModel` | OpenAI-compatible | | Custom `Model` | Falls back to estimation | **Not seeing the savings you want?** Headroom OSS is a framework. It was built to show what's possible on one developer's laptop, so it saves less than it could. We are building bigger savings for teams and enterprises in Headroom Enterprise, which adds: - More cost savings - Tokens secured - On premise deployment - Deep attribution - And many more features If you're a team or an enterprise, **talk to us**: [hello@headroomlabs.ai](mailto:hello@headroomlabs.ai) · [headroomlabs.ai](https://headroomlabs.ai) Everything in the open-source repo stays open source under Apache 2.0. Headroom provides specialized compressors for text-based content that isn't JSON or source code. Each one understands the structure of its content type and preserves what the LLM needs while dropping the noise. | Compressor | Input Type | What It Preserves | Typical Savings | |---|---|---|---| | `SearchCompressor` | grep/ripgrep output | Relevant matches, file diversity | 80-95% (measured ~91% on a synthetic 40-file corpus) | | `LogCompressor` | Build/test logs | Errors, stack traces, summaries | 85-95% (measured ~93% on a synthetic 1,000-line log) | | `DiffCompressor` | Unified diffs | Changed lines, context | 60-80% (measured ~70% on a multi-file diff) | | `TextCrusher` | General text | Relevant sentences, anchors | 30-60% (its `target_ratio` defaults to 0.5; measured ~50% on real docs prose) | | `KompressCompressor` | General text fallback | Learned token scoring via ONNX | Highly variable -- `target_ratio` defaults to `None` ("model decides"), which measured only 6-24% on real prose; an explicit `target_ratio=0.5` measured ~40%. There is no fixed default range; see `benchmarks/text_crusher_quality_eval.py`. | ## SearchCompressor [#searchcompressor] Compresses search results (grep, ripgrep, ag) while keeping the matches that matter. ```python from headroom.transforms import SearchCompressor search_results = """ src/utils.py:42:def process_data(items): src/utils.py:43: \"\"\"Process items.\"\"\" src/models.py:15:class DataProcessor: src/models.py:89: def process(self, items): ... hundreds more matches ... """ compressor = SearchCompressor() result = compressor.compress(search_results, context="find process") print(f"Compressed {result.original_match_count} matches to {result.compressed_match_count}") print(result.compressed) ``` **What gets preserved:** - Exact query matches (lines containing the search term) - High-relevance matches (scored by BM25 similarity) - File diversity (results from different files are kept) - First/last matches (context from start and end) ### Configuration [#configuration] ```python from headroom.transforms import SearchCompressor, SearchCompressorConfig config = SearchCompressorConfig( max_total_matches=30, # Cap total matches kept across all files max_matches_per_file=5, # Cap matches kept per file (diversity) max_files=15, # Cap number of distinct files kept boost_errors=True, # Prioritize lines that look like errors context_keywords=["auth"], # Extra terms to bias selection toward ) compressor = SearchCompressor(config) ``` ## LogCompressor [#logcompressor] Compresses build and test output while preserving errors, warnings, and summaries. ```python from headroom.transforms import LogCompressor build_output = """ ===== test session starts ===== collected 500 items tests/test_foo.py::test_1 PASSED ... hundreds of passed tests ... tests/test_bar.py::test_fail FAILED AssertionError: expected 5, got 3 ===== 1 failed, 499 passed ===== """ compressor = LogCompressor() result = compressor.compress(build_output) print(result.compressed) print(f"Compression ratio: {result.compression_ratio:.1%}") ``` **What gets preserved:** - Errors and failures (any line with ERROR, FAILED, Exception) - Warnings - Full stack traces for debugging - Test/build summary lines - Section headers (structural markers like `=====`) **What gets dropped:** - Hundreds of `PASSED` lines - Verbose success output - Repeated patterns Omission markers count `ERROR`, `FAIL`, `WARN`, and `INFO` levels only among the dropped lines, not the full input. An error retained in the compressed output is therefore not advertised as omitted. Debug, trace, and unclassified lines are not listed in the level breakdown. When only debug, trace, or unclassified lines are dropped, the marker is simply `[N lines omitted]`, without a severity breakdown. `result.stats` continues to report whole-input totals; the marker's narrower counts do not change those statistics. CCR still retains the exact original log when enabled. ## DiffCompressor [#diffcompressor] Compresses unified diffs while keeping the actual changes and enough context to understand them. ```python from headroom.transforms import DiffCompressor diff_output = """ diff --git a/src/main.py b/src/main.py --- a/src/main.py +++ b/src/main.py @@ -42,7 +42,7 @@ def process(items): - return [x for x in items] + return [x.strip() for x in items if x] """ compressor = DiffCompressor() result = compressor.compress(diff_output) ``` ## TextCrusher [#textcrusher] Extractive prose compression -- it keeps the most relevant input sentences verbatim (selection, not rewriting). Best for documentation, README files, and prose content. `TextCrusher` lives in its own module rather than the `headroom.transforms` package root: ```python from headroom.transforms.text_crusher import TextCrusher long_text = """ ... thousands of lines of documentation ... """ compressor = TextCrusher() result = compressor.compress(long_text, context="authentication") print(result.compressed) print(f"{result.original_tokens} -> {result.compressed_tokens} tokens") ``` **What gets preserved:** - Paragraphs relevant to the context query - Headers and section markers - Document structure and organization ## Kompress [#kompress] ```python from headroom.transforms.kompress_compressor import KompressCompressor compressor = KompressCompressor() result = compressor.compress(long_output) print(f"Before: {result.original_tokens} tokens") print(f"After: {result.compressed_tokens} tokens") print(f"Saved: {result.savings_percentage:.1f}%") ``` The old LLMLingua transform and helper functions are no longer exported. Use Kompress and ContentRouter for text compression. ## Content Type Detection [#content-type-detection] If you're building your own routing logic, you can use the content type detector directly: ```python from headroom.transforms import detect_content_type, ContentType, SearchCompressor, LogCompressor # A single file:line:content line isn't enough signal -- the detector wants # several matching lines before it calls something SEARCH_RESULTS. content = "src/main.py:42:def process():\nsrc/utils.py:10:def helper():\nsrc/models.py:5:class Foo:" detection = detect_content_type(content) if detection.content_type == ContentType.SEARCH_RESULTS: result = SearchCompressor().compress(content, context="process") elif detection.content_type == ContentType.BUILD_OUTPUT: result = LogCompressor().compress(content) elif detection.content_type == ContentType.PLAIN_TEXT: from headroom.transforms.text_crusher import TextCrusher result = TextCrusher().compress(content, context="process") ``` ## When Each Compressor Is Used [#when-each-compressor-is-used] The ContentRouter selects the right compressor automatically. Here's when each fires: | Content Pattern | Compressor | Detection Signal | |---|---|---| | `file:line:content` lines | SearchCompressor | grep/ripgrep output format | | `file-line-content` context lines (`grep -A/-B/-C`) | SearchCompressor | grep/ripgrep context-line format (same section carving as match lines; `file:line:content` and context lines may mix with `--` separators) | | pytest, npm, cargo markers | LogCompressor | Build tool output patterns | | `---/+++` and `@@` markers | DiffCompressor | Unified diff format | | Prose, documentation | KompressCompressor | ContentRouter's default for `PLAIN_TEXT` (`CompressionStrategy.TEXT` and `KOMPRESS` share the same ML compressor dispatch -- see `headroom/transforms/content_router.py:3524-3540`) | | Long plain text that exceeds Kompress's size gate | TextCrusher | Request-path-safe fallback when the content is too large for synchronous ONNX inference (`_kompress_max_tokens`); TextCrusher is not ContentRouter's default path for ordinary prose | ## Performance [#performance] Speed depends heavily on input size and shape; the figures below are one measurement each (warm process, this package version), not a guarantee: | Compressor | Measured Input | Measured Output | Measured Speed (warm) | |---|---|---|---| | SearchCompressor | 1,000 matches across 60 files | 30 matches | ~2.0ms | | LogCompressor | 5,000 lines, ~12 errors | 67 lines | ~17ms | | DiffCompressor | 30-file / 90-hunk synthetic diff | changed hunks only | ~0.4ms | | TextCrusher | 10,000 chars of prose | ~50% of input tokens (`target_ratio` default) | ~0.7ms | | KompressCompressor | Plain text | Highly variable (see the ranges above) | model-dependent; first call after process start pays a one-time model-load cost | Solutions for common Headroom issues. ## Proxy Server Issues [#proxy-server-issues] ### Proxy will not start [#proxy-will-not-start] **Symptom**: `headroom proxy` fails or hangs. ```bash # Check if port is already in use lsof -i :8787 # Try a different port headroom proxy --port 8788 # Check for missing dependencies pip install "headroom-ai[proxy]" # Run with debug logging headroom proxy --log-file ~/.headroom/logs/proxy.jsonl --log-messages ``` ### Connection refused when calling proxy [#connection-refused-when-calling-proxy] **Symptom**: `curl: (7) Failed to connect to localhost port 8787` ```bash # Verify proxy is running curl http://localhost:8787/health # Check if proxy started on a different port ps aux | grep headroom ``` ### Dashboard / doctor point at the wrong port [#dashboard--doctor-point-at-the-wrong-port] **Symptom**: `headroom wrap ...` reports the proxy on `8788` (or another port), but `headroom dashboard` opens `http://127.0.0.1:8787/dashboard`, which shows nothing. `wrap` does not always use `8787`: a Copilot-subscription session takes a dedicated port so it never claims the shared one, and any wrap falls forward to the next free port when `8787` is already bound. `headroom dashboard` and `headroom inspect` auto-detect the most recently started live wrap session, so this normally resolves itself. `headroom doctor` keeps the requested port and reports a live proxy found elsewhere, along with the command to rerun it against that port. If a read command still lands on the wrong port, an explicit value is overriding detection — check it: ```bash # An exported HEADROOM_PORT always wins over auto-detection echo $HEADROOM_PORT # Which ports have live wrap sessions registered ls ~/.headroom/clients/ # Confirm which one actually answers curl -s http://127.0.0.1:8788/health # Force a specific port headroom dashboard --port 8788 ``` ### Proxy returns errors for some requests [#proxy-returns-errors-for-some-requests] **Symptom**: Some requests work, others fail with 502/503. ```bash # Check proxy logs for the actual error headroom proxy --log-file ~/.headroom/logs/proxy.jsonl --log-messages # Verify API key is set echo $OPENAI_API_KEY # or ANTHROPIC_API_KEY # Test the underlying API directly curl https://api.openai.com/v1/models \ -H "Authorization: Bearer $OPENAI_API_KEY" ``` ### Windows: ML content detection hangs or silently falls back [#windows-ml-content-detection-hangs-or-silently-falls-back] **Symptom**: On Windows 11 24H2+, every proxied request stalls (historically `Optimization failed: TimeoutError`), or the first detection in each process burns ~5 seconds and compression quality drops because detection runs on the non-ML fallback tiers. The proxy log may show `magika ONNX session init timed out`. **Cause**: The Rust core loads ONNX Runtime dynamically. Without `ORT_DYLIB_PATH`, the bare Windows DLL search resolves `onnxruntime.dll` to `C:\Windows\System32\onnxruntime.dll` — the Windows ML OS component (1.17.x), which deadlocks ort session initialization instead of returning an error. **Fix**: Headroom pins `ORT_DYLIB_PATH` automatically at import time to the DLL inside the `onnxruntime` pip package (included in `headroom-ai[proxy]`). Confirm in the startup log: ``` Pinned ORT_DYLIB_PATH to bundled ONNX Runtime: ...\onnxruntime\capi\onnxruntime.dll ``` If the pin is skipped (library install without `onnxruntime`), either install it or point the variable at any modern ONNX Runtime yourself: ```powershell pip install onnxruntime # or $env:ORT_DYLIB_PATH = "C:\path\to\onnxruntime.dll" ``` `HEADROOM_MAGIKA_INIT_TIMEOUT_SECS` (default `5`) bounds the init as a safety net; on timeout detection degrades to non-ML tiers for the process lifetime. ## No Token Savings [#no-token-savings] **Symptom**: `stats['session']['tokens_saved_total']` is 0. **Diagnosis**: ```python stats = client.get_stats() print(f"Mode: {stats['config']['mode']}") # Should be "optimize" print(f"SmartCrusher: {stats['transforms']['smart_crusher_enabled']}") ``` **Common causes**: - Mode is `audit` (observation only, no modifications) - Messages do not contain tool outputs - Tool outputs are below the 200-token threshold - Data is not compressible (high uniqueness, code, grep results) **Solutions**: ```ts twoslash import { compress } from 'headroom-ai'; // Ensure the proxy is running in optimize mode // (default, unless --no-optimize was passed) const result = await compress(messages, { model: 'gpt-4o' }); console.log(`Saved: ${result.tokensSaved} tokens`); console.log(`Compressed: ${result.compressed}`); ``` ```python # 1. Ensure mode is "optimize" client = HeadroomClient( original_client=OpenAI(), provider=OpenAIProvider(), default_mode="optimize", # NOT "audit" ) # 2. Or override per-request response = client.chat.completions.create( model="gpt-4o", messages=messages, headroom_mode="optimize", ) # 3. Lower the compression threshold config = HeadroomConfig() config.smart_crusher.min_tokens_to_crush = 100 # Default is 200 ``` ### Dashboard shows 0 compressed/saved tokens after upgrading to 0.31.0 [#dashboard-shows-0-compressedsaved-tokens-after-upgrading-to-0310] **Symptom**: After upgrading from 0.27.0 to 0.31.0, the dashboard's compression / "Tokens Saved" figures read ~0, even though total token spend is the same or **lower** than before. **Cause**: This is a default-mode change, not a regression. 0.31.0 ships the `coding` savings profile as the out-of-box default, and `coding` runs the proxy in **cache** mode (see [Savings profiles](/docs/proxy#savings-profiles)). Cache mode freezes the provider prefix and compresses only the newest turn *delta* — this deliberately avoids busting the provider's prompt cache. So the **compression** figure is small, and the savings shift to **cheaper prefix-cache reads** (cached input tokens are billed at a fraction of list price). On a short prompt there is little delta to compress, so the compression tile can read ~0 while your actual cost still drops. **Where the savings show up**: Look at the **Prefix Cache Impact** panel and the **Compression vs Cache** tile on the dashboard — these reflect cache-read savings rather than per-request compression. The headline "Tokens Saved" tile only counts compression, so in cache mode it understates the real benefit. **To get 0.27.0-style compression numbers back**: run the proxy in token mode, which prioritizes visible compression. Use this to compare or diagnose, not as a permanent setting — `cache` is the default because prefix-cache stability is usually worth more on a real agent workload than a larger headline compression number: ```bash # Per run headroom proxy --mode token # Or pick a token-mode profile HEADROOM_SAVINGS_PROFILE=balanced headroom proxy # ~70% target HEADROOM_SAVINGS_PROFILE=agent-90 headroom proxy # ~90% target ``` Trade-off: token mode maximizes visible compression but rewrites prior turns, which can reduce provider prefix-cache hits — so raw compression goes up while cache-read savings go down. Cache mode is the default because, for long coding sessions, preserving the prefix cache usually wins overall. ## Claude Code context window is larger through the proxy [#claude-code-context-window-is-larger-through-the-proxy] **Symptom**: After pointing Claude Code at Headroom (`ANTHROPIC_BASE_URL`), `/context all` shows **more** tokens used than a direct session — the "System tools" and "MCP tools" lines grow by tens of thousands of tokens, before you send any message. **Cause**: Claude Code normally defers most tool schemas behind its server-side **Tool Search Tool** (it sends only tool *names* and loads full schemas on demand). It enables this only when it believes it is talking directly to `api.anthropic.com`. The moment `ANTHROPIC_BASE_URL` is a custom host, Claude Code can't assume the endpoint supports the feature, so it falls back to **eagerly** materializing every tool schema into the local context window. This is a Claude Code client-side decision made before the request reaches the proxy — no proxy header can reverse it. **Solution**: set `ENABLE_TOOL_SEARCH` so Claude Code keeps deferring tools through the proxy. The proxy forwards the `tool_reference` blocks correctly, so deferral works end-to-end (both streaming and non-streaming, subscription and API-key auth). ```bash # Easiest: `headroom wrap claude` sets ENABLE_TOOL_SEARCH=true automatically. headroom wrap claude # Choose the mode (true = always defer, the default; auto / auto:N = defer only # when tool definitions exceed N% of the budget; false = off): headroom wrap claude --tool-search auto # Running `claude` manually instead of via wrap? Set it yourself: ENABLE_TOOL_SEARCH=true ANTHROPIC_BASE_URL=http://localhost:8787 claude ``` **Verify (before / after)** with `/context all` in a fresh session, no messages sent: | Section | Eager (no `ENABLE_TOOL_SEARCH`) | Deferred (`ENABLE_TOOL_SEARCH=true`) | | --- | --- | --- | | System tools | fully materialized | deferred subset | | MCP tools | every tool shows a token cost | `(loaded on-demand)`, 0 tokens | When deferral is off, the proxy log also prints a one-time hint naming the fix. See [issue #746](https://github.com/headroomlabs-ai/headroom/issues/746) for the full analysis. Anthropic's VSCode extension webview does not currently render the deferred-tool content blocks that `ENABLE_TOOL_SEARCH=true` enables through Headroom. Tool results can show up as `unsupported content type` in the extension even though the standalone `claude` CLI works correctly. If you use Claude Code inside VSCode, set `ENABLE_TOOL_SEARCH=false` for that target and restart the Headroom deployment. See [issue #2028](https://github.com/headroomlabs-ai/headroom/issues/2028). ## Remote Control unavailable through custom ANTHROPIC_BASE_URL [#remote-control-unavailable-through-custom-anthropic_base_url] **Symptom**: When Claude Code runs with `ANTHROPIC_BASE_URL` set to a custom host (for example, Headroom), the Remote Control menu is absent. Invoking `/remote-control` reports `Remote Control is only available when using Claude via api.anthropic.com`. **Cause**: A client-side gate, evaluated before any traffic reaches Headroom. Claude Code v2.1.196+ parses `ANTHROPIC_BASE_URL` and compares the host against `api.anthropic.com` exactly, so a loopback address fails it. The check is satisfied by any one of: - `ANTHROPIC_BASE_URL` unset, or naming `api.anthropic.com` as its host, or - `ANTHROPIC_UNIX_SOCKET` pointing at a Unix domain socket — but see [below](#why-the-unix-socket-route-does-not-help): it passes this check and fails a different one. Note that Remote Control does **not** honor `_CLAUDE_CODE_ASSUME_FIRST_PARTY_BASE_URL`, the flag that re-enables the 1M window and on-demand tool loading — it reads the base URL directly. Headroom cannot re-enable Remote Control from the server side the way `--1m` restores the `context-1m` beta header, because the eligibility decision and the bridge connection both happen in the client. **Fix**: Either launch Claude directly, without `ANTHROPIC_BASE_URL`, for sessions that need Remote Control and use Headroom for the rest, or keep both with the [HTTPS proxy route](#keep-remote-control-with-the-https-proxy-route) below. ### Keep Remote Control with the HTTPS proxy route [#keep-remote-control-with-the-https-proxy-route] Leave `ANTHROPIC_BASE_URL` unset, so the host check passes, and reach Headroom through `HTTPS_PROXY` instead — the [enterprise proxy path](https://code.claude.com/docs/en/network-config) Claude Code supports. A local [mitmproxy](https://mitmproxy.org/) decrypts `api.anthropic.com` only, with its CA trusted through `NODE_EXTRA_CA_CERTS`, and an addon hands the `/v1/messages*` calls to Headroom. Every other host, including `claude.ai` where the Remote Control bridge lives, stays a raw tunnel, and subscription auth is untouched. ```bash mitmdump --listen-host 127.0.0.1 --listen-port 8790 \ --allow-hosts '^api\.anthropic\.com(:443)?$' \ -s examples/claude_code_remote_control/route_to_headroom.py ``` ```json { "env": { "HTTPS_PROXY": "http://127.0.0.1:8790", "NO_PROXY": "localhost,127.0.0.1,::1", "NODE_EXTRA_CA_CERTS": "/Users/you/.mitmproxy/mitmproxy-ca-cert.pem" }, "remoteControlAtStartup": true } ``` Headroom's upstream client honours `HTTPS_PROXY`. When the Headroom proxy is started by a Claude Code hook (`headroom init hook ensure`), it inherits Claude Code's environment, so its own upstream calls would come back through mitmproxy. Prefix those hook commands with `env -u HTTPS_PROXY -u HTTP_PROXY -u NODE_EXTRA_CA_CERTS`. The addon also marks the requests it redirects, so a second pass goes straight to Anthropic instead of looping. Remove `ANTHROPIC_BASE_URL` from your shell and every active Claude settings scope, including `~/.claude/settings.json`, project `.claude/settings.json`, and project `.claude/settings.local.json`. A project-scoped `headroom init claude` writes it to the last file, so removing only the user setting can leave Remote Control disabled. Keep the other environment entries and hooks intact. Full setup, verification steps, caveats and rollback: [`examples/claude_code_remote_control`](https://github.com/headroomlabs-ai/headroom/tree/main/examples/claude_code_remote_control). ### Why the Unix socket route does not help [#why-the-unix-socket-route-does-not-help] `ANTHROPIC_UNIX_SOCKET` satisfies the host check above, so it looks like a way to keep a first-party base URL while routing through Headroom. It does not work, and the reason is worth recording so nobody re-derives it. That variable is reserved for `claude ssh`, where the process on the other end of the socket supplies credentials. Claude Code 2.1.198 carries the diagnostic verbatim: ``` ANTHROPIC_UNIX_SOCKET is set (claude ssh remote), and the local proxy is API-key-authed. ``` Setting it therefore makes Claude Code classify the session as API-key auth. Remote Control separately requires *claude.ai subscription* auth — the binary's eligibility messages include `Remote Control requires claude.ai subscription auth.` — so the one variable opens the host gate and closes the subscription gate at the same time. Confirmed on Linux against Claude Code 2.1.198 with `headroom proxy --uds` and no API key set. The session starts with: ``` ⚠ claude.ai connectors are disabled because ANTHROPIC_API_KEY or another auth source is set and takes precedence over your claude.ai login ``` sends `Not logged in · Please run /login`, and after a successful subscription login still fails inside the SDK, because nothing ever attached a credential: ``` API invalid_api_key: Could not resolve authentication method. Expected one of apiKey, authToken, credentials, config, or profile to be set. ``` No proxy-side change can repair this: the eligibility decision happens in the client before any request is made. **What remains**: a transport that leaves `ANTHROPIC_BASE_URL` unset and does not set `ANTHROPIC_UNIX_SOCKET` — an `HTTPS_PROXY` CONNECT/MITM front end with a locally-generated CA trusted through `NODE_EXTRA_CA_CERTS`. That keeps the host genuinely `api.anthropic.com` and leaves subscription auth untouched: see [Keep Remote Control with the HTTPS proxy route](#keep-remote-control-with-the-https-proxy-route) ([issue #1779](https://github.com/headroomlabs-ai/headroom/issues/1779)). `headroom proxy --uds` is still useful as a transport, just not for this. The upstream request to allow loopback proxies is tracked at [anthropics/claude-code#76653](https://github.com/anthropics/claude-code/issues/76653). `ENABLE_TOOL_SEARCH` is unaffected and can stay enabled for context-window savings while routing through Headroom. ## Server-managed settings unavailable through custom ANTHROPIC_BASE_URL [#server-managed-settings-unavailable-through-custom-anthropic_base_url] **Symptom**: Settings pushed from **Admin Settings > Claude Code > Managed settings** in the claude.ai console (server-managed settings) don't apply to sessions running through Headroom, even though they apply fine without the proxy. **Cause**: This is a Claude-side gate, not a Headroom limitation. Per Anthropic's docs, server-managed settings require a direct connection to `api.anthropic.com`; if `ANTHROPIC_BASE_URL` is set to any non-default host — which is exactly what wrapping via Headroom does — Claude Code skips the settings fetch entirely for that session. The request never reaches Headroom, so there is no endpoint for Headroom to implement or proxy. This is separate from the OS-level `managed-settings.json` file (macOS `/Library/Application Support/ClaudeCode/`, Linux `/etc/claude-code/`, Windows `C:\Program Files\ClaudeCode\`): that file is read straight from local disk at startup and is unaffected by `ANTHROPIC_BASE_URL` or Headroom. If that file isn't taking effect, the cause is unrelated to proxying (path, permissions, or JSON syntax) — check `claude --debug-file ` and search the log for `Remote settings`. **Fix**: None available on the Headroom side — this is an intentional Anthropic security boundary (a proxy in the path could otherwise forge org policy). If your org relies on server-managed settings, deploy the same policy as [endpoint-managed settings](https://code.claude.com/docs/en/settings#settings-files) (MDM profile, Windows registry, or a local `managed-settings.json`) instead, since those are read locally and unaffected by proxying. See [Server-managed settings platform availability](https://code.claude.com/docs/en/server-managed-settings#platform-availability) and [issue #3074](https://github.com/headroomlabs-ai/headroom/issues/3074). ## Compression Too Aggressive [#compression-too-aggressive] **Symptom**: LLM responses are missing information that was in tool outputs. ```python # 1. Keep more items config = HeadroomConfig() config.smart_crusher.max_items_after_crush = 50 # Default: 15 # 2. Disable SmartCrusher entirely config.smart_crusher.enabled = False ``` `client.chat.completions.create(..., headroom_tool_profiles={...})` is accepted but not currently wired through `TransformPipeline.apply()` -- the kwarg is documented in the pipeline's own docstring but never read in its body (`headroom/transforms/pipeline.py`), so passing it changes nothing. There is also no `skip_compression` key on the per-tool compression profile (`CompressionProfile` in `headroom/config.py` only has `bias`, `min_k`, `max_k`) -- passing a plain dict there would raise `AttributeError` if a code path did consume it. Use options 1 and 2 above instead. ## High Latency [#high-latency] **Symptom**: Requests take longer than expected. **Diagnosis**: ```python import time import logging logging.basicConfig(level=logging.DEBUG) start = time.time() response = client.chat.completions.create(...) print(f"Total time: {time.time() - start:.2f}s") ``` **Solutions**: ```python # 1. Use BM25 instead of embeddings (faster) config = HeadroomConfig() config.smart_crusher.relevance.tier = "bm25" # 2. Increase threshold to skip small payloads config.smart_crusher.min_tokens_to_crush = 500 # 3. Disable transforms you don't need config.cache_aligner.enabled = False # Note: the old rolling-window "drop messages from history" stage was # retired (Phase B PR-B1); `HeadroomConfig` has no `rolling_window` field # any more (`headroom/config.py`), so there is nothing left to disable there. ``` ## Installation Issues [#installation-issues] ### pipx installs an older Headroom version [#pipx-installs-an-older-headroom-version] **Symptom**: PyPI shows a newer `headroom-ai` release, but `pipx install` or `pipx upgrade` keeps an older version. A pinned install can also fail with `No matching distribution found`. **Cause**: `pipx` resolves packages inside its app virtual environment. If that environment uses a Python version that Headroom does not publish wheels for yet, pip may skip newer releases and choose the newest compatible build it can use. Check the interpreter: ```bash pipx list ``` Install with a supported Python explicitly: ```bash pipx install --python python3.13 "headroom-ai[all]" ``` For a pinned release: ```bash pipx install --python python3.13 "headroom-ai[all]==0.21.4" ``` If you already have Headroom installed under `pipx`, uninstall it first or reinstall it with the supported interpreter. ### Docker image frozen at an old version [#docker-image-frozen-at-an-old-version] **Symptom**: `headroom --version` inside the running container reports an old release (e.g. `0.27.0`) no matter how recently you pulled the image. **Cause**: the project moved from the `chopratejas` personal account to the `headroomlabs-ai` org, and GHCR does not redirect packages across that move. `ghcr.io/chopratejas/headroom` still exists but stopped receiving pushes at that point, so it is permanently frozen at the last version built under the old namespace. A `HEADROOM_DOCKER_IMAGE` override, an install script, or a tutorial saved from before the move can still point there. Pull the current image instead: ```bash docker pull ghcr.io/headroomlabs-ai/headroom:latest ``` If you set `HEADROOM_DOCKER_IMAGE` yourself, check it does not still read `ghcr.io/chopratejas/headroom`. See [Docker install](/docs/docker-install) for the current default and the available tags. ### pip install fails with C++ compilation error [#pip-install-fails-with-c-compilation-error] **Symptom**: `RuntimeError: Unsupported compiler -- at least C++11 support is needed!` ```bash # Linux / Debian-based (including Docker) apt-get install -y build-essential && pip install headroom-ai # macOS (Xcode command line tools) xcode-select --install && pip install headroom-ai ``` For Docker, install and remove build tools in one layer: ```dockerfile FROM python:3.11-slim RUN apt-get update && apt-get install -y --no-install-recommends build-essential \ && pip install "headroom-ai[proxy]" \ && apt-get purge -y build-essential && apt-get autoremove -y \ && rm -rf /var/lib/apt/lists/* ``` ### ModuleNotFoundError: No module named 'headroom' [#modulenotfounderror-no-module-named-headroom] ```bash # Check it is installed in the right environment pip show headroom-ai # If using virtual environment, ensure it is activated source venv/bin/activate # Reinstall pip install --upgrade headroom-ai ``` ### Missing optional dependency [#missing-optional-dependency] ```bash # For proxy server pip install "headroom-ai[proxy]" # For embedding-based relevance scoring pip install "headroom-ai[relevance]" # For code compression (tree-sitter) pip install "headroom-ai[code]" # For everything pip install "headroom-ai[all]" ``` ### Windows: Defender blocks ast-grep-cli (sg.exe) during install [#windows-defender-blocks-ast-grep-cli-sgexe-during-install] **Symptom**: On Windows, `uv tool install "headroom-ai[all]"` (or `pip install`) fails while installing the `ast-grep-cli` wheel, and Windows Defender flags `sg.exe`: ```text error: Failed to install: ast_grep_cli-0.44.1-py3-none-win_amd64.whl (ast-grep-cli==0.44.1) Caused by: failed to open file ...\ast_grep_cli-0.44.1.data\scripts\sg.exe: The operation did not complete successfully because the file contains a virus or potentially unwanted software. (os error 225) Threat: Trojan:Win64/Lazy!MTB ``` **Cause**: A known **false positive** in the upstream `ast-grep-cli` wheel's bundled `sg.exe` ([ast-grep/ast-grep#2799](https://github.com/ast-grep/ast-grep/issues/2799)), not a Headroom issue. `ast-grep` is a base dependency, so the block also affects the `[proxy]` extra. Headroom uses `ast-grep` only for optional AST-based Read-output outlining and **runs normally without it** — the only impact is the install-time quarantine. **Workarounds** (safest first): 1. **Run the proxy in Docker** — no local wheel is installed, so Defender is never triggered. See [Docker install](/docs/docker-install); the image is `ghcr.io/headroomlabs-ai/headroom`. 2. **Restore the file from quarantine and retry** — open **Windows Security → Virus & threat protection → Protection history**, select the `sg.exe` detection, choose **Restore**, then re-run the install command. This changes no persistent settings. 3. **Add a temporary, scoped Defender exclusion during install** (last resort; requires an elevated PowerShell). Only do this if you accept excluding a *known false positive*, and remove the exclusion afterward: ```powershell # Scope the exclusion to uv's tools directory, install, then remove it $uvTools = (uv tool dir) Add-MpPreference -ExclusionPath $uvTools uv tool install "headroom-ai[all]" Remove-MpPreference -ExclusionPath $uvTools ``` Do **not** disable Defender wholesale — keep the exclusion narrow and temporary. 4. **Report the false positive to Microsoft** so a corrected signature ships for everyone: submit `sg.exe` at the [Microsoft Security Intelligence sample submission](https://www.microsoft.com/en-us/wdsi/filesubmission) page. ### uv build errors: "src does not appear to be a Python project" [#uv-build-errors-src-does-not-appear-to-be-a-python-project] **Symptom**: `uv tool install "headroom-ai[all]"` fails to build a dependency (commonly `litellm` or `cryptography`) with: ```text error: Failed to build: == Caused by: `src does not appear to be a Python project, as neither `pyproject.toml` nor `setup.py` are present` ``` or a wheel install fails with `Unknown wheel data type: .DS_Store`. **Cause**: Corrupted or stale entries in uv's local build/wheel cache, not a Headroom dependency-pin problem. Headroom does not pin exact versions of `litellm` or `cryptography` that would trigger this. **Fix**: clear uv's cache and reinstall: ```bash uv cache clean uv tool install "headroom-ai[all]" ``` To clear the cache for just one package instead: ```bash uv cache clean litellm ``` If the failure is specifically for `ast-grep-cli==0.44.1`, that release is already excluded by Headroom's dependency pin (`ast-grep-cli>=0.30.0,!=0.44.1`) due to a compromised supply-chain build ([ast-grep/ast-grep#2799](https://github.com/ast-grep/ast-grep/issues/2799)) — `uv cache clean` and a plain reinstall should pick up a safe version automatically. ## Provider-Specific Issues [#provider-specific-issues] ### OpenAI: Invalid API key [#openai-invalid-api-key] ```python import os from openai import OpenAI api_key = os.environ.get("OPENAI_API_KEY") if not api_key: raise ValueError("OPENAI_API_KEY not set") client = HeadroomClient( original_client=OpenAI(api_key=api_key), provider=OpenAIProvider(), ) ``` ### Anthropic: Authentication error [#anthropic-authentication-error] ```python import os from anthropic import Anthropic api_key = os.environ.get("ANTHROPIC_API_KEY") client = HeadroomClient( original_client=Anthropic(api_key=api_key), provider=AnthropicProvider(), ) ``` ### Unknown model warnings [#unknown-model-warnings] ```python # For custom/fine-tuned models, specify context limit client = HeadroomClient( original_client=OpenAI(), provider=OpenAIProvider(), model_context_limits={ "ft:gpt-4o-2024-08-06:my-org::abc123": 128000, "my-custom-model": 32000, }, ) ``` ## ValidationError on Setup [#validationerror-on-setup] ```python result = client.validate_setup() print(result) # Common issues: # {"provider": {"ok": False, "error": "No API key"}} # -> Set OPENAI_API_KEY or pass api_key to OpenAI() # # {"storage": {"ok": False, "error": "unable to open database"}} # -> Check path permissions, use :memory: for testing # # {"config": {"ok": False, "error": "Invalid mode"}} # -> Use "audit" or "optimize" only ``` For testing, use in-memory storage: ```python client = HeadroomClient( original_client=OpenAI(), provider=OpenAIProvider(), store_url="sqlite:///:memory:", ) ``` ## Debugging Techniques [#debugging-techniques] ### Enable Full Logging [#enable-full-logging] ```python import logging # See everything logging.basicConfig( level=logging.DEBUG, format="%(asctime)s %(name)s %(levelname)s %(message)s", ) # Or just Headroom logs logging.getLogger("headroom").setLevel(logging.DEBUG) ``` ### Use Simulation to Inspect Transforms [#use-simulation-to-inspect-transforms] ```python plan = client.chat.completions.simulate( model="gpt-4o", messages=messages, ) print(f"Tokens: {plan.tokens_before} -> {plan.tokens_after}") print(f"Transforms: {plan.transforms}") print(f"Waste signals: {plan.waste_signals}") import json print(json.dumps(plan.messages_optimized, indent=2)) ``` ### Test Transforms Directly [#test-transforms-directly] ```python from headroom import SmartCrusher, Tokenizer, OpenAIProvider from headroom.config import SmartCrusherConfig import json config = SmartCrusherConfig() crusher = SmartCrusher(config) # Tokenizer wraps a provider-specific TokenCounter -- it takes no # zero-argument form (headroom/tokenizer.py). provider = OpenAIProvider() tokenizer = Tokenizer(provider.get_token_counter("gpt-4o"), "gpt-4o") messages = [ { "role": "tool", "content": json.dumps({"items": list(range(100))}), "tool_call_id": "1", } ] result = crusher.apply(messages, tokenizer) print(f"Tokens: {result.tokens_before} -> {result.tokens_after}") ``` ## Getting Help [#getting-help] 1. Enable debug logging and check the output 2. Use `simulate()` to see what transforms would apply 3. Run `validate_setup()` for configuration issues 4. File an issue at [github.com/headroomlabs-ai/headroom](https://github.com/headroomlabs-ai/headroom/issues) with your Headroom version, Python version, provider, debug log output, and minimal reproduction code Headroom integrates with the [Vercel AI SDK](https://sdk.vercel.ai) through three patterns: a one-liner wrapper, composable middleware, and standalone message compression. ## Installation [#installation] ```bash npm install headroom-ai ai @ai-sdk/openai ``` The TypeScript SDK sends messages to a local Headroom proxy for compression. Start the proxy before using the SDK: ```bash pip install "headroom-ai[proxy]" headroom proxy ``` ## withHeadroom() one-liner [#withheadroom-one-liner] The simplest integration. Wraps any Vercel AI SDK language model with automatic compression: ```ts twoslash import { withHeadroom } from 'headroom-ai/vercel-ai'; import { openai } from '@ai-sdk/openai'; import { generateText } from 'ai'; const model = withHeadroom(openai('gpt-4o')); const { text } = await generateText({ model, messages: [ { role: 'user', content: 'Summarize these results...' }, ], }); ``` `withHeadroom()` calls `wrapLanguageModel` + `headroomMiddleware()` under the hood. It works with any provider (`@ai-sdk/openai`, `@ai-sdk/anthropic`, `@ai-sdk/google`, etc.). ## headroomMiddleware() for composition [#headroommiddleware-for-composition] Use the middleware directly when you need to compose it with other middleware: ```ts twoslash // @noErrors import { headroomMiddleware } from 'headroom-ai/vercel-ai'; import { wrapLanguageModel } from 'ai'; import { openai } from '@ai-sdk/openai'; const model = wrapLanguageModel({ model: openai('gpt-4o'), middleware: headroomMiddleware(), }); ``` Pass options to control compression behavior: ```ts twoslash import { headroomMiddleware } from 'headroom-ai/vercel-ai'; const middleware = headroomMiddleware({ model: 'gpt-4o', baseUrl: 'http://localhost:8787', }); ``` ## compressVercelMessages() standalone [#compressvercelmessages-standalone] Compress Vercel-format messages directly without wrapping a model. Useful for custom pipelines: ```ts twoslash import { compressVercelMessages } from 'headroom-ai/vercel-ai'; const result = await compressVercelMessages(messages, { model: 'gpt-4o', }); console.log(`Saved ${result.tokensSaved} tokens`); // result.messages is in Vercel format, ready for the AI SDK ``` ## Streaming with streamText [#streaming-with-streamtext] Compression happens before the request. Streaming responses are unaffected: ```ts twoslash import { withHeadroom } from 'headroom-ai/vercel-ai'; import { openai } from '@ai-sdk/openai'; import { streamText } from 'ai'; const model = withHeadroom(openai('gpt-4o')); const result = streamText({ model, messages: longConversation, }); for await (const chunk of result.textStream) { process.stdout.write(chunk); } ``` ## generateObject with compressed context [#generateobject-with-compressed-context] Works with structured output: ```ts twoslash // @noErrors import { withHeadroom } from 'headroom-ai/vercel-ai'; import { openai } from '@ai-sdk/openai'; import { generateText, Output } from 'ai'; import { z } from 'zod'; const model = withHeadroom(openai('gpt-4o')); const { output } = await generateText({ model, output: Output.object({ schema: z.object({ summary: z.string(), severity: z.enum(['low', 'medium', 'high']), }), }), messages: largeConversationHistory, }); ``` ## How it works [#how-it-works] 1. Messages are converted from Vercel format to OpenAI format 2. Headroom compresses them via the proxy's `/v1/compress` endpoint 3. Compressed messages are converted back to Vercel format 4. The original model receives the smaller prompt All other model behavior (tool calling, structured output, streaming) is unchanged. Provider state the AI SDK attaches to the prompt travels through untouched: `reasoning` parts (with the Anthropic `signature` or `redactedData`, OpenAI `itemId`, Google `thoughtSignature` they carry) stay in place, tool calls return to their place among them, and `providerOptions` on messages, text and file parts, tool calls and tool results come back exactly as sent. Tool results keep their output kind: an `error-text` or `error-json` result is still an error, an `execution-denied` result keeps its reason, and a multi-part `content` result (a screenshot, a PDF, an MCP result) keeps its file and custom members byte for byte while its text members compress like any other tool output. Only large tool results change. The official Claude Code extension for VS Code embeds Claude Code. Headroom can route its Anthropic API requests through the same local compression proxy used by `headroom wrap claude`, without changing your Anthropic sign-in or selected model. Pass `--1m` when you want Headroom to persist Claude Code's client-owned 1M model selector. Use `headroom wrap claude`. This page is specifically for Anthropic's official Claude Code extension inside VS Code. ## Requirements [#requirements] - VS Code 1.98 or newer - Anthropic's official Claude Code extension, signed in and working - Headroom with proxy dependencies: `pip install "headroom-ai[proxy]"` - Loopback access to `127.0.0.1` from the VS Code extension host Confirm that Claude Code works normally in VS Code before adding Headroom. This makes authentication or extension problems easier to distinguish from proxy configuration problems. ## Quick start [#quick-start] 1. Open a terminal in the project you use with Claude Code. 2. Start Headroom: ```bash headroom wrap vscode-claude ``` Headroom starts its proxy and adds two entries under `env` in the Claude Code user settings file: ```json { "env": { "ANTHROPIC_BASE_URL": "http://127.0.0.1:8787/p/your-project", "ENABLE_TOOL_SEARCH": "true" } } ``` `ANTHROPIC_BASE_URL` changes the endpoint, not the selected model. `ENABLE_TOOL_SEARCH` keeps Claude Code's on-demand tool loading enabled when it uses a custom endpoint. Existing settings and prior values for both variables are preserved for restoration. Headroom does not store or replace your Anthropic credentials. To opt into Claude Code's 1M context selector, run: ```bash headroom wrap vscode-claude --1m ``` The command writes a top-level setting alongside `env`: ```json { "model": "claude-opus-5[1m]" } ``` The selected model is trimmed and receives one `[1m]` suffix. When no model is selected, `HEADROOM_1M_MODEL` is used, followed by the built-in fallback. Headroom stores the exact prior model presence and value in its sidecar, preserves that snapshot across repeated runs, and restores it when you omit `--1m` or run `headroom unwrap vscode-claude`. 3. After the first configuration, run **Developer: Reload Window** from the VS Code Command Palette. 4. Keep the wrapper terminal running and use the Claude Code panel normally. ## Verify that it is working [#verify-that-it-is-working] While the wrapper is running: 1. Open `http://127.0.0.1:8787/health`; it should report a healthy proxy. 2. Send a message in the Claude Code panel. 3. Open the dashboard or proxy log whose locations are printed by the wrapper. Confirm that the request appears there; savings are recorded with each completed request. If the health check succeeds but no request appears in the dashboard or proxy log, reload the VS Code window and confirm that the extension host can reach the same `127.0.0.1` as Headroom. ## Settings location [#settings-location] The default user settings file is `~/.claude/settings.json` on macOS and Linux, or `%USERPROFILE%\.claude\settings.json` on Windows. `CLAUDE_CONFIG_DIR` is respected when set. To target another profile explicitly: ```bash headroom wrap vscode-claude --settings-file /path/to/.claude/settings.json ``` Use `--no-configure` to print the settings without editing a file. With `--no-configure --1m`, the resolved top-level `model` is printed too and neither the settings file nor the sidecar is written. The proxy URL includes the current directory as the project attribution name. Run the wrapper from the intended project directory. If you select another port, for example `--port 8788`, Headroom writes that same port to the settings file. ## Stop and undo [#stop-and-undo] Press `Ctrl+C` to stop the proxy. The endpoint remains configured so requests fail closed rather than silently bypassing Headroom while it is stopped. Restart it with `headroom wrap vscode-claude` before using Claude Code again. Restore the values that existed before Headroom configured the extension: ```bash headroom unwrap vscode-claude ``` Headroom records the two environment values it owns and, when `--1m` is active, the model snapshot in a sidecar next to the Claude settings file. It refuses malformed settings or conflicting edits rather than overwriting them. Unrelated Claude settings are preserved. If you used `--settings-file` during setup, pass the same option when undoing it: ```bash headroom unwrap vscode-claude --settings-file /path/to/.claude/settings.json ``` ## Remote development [#remote-development] For Dev Containers, SSH, or WSL, `127.0.0.1` must refer to the environment where the Claude Code process runs. Run Headroom there or forward the selected port, and pass that environment's Claude settings file with `--settings-file` when automatic discovery does not match it. ## Troubleshooting [#troubleshooting] - Check `http://127.0.0.1:8787/health` while the wrapper is running. - Run `headroom wrap vscode-claude --port 8788` if port 8787 is occupied. - Reload the VS Code window after changing Claude Code settings. - Keep the wrapper process running for the entire Claude Code session. A stopped proxy intentionally does not fall back to a direct Anthropic connection. - If configuration reports a conflict, inspect `~/.claude/settings.json`; Headroom will not replace a managed value that changed after setup. - If you use `CLAUDE_CONFIG_DIR`, launch Headroom from an environment where it is set to the same value used by Claude Code. - This integration is for the Claude Code extension, not the Claude desktop app. Claude Code and Anthropic decide whether the selected model and account can use the 1M tier. Local settings tests prove persistence and restoration only; they do not prove a live entitled VS Code session, extension behavior, or a 1M context window. Headroom integrates below VS Code's native Copilot model picker. It overrides the Copilot API proxy endpoint, not the model: if the user selects GPT-5.5, a GPT-5.6 variant, Claude Sonnet, Claude Opus, or another Copilot model, the same model ID travels through Headroom to GitHub's Copilot API. No `Headroom` model appears in the picker. Headroom does not patch the built-in extension, terminate TLS, or edit Codex configuration. ## Requirements [#requirements] - Current stable VS Code with GitHub Copilot enabled and signed in - A GitHub account with Copilot access - Headroom with proxy dependencies: `pip install "headroom-ai[proxy]"` - Loopback access to `127.0.0.1` from the VS Code extension host ## One-time authentication [#one-time-authentication] VS Code keeps its Copilot token in extension secret storage. Headroom deliberately does not read or modify that encrypted store, so authorize Headroom separately: ```bash headroom copilot-auth login ``` Open the printed GitHub device URL, enter the code, and approve it. Headroom saves the reusable OAuth credential in its own auth file with user-only permissions. At launch it exchanges that credential for a short-lived Copilot API token; that token remains in the proxy process and is never written to VS Code settings. ## Start [#start] Run from the project whose savings should receive attribution: ```bash headroom wrap vscode ``` The command: 1. validates Copilot subscription access and resolves the account API endpoint; 2. starts Headroom on `127.0.0.1:8787` with the short-lived upstream token; 3. adds a marker-owned block to VS Code user settings containing `github.copilot.advanced.debug.overrideProxyUrl` (inline completions) and `github.copilot.advanced.debug.overrideCapiUrl` (chat); 4. keeps running until `Ctrl+C` so the local proxy is available to VS Code. Continue using Copilot's normal model picker. The request body—and therefore the selected model—is not rewritten by the VS Code integration. ## What is routed [#what-is-routed] The shipped Copilot extension resolves both its chat/agent endpoint and its completions-core endpoint through the proxy override. This covers native model selection without registering duplicate models. Some ancillary Copilot services (telemetry, GitHub API calls, MCP, embeddings, model discovery, cloud agents) use separate endpoints and are intentionally not redirected. ```text Copilot UI: user selects model M -> native Copilot request with model M -> http://127.0.0.1:8787/p// -> Headroom compression, cache alignment, metrics, attribution -> authenticated GitHub Copilot API, still with model M -> response from model M ``` ## Safe settings lifecycle [#safe-settings-lifecycle] Headroom edits only a marked block in VS Code's `settings.json`. Existing JSONC comments, formatting, trailing commas, and unrelated settings remain byte-for-byte unchanged. Headroom refuses malformed files, incomplete markers, or a pre-existing unmanaged Copilot endpoint override instead of overwriting them. | Platform | Stable VS Code user settings | |---|---| | macOS | `~/Library/Application Support/Code/User/settings.json` | | Windows | `%APPDATA%\\Code\\User\\settings.json` | | Linux | `${XDG_CONFIG_HOME:-~/.config}/Code/User/settings.json` | For Insiders, VSCodium, portable installations, a custom `--user-data-dir`, or a remote extension host, provide the exact user settings file: ```bash headroom wrap vscode --settings-file /path/to/User/settings.json ``` Use `--no-configure` to print the two settings without editing a file. ## Model coverage [#model-coverage] There is no static Headroom model list. Availability stays controlled by Copilot and the signed-in account. To validate a model, select it in VS Code and send a short prompt; Headroom forwards the model identifier unchanged. This naturally covers newly added Copilot models without a Headroom release. The newest Copilot models may use the OpenAI Responses API instead of the legacy Chat Completions API. Headroom proxies both routes. Do not treat a model's `unsupported_api_for_model` response from `/chat/completions` as a proxy failure; VS Code uses the endpoint supported by that model. Live verification on August 27, 2026 confirmed successful responses through Headroom's `/responses` route for each of these requested model IDs: - `gpt-5.5` - `gpt-5.6-luna` - `gpt-5.6-sol` - `gpt-5.6-terra` Headroom retained each requested model alias. GitHub may identify the resolved snapshot in response metadata (for example, a request for `gpt-5.5` returned `gpt-5.5-2026-04-23`). Model availability remains subject to the signed-in user's Copilot plan and organization policy. The proxy supports the native Copilot OpenAI-compatible request paths used by GPT and Claude models. Inline completions retain the editor's own `ghu_` OAuth bearer and native integration metadata on the completions host, including an operator-configured gateway with a base-path prefix. Headroom does not replace that editor identity with the proxy operator's Copilot seat or add chat-specific headers. Chat and Responses retain their existing managed Copilot API-token policy; a raw OAuth bearer on those routes does not gain the inline exception. A configured base path is removed only for the same scheme and authority, at a complete path-segment boundary. Other recognized completions endpoints keep their native `/v1` paths. The inline auth branch logs only the credential kind (`ghu_***`), not the editor's OAuth token. Case variants of unrelated provider API-key headers are removed before any Copilot auth branch forwards the request. ## Stop and undo [#stop-and-undo] Press `Ctrl+C` to stop the session proxy. The VS Code endpoint setting remains so future `headroom wrap vscode` runs need no reconfiguration; while the proxy is stopped, Copilot requests will fail closed instead of bypassing Headroom. Remove only Headroom's settings block with: ```bash headroom unwrap vscode ``` Use the same `--settings-file` override used during setup. Other VS Code and Copilot settings are preserved. ## Remote development [#remote-development] Copilot may run in the local or remote extension host depending on the workspace. For Dev Containers, SSH, or WSL, `127.0.0.1` must refer to the host running Headroom. Run Headroom in that environment or forward the chosen port. Portable, remote, and profile-specific settings should use `--settings-file` explicitly. ## Enterprise [#enterprise] GitHub.com Enterprise Cloud normally requires no override. Copilot Business and Enterprise seats are routed to the host GitHub advertises during token exchange (`api.business.githubcopilot.com` or `api.enterprise.githubcopilot.com`), which is the host GitHub's own clients use. This matters on networks that follow GitHub's [subscription-based network routing](https://docs.github.com/en/copilot/how-tos/administer-copilot/manage-for-enterprise/manage-access/manage-network-access): their firewall allows only the plan host, and the generic `api.githubcopilot.com` is blocked. If the plan host rejects a model the generic host serves, set `GITHUB_COPILOT_USE_ADVERTISED_HOST=0` to route through the generic host again. An explicit `GITHUB_COPILOT_API_URL` still wins over both. For GitHub Enterprise Server/custom domains, set `GITHUB_COPILOT_ENTERPRISE_URL` or `GITHUB_COPILOT_ENTERPRISE_DOMAIN` before both `copilot-auth login` and `wrap vscode`. ## Verification and troubleshooting [#verification-and-troubleshooting] - `headroom copilot-auth status` should report `logged in`. - `http://127.0.0.1:8787/health` should be healthy while the wrapper runs. - The health payload's OpenAI upstream should be the Copilot API endpoint. - Select several native models and confirm Headroom metrics show each request. - If a model is unavailable, verify the Copilot account entitlement; Headroom does not add or rename models. - If connection is refused, keep the wrapper running and check loopback/remote port reachability. - If Headroom records no requests while Copilot keeps working, the window is probably using a VS Code profile with its own settings. `wrap vscode` warns about each such profile and prints the `--settings-file` path to configure it. - Use `--port 8788` when the default port is occupied; settings update safely. - If Headroom refuses settings, repair the reported JSONC/marker conflict or use `--no-configure` and apply the printed settings manually.