API Reference
@chai-ai/anthropic
Anthropic model provider adapter for Chai AI SDK.
The @chai-ai/anthropic package provides the adapter layer for connecting Chai AI agents to Anthropic's Claude models using the official @anthropic-ai/sdk Node SDK. Under the hood, this uses the modern Messages API structure.
Installation
Ensure you have installed the Anthropic provider package alongside chai-ai:
npm install @chai-ai/anthropicAuthentication
The provider automatically reads the Anthropic API Key from the environment:
export ANTHROPIC_API_KEY="your-anthropic-api-key"API Reference
anthropic(modelId)
Creates a LanguageModel descriptor for an Anthropic model.
- Parameters:
modelId: string— The identifier of the Anthropic model (e.g.,"claude-haiku-4-5","claude-3-5-sonnet-20241022").
- Returns:
LanguageModelinterface instance.
import { Agent } from "chai-ai";
import { anthropic } from "@chai-ai/anthropic";
const model = anthropic("claude-haiku-4-5");
const agent = new Agent({
name: "claude-agent",
instructions: "You are an Anthropic-powered assistant.",
model,
});Features & Implementation Details
- Messages API Integration: Maps message history structures directly onto the Anthropic Messages API (
client.messages.create). - Consecutive Message Coalescing: Anthropic's API strictly enforces alternating user/assistant roles. Consecutive tool execution results or user corrections are automatically coalesced into single message blocks by the adapter to prevent validation errors.
- Client-Side Cancellation: Fully threads abort signals (
AbortSignal) into the request configuration context to terminate in-flight connections on timeout. - Token Usage Mapping: Translates token counts from the Anthropic SDK events into standard Chai AI SDK
TokenUsagemetrics, calculating total tokens asinput_tokens + output_tokens.