How to call Claude with the OpenAI SDK

Use Claude Opus, Sonnet and Haiku through the OpenAI SDK by changing only the model ID.

Change the model ID

Keep the OpenAI client pointed at TokenRex and set the model to a Claude ID such as claude-sonnet-5.

If you prefer Anthropic's own message format, TokenRex also accepts the Anthropic Messages format for Claude models.

Example

from openai import OpenAI

client = OpenAI(api_key="YOUR_TOKENREX_KEY", base_url="https://tokenrex.ai/api/public")

resp = client.chat.completions.create(
    model="claude-sonnet-5",
    messages=[{"role": "user", "content": "Hello"}],
)
print(resp.choices[0].message.content)
Get API keyCost calculatorDocs

Questions

Do I need a separate key for each provider?
No. One TokenRex API key works for every enabled model; change only the model ID.

Related