@sisu-ai/adapter-anthropic
v7.0.0
Published
Anthropic Messages API adapter with tool calling and streaming.
Maintainers
Readme
@sisu-ai/adapter-anthropic
Anthropic Messages API adapter with tool calling and streaming.
Setup
npm i @sisu-ai/adapter-anthropic- Env:
ANTHROPIC_API_KEY(preferred) orAPI_KEYrequired - Optional base URL:
ANTHROPIC_BASE_URL(orBASE_URL) - Optional debug:
DEBUG_LLM=1to log redacted request/response summaries on errors
Usage
import { anthropicAdapter } from '@sisu-ai/adapter-anthropic';
const model = anthropicAdapter({ model: 'claude-3-5-sonnet-20240620' });
// with a self-hosted proxy
const model = anthropicAdapter({ model: 'claude-3-opus-20240229', baseUrl: 'https://api.anthropic.com' });Tools
- Sends
toolsin Anthropic format (name,description,input_schema). - Sends
tool_choiceonly whentoolsexist (Anthropic rejectstool_choicewithout tools). - Maps assistant tool calls to
{ id, name, arguments }for the middleware loop. - Maps tool results into Anthropic
tool_resultblocks and back.
Streaming
- Supports server‑sent events from the Messages API.
- Emits
{ type: 'token', token }events for content deltas and a final{ type: 'assistant_message', message }.
Options
anthropicAdapter({
model: 'claude-3-5-sonnet-20240620',
apiKey?: string, // default: ANTHROPIC_API_KEY or API_KEY
baseUrl?: string, // default: https://api.anthropic.com (or ANTHROPIC_BASE_URL / BASE_URL)
anthropicVersion?: string, // default: 2023-06-01
timeout?: number, // default: 60000 ms
maxRetries?: number, // default: 3 (with backoff; 4xx except 429 are not retried)
});Message mapping
- System messages →
systemstring (joined if multiple system messages appear) - User messages →
{ role: 'user', content: [{ type: 'text', text }] } - Assistant messages with tool calls →
tool_useblocks with{ id, name, input } - Tool messages → user
tool_resultblocks with{ tool_use_id | name, content }
Usage reporting
The adapter maps usage.input_tokens and usage.output_tokens to { promptTokens, completionTokens, totalTokens }.
Debugging
DEBUG_LLM=1prints sanitized payloads and error bodies.- Combine with your app logger’s
LOG_LEVEL=debugto see middleware events.
Community & Support
Discover what you can do through examples or documentation. Check it out at https://github.com/finger-gun/sisu. Example projects live under examples/ in the repo.
