Docs navigation

SDKs

Python SDK

The official tokenrouter package for Python.

The tokenrouter package mirrors the OpenAI Python SDK, with sync and async clients.

Install

bash
pip install tokenrouter

Create a client

python
from tokenrouter import TokenRouter

client = TokenRouter()  # reads TOKENROUTER_API_KEY from the environment
# or explicitly:
# client = TokenRouter(api_key="tr_your_key_here", base_url="https://api.tokenrouter.io/v1")

Chat completion

python
response = client.chat.completions.create(
    model="openai/gpt-5-mini",
    messages=[
        {"role": "system", "content": "You are a concise assistant."},
        {"role": "user", "content": "What is an AI gateway?"},
    ],
)

print(response.choices[0].message.content)
print(response.usage)  # token counts for this call

Streaming

python
stream = client.chat.completions.create(
    model="anthropic/claude-sonnet-4-5",
    stream=True,
    stream_options={"include_usage": True},
    messages=[{"role": "user", "content": "Write a haiku about hard caps."}],
)

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

Async

python
import asyncio
from tokenrouter import AsyncTokenRouter

client = AsyncTokenRouter()

async def main() -> None:
    stream = await client.chat.completions.create(
        model="auto:cost",
        stream=True,
        messages=[{"role": "user", "content": "One-line summary of BYOK?"}],
    )
    async for chunk in stream:
        if chunk.choices and chunk.choices[0].delta.content:
            print(chunk.choices[0].delta.content, end="", flush=True)

asyncio.run(main())
  • client.embeddings.create(...) and client.models.list() match the OpenAI SDK.
  • Prefer the stock openai package? It works unchanged with base_url="https://api.tokenrouter.io/v1" and your tr_ key.