Type-safe Claude API wrapper with streaming, function calling, and conversation memory.
- Full Claude 3.5 Sonnet/Opus support
- Streaming with async iterators
- Function/Tool calling
- Conversation memory management
- TypeScript-first design
- Automatic retry with backoff
- Token counting
npm install claude-api-wrapperimport { Claude } from 'claude-api-wrapper';
const claude = new Claude({
apiKey: process.env.ANTHROPIC_API_KEY!,
});
// Simple completion
const response = await claude.complete('Explain quantum computing');
console.log(response.content);
// Streaming
for await (const chunk of claude.stream('Write a poem about coding')) {
process.stdout.write(chunk);
}
// With conversation memory
const conversation = claude.conversation();
await conversation.send('My name is Alice');
const reply = await conversation.send('What is my name?');
// "Your name is Alice"| Option | Type | Default | Description |
|---|---|---|---|
apiKey |
string | required | Anthropic API key |
model |
string | 'claude-3-5-sonnet' |
Model to use |
maxTokens |
number | 4096 |
Max output tokens |
temperature |
number | 1 |
Sampling temperature |
// Simple completion
const response = await claude.complete(prompt, options?);
// Streaming
for await (const chunk of claude.stream(prompt, options?)) {
// chunk is string
}
// With system prompt
const response = await claude.complete(prompt, {
system: 'You are a helpful coding assistant',
});
// Conversation with memory
const conv = claude.conversation({ system: 'You are helpful' });
await conv.send('Hello');
await conv.send('Follow up question');
conv.clear(); // Reset memoryconst response = await claude.complete('What is the weather in Tokyo?', {
tools: [
{
name: 'get_weather',
description: 'Get current weather for a location',
input_schema: {
type: 'object',
properties: {
location: { type: 'string', description: 'City name' },
},
required: ['location'],
},
},
],
});
if (response.toolUse) {
const { name, input } = response.toolUse;
// name: 'get_weather', input: { location: 'Tokyo' }
// Execute tool and continue conversation
const result = await getWeather(input.location);
const final = await claude.complete('', {
toolResult: { id: response.toolUse.id, content: result },
});
}import { ClaudeError, RateLimitError } from 'claude-api-wrapper';
try {
await claude.complete(prompt);
} catch (error) {
if (error instanceof RateLimitError) {
console.log('Rate limited, retry after:', error.retryAfter);
}
}MIT