Wrapping the Client
Copy
import { Muxx } from 'muxx';
import Anthropic from '@anthropic-ai/sdk';
const muxx = new Muxx();
const client = muxx.wrap(new Anthropic());
const response = await client.messages.create({
model: 'claude-3-5-sonnet-20241022',
max_tokens: 1024,
messages: [{ role: 'user', content: 'Hello!' }],
});
Streaming
Copy
const stream = client.messages.stream({
model: 'claude-3-5-sonnet-20241022',
max_tokens: 1024,
messages: [{ role: 'user', content: 'Write a poem' }],
});
for await (const event of stream) {
if (event.type === 'content_block_delta' && event.delta.type === 'text_delta') {
process.stdout.write(event.delta.text);
}
}
System Prompts
Copy
const response = await client.messages.create({
model: 'claude-3-5-sonnet-20241022',
max_tokens: 1024,
system: 'You are a helpful coding assistant.',
messages: [{ role: 'user', content: 'Write a Python sort function' }],
});
Tool Use
Copy
const tools: Anthropic.Tool[] = [
{
name: 'get_weather',
description: 'Get current weather for a location',
input_schema: {
type: 'object',
properties: {
location: {
type: 'string',
description: 'City and country',
},
},
required: ['location'],
},
},
];
const response = await client.messages.create({
model: 'claude-3-5-sonnet-20241022',
max_tokens: 1024,
tools,
messages: [{ role: 'user', content: "What's the weather in Tokyo?" }],
});
With Traces
Copy
async function analyzeDocument(doc: string): Promise<string> {
return muxx.trace('document-analysis', async () => {
const analysis = await muxx.span('analyze', async () => {
return client.messages.create({
model: 'claude-3-5-sonnet-20241022',
max_tokens: 2048,
messages: [{ role: 'user', content: `Analyze: ${doc}` }],
});
});
return analysis.content[0].type === 'text' ? analysis.content[0].text : '';
});
}
Model Selection
Copy
// Fast and capable
await client.messages.create({ model: 'claude-3-5-sonnet-20241022', ... });
// Fast and cost-effective
await client.messages.create({ model: 'claude-3-5-haiku-20241022', ... });
// Most powerful
await client.messages.create({ model: 'claude-3-opus-20240229', ... });