Back to blog

AI for beginnersGuide

OpenAI-compatible API: what base_url is and how to switch

An OpenAI-compatible API accepts the same requests as OpenAI's. Change base_url, the key and the model id, and your code sends its first request to CheapTokens.

By CheapTokens TeamPublished Updated 5 min read

Three values to change when switching to an OpenAI-compatible API: base_url, API key and model id
On this page
  1. What does "OpenAI-compatible" mean?
  2. What is base_url?
  3. The three values you change
  4. First request in Python
  5. The same request with curl
  6. What you should see
  7. What does it cost?
  8. If it does not work
  9. Next steps

An OpenAI-compatible API is an API that accepts requests in the same format as OpenAI's Chat Completions API and answers in the same format. To move an app to another provider, you usually change three values: base_url (where requests go), the API key, and the model id. The rest of your code, including the message list and the way you read the answer, stays as it is.

This guide explains the idea in plain words and ends with a working first request to CheapTokens in Python and curl. Facts about CheapTokens were checked against its documentation on October 22, 2026.

What does "OpenAI-compatible" mean?#

An API is a set of rules for talking to a service over the internet (if the word is new, start with how your first API request works). OpenAI's chat format became a common standard: many providers and tools accept the same request body and return the same response shape. Such an API is called OpenAI-compatible.

In practice that means three things are the same on any compatible provider:

  • The request: a JSON body with a model field and a messages list, where each message has a role (system, user or assistant) and content.
  • The endpoint path: /chat/completions, added after the base URL.
  • The response: a JSON object where the generated text is in choices[0].message.content.

Because the format is shared, official SDKs and many apps can talk to any compatible provider. They only need to know where to send the request.

What is base_url?#

base_url is the address that every request starts with. An SDK builds the full address by adding the path of the call to it.

Part Example
base_url https://api.cheaptokens.dev/v1
Path added by the SDK /chat/completions
Full address of the request https://api.cheaptokens.dev/v1/chat/completions

So when you set a different base_url, the same code sends the same kind of request to a different server. The CheapTokens base URL is https://api.cheaptokens.dev/v1. Its API is compatible with OpenAI Chat Completions and with Anthropic Messages, and one prepaid balance covers all models.

The three values you change#

Value What it is Where it comes from
base_url Address of the API https://api.cheaptokens.dev/v1
API key Your secret password for the API Your CheapTokens account. See what an API key is and how to store it safely
Model id The exact name of the model in the model field The CheapTokens documentation

The model id deserves attention. Different providers give the same model different ids, so an id from another service will not work here. Take ids from the CheapTokens documentation. The ids available now:

Model Model id Context Max output per answer
GLM-5.3-Flash zai-org/GLM-5.3-Flash 390K 8,192 tokens
DeepSeek-V4-Flash deepseek-ai/DeepSeek-V4-Flash-0731 380K 32,768 tokens
MiniMax-M2.7 MiniMaxAI/MiniMax-M2.7 180K 8,192 tokens

DeepSeek-V4-Flash returns up to 32,768 tokens per answer and has a 380K context; GLM-5.3-Flash has a 390K context; MiniMax-M2.7 has a 180K context. All three are billed from the same balance, so switching between them means changing only the model id.

First request in Python#

Install the openai package, then store your key in an environment variable instead of writing it into the code:

bash
pip install openai
export CHEAPTOKENS_API_KEY="YOUR_CHEAPTOKENS_KEY"

Now the code. The only difference from a standard OpenAI example is the base_url argument, the key and the model id:

python
import os
from openai import OpenAI

client = OpenAI(
    base_url="https://api.cheaptokens.dev/v1",   # 1. where requests go
    api_key=os.environ["CHEAPTOKENS_API_KEY"],   # 2. your CheapTokens key
)

response = client.chat.completions.create(
    model="MiniMaxAI/MiniMax-M2.7",                # 3. model id from the docs
    messages=[
        {"role": "system", "content": "You answer briefly."},
        {"role": "user", "content": "Explain base_url in one sentence."},
    ],
)

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

The key in the example is a placeholder: replace YOUR_CHEAPTOKENS_KEY with your own key and never commit a real key to a repository.

If an app you already have reads settings from a config file or environment variables, you do not need to edit its code at all. Look for fields called base URL, API base or endpoint, and fill in the same three values there.

The same request with curl#

curl shows what the SDK does behind the scenes: the base URL plus the path, the key in the Authorization header, and a JSON body.

bash
curl https://api.cheaptokens.dev/v1/chat/completions \
  -H "Authorization: Bearer $CHEAPTOKENS_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "MiniMaxAI/MiniMax-M2.7",
    "messages": [
      {"role": "user", "content": "Explain base_url in one sentence."}
    ]
  }'

What you should see#

When everything is set correctly, the Python script prints a short text answer, and curl prints a JSON object. Look for the choices list: the text of the answer is in choices[0].message.content. If you see that, your first request to CheapTokens worked, and you can move on to your real app.

What does it cost?#

Billing is by tokens, counted separately for input and output (see input vs output tokens). For example, MiniMax-M2.7 currently costs $0.05 per 1M input tokens and $0.15 per 1M output tokens. The table below always shows current prices for all models, with no peak-hour surcharges.

Model Context Input $/1M Output $/1M OpenRouter in OpenRouter out
GLM-5.3-Flash reasoning 390K $0.02 $0.04 $0.0413 $0.0758
DeepSeek-V4-Flash 380K $0.015 $0.10 $0.0475 $0.1568
MiniMax-M2.7 180K $0.05 $0.15 $0.30 $1.20

Prices in USD per 1M tokens. OpenRouter figures shown for comparison and may change.

Prices for the full list of models are also on the CheapTokens pricing page.

If it does not work#

  • Authentication error (usually 401): the key is missing, mistyped or has extra spaces. Check that the environment variable is set in the same terminal where you run the script.
  • "Not found" (usually 404): check base_url. It must end with /v1, and you should not add /chat/completions yourself when you use an SDK, or the path will be doubled.
  • Unknown model: copy the model id exactly from the table above or from the documentation, including capitals and the part before the slash.
  • No connection or timeout: see common connection errors and how to fix them.
  • Answer cut off mid-sentence: the max_tokens limit was reached, which is covered in why an AI answer stops mid-sentence.

Next steps#

Once the first request works, you can add streaming in Python, plug the same three values into your editor or agent, or read how to read AI model names. Setup details and a full list of models are in the CheapTokens documentation.