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.
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"
.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.
| Client | Value to enter |
|---|---|
| OpenAI Python / Node SDK | https://api.tokmodel.ai/v1 |
Tools that append /v1 themselves | https://api.tokmodel.ai |
| Anthropic-compatible clients | https://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
| Status | Likely cause | What to check |
|---|---|---|
401 | Missing or invalid key | The variable holds an active TokModel key, not an OpenAI key, and the shell that runs the script has it exported. |
403 | Model not allowed for this key | Whether the key is scoped to a different model than the one you passed. |
404 | Wrong path or model ID | The base URL is https://api.tokmodel.ai/v1 and the model ID matches the pricing page exactly. |
429 | Rate or concurrency limit | Your key's concurrency setting and retry after the interval in the response. |
06Next steps
- Browse all available models and per-token rates on the pricing page.
- Read request parameters, tool calling and error codes in the API documentation.
- Track spend and per-request usage in the console.
Ready to try it?
Create an account, copy a key, and run the script above. New accounts include a trial credit.