Guides

How to Use the OpenAI SDK With a Compatible API

THE SHORT ANSWER

To use the OpenAI SDK with Tokely, configure three values: the Tokely base URL, a Tokely API key and a model ID supported by the endpoint you call. The SDK handles the familiar request format, while Tokely handles the documented model route. OpenAI compatibility does not imply support for every OpenAI endpoint or parameter.

Examples follow Tokely’s documentation checked October 15, 2026. They are integration examples, not a record of a live generation test.

How do you connect from Python?

Install the SDK:

Terminal
python3 -m pip install openai

Set your key in the same terminal:

Terminal
export TOKELY_API_KEY='<YOUR_TOKELY_API_KEY>'

Save this as hello.py:

python
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["TOKELY_API_KEY"],
    base_url="https://api.tokely.me/v1",
    timeout=30.0,
    max_retries=0,
)

response = client.chat.completions.create(
    model="gpt-5.4-mini",
    messages=[{"role": "user", "content": "Explain API tokens in one sentence."}],
    max_tokens=256,
)
print(response.choices[0].message.content)
print(response.usage)

Run it with python3 hello.py. The example explicitly disables automatic SDK retries so one invocation does not silently become several client attempts. Production retry policy should match the application deadline and the gateway’s behavior. See the official Python SDK for client configuration.

How do you connect from JavaScript?

Install the package with npm install openai. Using the same environment variable, save this as hello.mjs:

javascript
import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.TOKELY_API_KEY,
  baseURL: "https://api.tokely.me/v1",
  timeout: 30000,
  maxRetries: 0,
});

const response = await client.chat.completions.create({
  model: "gpt-5.4-mini",
  messages: [{ role: "user", content: "Explain API tokens in one sentence." }],
  max_tokens: 256,
});
console.log(response.choices[0].message.content);
console.log(response.usage);

Run it with node hello.mjs. Python uses base_url; JavaScript uses baseURL. Both examples follow the Tokely quickstart.

Why might the first request fail?

An authentication error can mean the key is missing, revoked or issued by the wrong service. A permission error can indicate a key or account restriction. An unsupported-model error can mean the ID is incorrect or incompatible with the endpoint. Check account balance and output budget as well.

Use the base URL ending in /v1. Do not append /chat/completions to an SDK base URL, because the SDK supplies the endpoint path.

Can you switch every existing OpenAI call this way?

Only if Tokely supports the contract your application uses. Responses, file inputs, tools, structured output and stored conversation state need separate review. Use Tokely’s migration guide before changing production traffic.

Keep the key on your backend, select an eligible model from the catalog, and inspect usage after each request while establishing your cost baseline.

More clarity. Better decisions.Back to top ↑
PUT IT INTO PRACTICE

Find your model.
Make your next move.

Compare current prices, choose a model, and build with one API.