The Headroom TypeScript SDK ships a low-level HTTP client (Documentation 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.
HeadroomClient), drop-in wrappers for OpenAI and Anthropic SDKs (withHeadroom()), a Vercel AI SDK middleware (headroomMiddleware()), and a dry-run simulation helper (simulate()). All are available from the headroom-ai package.
HeadroomClient
HeadroomClient is a typed HTTP client that speaks directly to the Headroom proxy. Use it when you want fine-grained control over compression, metrics, and retrieval without wrapping a provider SDK.
Constructor
Proxy URL. Defaults to
HEADROOM_BASE_URL env var or http://localhost:8787.Bearer token for the proxy’s inbound auth gate (
HEADROOM_PROXY_TOKEN). Reads HEADROOM_API_KEY when unset.Request timeout in milliseconds. Defaults to
30000.Return the original messages unmodified when the proxy is unreachable, instead of throwing. Defaults to
true.Retry attempts on recoverable errors (5xx, connection failures). Defaults to
1.Your upstream LLM provider key. Forwarded as
Authorization: Bearer (OpenAI path) or x-api-key (Anthropic path) on passthrough requests.Default optimization mode for all requests through this client (e.g.
"optimize", "audit", "simulate").Fine-grained compression configuration object. See the config type reference below.
Integration slug sent as
X-Headroom-Stack on every request (e.g. "adapter_ts_openai").Core methods
client.compress(messages, options?)
Compress an array of OpenAI-format messages via POST /v1/compress.
Promise<CompressResult>. See the compress() reference for the full return-type documentation.
client.chat.completions.create(params)
OpenAI-compatible passthrough — routes through POST /v1/chat/completions with automatic compression. Supports streaming.
client.messages.create(params)
Anthropic-compatible passthrough — routes through POST /v1/messages with automatic compression. Supports streaming.
client.health()
Check proxy liveness. Returns Promise<HealthStatus>.
client.proxyStats()
Fetch comprehensive proxy statistics from GET /stats. Returns Promise<ProxyStats>.
client.retrieve(hash, options?)
Retrieve original content from the CCR store by hash. Returns Promise<RetrieveResult | RetrieveSearchResult>.
client.close()
No-op for the HTTP client; included for API parity with future WebSocket clients.
withHeadroom()
withHeadroom() wraps an existing OpenAI or Anthropic SDK instance so all API calls are silently compressed before reaching the provider. No code changes to call sites are required.
OpenAI
Anthropic
headroomMiddleware()
Integrates with the Vercel AI SDK middleware pipeline. Compress context before everystreamText or generateText call.
simulate()
Dry-run compression — callsPOST /v1/compress in simulate mode to preview what would happen without sending any request to an LLM.
Signature
SimulateOptions
Model name for token counting. Defaults to
"gpt-4o".Proxy URL. Defaults to
http://localhost:8787.Proxy bearer token. Reads
HEADROOM_API_KEY when unset.Override specific compression sub-system configuration.
Re-use an existing
HeadroomClient instance.SimulationResult fields
Estimated token count before compression.
Estimated token count after compression.
Estimated tokens saved.
Human-readable savings summary (e.g.
"68% reduction").Which transforms would run.
Detected patterns of context waste (redundant tool outputs, stale reads, etc.).
Structured diff showing exactly what would be removed.
Format utilities
detectFormat(messages)
Detect the message format of an array.
toOpenAI(messages)
Convert messages from any supported format to OpenAI format.
fromOpenAI(messages, targetFormat)
Convert OpenAI-format messages back to a target format.
Error classes
All errors extendHeadroomError which extends the native Error. The details field carries structured context.
| Class | HTTP status | When thrown |
|---|---|---|
HeadroomConnectionError | — | Proxy is unreachable (ECONNREFUSED, timeout) |
HeadroomAuthError | 401 | Missing or invalid HEADROOM_API_KEY / proxy token |
HeadroomCompressError | 4xx / 5xx | Proxy returned an error; carries .statusCode and .errorType |
ConfigurationError | — | Invalid configuration |
ProviderError | — | Upstream LLM provider error |
StorageError | — | CCR store failure |
TokenizationError | — | Token counting failure |
CacheError | — | Semantic cache failure |
ValidationError | — | Request schema validation failure |
TransformError | — | An individual compression transform failure |
mapProxyError()
Utility that maps raw HTTP status + error type string to the correct subclass:HeadroomConfig type
Pass aHeadroomConfig object to HeadroomClient or simulate() to override specific compression behaviors:
HeadroomConfig and all sub-types (ToolCrusherConfig, SmartCrusherConfig, CCRConfig, CacheAlignerConfig, etc.) exported from headroom-ai for the full field list.