DEVSAIO

OpenAI Python SDK with a custom base URL: complete guide

Everything you need for production use: clients, streaming, async, timeouts, retries and error handling, with any model.

DEVSAIO team · 2 min read
Share on X

The official openai Python package is the easiest way to call any OpenAI-compatible API from Python.

Client setup

python
import os
from openai import OpenAI

client = OpenAI(
    base_url="https://devsaio.lol/v1",
    api_key=os.environ["DEVSAIO_API_KEY"],
    timeout=120,      # seconds; long answers need time
    max_retries=2,    # retries on connection errors, 429 and 5xx
)

Create the client once and reuse it; it keeps connections open.

A normal request

python
r = client.chat.completions.create(
    model="claude-sonnet-4-6",
    messages=[
        {"role": "system", "content": "You are a senior Python reviewer."},
        {"role": "user", "content": "Review: def add(a,b): return a+b"},
    ],
    max_tokens=600,
)
print(r.choices[0].message.content)
print(r.usage)  # prompt_tokens, completion_tokens

Streaming

python
stream = client.chat.completions.create(model="claude-sonnet-4-6", messages=[{"role": "user", "content": "Tell me a story"}], stream=True)
for chunk in stream:
    if chunk.choices and chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)

Async

python
import asyncio
from openai import AsyncOpenAI

aclient = AsyncOpenAI(base_url="https://devsaio.lol/v1", api_key=os.environ["DEVSAIO_API_KEY"])

async def ask(q):
    r = await aclient.chat.completions.create(model="claude-haiku-5-5", messages=[{"role": "user", "content": q}])
    return r.choices[0].message.content

print(asyncio.run(asyncio.gather(*(ask(f"Fact #{i} about Mars") for i in range(5)))))

Error handling

python
import openai

try:
    r = client.chat.completions.create(model="claude-sonnet-4-6", messages=[{"role": "user", "content": "hi"}])
except openai.AuthenticationError:
    print("Bad or revoked key")
except openai.RateLimitError:
    print("Slow down and retry")
except openai.APIStatusError as e:
    print("API error", e.status_code, e.message)

See handling errors and retries for which errors are safe to retry.

Try it with one API key

Claude, GPT, Gemini, DeepSeek, image and video models through one OpenAI- and Anthropic-compatible endpoint. Prepaid, pay only for what you use.

Keep reading