TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/headroomlabs-ai/headroom/llms.txt
Use this file to discover all available pages before exploring further.
compress() function is the primary entry point for reducing message context in any TypeScript or JavaScript application. It accepts a message array in any supported format — OpenAI, Anthropic, Vercel AI SDK, or Google Gemini — auto-detects the format, routes through the Headroom proxy for compression, and returns compressed messages in the same format as the input.
compress() requires a running Headroom proxy. Start one with headroom proxy (default port 8787) or headroom wrap <tool>. The baseUrl option defaults to http://localhost:8787 and can be overridden via HEADROOM_BASE_URL.Signature
Parameters
The message array to compress. Accepts OpenAI (
role/content objects), Anthropic (role/content), Vercel AI SDK, or Google Gemini format. Format is detected automatically via detectFormat().Optional configuration for the compression call. All fields are optional.
Return value — CompressResult
Compressed messages in the same format as the input (OpenAI, Anthropic, Vercel AI SDK, or Gemini).
Token count of the original uncompressed message array.
Token count after compression.
Absolute tokens saved:
tokensBefore - tokensAfter.Ratio of compressed to original size (
tokensAfter / tokensBefore). A value of 0.3 means 70 % reduction.Names of the compression transforms that ran (e.g.
["SmartCrusher", "ToolCrusher", "CCR"]).CCR (Compress-Cache-Retrieve) content hashes for any content archived in the retrieval store. The agent can call
headroom_retrieve with these hashes.true when at least one transform reduced the context. false on a passthrough (e.g. fallback mode, or messages already within budget).Usage examples
Error handling
Whenfallback: false is set, compress() surfaces the full error hierarchy. Import error classes directly:
| Class | Trigger |
|---|---|
HeadroomConnectionError | Proxy is unreachable (ECONNREFUSED, timeout) |
HeadroomAuthError | HTTP 401 — missing or invalid API key |
HeadroomCompressError | HTTP 4xx/5xx from the proxy; carries statusCode and errorType |
ConfigurationError | Invalid configuration sent to the proxy |
ProviderError | Upstream LLM provider returned an error |
StorageError | CCR storage failure |
TokenizationError | Token-count calculation failed |
CacheError | Semantic cache failure |
ValidationError | Request failed schema validation |
TransformError | A specific compression transform failed |
Compression hooks
Pass aCompressionHooks instance to intercept the compression lifecycle: