CC-301l · Module 2

SDK Patterns

3 min read

The Anthropic SDK (available for TypeScript, Python, and other languages) handles authentication, request formatting, error handling, and streaming. The TypeScript SDK (@anthropic-ai/sdk) is the most natural choice for projects already in the Claude Code ecosystem. Install it, set the ANTHROPIC_API_KEY environment variable, and you have a fully typed client ready to make API calls.

The critical SDK patterns for production use: streaming for long responses (process tokens as they arrive rather than waiting for the complete response), error handling with retries (the API occasionally returns 429 rate limit or 529 overloaded errors — implement exponential backoff), and conversation management (maintain message history for multi-turn interactions without sending redundant context).

import Anthropic from '@anthropic-ai/sdk';
const client = new Anthropic();

// Streaming for real-time output
const stream = client.messages.stream({
  model: 'claude-sonnet-4-20250514',
  max_tokens: 2048,
  messages: [{ role: 'user', content: prompt }],
});

stream.on('text', (text) => {
  process.stdout.write(text); // Real-time token output
});

const finalMessage = await stream.finalMessage();
console.log(`\nTokens used: ${finalMessage.usage.input_tokens + finalMessage.usage.output_tokens}`);