# OpenAI-Compatible Chat Completions

Applications that use the OpenAI Chat Completions format can send the same supported request shape to an AISIX gateway at `POST /v1/chat/completions`. The gateway authenticates the caller, resolves the model alias, applies gateway policy, and dispatches the request through the selected provider adapter.

The compatibility is client-facing: the upstream can be OpenAI or another supported provider. The gateway returns a supported OpenAI-compatible response shape while the adapter handles provider translation.

This guide describes the Chat Completions proxy behavior. For a runnable SDK setup, see [OpenAI SDK](https://docs.api7.ai/ai-gateway/getting-started/openai-sdk.md).

## What the Client Sends[​](#what-the-client-sends "Direct link to What the Client Sends")

The client sends three gateway-facing values:

* The base URL is the AISIX proxy API root, which is the gateway origin followed by `/v1`.
* The API key is an AISIX caller API key.
* The model value is an AISIX model alias, such as `gpt-4o-prod`.

The request body keeps the OpenAI-compatible format, including messages, tools, streaming options, and supported multimodal fields. Provider credentials, upstream model IDs, routing policy, rate limits, guardrails, and other gateway policy stay in AISIX. For audio content blocks and generated audio, see [Audio Input and Output with Chat Completions](https://docs.api7.ai/ai-gateway/endpoints/chat-audio.md).

Send the caller API key with the standard bearer token format:

```
Authorization: Bearer YOUR_CALLER_API_KEY
```

AISIX also accepts `x-api-key: YOUR_CALLER_API_KEY` for compatibility. Use the bearer token format for OpenAI-compatible clients when the client supports it.

Export the gateway connection and request values used below:

```
# AISIX_PROXY has no trailing slash or endpoint path such as /v1.

# The local quickstarts use http://127.0.0.1:3000.

export AISIX_PROXY="YOUR_AISIX_GATEWAY_URL"

export AISIX_API_KEY="YOUR_CALLER_API_KEY"

export AISIX_MODEL="gpt-4o-prod"
```

## Send a Chat Completions Request[​](#send-a-chat-completions-request "Direct link to Send a Chat Completions Request")

Use `POST /v1/chat/completions` as the default route for OpenAI-compatible chat clients.

Send a chat-completions request through AISIX:

```
curl -sS -X POST "${AISIX_PROXY}/v1/chat/completions" \

  -H "Authorization: Bearer ${AISIX_API_KEY}" \

  -H "Content-Type: application/json" \

  -d '{

    "model": "'"${AISIX_MODEL}"'",

    "messages": [

      {"role": "user", "content": "Hello from AISIX."}

    ]

  }'
```

A successful response uses the OpenAI-compatible chat-completions format. The response keeps `model` set to the caller-facing alias from the request.

When an upstream that speaks the OpenAI chat completions format reports the `service_tier` it served the request at, the response, or each streamed chunk, carries that value unchanged. The field is omitted when the upstream reports its tier in another protocol's vocabulary, such as Anthropic or Amazon Bedrock. It is also omitted when the upstream reported none, and on a response served from the response cache. The tier every upstream reports is recorded on the usage event; see [Read the Served Service Tier](https://docs.api7.ai/ai-gateway/observability/metrics-and-usage-events.md#read-the-served-service-tier).

## Request Parameters and Choices[​](#request-parameters-and-choices "Direct link to Request Parameters and Choices")

AISIX forwards each request parameter according to what the target provider's API defines for it, and returns every choice the upstream answers, with its `logprobs`, `refusal`, and `annotations`. See [Request Parameter Handling](https://docs.api7.ai/ai-gateway/endpoints/request-parameters.md).

## Message Roles[​](#message-roles "Direct link to Message Roles")

Each message's `role` is `system`, `developer`, `user`, `assistant`, `tool`, or the legacy `function`, in lowercase. AISIX rejects any other value, including a differently cased role such as `Developer`, with `400 invalid_request_error` before the request reaches a provider.

A `developer` message carries application instructions. OpenAI's reasoning models use it in place of a `system` message, and clients such as coding agents send it to those models. Only the OpenAI API receives the message under that role. Every other upstream receives it in the place that upstream keeps system instructions:

| Upstream                                                                                        | What the upstream receives                                                                                                                                                                                                                         |
| ----------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Provider keys whose `provider` is `openai`                                                      | The `developer` message unchanged, for every model and whatever the key's `api_base`.                                                                                                                                                              |
| Other providers on the `openai` adapter, such as DeepSeek or Google AI Studio, and Azure OpenAI | A `system` message in the same position.                                                                                                                                                                                                           |
| OpenAI-compatible and partner models on Vertex AI, such as Llama or Mistral                     | A `system` message in the same position.                                                                                                                                                                                                           |
| Anthropic, and Claude on Vertex AI                                                              | The top-level `system` field, joined with any leading system messages. A developer message is moved there from anywhere in the conversation, not only from the start.                                                                              |
| Gemini on Vertex AI                                                                             | The top-level `systemInstruction`.                                                                                                                                                                                                                 |
| Amazon Bedrock                                                                                  | The system instructions: the Converse `system` blocks, or the top-level `system` field of the Anthropic request body that non-streaming Claude requests use. A request served by the Bedrock Chat Completions endpoint keeps the `developer` role. |

Guardrails treat a developer message the way they treat a system message. See [Choose the Input Message Window](https://docs.api7.ai/ai-gateway/traffic-controls/guardrails/behavior.md#choose-the-input-message-window).

## Discover Available Models[​](#discover-available-models "Direct link to Discover Available Models")

`GET /v1/models` returns every concrete model alias visible to the caller API key, including direct, routing, semantic, and ensemble aliases. Wildcard aliases are patterns rather than concrete model names, so they are not listed. A key that allows every model sees every concrete alias, while a restricted key sees only the aliases its allowlist permits.

List the model aliases visible to the caller API key:

```
curl -sS "${AISIX_PROXY}/v1/models" \

  -H "Authorization: Bearer ${AISIX_API_KEY}"
```

## Handle Errors[​](#handle-errors "Direct link to Handle Errors")

The Chat Completions route returns errors in an OpenAI-style envelope. Use the error type before the status code when you need to distinguish caller authentication, model access, policy blocks, rate limits, and upstream failures.

For the full error and header reference, see [Headers and Error Codes](https://docs.api7.ai/ai-gateway/reference/headers-and-error-codes.md).

## Next Steps[​](#next-steps "Direct link to Next Steps")

You have now seen how OpenAI-compatible clients call AISIX. Continue with [Audio Input and Output with Chat Completions](https://docs.api7.ai/ai-gateway/endpoints/chat-audio.md), [Streaming](https://docs.api7.ai/ai-gateway/endpoints/streaming.md), or [Tool Calling](https://docs.api7.ai/ai-gateway/endpoints/tool-calling.md).

Use [OpenAI Client with Anthropic Upstream](https://docs.api7.ai/ai-gateway/endpoints/openai-client-to-anthropic.md) when the application should keep the OpenAI-compatible client shape while AISIX calls Anthropic upstream.
