DEV Community

ayi
ayi

Posted on Fully Autonomous

How to Use an OpenAI-Compatible API for Multiple LLM Models

If you are building an app that calls more than one language model, the integration work can spread quickly: separate SDKs, credentials, base URLs, and response quirks. An OpenAI-compatible API gives you a familiar client interface for models exposed through that contract. In this guide, I use YW Code API as a concrete example and show the setup in Node.js and Python.

The important caveat: “OpenAI-compatible” describes an API surface. It does not mean every model supports every OpenAI parameter or feature. Always check the model list and test the capabilities your application needs.

1. Create a key and check the models it can access

Create an API key using the YW Code quick-start guide. The models available to a key can depend on the account, the key’s group, and current routing, so do not copy a model name from an old example and assume it is enabled for you.

You can query the live list with the standard models endpoint:

curl https://api.ywcode.top/v1/models \
  -H "Authorization: Bearer $YWCODE_API_KEY"
Enter fullscreen mode Exit fullscreen mode

Use a returned model ID in the request’s model field. The model list guide explains how to check access and troubleshoot a model that is missing.

2. Point an OpenAI SDK at the gateway

Most OpenAI SDKs accept a custom base URL. Keep the key in an environment variable and set the base URL to the gateway’s documented endpoint.

import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.YWCODE_API_KEY,
  baseURL: "https://api.ywcode.top/v1",
});

const model = process.env.YWCODE_MODEL; // Set this to an ID returned by /v1/models.

const response = await client.chat.completions.create({
  model,
  messages: [{ role: "user", content: "Summarize this paragraph in one sentence." }],
});

console.log(response.choices[0]?.message?.content);
Enter fullscreen mode Exit fullscreen mode

The same pattern works with Python’s OpenAI SDK:

import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["YWCODE_API_KEY"],
    base_url="https://api.ywcode.top/v1",
)

response = client.chat.completions.create(
    model=os.environ["YWCODE_MODEL"],
    messages=[{"role": "user", "content": "Summarize this paragraph in one sentence."}],
)

print(response.choices[0].message.content)
Enter fullscreen mode Exit fullscreen mode

See the OpenAI-compatible API reference for the current base URL, authentication format, endpoints, and examples.

3. Keep model-specific behavior explicit

A stable client setup makes it easier to change a model, but the model is still part of your application’s behavior. Before switching, verify:

  • The model ID is visible to the API key you are using.
  • The endpoint is supported for that model. The documented API includes Chat Completions and Responses, while other capabilities depend on the model and upstream support.
  • Parameters such as tool calling, images, streaming, and output limits are supported by the selected model.
  • Your code handles the response shape and usage fields you actually receive.

For production systems, make the model configurable per environment and add a small integration test for each model family you rely on. That is safer than assuming that an identical SDK call produces identical behavior everywhere.

4. Protect the key and handle errors

Never put a server API key in browser JavaScript, a public repository, or a mobile app bundle. Send requests through your backend, load the secret from an environment or secret manager, and rotate it if it is exposed.

When a request fails, check the base URL and key first, then confirm the model ID with /v1/models. Also verify that the key’s group has access to the model and that the account has sufficient quota. A parameter rejected by one model may be valid for another, so reduce the request to a minimal prompt before changing retry behavior. Avoid retrying every error blindly; validation failures will not be fixed by another identical request.

When an OpenAI-compatible gateway is useful

A gateway can reduce configuration drift when an application needs one place to manage API keys, model access, and usage records. YW Code API documents a shared OpenAI-compatible base URL and setup guides for common developer tools. The best fit still depends on the models enabled for your account and the features your workload needs.

Start with the YW Code API documentation and verify the live model list before integrating. The examples here are deliberately small: establish the connection first, then add streaming, tools, and model-specific parameters one capability at a time.

Disclosure: I operate YW Code API, which is used for the endpoint examples in this guide. The SDK configuration pattern applies to other OpenAI-compatible APIs as well.

Top comments (0)