Anthropic SDK
Auto-compress messages in the Anthropic TypeScript SDK with a single withHeadroom() wrapper.
Headroom wraps the Anthropic TypeScript SDK to automatically compress messages before every messages.create() call. All other methods pass through unchanged.
Installation
npm install headroom-ai @anthropic-ai/sdkProxy required
The TypeScript SDK sends messages to a local Headroom proxy for compression. Start the proxy before using the SDK:
pip install "headroom-ai[proxy]"
headroom proxyQuick start
import { } from 'headroom-ai/anthropic';
import Anthropic from '@anthropic-ai/sdk';
const = (new Anthropic());
const = await .messages.create({
: 'claude-sonnet-4-5-20250929',
: longConversation,
: 1024,
});Every call to client.messages.create() compresses messages first. The response format is identical to the unwrapped client.
How it works
withHeadroom() returns a proxy around your Anthropic client that intercepts messages.create():
- Sends your
messagesto the Headroom proxy's/v1/compressendpoint as they are, in Anthropic format - Forwards the request to Anthropic with the compressed messages in place
Message format
Nothing is converted. POST /v1/compress compresses Anthropic content blocks natively — see Message format — and returns the same shape it received, so every block reaches Anthropic exactly as you built it:
thinkingandredacted_thinkingblocks keep theirsignatureanddata, which Anthropic requires back unmodified within a tool-use turncache_controlmarkers stay on the blocks that carry themtool_useandtool_resultblocks keep their ids; only the content of largetool_resultblocks 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
Pass compression options as the second argument:
import { } from 'headroom-ai/anthropic';
import Anthropic from '@anthropic-ai/sdk';
const = (new Anthropic(), {
: 'claude-sonnet-4-5-20250929',
: 'http://localhost:8787',
});Streaming
Streaming works normally. Compression happens before the request:
import { } from 'headroom-ai/anthropic';
import Anthropic from '@anthropic-ai/sdk';
const = (new Anthropic());
const = await .messages.create({
: 'claude-sonnet-4-5-20250929',
: longConversation,
: 1024,
: true,
});Tool use
Tool results are where compression has the biggest impact. Large JSON payloads from tool calls are compressed automatically:
import { } from 'headroom-ai/anthropic';
import Anthropic from '@anthropic-ai/sdk';
const = (new Anthropic());
const = await .messages.create({
: 'claude-sonnet-4-5-20250929',
: 1024,
: [
{ : 'user', : 'What went wrong?' },
{
: 'assistant',
: [
{ : 'tool_use', : 'toolu_1', : 'get_logs', : { : 'api' } },
],
},
{
: 'user',
: [
{
: 'tool_result',
: 'toolu_1',
: hugeLogOutput, // Compressed automatically
},
],
},
],
: [{ : 'get_logs', : 'Get logs', : { : 'object', : {} } }],
});