Anthropic
Using Anthropic Claude models through AI Gateway.
Overview
AI Gateway supports Anthropic's Claude models with automatic translation from the OpenAI API format. Use the familiar OpenAI SDK to access Claude Opus 4, Claude Sonnet 4, Claude 3.5, and Claude 3 Haiku.
Supported Models
| Model | Context Window | Max Output | Best For |
|---|---|---|---|
claude-opus-4 | 200K | 32K | Most capable, complex tasks |
claude-sonnet-4 | 200K | 64K | Balanced performance |
claude-3-5-sonnet | 200K | 8K | General purpose, fast |
claude-3-opus | 200K | 4K | Previous flagship |
claude-3-sonnet | 200K | 4K | Previous generation |
claude-3-haiku | 200K | 4K | Fast, cost-effective |
Setup
1. Get Anthropic API Key
- Go to Anthropic Console
- Create a new API key
- Copy the key (starts with
sk-ant-)
2. Add Key to AI Gateway
- Navigate to AI Gateway Settings
- Click Add Key under Provider Keys
- Select "Anthropic"
- Paste your API key
- Click Save
Usage
Basic Chat Completion
Use the OpenAI SDK format - AI Gateway handles the translation:
import OpenAI from 'openai';
const openai = new OpenAI({
baseURL: 'https://api.transactional.dev/ai/v1',
apiKey: process.env.GATEWAY_API_KEY,
});
const response = await openai.chat.completions.create({
model: 'claude-3-5-sonnet',
messages: [
{ role: 'system', content: 'You are a helpful assistant.' },
{ role: 'user', content: 'What is the capital of France?' }
],
});
console.log(response.choices[0].message.content);With Streaming
const stream = await openai.chat.completions.create({
model: 'claude-3-5-sonnet',
messages: [{ role: 'user', content: 'Tell me a story' }],
stream: true,
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content || '');
}With Tool Use
Claude's tool use is supported via OpenAI's function calling format:
const response = await openai.chat.completions.create({
model: 'claude-3-5-sonnet',
messages: [{ role: 'user', content: 'What is the weather in Paris?' }],
tools: [
{
type: 'function',
function: {
name: 'get_weather',
description: 'Get the current weather for a location',
parameters: {
type: 'object',
properties: {
location: { type: 'string', description: 'City name' }
},
required: ['location']
}
}
}
],
});Long Context
Claude supports up to 200K tokens of context:
const response = await openai.chat.completions.create({
model: 'claude-3-5-sonnet',
messages: [
{ role: 'user', content: veryLongDocument }, // Up to 200K tokens
{ role: 'user', content: 'Summarize the key points' }
],
});OpenAI to Anthropic Translation
AI Gateway automatically handles format differences:
| OpenAI Format | Anthropic Format |
|---|---|
system message | system parameter |
function | tool_use |
tool_choice: "auto" | tool_choice: { type: "auto" } |
response_format: json | JSON instruction in prompt |
System Messages
System messages are automatically extracted and sent as Anthropic's system parameter:
// Your code (OpenAI format)
messages: [
{ role: 'system', content: 'You are helpful.' },
{ role: 'user', content: 'Hello' }
]
// AI Gateway sends to Anthropic
{
system: 'You are helpful.',
messages: [
{ role: 'user', content: 'Hello' }
]
}Parameters
Supported parameters with automatic translation:
| OpenAI Parameter | Anthropic Equivalent |
|---|---|
temperature | temperature (0-1, scaled) |
max_tokens | max_tokens |
top_p | top_p |
stop | stop_sequences |
stream | stream |
tools | tools |
Parameter Differences
- Temperature: OpenAI uses 0-2, Anthropic uses 0-1. AI Gateway scales automatically.
- Max Tokens: Required for Anthropic. AI Gateway sets a default if not provided.
Pricing
AI Gateway passes through Anthropic's pricing. Current rates (as of Jan 2025):
| Model | Input | Output |
|---|---|---|
| claude-opus-4 | $15.00/1M | $75.00/1M |
| claude-sonnet-4 | $3.00/1M | $15.00/1M |
| claude-3-5-sonnet | $3.00/1M | $15.00/1M |
| claude-3-opus | $15.00/1M | $75.00/1M |
| claude-3-haiku | $0.25/1M | $1.25/1M |
Best Practices
Model Selection
- claude-opus-4: Complex reasoning, analysis, code generation
- claude-sonnet-4: Balanced performance for most tasks
- claude-3-5-sonnet: Fast responses, general purpose
- claude-3-haiku: Quick tasks, lower cost
System Prompts
Claude responds well to clear, detailed system prompts:
const response = await openai.chat.completions.create({
model: 'claude-3-5-sonnet',
messages: [
{
role: 'system',
content: `You are an expert technical writer.
- Use clear, concise language
- Include code examples when relevant
- Format responses in markdown`
},
{ role: 'user', content: 'Explain async/await in JavaScript' }
],
});Troubleshooting
Model not found
Ensure you've added your Anthropic API key in Settings.
Rate limits
Anthropic has different rate limits than OpenAI. Check your Anthropic console.
Long responses cut off
Always set max_tokens for Claude models:
const response = await openai.chat.completions.create({
model: 'claude-3-5-sonnet',
max_tokens: 4096, // Always set for Anthropic
messages: [...],
});Next Steps
- OpenAI Integration
- Fallback Configuration - Use Claude as fallback
- Cost Tracking - Monitor Claude costs
On This Page
- Overview
- Supported Models
- Setup
- 1. Get Anthropic API Key
- 2. Add Key to AI Gateway
- Usage
- Basic Chat Completion
- With Streaming
- With Tool Use
- Long Context
- OpenAI to Anthropic Translation
- System Messages
- Parameters
- Parameter Differences
- Pricing
- Best Practices
- Model Selection
- System Prompts
- Troubleshooting
- Model not found
- Rate limits
- Long responses cut off
- Next Steps