The official TypeScript library for the Anthropic API
The @anthropic-ai/sdk is Anthropic's official TypeScript/JavaScript client library for accessing Claude AI models through their API. With over 5.5 million weekly downloads, it has become the standard implementation for developers building applications with Claude's conversational AI capabilities. The SDK provides type-safe interfaces for the Messages API, streaming responses, tool usage, and beta features.
The library exists to abstract the complexity of making direct HTTP requests to Anthropic's API while providing robust error handling, automatic retries, and TypeScript type definitions. It handles authentication, request formatting, response parsing, and provides helpers for advanced features like function calling and multi-modal inputs. The SDK works in both Node.js and browser environments with appropriate configuration.
The primary users are developers building conversational AI applications, chatbots, content generation tools, and AI-powered automation. Companies integrating Claude for customer support, document analysis, code generation, and creative writing workflows rely on this SDK for production deployments. It's designed for developers who need direct, feature-complete access to Claude models without the overhead of multi-provider abstractions.
import Anthropic from '@anthropic-ai/sdk';
const client = new Anthropic({
apiKey: process.env.ANTHROPIC_API_KEY
});
async function analyzeCodeWithTools() {
const conversation = [
{ role: 'user', content: 'Calculate the factorial of 5 and explain the result' }
];
const tools = [{
name: 'calculate_factorial',
description: 'Calculates the factorial of a number',
input_schema: {
type: 'object',
properties: {
number: { type: 'integer', description: 'The number to calculate factorial for' }
},
required: ['number']
}
}];
let response = await client.messages.create({
model: 'claude-sonnet-4-20250514',
max_tokens: 1024,
messages: conversation,
tools
});
console.log('Initial response:', response.stop_reason);
if (response.stop_reason === 'tool_use') {
const toolUse = response.content.find(block => block.type === 'tool_use');
const factorial = (n) => n <= 1 ? 1 : n * factorial(n - 1);
const result = factorial(toolUse.input.number);
conversation.push({ role: 'assistant', content: response.content });
conversation.push({
role: 'user',
content: [{
type: 'tool_result',
tool_use_id: toolUse.id,
content: String(result)
}]
});
response = await client.messages.create({
model: 'claude-sonnet-4-20250514',
max_tokens: 1024,
messages: conversation,
tools
});
}
const textContent = response.content.find(block => block.type === 'text');
console.log('Final answer:', textContent.text);
}
analyzeCodeWithTools();Conversational Chatbots: Build interactive chat interfaces where Claude maintains context across multiple messages, handles follow-up questions, and provides natural language responses. The SDK's streaming support enables real-time token-by-token display for better user experience.
Tool-Augmented AI Agents: Create AI assistants that can call external functions and APIs by declaring tools in your requests. Claude decides when to use tools, the SDK handles the structured output, and your code executes the function before sending results back to continue the conversation.
Document Analysis and Processing: Upload documents and images for Claude to analyze, extract information, summarize content, or answer questions about visual data. The SDK supports multi-modal inputs through the Messages API with proper content type handling.
Code Generation and Review: Integrate Claude into development workflows for generating code snippets, reviewing pull requests, explaining complex logic, or converting between programming languages. The high token limits and code-optimized models make this particularly effective.
Content Moderation and Classification: Process user-generated content at scale by sending text to Claude for safety classification, sentiment analysis, or topic categorization. The SDK's error handling and retry logic ensure reliable operation in high-throughput scenarios.
npm install @anthropic-ai/sdkpnpm add @anthropic-ai/sdkbun add @anthropic-ai/sdk