Cookbook
Migrate an OpenAI-compatible client
Move code that already uses an OpenAI-compatible client to AI Hub by changing its settings, not its logic.
The snippets below adapt an existing client and assume its messages conversation already exists. For a standalone program, use First request.
1. Change three settings
| Setting | Value |
|---|---|
| Base URL | https://tokenhub-api.belive.sg/api/v1 |
| API key | An AI Hub key, from the environment |
| Model | An exact author/slug id from the catalogue |
import os from openai import OpenAI client = OpenAI( base_url=os.environ["TOKENHUB_BASE_URL"], # https://tokenhub-api.belive.sg/api/v1 api_key=os.environ["TOKENHUB_API_KEY"], ) r = client.chat.completions.create(model=os.environ["TOKENHUB_MODEL"], messages=messages)
import OpenAI from "openai"; const client = new OpenAI({ baseURL: process.env.TOKENHUB_BASE_URL, // https://tokenhub-api.belive.sg/api/v1 apiKey: process.env.TOKENHUB_API_KEY, }); const r = await client.chat.completions.create({ model: process.env.TOKENHUB_MODEL!, messages });
2. Check what changes
- The base URL ends in
/api/v1. Do not add another/v1: the SDK appends/chat/completionsitself. - Model ids carry an author, such as
deepseek/…orqwen/…. A bare OpenAI model name returns404. - Check each option you send against the model's
supported_parameters. Provider routing fields such asprovider.onlywork; an unknown key insideproviderreturns400 unsupported_parameterinstead of being dropped. - Streams end with
data: [DONE]and carry usage on a final chunk with an emptychoicesarray. See Terminal chat. - Errors keep the OpenAI shape and a stable
type; see Errors for which are safe to retry.
3. Leave everything else alone
Keep your other providers, retries and logging as they are. A migration is done when one request and one streamed request succeed with the new settings and your existing tests pass.