Getting Started with Antbase in Python
ยท5 min read
๐Ÿ

Getting Started with Antbase in Python

Use the standard OpenAI Python SDK with Antbase. Covers installation, basic chat, streaming, tool calling, and async usage.

pythonsdkquickstart

Most Python developers building with LLMs start the same way: pip install openai, hardcode a model name, and ship it. It works until it does not. The model goes down, your app goes down. The model gets deprecated, you scramble to update. Costs creep up because every request hits the same premium endpoint, even when half your traffic is simple tasks that a free model could handle in its sleep.

Antbase slots into your existing Python code with a two-line change โ€” swap the base URL and API key. That is it. No new SDK to learn, no wrapper library, no migration. Your existing OpenAI client code, your streaming logic, your tool calling, your async patterns โ€” they all work unchanged. But now every request gets classified, routed to the optimal model, and automatically retried with a fallback provider if something goes wrong.

This is the foundation for everything else in this blog. Whether you go on to build LangChain pipelines, CrewAI agents, or a simple script that processes CSV files โ€” it all starts with the same OpenAI client pointing at Antbase. Learn this pattern once and you have intelligent model routing everywhere.

Antbase is fully OpenAI-compatible, so you use the standard openai Python package. No custom SDK needed โ€” just change the base URL and API key.

Installation

bash
pip install openai

Basic Chat Completion

python
from openai import OpenAI

client = OpenAI(
    base_url="https://antbase.ai/v1",
    api_key="ant_your-api-key",
)

response = client.chat.completions.create(
    model="auto",  # Antbase routes to the best model
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Explain Python decorators in 3 sentences."},
    ],
)

print(response.choices[0].message.content)

Streaming

python
stream = client.chat.completions.create(
    model="auto",
    messages=[{"role": "user", "content": "Write a haiku about programming."}],
    stream=True,
)

for chunk in stream:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)

Async Usage

python
from openai import AsyncOpenAI
import asyncio

client = AsyncOpenAI(
    base_url="https://antbase.ai/v1",
    api_key="ant_your-api-key",
)

async def main():
    response = await client.chat.completions.create(
        model="auto",
        messages=[{"role": "user", "content": "Hello!"}],
    )
    print(response.choices[0].message.content)

asyncio.run(main())

Tips

  • โ€ขAll standard OpenAI parameters work: temperature, max_tokens, top_p, stop, tools, response_format, etc.
  • โ€ขUse model="auto" for intelligent routing. Use a specific model name to bypass routing and hit that model directly.
  • โ€ขThe response includes the same fields as OpenAI: id, choices, usage. Your existing parsing code works unchanged.
  • โ€ขFor production, set ANTBASE_API_KEY as an environment variable and use client = OpenAI(base_url=..., api_key=os.environ['ANTBASE_API_KEY']).

Try ANT routing today

Drop-in OpenAI-compatible API. Change your base URL and every request gets intelligent routing across 30+ providers.