Guides
OpenAI and Anthropic SDKs
Point the OpenAI or Anthropic SDK at Kalsenti by changing two settings: the base URL and the API key.
On this page
Base URLs
Both SDKs work unchanged once you give them a Kalsenti key and the right base URL. They add different paths to it, so the two base URLs differ:
| SDK | Base URL | Key header it sends |
|---|---|---|
| OpenAI SDK | https://gateway.kalsenti.ai/v1 | Authorization: Bearer |
| Anthropic SDK | https://gateway.kalsenti.ai | x-api-key |
The Anthropic SDK adds /v1 itself, so giving it the /v1 address too would ask for /v1/v1/messages, which doesn’t exist. Use the address shown on your dashboard’s Developer page if it differs from these.
OpenAI SDK
# pip install openai
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["KALSENTI_API_KEY"],
base_url="https://gateway.kalsenti.ai/v1",
)
reply = client.chat.completions.create(
model="deepseek-v4.1-flash-1017-raw",
messages=[{"role": "user", "content": "Hello"}],
)
print(reply.choices[0].message.content)The same client also reaches Responses (client.responses.create) and the model list (client.models.list). Other parts of the SDK, such as embeddings, images, audio, files and batches, call endpoints Kalsenti doesn’t have and get a 404.
Anthropic SDK
# pip install anthropic
import os
from anthropic import Anthropic
client = Anthropic(
api_key=os.environ["KALSENTI_API_KEY"],
base_url="https://gateway.kalsenti.ai",
)
message = client.messages.create(
model="deepseek-v4.1-flash-1017-raw",
max_tokens=1024,
messages=[{"role": "user", "content": "Hello"}],
)
print(message.content[0].text)The same client also reaches count tokens (client.messages.count_tokens in Python, countTokens in JavaScript) and the model list (client.models.list). Message batches and files aren’t available.
Streaming with an SDK
Each SDK’s own streaming helpers work as they do anywhere else:
# OpenAI SDK
stream = client.chat.completions.create(
model="deepseek-v4.1-flash-1017-raw",
messages=[{"role": "user", "content": "Hello"}],
stream=True,
)
for chunk in stream:
if chunk.choices and chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)
# Anthropic SDK
with anthropic_client.messages.stream(
model="deepseek-v4.1-flash-1017-raw",
max_tokens=1024,
messages=[{"role": "user", "content": "Hello"}],
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)Here client is an OpenAI SDK client and anthropic_client or anthropicClient an Anthropic SDK client, set up as above. For the raw events behind these, see the streaming sections of chat completions and Messages.
Retries in the SDKs
Both SDKs retry some failed requests on their own, by default up to two more times. That includes every 429, so a request refused for low credit is also tried again before you see the error. Where Kalsenti sends a retry-after header, they wait that long.
- To handle retries yourself, as Errors suggests, turn them off.
- Each SDK gives up on a request after 10 minutes by default. For replies that could take longer, raise its timeout, or stream them.
client = OpenAI(
api_key=os.environ["KALSENTI_API_KEY"],
base_url="https://gateway.kalsenti.ai/v1",
max_retries=0, # retry yourself, on your own terms
)With retries on, send an Idempotency-Key so a retried request can’t run twice. Pass it as an extra header, for example extra_headers={"Idempotency-Key": "order-4711"} in Python. See Idempotency keys.