headroom/sdk/typescript/examples
2026-04-06 21:24:36 +06:00
..
basic-compress.ts typescript coverage 2026-04-06 21:24:36 +06:00
ccr-retrieve.ts typescript coverage 2026-04-06 21:24:36 +06:00
hooks-custom-compression.ts typescript coverage 2026-04-06 21:24:36 +06:00
middleware-composition.ts typescript coverage 2026-04-06 21:24:36 +06:00
multi-provider.ts typescript coverage 2026-04-06 21:24:36 +06:00
openai-anthropic-adapters.ts typescript coverage 2026-04-06 21:24:36 +06:00
README.md typescript coverage 2026-04-06 21:24:36 +06:00
shared-context-multi-agent.ts typescript coverage 2026-04-06 21:24:36 +06:00
simulation-dry-run.ts typescript coverage 2026-04-06 21:24:36 +06:00
streaming-chat.ts typescript coverage 2026-04-06 21:24:36 +06:00
structured-output.ts typescript coverage 2026-04-06 21:24:36 +06:00
tool-calling-agent.ts typescript coverage 2026-04-06 21:24:36 +06:00
with-headroom-vercel.ts typescript coverage 2026-04-06 21:24:36 +06:00

headroom-ai examples

Runnable examples showing how to use the Headroom TypeScript SDK for context compression.

Prerequisites

  • Node.js 18+
  • A running Headroom proxy (pip install "headroom-ai[proxy]" && headroom proxy)
  • OPENAI_API_KEY set in your environment (most examples use OpenAI)
  • Optional: ANTHROPIC_API_KEY for Anthropic examples

Running

cd sdk/typescript
npm install
npx tsx examples/<filename>.ts

Examples

Vercel AI SDK

Example Description
with-headroom-vercel.ts One-liner withHeadroom(openai('gpt-4o')) — simplest integration
streaming-chat.ts withHeadroom + streamText for real-time streaming
tool-calling-agent.ts Multi-step agent with tools, context auto-compressed each step
structured-output.ts Extract structured data with Output.object() from compressed context
middleware-composition.ts Stack headroomMiddleware with other middlewares (extractReasoningMiddleware)
multi-provider.ts Same compression across GPT-4o and GPT-4o-mini

Core SDK

Example Description
basic-compress.ts compress() function — compress then send to any LLM
simulation-dry-run.ts simulate() — see what compression would do without calling the LLM
hooks-custom-compression.ts CompressionHooks — customize with pre/post hooks and per-message biases
shared-context-multi-agent.ts SharedContext — compressed handoff between agents (70-90% savings)
ccr-retrieve.ts CCR — retrieve original content after compression (lossless)

Native SDK Adapters

Example Description
openai-anthropic-adapters.ts withHeadroom for native OpenAI and Anthropic SDKs (no Vercel AI SDK)