Skip to content

SDK Specifics: Anthropic vs OpenAI

Overview

Each provider's SDK has different APIs, async patterns, and error handling.

Choosing an SDK affects your entire architecture.


Anthropic SDK

Key Features

from anthropic import Anthropic
import anthropic

class AnthropicSDK:
    """Anthropic SDK patterns"""

    def __init__(self):
        self.client = Anthropic(api_key="sk-ant-...")

    def sync_call(self):
        """Synchronous API call"""

        response = self.client.messages.create(
            model="claude-3-5-sonnet-20241022",
            max_tokens=1024,
            messages=[
                {"role": "user", "content": "Hello"}
            ]
        )

        return response.content[0].text

    async def async_call(self):
        """Async API call"""

        async_client = Anthropic()  # Auto-uses async

        response = await async_client.messages.create(
            model="claude-3-5-sonnet-20241022",
            max_tokens=1024,
            messages=[
                {"role": "user", "content": "Hello"}
            ]
        )

        return response.content[0].text

    def handle_errors(self):
        """Anthropic error handling"""

        try:
            response = self.client.messages.create(
                model="claude-3-5-sonnet",
                messages=[{"role": "user", "content": ""}]
            )
        except anthropic.APIError as e:
            # API error
            print(f"API Error: {e}")
        except anthropic.RateLimitError:
            # Rate limit
            print("Rate limited")
        except anthropic.APIConnectionError:
            # Network error
            print("Connection error")

OpenAI SDK

Key Features

from openai import OpenAI
import openai

class OpenAISDK:
    """OpenAI SDK patterns"""

    def __init__(self):
        self.client = OpenAI(api_key="sk-...")

    def sync_call(self):
        """Synchronous API call"""

        response = self.client.chat.completions.create(
            model="gpt-4",
            messages=[
                {"role": "user", "content": "Hello"}
            ]
        )

        return response.choices[0].message.content

    async def async_call(self):
        """Async API call"""

        async_client = OpenAI()

        response = await async_client.chat.completions.create(
            model="gpt-4",
            messages=[
                {"role": "user", "content": "Hello"}
            ]
        )

        return response.choices[0].message.content

    def handle_errors(self):
        """OpenAI error handling"""

        try:
            response = self.client.chat.completions.create(
                model="gpt-4",
                messages=[{"role": "user", "content": ""}]
            )
        except openai.APIError as e:
            # API error
            print(f"API Error: {e}")
        except openai.RateLimitError:
            # Rate limit
            print("Rate limited")
        except openai.APIConnectionError:
            # Network error
            print("Connection error")

Comparison

Feature Anthropic OpenAI
Streaming .stream() stream=True
Async AsyncAnthropic AsyncOpenAI
Tool Use tool_use blocks tool_calls
Vision Yes Yes
Error Types anthropic.* openai.*

3 Warnings ⚠️

Warning 1: API Key Management

# ❌ WRONG
ANTHROPIC_KEY = "sk-ant-..."
OPENAI_KEY = "sk-..."
# Hardcoded keys!

# ✅ RIGHT
import os
ANTHROPIC_KEY = os.getenv("ANTHROPIC_API_KEY")
OPENAI_KEY = os.getenv("OPENAI_API_KEY")

Warning 2: Response Format

# ❌ WRONG
# Assume same response structure
response = client.messages.create(...)
content = response.content  # Different per SDK!

# ✅ RIGHT
# Use SDK-specific accessors
if provider == "anthropic":
    content = response.content[0].text
elif provider == "openai":
    content = response.choices[0].message.content

Warning 3: Timeout Handling

# ❌ WRONG
# Assume same timeout mechanism

# ✅ RIGHT
# Set timeouts per SDK
anthropic_client = Anthropic(timeout=30.0)
openai_client = OpenAI(timeout=30.0)

Last Updated: August 9, 2026