OpenAI Compatible API Guide

OpenAI Compatible API: The Universal Interface Standard

The OpenAI Compatible API has become the de facto standard interface for AI language model APIs. Dozens of providers, open-source projects, and relay services implement this same interface, allowing you to switch between models and providers without changing your application code. This guide explains the standard, who implements it, and how to take advantage of compatibility.

What Is the OpenAI Compatible API?

The OpenAI Compatible API refers to any service that accepts the same request format and returns the same response format as the OpenAI /v1/chat/completions endpoint. The core interface includes:

The key benefit is portability: code written for OpenAI works with any compatible provider by simply changing the base URL.

Providers Implementing OpenAI Compatible API

Switching Providers with One Line

from openai import OpenAI

# OpenAI direct
client = OpenAI(api_key="sk-openai-key")

# Switch to claude4u.com relay (supports multiple providers)
client = OpenAI(
    api_key="your-relay-key",
    base_url="https://claude4u.com/v1"
)

# Switch to local Ollama
client = OpenAI(
    api_key="ollama",
    base_url="http://localhost:11434/v1"
)

# Same code works with all providers
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Hello!"}]
)
print(response.choices[0].message.content)

The Standard Request Format

{
    "model": "gpt-4o",
    "messages": [
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "What is 2+2?"}
    ],
    "temperature": 0.7,
    "max_tokens": 1000,
    "stream": false
}

The Standard Response Format

{
    "id": "chatcmpl-abc123",
    "object": "chat.completion",
    "created": 1700000000,
    "model": "gpt-4o",
    "choices": [{
        "index": 0,
        "message": {
            "role": "assistant",
            "content": "2+2 equals 4."
        },
        "finish_reason": "stop"
    }],
    "usage": {
        "prompt_tokens": 20,
        "completion_tokens": 8,
        "total_tokens": 28
    }
}

Building Provider-Agnostic Applications

import os
from openai import OpenAI

def create_client():
    """Create an OpenAI-compatible client from environment variables."""
    return OpenAI(
        api_key=os.environ["API_KEY"],
        base_url=os.environ.get("API_BASE_URL", "https://claude4u.com/v1")
    )

def generate_response(client, model, messages, **kwargs):
    """Provider-agnostic generation function."""
    return client.chat.completions.create(
        model=model,
        messages=messages,
        **kwargs
    )

# Usage - switch providers via environment variables
client = create_client()
response = generate_response(
    client,
    model=os.environ.get("MODEL", "gpt-4o"),
    messages=[{"role": "user", "content": "Hello!"}]
)

Node.js Provider-Agnostic Setup

import OpenAI from 'openai';

const client = new OpenAI({
    apiKey: process.env.API_KEY,
    baseURL: process.env.API_BASE_URL || 'https://claude4u.com/v1'
});

// Works with any OpenAI-compatible provider
async function chat(userMessage) {
    const response = await client.chat.completions.create({
        model: process.env.MODEL || 'gpt-4o',
        messages: [{ role: 'user', content: userMessage }]
    });
    return response.choices[0].message.content;
}

Feature Compatibility Matrix

Not all providers support every feature. Here is what to check:

Warning: While the request and response formats are standardized, model behavior varies significantly between providers. Always test your specific use case when switching providers, especially for function calling and structured output.

Using with Popular Frameworks

# LangChain
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    model="gpt-4o",
    base_url="https://claude4u.com/v1",
    api_key="your-relay-key"
)

# AutoGen
config_list = [{
    "model": "gpt-4o",
    "api_key": "your-relay-key",
    "base_url": "https://claude4u.com/v1"
}]
Tip: claude4u.com provides the most comprehensive OpenAI-compatible interface available, supporting chat completions, embeddings, and image generation across OpenAI, Claude, and Gemini models. Write your code once and access the best model for each task through a single, unified API.

Get Started with 轻舟 AI

Stable, fast AI API relay — supports Claude, OpenAI, Gemini and more

Sign Up Free