Want more savings? Headroom for teams and enterprises: talk to us →
Skip to Content
Headroom

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/sdk

Proxy 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 proxy

Quick 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():

  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

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:

  • 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

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', : {} } }],
});

On this page