Connect with the OpenAI SDK
In an SDK, update where you create your client: give it Mubit's base URL and your Mubit key. With curl, change the URL and the key in its header. Keep your model IDs and the rest of your code.
Base URL
The base URL is https://api.mubit.ai/openai/v1. It ends in /v1, as the OpenAI SDK expects.
Use the base URL your console's Connect page shows.
Key
Pass your Mubit key as the SDK's API key. The SDK sends it in the Authorization: Bearer header.
The examples read the key from MUBIT_API_KEY. To create a key and set it, follow step 1 of the Quickstart.
Send a call
Install the SDK: pip install openai for Python, or npm install openai for TypeScript.
import os
from openai import OpenAI
client = OpenAI(
base_url="https://api.mubit.ai/openai/v1",
api_key=os.environ["MUBIT_API_KEY"],
)
instructions = (
"Sort the support message into one category: "
"billing, bug, account or other. Reply with the category only."
)
response = client.chat.completions.create(
model="gpt-6-luna",
messages=[
{"role": "system", "content": instructions},
{"role": "user", "content": "I was charged twice for my October invoice."},
],
)
print(response.choices[0].message.content)The TypeScript examples use top-level await, so run them as ES modules: save the file as openai.mts and run npx tsx openai.mts, or add "type": "module" to your package.json.
Chat Completions and Responses both work through the same base URL.
Stream the answer
Set stream to true. Mubit streams the answer back to your app.
import os
from openai import OpenAI
client = OpenAI(
base_url="https://api.mubit.ai/openai/v1",
api_key=os.environ["MUBIT_API_KEY"],
)
stream = client.chat.completions.create(
model="gpt-6-luna",
messages=[
{"role": "system", "content": "Reply to the support message in two short sentences."},
{"role": "user", "content": "I was charged twice for my October invoice."},
],
stream=True,
)
for chunk in stream:
if chunk.choices and chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)
print()curl -N prints each event as it arrives.
Or set the SDK's environment variables
The OpenAI SDK reads its base URL and key from the environment when your code doesn't pass them. To point an app at Mubit without a code change, set these two variables where it runs:
export OPENAI_BASE_URL=https://api.mubit.ai/openai/v1
export OPENAI_API_KEY="$MUBIT_API_KEY"See the call in Requests
- In the console, open Requests.
- Find your call at the top. The list is newest first.
- Check Model asked. It shows the model ID your app sent.
- Select the row. The call opens with its Request and its Reply.
A streamed call shows its whole reply.
What Mubit forwards and trains on
- Forwarded: when Mubit sends a call on to OpenAI, it sends your request with your OpenAI key, or the starter credit's key, in place of your Mubit key. It doesn't pass on cookies,
x-middleware-storeorx-middleware-record. On the starter credit, some models, paths and options are refused: see Starter credit errors. - Trained on: adapters train only on kept examples: the recorded calls Mubit accepts for a task. A call recorded without its request and reply is never a kept example.
Frameworks and coding tools
Frameworks covers the Vercel AI SDK, LangChain and the OpenAI Agents SDK, and Coding tools covers Codex and Aider. Turn the Agents SDK's tracing off first, as Frameworks shows: with it on, the SDK can send your Mubit key to OpenAI.
If a call fails
The error names its code. Troubleshooting lists the codes and what to do. Each answer carries the call's ID in the x-middleware-request-id header: see Find a call's request ID.