- Spec files
npm-anthropic-ai--sdk
Describes: pkg:npm/@anthropic-ai/sdk@0.61.x
- Description
- The official TypeScript library for the Anthropic API providing comprehensive client functionality for Claude AI models.
- Author
- tessl
- Last updated
index.md docs/
1# Anthropic SDK23The official TypeScript library for the Anthropic API, providing comprehensive client functionality for Claude AI models. This SDK enables developers to integrate Claude's conversational AI capabilities into their applications with full type safety, streaming support, and built-in error handling.45## Package Information67- **Package Name**: @anthropic-ai/sdk8- **Package Type**: npm9- **Language**: TypeScript10- **Installation**: `npm install @anthropic-ai/sdk`1112## Core Imports1314```typescript15import Anthropic from "@anthropic-ai/sdk";16```1718For named imports:1920```typescript21import { Anthropic, type ClientOptions } from "@anthropic-ai/sdk";22```2324CommonJS:2526```javascript27const Anthropic = require("@anthropic-ai/sdk");28```2930## Basic Usage3132```typescript33import Anthropic from "@anthropic-ai/sdk";3435const client = new Anthropic({36apiKey: process.env.ANTHROPIC_API_KEY, // defaults to process.env["ANTHROPIC_API_KEY"]37});3839async function main() {40const message = await client.messages.create({41max_tokens: 1024,42messages: [{ role: "user", content: "Hello, Claude!" }],43model: "claude-3-5-sonnet-latest",44});4546console.log(message.content);47}4849main();50```5152## Architecture5354The Anthropic SDK is built around these key components:5556- **Client Class**: Main entry point (`Anthropic`) with authentication, HTTP handling, and resource management57- **Resource APIs**: Specialized interfaces for different API endpoints (Messages, Models, Completions, Beta)58- **Type System**: Comprehensive TypeScript definitions for all API parameters and responses59- **Streaming Support**: Server-sent events handling for real-time responses60- **Error Handling**: Rich error hierarchy with detailed API error information61- **Authentication**: Flexible authentication with API keys or auth tokens6263## Capabilities6465### Client Configuration6667Core client initialization and configuration options for authentication, timeouts, retries, and custom request handling.6869```typescript { .api }70class Anthropic extends BaseAnthropic {71constructor(options?: ClientOptions);7273completions: Completions;74messages: Messages;75models: Models;76beta: Beta;77}7879interface ClientOptions {80apiKey?: string | null;81authToken?: string | null;82baseURL?: string | null;83timeout?: number;84maxRetries?: number;85fetch?: Fetch;86dangerouslyAllowBrowser?: boolean;87}88```8990[Client Configuration](./client-configuration.md)9192### Messages API9394Primary interface for conversational AI interactions with Claude models. Supports both streaming and non-streaming responses, with comprehensive message formatting and tool usage capabilities.9596```typescript { .api }97class Messages extends APIResource {98create(params: MessageCreateParamsNonStreaming): APIPromise<Message>;99create(params: MessageCreateParamsStreaming): APIPromise<Stream<RawMessageStreamEvent>>;100stream(params: MessageStreamParams): MessageStream;101countTokens(params: MessageCountTokensParams): APIPromise<MessageTokensCount>;102}103104interface MessageCreateParams {105model: Model;106messages: MessageParam[];107max_tokens: number;108system?: string;109tools?: Tool[];110tool_choice?: ToolChoice;111temperature?: number;112stream?: boolean;113}114```115116[Messages API](./messages-api.md)117118### Models API119120Access to model information and capabilities, allowing applications to discover available models and their specifications.121122```typescript { .api }123class Models extends APIResource {124retrieve(modelID: string, params?: ModelRetrieveParams): APIPromise<ModelInfo>;125list(params?: ModelListParams): PagePromise<ModelInfosPage, ModelInfo>;126}127128interface ModelInfo {129id: string;130display_name: string;131created_at: string;132context_length: number;133max_tokens?: number;134}135```136137[Models API](./models-api.md)138139### Completions API (Legacy)140141Legacy text completion interface for backwards compatibility. New applications should use the Messages API.142143```typescript { .api }144class Completions extends APIResource {145create(params: CompletionCreateParamsNonStreaming): APIPromise<Completion>;146create(params: CompletionCreateParamsStreaming): APIPromise<Stream<Completion>>;147}148149interface CompletionCreateParams {150model: Model;151prompt: string;152max_tokens_to_sample: number;153temperature?: number;154stream?: boolean;155}156```157158[Completions API](./completions-api.md)159160### Beta Features161162Access to experimental and beta features including file handling, advanced model capabilities, and new API endpoints.163164```typescript { .api }165class Beta extends APIResource {166files: Files;167models: Models;168messages: Messages;169}170```171172[Beta Features](./beta-features.md)173174### Error Handling175176Comprehensive error hierarchy with detailed information for debugging and error recovery.177178```typescript { .api }179class AnthropicError extends Error {}180class APIError extends AnthropicError {181status: number;182headers: Headers;183}184class AuthenticationError extends APIError {}185class RateLimitError extends APIError {}186class BadRequestError extends APIError {}187```188189[Error Handling](./error-handling.md)190191## Core Types192193```typescript { .api }194type Model =195| "claude-3-7-sonnet-latest"196| "claude-3-7-sonnet-20250219"197| "claude-sonnet-4-20250514"198| "claude-sonnet-4-0"199| "claude-4-sonnet-20250514"200| "claude-3-5-sonnet-latest"201| "claude-3-5-sonnet-20241022"202| "claude-3-5-sonnet-20240620"203| "claude-3-5-haiku-latest"204| "claude-3-5-haiku-20241022"205| "claude-opus-4-0"206| "claude-opus-4-20250514"207| "claude-4-opus-20250514"208| "claude-opus-4-1-20250805"209| "claude-3-opus-latest"210| "claude-3-opus-20240229"211| "claude-3-haiku-20240307"212| "claude-2.1"213| "claude-2.0"214| "claude-instant-1.2";215216interface Message {217id: string;218type: "message";219role: "assistant";220content: ContentBlock[];221model: Model;222stop_reason: StopReason | null;223stop_sequence: string | null;224usage: Usage;225}226227interface MessageParam {228role: "user" | "assistant";229content: string | ContentBlockParam[];230}231232interface Usage {233input_tokens: number;234output_tokens: number;235}236237type StopReason = "end_turn" | "max_tokens" | "stop_sequence" | "tool_use";238```