Anthropic Python SDK

Prerequisites

Installation

terminal
bash
pip install anthropic

Basic Usage

example.py
python
from anthropic import Anthropic

client = Anthropic(
    base_url="https://api.chuizi.ai/anthropic",
    api_key="ck-your-key-here",
)

message = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Hello!"}
    ],
)

print(message.content[0].text)

Streaming

example.py
python
with client.messages.stream(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Write a web scraper in Python"}
    ],
) as stream:
    for text in stream.text_stream:
        print(text, end="")

Prompt Caching

Chuizi.AI natively supports Anthropic's Prompt Caching, which can significantly reduce costs for repeated content:

example.py
python
response = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    system=[
        {
            "type": "text",
            "text": "You are a code review assistant...(long system prompt)",
            "cache_control": {"type": "ephemeral"}
        }
    ],
    messages=[
        {"role": "user", "content": "Review this code: ..."}
    ],
)

Configuration via Environment Variables

terminal
bash
export ANTHROPIC_BASE_URL=https://api.chuizi.ai/anthropic
export ANTHROPIC_API_KEY=ck-your-key-here

Then in your Python code, the client reads these automatically:

example.py
python
# Automatically reads environment variables
client = Anthropic()

Verify the Configuration

Run the basic usage example above. If you receive a response from Claude, the configuration is correct. You can also check request logs in the Chuizi.AI dashboard.

Common Issues

Model Name Format

When using the Anthropic native protocol, model names do not need the provider/ prefix. Use claude-sonnet-4-6, claude-opus-4-6, claude-haiku-4-5, etc. directly.

base_url vs api_url

The Anthropic SDK uses the base_url parameter (not api_base or api_url). Make sure you set it to https://api.chuizi.ai/anthropic.

Difference from the OpenAI SDK

The Anthropic SDK uses the native Messages API format without OpenAI format conversion. If you need to use models from multiple providers in the same project, consider using the OpenAI SDK with Chuizi.AI's /v1 endpoint instead.

Next Steps