Home/Docs/OpenAI Python SDK

Switch to TokModel with the OpenAI Python SDK

Already using the OpenAI Python SDK? Keep your Chat Completions code and point the client at TokModel. This guide sets the base URL, picks a model, and sends your first request.

Updated 2026-11-256 min readPython 3.8+

01Create a TokModel API key

Sign in to the console and create a key under API Key Management. Use this key for requests to TokModel, not your OpenAI key.

Then open the pricing page, choose a model that supports Chat Completions, and copy its exact model ID. If your key is restricted to specific models, pick one your key allows.

02Install the SDK and set environment variables

Install or upgrade the official SDK:

Shellpython -m pip install -U openai

Then export your key and model ID in the same shell you run the script from.

macOS or Linux

Shellexport TOKMODEL_API_KEY="your-tokmodel-api-key"
export TOKMODEL_MODEL="your-model-id"

Windows PowerShell

PowerShell$env:TOKMODEL_API_KEY = "your-tokmodel-api-key"
$env:TOKMODEL_MODEL = "your-model-id"
!Replace both placeholders. Keep the key out of source control — the .env file should be listed in .gitignore.

03Send your first request

Save this as tokmodel_quickstart.py:

Pythonimport os

from openai import OpenAI

client = OpenAI(
    api_key=os.environ["TOKMODEL_API_KEY"],
    base_url="https://api.tokmodel.ai/v1",
)

response = client.chat.completions.create(
    model=os.environ["TOKMODEL_MODEL"],
    messages=[{"role": "user", "content": "Reply with: API connected."}],
)

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

Run it:

Shellpython tokmodel_quickstart.py

A model reply means your client is connected. This is a real API request, so the call appears in your console usage and is billed at the rates on the pricing page.

Stream the response

Streaming works the same way as with OpenAI. Set stream=True and read the chunks:

Pythonstream = client.chat.completions.create(
    model=os.environ["TOKMODEL_MODEL"],
    messages=[{"role": "user", "content": "Count to five."}],
    stream=True,
)

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

04Why the base URL ends in /v1

The Python SDK appends the endpoint path to whatever you pass as base_url. It calls {base_url}/chat/completions, so the value must be the API root, https://api.tokmodel.ai/v1 — not the full /chat/completions URL.

ClientValue to enter
OpenAI Python / Node SDKhttps://api.tokmodel.ai/v1
Tools that append /v1 themselveshttps://api.tokmodel.ai
Anthropic-compatible clientshttps://api.tokmodel.ai (uses /v1/messages)

The server accepts both forms, but passing the wrong one for your client is the most common cause of a 404 on the first call. See the API documentation for the full endpoint list.

05Fix common errors

StatusLikely causeWhat to check
401Missing or invalid keyThe variable holds an active TokModel key, not an OpenAI key, and the shell that runs the script has it exported.
403Model not allowed for this keyWhether the key is scoped to a different model than the one you passed.
404Wrong path or model IDThe base URL is https://api.tokmodel.ai/v1 and the model ID matches the pricing page exactly.
429Rate or concurrency limitYour key's concurrency setting and retry after the interval in the response.

06Next steps

Ready to try it?

Create an account, copy a key, and run the script above. New accounts include a trial credit.