- 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
models-api.md docs/
1# Models API23The Models API provides access to information about available Claude models, their capabilities, and specifications. Use this API to discover which models are available and their technical details.45## Capabilities67### Retrieve Model Information89Get detailed information about a specific model, including its capabilities and specifications.1011```typescript { .api }12/**13* Get information about a specific model14* @param modelID - The model identifier15* @param params - Optional parameters for the request16* @returns Promise resolving to model information17*/18retrieve(19modelID: string,20params?: ModelRetrieveParams21): APIPromise<ModelInfo>;2223interface ModelRetrieveParams {24/** Beta features to enable for this request */25betas?: AnthropicBeta;26}27```2829**Usage Examples:**3031```typescript32import Anthropic from "@anthropic-ai/sdk";3334const client = new Anthropic();3536// Get information about a specific model37const modelInfo = await client.models.retrieve("claude-3-5-sonnet-latest");3839console.log(`Model: ${modelInfo.display_name}`);40console.log(`Context length: ${modelInfo.context_length} tokens`);41console.log(`Created: ${modelInfo.created_at}`);4243// Get model info with beta features44const betaModelInfo = await client.models.retrieve(45"claude-3-5-sonnet-latest",46{ betas: ["computer-use-2024-10-22"] }47);48```4950### List Available Models5152Get a list of all available models, with the most recently released models listed first.5354```typescript { .api }55/**56* List all available models57* @param params - Optional parameters for filtering and pagination58* @returns PagePromise for iterating through model list59*/60list(params?: ModelListParams): PagePromise<ModelInfosPage, ModelInfo>;6162interface ModelListParams {63/** Maximum number of models to return per page */64limit?: number;65/** Pagination cursor for next page */66after?: string;67/** Pagination cursor for previous page */68before?: string;69/** Beta features to enable for this request */70betas?: AnthropicBeta;71}72```7374**Usage Examples:**7576```typescript77// List all available models78const models = await client.models.list();7980for (const model of models.data) {81console.log(`${model.id}: ${model.display_name}`);82}8384// List with pagination85const firstPage = await client.models.list({ limit: 5 });86console.log(`Found ${firstPage.data.length} models`);8788if (firstPage.has_more) {89const nextPage = await client.models.list({90limit: 5,91after: firstPage.last_id92});93}9495// Iterate through all models automatically96for await (const model of client.models.list()) {97console.log(model.display_name);98}99```100101## Model Information Types102103```typescript { .api }104interface ModelInfo {105/** Unique model identifier */106id: string;107/** Human-readable model name */108display_name: string;109/** Model creation timestamp */110created_at: string;111/** Maximum context length in tokens */112context_length: number;113/** Maximum output tokens (if specified) */114max_tokens?: number;115/** Model type indicator */116type: "model";117}118119interface ModelInfosPage {120/** Array of model information objects */121data: ModelInfo[];122/** Whether there are more results available */123has_more: boolean;124/** ID of the first model in this page */125first_id?: string;126/** ID of the last model in this page */127last_id?: string;128}129```130131## Available Models132133The following models are commonly available (availability may vary):134135```typescript { .api }136type Model =137| "claude-3-7-sonnet-latest"138| "claude-3-7-sonnet-20250219"139| "claude-sonnet-4-20250514"140| "claude-sonnet-4-0"141| "claude-4-sonnet-20250514"142| "claude-3-5-sonnet-latest"143| "claude-3-5-sonnet-20241022"144| "claude-3-5-sonnet-20240620"145| "claude-3-5-haiku-latest"146| "claude-3-5-haiku-20241022"147| "claude-opus-4-0"148| "claude-opus-4-20250514"149| "claude-4-opus-20250514"150| "claude-opus-4-1-20250805"151| "claude-3-opus-latest"152| "claude-3-opus-20240229"153| "claude-3-haiku-20240307"154| "claude-2.1"155| "claude-2.0"156| "claude-instant-1.2";157```158159**Model Characteristics:**160161- **Claude Sonnet 4**: Newest generation with enhanced capabilities and reasoning162- **Claude Opus 4**: Most powerful Claude 4 model, best for highly complex tasks163- **Claude 3.7 Sonnet**: Advanced Sonnet with improved performance164- **Claude 3.5 Sonnet**: Latest stable Sonnet model, excellent for complex reasoning165- **Claude 3.5 Haiku**: Fast and efficient, great for quick responses166- **Claude 3 Opus**: Most powerful Claude 3 model for complex tasks167- **Claude 3 Haiku**: Fastest Claude 3 model, optimized for speed168- **Claude 2.1**: Previous generation, still capable for many tasks169- **Claude 2.0**: Earlier version with good general capabilities170- **Claude Instant 1.2**: Legacy fast model, being phased out171172## Model Selection Guide173174**For maximum capability and reasoning:**175- `claude-opus-4-20250514`176- `claude-sonnet-4-20250514`177- `claude-3-7-sonnet-latest`178179**For balanced performance:**180- `claude-3-5-sonnet-latest`181- `claude-3-5-sonnet-20241022`182183**For speed and efficiency:**184- `claude-3-5-haiku-latest`185- `claude-3-5-haiku-20241022`186187**Usage Examples:**188189```typescript190// Check if a specific model is available191const models = await client.models.list();192const isOpusAvailable = models.data.some(m => m.id === "claude-3-opus-20240229");193194if (isOpusAvailable) {195const message = await client.messages.create({196model: "claude-3-opus-20240229",197max_tokens: 1024,198messages: [{ role: "user", content: "Complex reasoning task..." }],199});200}201202// Find the latest Sonnet model203const sonnetModels = models.data.filter(m =>204m.id.includes("sonnet") && m.id.includes("claude-3")205);206const latestSonnet = sonnetModels[0]; // Models are ordered by creation date207208// Check context length for long documents209const modelInfo = await client.models.retrieve("claude-3-sonnet-20240229");210if (modelInfo.context_length >= 200000) {211console.log("This model can handle very long documents");212}213```214215## Pagination Support216217```typescript { .api }218/**219* Page object for model listings with automatic pagination support220*/221class ModelInfosPage extends Page<ModelInfo> {222/** Get the next page of results */223nextPage(): Promise<ModelInfosPage | null>;224/** Get the previous page of results */225previousPage(): Promise<ModelInfosPage | null>;226/** Iterate through all pages automatically */227[Symbol.asyncIterator](): AsyncIterableIterator<ModelInfo>;228}229```230231**Usage Examples:**232233```typescript234// Manual pagination235let page = await client.models.list({ limit: 10 });236while (page) {237for (const model of page.data) {238console.log(model.display_name);239}240page = await page.nextPage();241}242243// Automatic iteration through all models244for await (const model of client.models.list()) {245console.log(`${model.id}: ${model.context_length} tokens`);246}247```248249## Beta Features250251Some model information may require beta feature access:252253```typescript { .api }254type AnthropicBeta =255| "computer-use-2024-10-22"256| "pdfs-2024-09-25"257| "token-counting-2024-11-01"258| string[];259```260261**Usage Examples:**262263```typescript264// Access beta model features265const betaModels = await client.models.list({266betas: ["computer-use-2024-10-22"],267});268269// Check for beta-specific capabilities270const computerUseModel = await client.models.retrieve(271"claude-3-5-sonnet-20241022",272{ betas: ["computer-use-2024-10-22"] }273);274```275276## Error Handling277278```typescript { .api }279// Common errors when working with models280try {281const model = await client.models.retrieve("nonexistent-model");282} catch (error) {283if (error instanceof NotFoundError) {284console.log("Model not found");285} else if (error instanceof AuthenticationError) {286console.log("Invalid API key");287}288}289```