Swape.AISwape.AI Docs

Anthropic SDK

Use the official Anthropic SDK with OpenModel by changing the base URL

The official Anthropic SDK works with OpenModel out of the box. Point it to the OpenModel base URL and use your OpenModel API key to access any model available through the Messages protocol.

Supported Providers

The Messages protocol (/v1/messages) serves multiple providers through a single SDK. All the examples on this page work with any provider below — just change the model name:

ProviderExample ModelsNotes
Anthropicclaude-sonnet-4-20250514, claude-haiku-4-20250414Native Messages protocol
DeepSeekdeepseek-chat, deepseek-reasoner
DashScope (Alibaba Cloud)qwen3-maxAlso supports the Responses protocol
Xiaomi (MiMo)mimo-v2.5-pro, mimo-v2-flash

See Available Models for the full provider and model list.

Installation

pip install anthropic

Configuration

Python

import anthropic

client = anthropic.Anthropic(
    base_url="https://api.openmodel.ai",
    api_key="om-your-api-key",
)

Node.js

import Anthropic from "@anthropic-ai/sdk"

const client = new Anthropic({
  baseURL: "https://api.openmodel.ai",
  apiKey: "om-your-api-key",
})

Note: The Anthropic SDK base URL should be https://api.openmodel.ai (without /v1). The SDK appends the /v1/messages path automatically.

Environment Variables

Configure the SDK using environment variables:

export ANTHROPIC_BASE_URL="https://api.openmodel.ai"
export ANTHROPIC_API_KEY="om-your-api-key"

Then initialize the client without explicit parameters:

import anthropic

client = anthropic.Anthropic()  # Reads from environment variables

Examples

Basic Message

import anthropic

client = anthropic.Anthropic(
    base_url="https://api.openmodel.ai",
    api_key="om-your-api-key",
)

message = client.messages.create(
    model="claude-sonnet-4-20250514",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Explain how a neural network learns."}
    ],
)

print(message.content[0].text)

Tip: The same code works for all Messages protocol providers. To use DeepSeek instead of Anthropic, just change model="claude-sonnet-4-20250514" to model="deepseek-chat".

System Prompt

import anthropic

client = anthropic.Anthropic(
    base_url="https://api.openmodel.ai",
    api_key="om-your-api-key",
)

message = client.messages.create(
    model="claude-sonnet-4-20250514",
    max_tokens=1024,
    system="You are an expert Python developer. Provide concise, production-ready code.",
    messages=[
        {"role": "user", "content": "Write a function to merge two sorted arrays."}
    ],
)

print(message.content[0].text)

Streaming

import anthropic

client = anthropic.Anthropic(
    base_url="https://api.openmodel.ai",
    api_key="om-your-api-key",
)

with client.messages.stream(
    model="claude-sonnet-4-20250514",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Write a detailed guide to making sourdough bread."}
    ],
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)
print()

Multi-Turn Conversation

import anthropic

client = anthropic.Anthropic(
    base_url="https://api.openmodel.ai",
    api_key="om-your-api-key",
)

message = client.messages.create(
    model="claude-sonnet-4-20250514",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "What is the capital of France?"},
        {"role": "assistant", "content": "The capital of France is Paris."},
        {"role": "user", "content": "What is its population?"},
    ],
)

print(message.content[0].text)

Tool Use

import anthropic

client = anthropic.Anthropic(
    base_url="https://api.openmodel.ai",
    api_key="om-your-api-key",
)

tools = [
    {
        "name": "get_weather",
        "description": "Get the current weather for a location.",
        "input_schema": {
            "type": "object",
            "properties": {
                "location": {
                    "type": "string",
                    "description": "City and country, e.g. 'Tokyo, Japan'",
                },
            },
            "required": ["location"],
        },
    },
]

message = client.messages.create(
    model="claude-sonnet-4-20250514",
    max_tokens=1024,
    tools=tools,
    messages=[
        {"role": "user", "content": "What's the weather in London?"}
    ],
)

for block in message.content:
    if block.type == "tool_use":
        print(f"Tool: {block.name}")
        print(f"Input: {block.input}")

Async Usage (Python)

import asyncio
import anthropic

client = anthropic.AsyncAnthropic(
    base_url="https://api.openmodel.ai",
    api_key="om-your-api-key",
)

async def main():
    message = await client.messages.create(
        model="claude-sonnet-4-20250514",
        max_tokens=1024,
        messages=[
            {"role": "user", "content": "Hello!"}
        ],
    )
    print(message.content[0].text)

asyncio.run(main())

Error Handling

import anthropic

client = anthropic.Anthropic(
    base_url="https://api.openmodel.ai",
    api_key="om-your-api-key",
)

try:
    message = client.messages.create(
        model="claude-sonnet-4-20250514",
        max_tokens=1024,
        messages=[
            {"role": "user", "content": "Hello!"}
        ],
    )
except anthropic.AuthenticationError:
    print("Invalid API key. Check your credentials.")
except anthropic.RateLimitError:
    print("Rate limited. Slow down your requests.")
except anthropic.APIConnectionError:
    print("Connection failed. Check your network.")
except anthropic.APIStatusError as e:
    print(f"API error {e.status_code}: {e.message}")

For comprehensive error handling patterns, see Error Handling.

Next Steps

On this page