Tutorials

Switch From the OpenAI API in One Line

PNPriya NairJun 6, 20264 min read

If you already have code that talks to the OpenAI API, you are most of the way to using Model Database. Because Model Database is OpenAI-SDK compatible, migrating is usually a one-line change: point the SDK at a new base URL and use your Model Database key. Everything else, the request shape, the response shape, streaming, stays the same.

The payoff is big: that same code can now call hundreds of models from Anthropic, OpenAI, Google, Meta, Mistral, DeepSeek, and more, all billed from one prepaid balance.

The one line that changes

In the official OpenAI SDK you normally construct a client with a default base URL and your OpenAI key. To use Model Database, you override the base URL to https://modeldatabase.com/v1 and pass your mdb_live_ key. That is the whole migration.

Python

from openai import OpenAI

client = OpenAI(
    base_url="https://modeldatabase.com/v1",   # the one line
    api_key="mdb_live_xxxxxxxxxxxxxxxxxxxxxxxx",
)

resp = client.chat.completions.create(
    model="anthropic/claude-sonnet-4-6",
    messages=[{"role": "user", "content": "Hello!"}],
)
print(resp.choices[0].message.content)

Node.js

import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://modeldatabase.com/v1",   // the one line
  apiKey: process.env.MDB_API_KEY,
});

const resp = await client.chat.completions.create({
  model: "openai/gpt-4o",
  messages: [{ role: "user", content: "Hello!" }],
});
console.log(resp.choices[0].message.content);

Use environment variables for a true one-liner

The OpenAI SDK reads OPENAI_API_KEY and, in many setups, OPENAI_BASE_URL from the environment. If your code relies on those defaults, you may not even need to touch the source. Just set:

export OPENAI_BASE_URL="https://modeldatabase.com/v1"
export OPENAI_API_KEY="mdb_live_xxxxxxxxxxxxxxxxxxxxxxxx"

Now your existing program runs against Model Database with zero code edits. This is especially handy for trying out third-party tools that are hardcoded to the OpenAI SDK.

The only real change: model IDs

The one thing you should update is the model field. Instead of a bare OpenAI model name, use a namespaced Model Database ID so you can pick any provider:

To list everything available to your key, call the models endpoint:

curl https://modeldatabase.com/v1/models \
  -H "Authorization: Bearer $MDB_API_KEY"

Verify the switch worked

Because Model Database returns billing headers on every billable response, you can confirm requests are flowing through it. With curl -i you will see X-MDB-Charged-USD and X-MDB-Balance-USD in the response. In SDK code, those headers are accessible on the raw response object if you need to log them.

What you do not have to change

Streaming with "stream": true, the messages array structure, system prompts, temperature and other sampling parameters, and the choices[0].message.content response path all behave exactly as they do against OpenAI. Tooling built on the OpenAI SDK, retries, async clients, and so on continues to work.

Migrating is genuinely a one-line swap plus a model ID. Grab your key and top up at your dashboard, then check the docs for the full list of supported parameters and models.

← All articles Get your API key →