AI glossary · Agents and tools
What is an OpenAI-compatible API?
Also called: OpenAI compatibility, OpenAI-compatible endpoint, base_url
Definition
An OpenAI-compatible API is a model API that accepts OpenAI’s Chat Completions request format, so you can call it with the official OpenAI SDK by changing only the base URL, the API key and the model name.
Explained
How does an OpenAI-compatible API work?
Compatibility covers the request shape, not the model: features such as tool calling and structured outputs can still behave differently from one provider to the next. Our guide to the cheapest LLM APIs compares what the same request costs across them.
OpenAI’s Chat Completions shape, a messages list in and a choices array out, became the format other providers copy. Groq, OpenRouter, Google’s Gemini API, Anthropic and local servers such as Ollama all accept it, and the OpenAI SDK sends the same request to whatever base_url you set.
Compatible means the core works, not everything. Groq returns a 400 error for logprobs, logit_bias, top_logprobs and messages[].name, and turns temperature: 0 into 1e-8. Ollama doesn’t support tool_choice. Anthropic’s layer ignores response_format and the strict flag on tools, has no prompt caching, and is meant for testing and comparing models rather than long-term production use in most cases. Gemini’s is in beta and takes Gemini-only options through extra_body.
Example
Five base URLs, checked today
We sent the same Chat Completions request through the OpenAI Python SDK (3.28.0) to the four hosted URLs below with a deliberately invalid key. Each answered with an authentication error, which confirms the URL and path. Groq, OpenRouter and Anthropic replied 401 with an error object; Gemini replied 400 with its error object inside a list, so even error handling can differ.
Switching providers is then a three-line change: the base URL, the key and the model name, as in the OpenRouter example below.
| Provider | Base URL | Worth knowing |
|---|---|---|
| Groq | https://api.groq.com/openai/v1 | 400 error on logprobs, logit_bias, top_logprobs |
| OpenRouter | https://openrouter.ai/api/v1 | Model IDs look like provider/model |
| Google Gemini | https://generativelanguage.googleapis.com/v1beta/openai/ | Beta; Gemini options via extra_body |
| Anthropic | https://api.anthropic.com/v1/ | For testing; response_format ignored |
| Ollama (local) | http://localhost:11434/v1/ | Key required but ignored |
Base URLs from each provider’s compatibility docs, 2026-10-11. Ollama runs on your machine and was not probed.
import os
from openai import OpenAI
client = OpenAI(
base_url="https://openrouter.ai/api/v1",
api_key=os.environ["OPENROUTER_API_KEY"],
)
response = client.chat.completions.create(
model="google/gemini-3.8-flash",
messages=[
{"role": "system", "content": "Answer in one sentence."},
{"role": "user", "content": "What is an OpenAI-compatible API?"},
],
)
print(response.choices[0].message.content)Cost and quality
Why does OpenAI compatibility matter for switching providers?
One client library lets you compare models and prices across providers, or move to a cheaper one, without rewriting your code. Keep each provider’s API key in its own environment variable, never in the code.
Test the features you rely on, such as function calling, JSON output, streaming and usage fields, on each provider. Unsupported fields are often ignored silently rather than rejected, and provider-only features, such as Anthropic’s prompt caching, need the native API.
Don’t mix up
Does “OpenAI-compatible” mean every feature works the same?
- Chat Completions vs the Responses API
- “OpenAI-compatible” almost always means the Chat Completions endpoint. Support for OpenAI’s newer Responses API varies: Ollama, for example, offers a non-stateful
/v1/responses. - Same format vs same model
- A compatible API copies the request format, not the model. Prompts tuned for one model may need rework on another, as Anthropic’s compatibility docs point out.
FAQ
Questions people ask
Is there a free OpenAI-compatible API?
Yes, with limits. Google’s Gemini API has a free tier and an OpenAI-compatible endpoint, and Groq has a free plan with lower rate limits. Local servers such as Ollama expose an OpenAI-compatible endpoint on your own machine at no cost. Free tiers can treat your data differently: Google says free-tier content may be used to improve its products. Our Gemini and Groq key guides show how to sign up.
Which providers offer an OpenAI-compatible API?
Many do, including Groq, OpenRouter, Google Gemini and Anthropic, plus local and self-hosted servers such as Ollama and vLLM. Anthropic says its layer is mainly for testing and comparing models and recommends its native API for the full feature set. Each provider documents its own base URL and which parameters it ignores. The OpenRouter key guide covers one key for many models.
How do you use an OpenAI-compatible API in Python?
Install the openai package, then create the client with the provider’s endpoint and key: OpenAI(base_url=..., api_key=...). Call client.chat.completions.create with the provider’s own model name. Keep the key in an environment variable, not in your code, and read the provider’s compatibility notes for parameters it ignores or rejects before relying on them.
Go deeper
Try it and read more
- Free toolGet an AI API key: free tiers comparedWhich AI APIs you can use for free, which need a card, and step-by-step guides for Gemini, Groq, OpenRouter, Mistral, OpenAI and Claude keys.
- Free toolAI Model ComparisonCompare prices, context windows and features across models.
- Free toolLLM API Cost CalculatorEstimate per-request, daily and monthly API costs.
- Guide · 15 min readOpenAI vs Anthropic vs Gemini API message formats comparedOpenAI vs Anthropic message format, plus Gemini: system prompts, roles, images, tool calls, structured output, streaming and usage fields side by side.
- Guide · 12 min readThe cheapest LLM APIs right now, ranked from daily price dataThe cheapest LLM APIs ranked by blended price from daily data, the cheapest for tools, vision and long context, free tiers, and how to cut cost per task.
Related