Skip to content

OpenAI Python base url: Point Your SDK at a Gateway

Updated · TokenomixAI

The openai python base url is the value you pass to the client so chat requests go to another server instead of the default endpoint. The SDK keeps using the same chat completions format, so your parsing code doesn't change. You only need two things: the gateway address with /v1 and your own API key.

Set base_url and api_key when you create the client, and every chat request from the OpenAI Python SDK goes to the OpenAI-compatible gateway you choose, with no other change to your code.

How do you set the base url in the OpenAI Python SDK?

When you create the client, the OpenAI Python SDK takes two arguments that decide where requests go: base_url and api_key. Fill both and every chat call uses that address instead of the SDK default. There's no extra import and no replacement library.

Keep the key in an environment variable on your machine and read it when the client is created. Don't paste the key into a file that goes into a repo, and don't put it in a URL. TokenomixAI keys always start with sk-tkx-, which makes them easy to spot when you store several keys at once.

What that key format means is covered in OpenAI compatible API key.

The right OpenAI Python base url for chat completions

For an OpenAI-compatible endpoint, set base_url to https://api.tokenomixai.com/v1, with the /v1 on the end.

ParameterValue to set
base_urlhttps://api.tokenomixai.com/v1
api_keythe sk-tkx-... key you receive after payment
modela model from the list at tokenomixai.com/docs
endpointchat completions or responses

Don't add an endpoint name after /v1. The SDK appends that path itself, so an over-long base_url produces a doubled path and the server answers 404. If you get a 404 with a valid key, check the length of base_url first.

This rule covers OpenAI-compatible SDKs only. Claude Code takes ANTHROPIC_BASE_URL set to https://api.tokenomixai.com without /v1, because Claude Code adds /v1/messages itself. When both tools run side by side, don't copy the value from one to the other.

Chat request with the OpenAI Python SDK

The flow matches a normal chat request. Only the destination server and the model value change.

  1. Create the client with base_url and api_key as in the snippet below.
  2. Pick a model from the list in the documentation and write its name exactly in the model parameter.
  3. Fill messages with a system role for instructions and a user role for the question.
  4. Read the answer text and the token counts from the response object the SDK returns.
from openai import OpenAI

client = OpenAI(base_url="https://api.tokenomixai.com/v1", api_key="sk-tkx-...")
reply = client.chat.completions.create(
    model="MODEL_ID",  # salin ID model dari tokenomixai.com/docs
    messages=[{"role": "user", "content": "Halo"}],
)
print(reply.choices[0].message.content)

If the result is confusing, send the same request with curl. When curl works and Python fails, the problem sits in the client arguments or the environment variable, not in how the request is written.

curl https://api.tokenomixai.com/v1/chat/completions \
  -H "Authorization: Bearer sk-tkx-..." \
  -H "Content-Type: application/json" \
  -d '{"model": "MODEL_ID", "messages": [{"role": "user", "content": "Halo"}]}'

Both send a body in the same format, so the response content should match. A difference usually comes from a different model value or a different key on one side.

Errors when the base url or API key is wrong

  • 401: the key is empty, mistyped, or no longer usable. Full causes are in 401 API key error.
  • 404 with a doubled path: base_url already ends in /v1 and you appended an endpoint name again.
  • 404 that names a model: the model value isn't in the catalog. Copy it again exactly from the official docs.
  • Connection error: wrong domain, or your office network blocks api.tokenomixai.com.

To tell the two 404s apart, read what the error message points at. If it names a path, check base_url. If it names a model, check the model parameter.

Check quota before traffic grows

TokenomixAI quota is counted in tokens, not time windows, so there is no 5-hour limit. The key keeps working while tokens remain and before it expires.

curl https://api.tokenomixai.com/v1/quota -H "Authorization: Bearer sk-tkx-..."

The response includes status (active or exhausted), remainingTokens, maxTokens, usagePercent, expiresAt, and low, which is true at 10 percent or less. How to read each field is in check API quota.

There is also a rate limit of 600 requests per minute per account, shared by all keys on that account. If your script goes over it, the API returns 429 with a Retry-After header. Read that header and wait before resending instead of retrying in a tight loop.

One key for many text and code models

With the same base_url and key, you can change the model value per request without building a new client. One key covers many text and code models, so you can pick a smaller model for routine tasks and a stronger one for a large refactor.

Some models accept images as input, but the service covers text and code models only. There is no image, video or audio generation.

On top-up, remaining tokens and new tokens are added together, and validity follows the latest top-up. The key stays the same, so your Python code needs no edits.

Frequently asked questions

Can the OpenAI Python SDK talk to a server other than the default one?

Yes. The client accepts base_url and api_key, and the SDK sends chat requests in the same format to that address. Nothing else in your code changes besides those two arguments.

Should the base url include /v1 or not?

For OpenAI-compatible SDKs, write https://api.tokenomixai.com/v1. The SDK appends the endpoint path itself, so never add an endpoint name after it. For Claude Code, ANTHROPIC_BASE_URL is written without /v1.

Why does the request still fail when the API key is correct?

Check two things: whether base_url is too long and doubles the path, and whether the model name exists in the catalog. If both are right, test the connection to api.tokenomixai.com from the machine you're running on.

Is it safe to keep the API key in a Python file?

Better not. Put it in an environment variable on your machine or server and read it when the client is created. If a key lands in a repo by accident, replace it with a new one.

Is there a session time limit like coding tool subscriptions?

There is no 5-hour limit. Quota comes from the number of tokens, so the key keeps working while tokens remain and before it expires. What does apply is the rate limit of 600 requests per minute per account.

More articles