Migrate from Anthropic

Switch from direct Anthropic API to AI Gateway for unified API access.

Overview

Migrating from the Anthropic SDK to AI Gateway lets you use a unified OpenAI-compatible API for all providers. You gain caching, rate limiting, cost tracking, and easy provider switching.

Migration Options

You have two options:

  1. OpenAI SDK (recommended) - Use the OpenAI SDK with AI Gateway for a unified API
  2. Anthropic SDK - Continue using the Anthropic SDK with AI Gateway

Use the OpenAI SDK for all providers through AI Gateway:

Before (Direct Anthropic)

import Anthropic from '@anthropic-ai/sdk';
 
const anthropic = new Anthropic({
  apiKey: process.env.ANTHROPIC_API_KEY,
});
 
const response = await anthropic.messages.create({
  model: 'claude-3-5-sonnet-20241022',
  max_tokens: 1024,
  messages: [{ role: 'user', content: 'Hello!' }],
});

After (AI Gateway with OpenAI SDK)

import OpenAI from 'openai';
 
const client = new OpenAI({
  baseURL: 'https://api.transactional.dev/ai/v1',
  apiKey: process.env.GATEWAY_API_KEY,  // gw_sk_xxx
});
 
const response = await client.chat.completions.create({
  model: 'claude-3-5-sonnet',  // Anthropic model via Gateway
  messages: [{ role: 'user', content: 'Hello!' }],
  max_tokens: 1024,
});

Benefits of OpenAI SDK

  • Unified API - Same code for OpenAI, Anthropic, Google, etc.
  • Easy switching - Change providers by changing the model name
  • Familiar syntax - Most developers know the OpenAI API

Option 2: Continue with Anthropic SDK

Use the Anthropic SDK with AI Gateway:

Before (Direct Anthropic)

import Anthropic from '@anthropic-ai/sdk';
 
const anthropic = new Anthropic({
  apiKey: process.env.ANTHROPIC_API_KEY,
});

After (AI Gateway)

import Anthropic from '@anthropic-ai/sdk';
 
const anthropic = new Anthropic({
  baseURL: 'https://api.transactional.dev/ai/anthropic/v1',
  apiKey: process.env.GATEWAY_API_KEY,  // gw_sk_xxx
});
 
// All existing code works unchanged!
const response = await anthropic.messages.create({
  model: 'claude-3-5-sonnet-20241022',
  max_tokens: 1024,
  messages: [{ role: 'user', content: 'Hello!' }],
});

Step-by-Step Migration

1. Get Your Gateway API Key

  1. Go to AI Gateway Dashboard
  2. Click API Keys
  3. Create a new key (starts with gw_sk_)

2. Add Your Anthropic Provider Key

  1. Go to Provider Keys in the dashboard
  2. Add your Anthropic API key
  3. AI Gateway will use this to call Anthropic on your behalf

3. Update Your Code

Choose your preferred SDK and update:

OpenAI SDK (Recommended):

import OpenAI from 'openai';
 
const client = new OpenAI({
  baseURL: 'https://api.transactional.dev/ai/v1',
  apiKey: process.env.GATEWAY_API_KEY,
});

Anthropic SDK:

import Anthropic from '@anthropic-ai/sdk';
 
const anthropic = new Anthropic({
  baseURL: 'https://api.transactional.dev/ai/anthropic/v1',
  apiKey: process.env.GATEWAY_API_KEY,
});

API Mapping

When using the OpenAI SDK with Anthropic models:

Anthropic APIOpenAI API (via Gateway)
messages.create()chat.completions.create()
model: 'claude-3-5-sonnet-20241022'model: 'claude-3-5-sonnet'
system: 'You are...'messages: [{ role: 'system', content: '...' }]
max_tokens: 1024max_tokens: 1024

Example Conversion

Anthropic SDK:

const response = await anthropic.messages.create({
  model: 'claude-3-5-sonnet-20241022',
  max_tokens: 1024,
  system: 'You are a helpful assistant.',
  messages: [
    { role: 'user', content: 'Hello!' },
  ],
});
 
console.log(response.content[0].text);

OpenAI SDK via Gateway:

const response = await client.chat.completions.create({
  model: 'claude-3-5-sonnet',
  max_tokens: 1024,
  messages: [
    { role: 'system', content: 'You are a helpful assistant.' },
    { role: 'user', content: 'Hello!' },
  ],
});
 
console.log(response.choices[0].message.content);

Feature Compatibility

FeatureAnthropic SDKOpenAI SDK via Gateway
Chat messagesYesYes
StreamingYesYes
Tool useYesYes
Vision (images)YesYes
System promptsYesYes
JSON mode-Yes (via response_format)

Python Migration

Before (Direct Anthropic)

from anthropic import Anthropic
 
client = Anthropic(
    api_key=os.environ["ANTHROPIC_API_KEY"]
)
 
response = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello!"}]
)

After (OpenAI SDK via Gateway)

from openai import OpenAI
 
client = OpenAI(
    base_url="https://api.transactional.dev/ai/v1",
    api_key=os.environ["GATEWAY_API_KEY"]
)
 
response = client.chat.completions.create(
    model="claude-3-5-sonnet",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello!"}]
)

What You Get

After migration, you automatically get:

FeatureDescription
Unified APISame code for all providers
Semantic CachingAutomatic response caching
Cost TrackingReal-time cost monitoring
Rate LimitingConfigurable request limits
FallbackAutomatic failover to backup providers
AnalyticsUsage dashboards
Easy SwitchingChange providers by changing model name

Multi-Provider Example

With AI Gateway, switch providers without changing your code:

const client = new OpenAI({
  baseURL: 'https://api.transactional.dev/ai/v1',
  apiKey: process.env.GATEWAY_API_KEY,
});
 
// Anthropic Claude
const claude = await client.chat.completions.create({
  model: 'claude-3-5-sonnet',
  messages: [{ role: 'user', content: 'Hello!' }],
});
 
// OpenAI GPT-4
const gpt4 = await client.chat.completions.create({
  model: 'gpt-4o',
  messages: [{ role: 'user', content: 'Hello!' }],
});
 
// Google Gemini
const gemini = await client.chat.completions.create({
  model: 'gemini-1.5-pro',
  messages: [{ role: 'user', content: 'Hello!' }],
});

Streaming

Streaming works with both approaches:

OpenAI SDK:

const stream = await client.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 || '');
}

Tool Use

Tool use is supported:

const response = await client.chat.completions.create({
  model: 'claude-3-5-sonnet',
  messages: [{ role: 'user', content: 'What is the weather?' }],
  tools: [
    {
      type: 'function',
      function: {
        name: 'get_weather',
        description: 'Get weather for a location',
        parameters: {
          type: 'object',
          properties: {
            location: { type: 'string' },
          },
        },
      },
    },
  ],
});

Rollback

If you need to rollback, revert to direct Anthropic:

import Anthropic from '@anthropic-ai/sdk';
 
const anthropic = new Anthropic({
  apiKey: process.env.ANTHROPIC_API_KEY,
});

Troubleshooting

Model Not Found

  • Use the Gateway model name: claude-3-5-sonnet (not claude-3-5-sonnet-20241022)
  • Check available models in the dashboard

Response Format Different

When switching from Anthropic SDK to OpenAI SDK:

  • Anthropic: response.content[0].text
  • OpenAI: response.choices[0].message.content

Tool Call Format Different

Tool calls have slightly different formats between SDKs. See the API documentation for details.

Next Steps