Transactional

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

ModelContext WindowMax OutputBest For
claude-opus-4200K32KMost capable, complex tasks
claude-sonnet-4200K64KBalanced performance
claude-3-5-sonnet200K8KGeneral purpose, fast
claude-3-opus200K4KPrevious flagship
claude-3-sonnet200K4KPrevious generation
claude-3-haiku200K4KFast, cost-effective

Setup

1. Get Anthropic API Key

  1. Go to Anthropic Console
  2. Create a new API key
  3. Copy the key (starts with sk-ant-)

2. Add Key to AI Gateway

  1. Navigate to AI Gateway Settings
  2. Click Add Key under Provider Keys
  3. Select "Anthropic"
  4. Paste your API key
  5. 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 FormatAnthropic Format
system messagesystem parameter
functiontool_use
tool_choice: "auto"tool_choice: { type: "auto" }
response_format: jsonJSON 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 ParameterAnthropic Equivalent
temperaturetemperature (0-1, scaled)
max_tokensmax_tokens
top_ptop_p
stopstop_sequences
streamstream
toolstools

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):

ModelInputOutput
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