Skip to content

Python SDK

The official SDKs work unchanged: you give them Kloria’s address and your key. Choose the SDK for the format you prefer, not for the model: Claude models accept both formats.

Fenêtre de terminal
pip install openai anthropic

The examples read the key from KLORIA_API_KEY (see Quickstart).

import os
from openai import OpenAI
client = OpenAI(
base_url="https://api.kloria.dev/v1",
api_key=os.environ["KLORIA_API_KEY"],
)
response = client.chat.completions.create(
model="claude-sonnet-5",
messages=[
{"role": "system", "content": "Answer briefly."},
{"role": "user", "content": "Explain what a webhook is."},
],
)
print(response.choices[0].message.content)
print(response.usage) # billed input and output tokens

To print the answer as it’s generated:

stream = client.chat.completions.create(
model="gpt-6.1-sol",
messages=[{"role": "user", "content": "Write a haiku about Git."}],
stream=True,
)
for chunk in stream:
if chunk.choices and chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)

Both SDKs can read their configuration from the environment. If you set these, you can create the client without arguments:

SDK Address Key
OpenAI OPENAI_BASE_URL=https://api.kloria.dev/v1 OPENAI_API_KEY
Anthropic ANTHROPIC_BASE_URL=https://api.kloria.dev ANTHROPIC_API_KEY

The SDKs raise a typed exception per HTTP status. The most common ones with Kloria:

import openai
try:
response = client.chat.completions.create(model="claude-sonnet-5", messages=[...])
except openai.AuthenticationError:
... # 401: key invalid, disabled or expired
except openai.PermissionDeniedError:
... # 403: balance used up, key quota reached, or model not allowed for this key
except openai.RateLimitError:
... # 429: too many requests, retry after a pause
except openai.APIStatusError as e:
print(e.status_code, e.message)

Each status is detailed in the API reference.

© 2026 Kloria — private beta