How to beginner · 3 min read

How to switch from OpenAI to Anthropic API

Quick answer
To switch from the OpenAI API to the Anthropic API, replace the OpenAI client with the Anthropic client and update your code to use anthropic.Anthropic with the system= parameter instead of role="system". Adjust model names to claude-3-5-sonnet-20241022 and update message formats accordingly.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (for OpenAI usage)
  • Anthropic API key
  • pip install openai>=1.0 anthropic>=0.20

Setup

Install both openai and anthropic Python packages to enable switching between APIs. Set environment variables for your API keys.

  • Set OPENAI_API_KEY for OpenAI
  • Set ANTHROPIC_API_KEY for Anthropic
bash
pip install openai>=1.0 anthropic>=0.20
output
$ pip install openai anthropic
Collecting openai
Collecting anthropic
Successfully installed openai-1.x anthropic-0.20.x

Step by step

Here is a runnable example showing how to switch from OpenAI's gpt-4o model to Anthropic's claude-3-5-sonnet-20241022 model using their respective SDKs.

python
import os

# OpenAI example
from openai import OpenAI
client_openai = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
response_openai = client_openai.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Hello from OpenAI!"}]
)
print("OpenAI response:", response_openai.choices[0].message.content)

# Anthropic example
import anthropic
client_anthropic = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
response_anthropic = client_anthropic.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=1024,
    system="You are a helpful assistant.",
    messages=[{"role": "user", "content": "Hello from Anthropic!"}]
)
print("Anthropic response:", response_anthropic.content[0].text)
output
OpenAI response: Hello from OpenAI!
Anthropic response: Hello from Anthropic!

Common variations

You can switch to async calls or streaming with both SDKs. Also, you can change models easily by updating the model parameter.

  • OpenAI supports models like gpt-4o-mini, gpt-4o
  • Anthropic supports claude-3-5-sonnet-20241022, claude-opus-4
python
import asyncio
import os
from openai import OpenAI
import anthropic

async def openai_async():
    client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
    response = await client.chat.completions.acreate(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": "Async hello OpenAI!"}]
    )
    print("OpenAI async response:", response.choices[0].message.content)

async def anthropic_async():
    client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
    response = await client.messages.acreate(
        model="claude-3-5-sonnet-20241022",
        max_tokens=512,
        system="You are a helpful assistant.",
        messages=[{"role": "user", "content": "Async hello Anthropic!"}]
    )
    print("Anthropic async response:", response.content[0].text)

async def main():
    await openai_async()
    await anthropic_async()

asyncio.run(main())
output
OpenAI async response: Async hello OpenAI!
Anthropic async response: Async hello Anthropic!

Troubleshooting

  • If you get authentication errors, verify your API keys are correctly set in environment variables.
  • Anthropic requires the system= parameter for system prompts; do not use role="system" in messages.
  • Check model names carefully; outdated names will cause errors.

Key Takeaways

  • Replace OpenAI client with anthropic.Anthropic and update message format accordingly.
  • Use system= parameter in Anthropic SDK instead of role="system" in messages.
  • Update model names to current Anthropic models like claude-3-5-sonnet-20241022.
  • Set API keys in environment variables OPENAI_API_KEY and ANTHROPIC_API_KEY.
  • Both SDKs support async and streaming; adapt your code to their respective async methods.
Verified 2026-04 · gpt-4o, gpt-4o-mini, claude-3-5-sonnet-20241022, claude-opus-4
Verify ↗