Python SDK
Two Python paths — anthropic for native Claude code, openai for migrations. Both work with one base_url change.
Install
bash
pip3 install anthropic
# or, for OpenAI-style code:
pip3 install openai
# In a venv (recommended):
# python3 -m venv .venv && source .venv/bin/activate
# pip install anthropic openaiAnthropic SDK
python
import os
from anthropic import Anthropic
client = Anthropic(
api_key=os.environ["API_AI_KEY"],
base_url="https://api-ai.app",
)
resp = client.messages.create(
model="claude-sonnet-5-5",
max_tokens=1024,
messages=[{"role": "user", "content": "Say hi."}],
)
print(resp.content[0].text)OpenAI-compatible
python
from openai import OpenAI
client = OpenAI(
api_key=os.environ["API_AI_KEY"],
base_url="https://api-ai.app/v1",
)
resp = client.chat.completions.create(
model="claude-sonnet-5-5",
messages=[{"role": "user", "content": "Say hi."}],
)
print(resp.choices[0].message.content)Async client
python
import asyncio
from anthropic import AsyncAnthropic
async def main():
client = AsyncAnthropic(base_url="https://api-ai.app")
resp = await client.messages.create(
model="claude-sonnet-5-5",
max_tokens=512,
messages=[{"role": "user", "content": "Hi async."}],
)
print(resp.content[0].text)
asyncio.run(main())Streaming
python
with client.messages.stream(
model="claude-sonnet-5-5",
max_tokens=512,
messages=[{"role": "user", "content": "Tell me a haiku."}],
) as stream:
for chunk in stream.text_stream:
print(chunk, end="", flush=True)Tool use
python
tools = [{
"name": "get_weather",
"description": "Get current weather for a city.",
"input_schema": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"],
},
}]
resp = client.messages.create(
model="claude-sonnet-5-5",
max_tokens=1024,
tools=tools,
messages=[{"role": "user", "content": "What's the weather in Paris?"}],
)
# resp.stop_reason == "tool_use"
# Inspect resp.content for tool_use blocks.Retry & error handling
python
from anthropic import APIStatusError
from tenacity import retry, stop_after_attempt, wait_exponential
@retry(
stop=stop_after_attempt(3),
wait=wait_exponential(multiplier=0.5, min=0.5, max=4),
retry_error_cls=APIStatusError,
)
def call():
return client.messages.create(
model="claude-sonnet-5-5",
max_tokens=512,
messages=[{"role": "user", "content": "Hi."}],
)Don't hardcode the key
Always read the key from an environment variable or a secret manager. Hardcoded keys leak into version control sooner or later.
Last updated: October 2, 2026Found a bug? admin@minedom-france.fr