ai-provider-anthropic-sdk
Anthropic SDK Patterns
Quick Guide: Use the official
@anthropic-ai/sdkpackage to interact with Claude models directly. Useclient.messages.create()for single-turn and multi-turn conversations. Useclient.messages.stream()for streaming with event-based consumption.max_tokensis always required. Content blocks are typed unions (text,tool_use,thinking). Useclient.messages.parse()withzodOutputFormat()for structured outputs. Tool use requires a tool-result loop -- Claude returnstool_useblocks, you execute the tool and send backtool_resultblocks. Extended thinking addsthinkingcontent blocks before the response.
<critical_requirements>
CRITICAL: Before Using This Skill
All code must follow project conventions in CLAUDE.md (kebab-case, named exports, import ordering,
import type, named constants)
(You MUST always provide max_tokens in every messages.create() / messages.stream() call -- it is required and has no default)
(You MUST handle the stop_reason field to detect end_turn, max_tokens, tool_use, and stop_sequence -- ignoring it causes silent truncation or broken tool loops)
(You MUST iterate over response.content blocks (not assume a single text block) -- responses can contain text, tool_use, and thinking blocks mixed together)
(You MUST handle errors using Anthropic.APIError and its subclasses -- never use bare catch blocks without error type checking)