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

SettingValue
Base URLhttps://tokenhub-api.belive.sg/api/v1
API keyAn AI Hub key, from the environment
ModelAn 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)

2. Check what changes

  • The base URL ends in /api/v1. Do not add another /v1: the SDK appends /chat/completions itself.
  • Model ids carry an author, such as deepseek/… or qwen/…. A bare OpenAI model name returns 404.
  • Check each option you send against the model's supported_parameters. Provider routing fields such as provider.only work; an unknown key inside provider returns 400 unsupported_parameter instead of being dropped.
  • Streams end with data: [DONE] and carry usage on a final chunk with an empty choices array. 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.