Python SDK
This page shows how to call Sakrylle API from Python. Two paths:
- OpenAI compatible: use the official
openaipackage, swapbase_urlfor Sakrylle, call/v1/chat/completions. - Anthropic native: use the official
anthropicpackage, swapbase_urlfor Sakrylle, call/v1/messages.
Examples are based on the official READMEs pulled via Context7. Refer to the upstream repos for the latest parameters and behavior: openai/openai-python, anthropics/anthropic-sdk-python.
Setup
Put the API key in an environment variable rather than hard-coding:
export SAKRYLLE_API_KEY="sk-xxxxxxxxxxxxxxxx"OpenAI compatible
Install
pip install openaiMinimal usage
The OpenAI client takes explicit base_url and api_key parameters. Point it at https://api.sakrylle.com/v1 and every call goes through the Sakrylle gateway, never reaching api.openai.com.
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["SAKRYLLE_API_KEY"],
base_url="https://api.sakrylle.com/v1",
)
completion = client.chat.completions.create(
model="gpt-5.6-sol",
messages=[
{"role": "system", "content": "You are a concise assistant."},
{"role": "user", "content": "Explain vector databases in one sentence."},
],
)
print(completion.choices[0].message.content)You can use environment variables instead of explicit arguments:
export OPENAI_API_KEY="$SAKRYLLE_API_KEY"
export OPENAI_BASE_URL="https://api.sakrylle.com/v1"from openai import OpenAI
client = OpenAI() # Reads the two env vars automaticallyStreaming
stream=True returns an iterator over chunks:
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["SAKRYLLE_API_KEY"],
base_url="https://api.sakrylle.com/v1",
)
stream = client.chat.completions.create(
model="gpt-5.6-sol",
messages=[{"role": "user", "content": "Count to 5."}],
stream=True,
)
for chunk in stream:
delta = chunk.choices[0].delta.content or ""
print(delta, end="", flush=True)Tool calling
Use the tools field exactly as in OpenAI's official API:
import json, os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["SAKRYLLE_API_KEY"],
base_url="https://api.sakrylle.com/v1",
)
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Look up the current weather for a city",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "City name"},
},
"required": ["city"],
},
},
}
]
resp = client.chat.completions.create(
model="gpt-5.6-sol",
messages=[{"role": "user", "content": "What's the weather in Shanghai today?"}],
tools=tools,
)
choice = resp.choices[0]
if choice.message.tool_calls:
for call in choice.message.tool_calls:
print("call:", call.function.name, json.loads(call.function.arguments))
else:
print(choice.message.content)Sakrylle does not alter OpenAI's tool calling protocol — whatever the model returns is what you get.
Anthropic native
Install
pip install anthropicMinimal usage
The Anthropic constructor also accepts base_url:
import os
from anthropic import Anthropic
client = Anthropic(
api_key=os.environ["SAKRYLLE_API_KEY"],
base_url="https://api.sakrylle.com",
)
message = client.messages.create(
model="claude-haiku-4-5-20251001",
max_tokens=1024,
messages=[
{"role": "user", "content": "Introduce Claude in one sentence."},
],
)
print(message.content[0].text)The SDK appends
/v1/messagesautomatically. Usehttps://api.sakrylle.comasbase_url— do not add/v1. The exact joining rules are documented in the anthropic-sdk-python README. The env varsANTHROPIC_API_KEYandANTHROPIC_BASE_URLwork as alternatives to explicit arguments.
Streaming
messages.stream() is a context manager that yields plain text fragments:
import os
from anthropic import Anthropic
client = Anthropic(
api_key=os.environ["SAKRYLLE_API_KEY"],
base_url="https://api.sakrylle.com",
)
with client.messages.stream(
model="claude-haiku-4-5-20251001",
max_tokens=1024,
messages=[{"role": "user", "content": "Write a four-line poem."}],
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
print()Error handling
Both SDKs use a similar exception hierarchy (AuthenticationError, RateLimitError, APIStatusError, etc.). Error codes align with upstream:
from openai import OpenAI, AuthenticationError, RateLimitError, APIStatusError
client = OpenAI(api_key="bad-key", base_url="https://api.sakrylle.com/v1")
try:
client.chat.completions.create(
model="gpt-5.6-sol",
messages=[{"role": "user", "content": "hi"}],
)
except AuthenticationError:
print("Key invalid or revoked")
except RateLimitError:
print("Rate limited, retry later")
except APIStatusError as e:
print("Upstream error:", e.status_code, e.response.text)For a full error table and debugging tips, see Errors.
