npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@shivam.dixit/token-budget-anthropic

v0.1.4

Published

Anthropic Messages API adapter for token-budget — context window and token budget management for Claude and Anthropic agents.

Readme

token-budget-anthropic

Anthropic Messages API adapter for token-budget.

No dependency on @anthropic-ai/sdk — the types here are structurally compatible with it (and with plain fetch/JSON usage), so either works without pulling the SDK in as a dependency.

Install

npm install @shivam.dixit/token-budget @shivam.dixit/token-budget-anthropic

token-budget is a peer dependency (semver range, not pinned).

Usage

import { TokenBudget } from '@shivam.dixit/token-budget';
import { toAnthropicMessages, fromAnthropicResponse } from '@shivam.dixit/token-budget-anthropic';

const budget = new TokenBudget({ maxTokens: 200000, reserve: 4096 });
budget.addMessage({ role: 'system', content: 'You are a helpful assistant.', pinned: true });
budget.addMessage({ role: 'user', content: 'What is the weather in Paris?' });

// Build the request: pinned system messages become Anthropic's separate
// `system` field (Anthropic has no `system` role inside `messages[]`).
const ctx = await budget.getContext();
const { system, messages } = toAnthropicMessages(ctx);

const response = await anthropicClient.messages.create({
  model: 'claude-opus-4-6',
  system,
  messages,
  max_tokens: 4096,
});

// Append the reply (including any tool_use blocks) back into the budget.
fromAnthropicResponse(response, budget);

// If the reply asked for a tool call, run it and append the result:
budget.addMessage({
  role: 'tool',
  content: [{ type: 'tool_result', toolUseId: 'toolu_...', result: 'Sunny, 22°C' }],
  toolCallId: 'toolu_...', // the same id as the tool_use block
});

API

| Export | Description | | --- | --- | | toAnthropicMessages(context) | Converts a raw BudgetMessage[] or a getContext() result into { system?, messages }. | | fromAnthropicContext(context) | Inverse: converts { system?, messages } back into addMessage-ready input. | | fromAnthropicResponse(response, budget) | Appends an Anthropic API response directly into a TokenBudget. | | countAnthropicOverhead(message, tools?) | Best-effort per-message + per-tool-definition token overhead estimate — Anthropic does not publish an exact formula. |

Content block mapping

| token-budget ContentBlock.type | Anthropic block | | --- | --- | | text | text | | tool_call | tool_use (block id becomes the message's id, for linking) | | tool_result | tool_result (toolUseId becomes the message's toolCallId) | | image | image | | document | document | | anything else | text (JSON-stringified) — a lossy fallback for unrecognized block types |

Anthropic has no tool role: a token-budget message with role: 'tool' is sent as { role: 'user', content: [{ type: 'tool_result', ... }] }, and restored back to role: 'tool' on the way in.

Known limitations

  • Only the first tool_use block in an assistant turn is linked via toolCallId. Anthropic supports multiple parallel tool calls per turn, but token-budget's toolCallId is scalar per message (a core Phase 1 constraint) — the common single-tool-call-per-turn pattern round-trips exactly; turns with several simultaneous tool calls will only link the first.
  • countAnthropicOverhead is a documented approximation, not ground truth — Anthropic does not publish exact per-message/per-tool overhead figures.

Testing

This package's test suite includes the shared adapter conformance suite from token-budget/test-utils (round-trip fidelity, tool-call atomicity, pinned-message handling, token accounting) plus a full round-trip integration test against a live-shaped response fixture.

The wider project

Part of the token-budget monorepo — the core package, the other framework/tokenizer adapters, benchmarks, and the flagship coding-agent example all live there. See the compatibility matrix for exactly what every adapter is tested against.

License

MIT