# Protocol Reference

The `ai-proxy` and `ai-proxy-multi` plugins share the same request protocol detection and conversion pipeline. The pipeline identifies the client format before routing to the configured provider or selected instance.

For plugin-specific configuration, see [`ai-proxy`](https://docs.api7.ai/hub/ai-proxy.md) and [`ai-proxy-multi`](https://docs.api7.ai/hub/ai-proxy-multi.md).

## Request Protocol Detection[​](#request-protocol-detection "Direct link to Request Protocol Detection")

The plugins identify the client protocol before matching it to a protocol supported by the selected provider or instance. The following detection rules apply to both plugins.

### Request Requirements[​](#request-requirements "Direct link to Request Requirements")

Requests that include `Content-Type` must use `application/json`. If the header is omitted, the plugins treat the body as JSON. They reject unsupported content types and invalid bodies before selecting a provider or instance.

The request body cannot exceed `max_req_body_size`, which defaults to 67,108,864 bytes. A request that exceeds this limit receives an HTTP 413 response. In API7 Gateway, this setting is available from 3.9.14 in the 3.9.x line and from 3.10.1 in the 3.10.x line. It is available in APISIX 3.17.0 and later.

### Detection Order[​](#detection-order "Direct link to Detection Order")

The plugins check the following rules in order:

| Client protocol         | Body signal                                                     | Path requirement                                               |
| ----------------------- | --------------------------------------------------------------- | -------------------------------------------------------------- |
| Bedrock Converse        | The request body contains a `messages` array.                   | The path ends in `/converse`; custom prefixes are allowed.     |
| Anthropic Messages      | The request body is a JSON object.                              | The path ends in `/v1/messages`; custom prefixes are allowed.  |
| OpenAI Responses        | The request body contains `input`.                              | The path ends in `/v1/responses`; custom prefixes are allowed. |
| OpenAI Chat Completions | The request body contains a `messages` array.                   | Any path matched by the route.                                 |
| OpenAI Embeddings       | The request body contains `input`, and no earlier rule matched. | Any path matched by the route.                                 |

The path-specific rules run before the body-only rules. This prevents Bedrock Converse and Anthropic Messages requests containing `messages` from being identified as Chat Completions. Responses and Embeddings requests both use `input`, so a request containing `input` but not `messages` is identified as Embeddings unless its path ends in `/v1/responses`.

Any other non-empty JSON object is treated as passthrough. This mode keeps the original request path and can reuse the original body when no request transformation changes it. Provider authentication and `override.endpoint` still apply. Empty or invalid request bodies are rejected.

Passthrough does not provide an AI protocol model to downstream AI-aware plugins. Usage extraction, prompt decoration, content moderation text extraction, and protocol conversion therefore do not run for the passthrough body.

The `ai-prompt-template` plugin runs earlier in the rewrite phase and can replace the request body before AI Proxy detects its protocol. The resulting body can match a named protocol or remain passthrough.

### After Detection[​](#after-detection "Direct link to After Detection")

For a named protocol, the plugin uses the detected protocol without conversion when the selected provider supports it. Otherwise, the plugin looks for a registered converter to a protocol the provider supports. The request is rejected if neither native support nor a compatible converter is available.

The detected protocol and request body determine whether `request_type` is recorded as `ai_stream` or `ai_chat` for logging plugins. Response parsing uses the upstream response's content type to distinguish streaming from non-streaming responses.

## Request Override Precedence[​](#request-override-precedence "Direct link to Request Override Precedence")

The final plugin configuration uses `override.llm_options.max_tokens` for provider-aware token-limit mapping. The provider maps that value to the field expected by the target protocol, then applies the matching `override.request_body` object.

Request-body objects are merged recursively. Arrays and scalar values replace the existing value rather than being combined. With `request_body_force_override: false`, client fields win and the override fills only missing values; with `true`, override values replace matching client fields. The request-body key names the target protocol after any conversion, such as `openai-chat`, `anthropic-messages`, or `bedrock-converse`.

## Failure Responses[​](#failure-responses "Direct link to Failure Responses")

| Condition                                                                                                      | Client-visible behavior        |
| -------------------------------------------------------------------------------------------------------------- | ------------------------------ |
| Request body exceeds `max_req_body_size`                                                                       | `413 Request Entity Too Large` |
| LLM connection or read times out before a response is available                                                | `504 Gateway Timeout`          |
| A streaming request ends before producing client output, including when protocol conversion produces no output | `502 Bad Gateway`              |
| A streaming converter receives a response it cannot parse in the selected format                               | `502 Bad Gateway`              |
| `ai-request-rewrite` receives a request without a body                                                         | `400 Bad Request`              |

Empty-stream handling was introduced in APISIX 3.19.0 and API7 Enterprise 3.9.19 and 3.10.6.

With `ai-proxy-multi`, a configured retry can run when the failure occurs before client output starts.

In API7 Enterprise 3.9.19 and 3.10.7, and in APISIX 3.19.0, a read failure after client output starts ends the stream without another retry. Reaching a response limit after output starts also ends the stream because the gateway cannot replace bytes already sent with an HTTP error response. Clients should detect incomplete streams because an initial HTTP 200 response does not prove that the stream completed successfully. See the [stream duration and response size limits](https://docs.api7.ai/hub/ai-proxy/configuration.md) when setting client timeouts and retry policies.

## Anthropic-to-OpenAI Conversion[​](#anthropic-to-openai-conversion "Direct link to Anthropic-to-OpenAI Conversion")

An Anthropic Messages client can send requests through `ai-proxy` or `ai-proxy-multi` to a backend that supports OpenAI Chat Completions. The plugins convert the client request to OpenAI format and convert the backend response to Anthropic format. This conversion supports a subset of the Anthropic Messages API: it preserves some fields, transforms others, and discards unsupported fields.

### When Conversion Applies[​](#when-conversion-applies "Direct link to When Conversion Applies")

The plugins identify an Anthropic Messages request when the request path ends in `/v1/messages` and the body is a JSON object. If the selected provider supports Anthropic Messages, the request uses that protocol without conversion. If the provider supports OpenAI Chat Completions instead, the shared converter translates the request and response.

The reverse client/backend pairing is not supported: an OpenAI Chat Completions client cannot use this converter to call an Anthropic Messages backend.

For `ai-proxy-multi`, the selected instance determines whether conversion is required. The [conversion configuration example](https://docs.api7.ai/hub/ai-proxy.md#convert-anthropic-requests-to-openai-compatible-backend) uses `ai-proxy`; for multi-instance configuration, see [`ai-proxy-multi`](https://docs.api7.ai/hub/ai-proxy-multi.md).

### Request Conversion[​](#request-conversion "Direct link to Request Conversion")

The converted request body is built from an allowlist. The following tables summarize the Anthropic inputs the converter reads. Unrecognized fields are discarded before the request reaches the backend.

#### Request Fields[​](#request-fields "Direct link to Request Fields")

| Anthropic field                         | OpenAI field                                  | Behavior                                                                                                                                                                                                   |
| --------------------------------------- | --------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `model`                                 | `model`                                       | Forwarded, unless the route pins a model with `options.model`.                                                                                                                                             |
| `max_tokens`                            | `max_completion_tokens`                       | Renamed.                                                                                                                                                                                                   |
| `stop_sequences`                        | `stop`                                        | Renamed.                                                                                                                                                                                                   |
| `temperature`, `top_p`                  | Same names                                    | Forwarded.                                                                                                                                                                                                 |
| `stream`                                | `stream`, plus `stream_options.include_usage` | The plugin sets `stream_options.include_usage` so that the stream carries usage.                                                                                                                           |
| `system`                                | A leading message with `role: system`         | Text blocks are concatenated into one string.                                                                                                                                                              |
| `tools[]` (custom tools)                | `tools[].function`                            | A tool name containing characters outside `[a-zA-Z0-9_-]`, or longer than 64 characters, is rewritten to satisfy the OpenAI naming rules. The original name is restored in the response.                   |
| `tool_choice`                           | `tool_choice`                                 | Converted. `{"type": "auto"}` becomes `"auto"`, `{"type": "any"}` becomes `"required"`, `{"type": "none"}` becomes `"none"`, and `{"type": "tool", "name": "..."}` becomes an object naming that function. |
| `tool_choice.disable_parallel_tool_use` | `parallel_tool_calls: false`                  | Converted.                                                                                                                                                                                                 |
| `thinking`                              | `reasoning_effort`                            | Approximated. A continuous `budget_tokens` value is mapped to one discrete effort level. The thresholds are release-dependent.                                                                             |
| `output_config.effort`                  | `reasoning_effort`                            | Used when `thinking.type` is `adaptive`. Release-dependent; see [Release Compatibility](#release-compatibility).                                                                                           |
| `output_format`, `output_config.format` | `response_format`                             | Release-dependent; see [Release Compatibility](#release-compatibility).                                                                                                                                    |
| `metadata.user_id`                      | `user`                                        | Renamed.                                                                                                                                                                                                   |
| `service_tier`                          | `service_tier`                                | Forwarded.                                                                                                                                                                                                 |

#### Message Content[​](#message-content "Direct link to Message Content")

| Anthropic content                | OpenAI equivalent                       | Behavior                                                                                                                                                |
| -------------------------------- | --------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `messages[].content` as a string | `messages[].content` as the same string | Forwarded.                                                                                                                                              |
| `text`                           | A text content part, or a plain string  | The shape depends on the release; see [Release Compatibility](#release-compatibility).                                                                  |
| `image` with a base64 source     | `image_url` with a `data:` URL          | Converted. Whether the backend model accepts image input varies by model.                                                                               |
| `image` with a URL source        | `image_url` with the same URL           | Forwarded unchanged.                                                                                                                                    |
| `document` with a base64 source  | `image_url` with a `data:` URL          | Approximation. The document bytes are placed in a field the OpenAI schema defines for images, so whether a backend accepts them is outside that schema. |
| `tool_use`                       | An assistant message with `tool_calls`  | Converted. Tool-name handling in message history is release-dependent; see [Release Compatibility](#release-compatibility).                             |
| `tool_result`                    | A message with `role: tool`             | Converted. Its ordering relative to ordinary text is release-dependent; see [Release Compatibility](#release-compatibility).                            |

#### Dropped Fields[​](#dropped-fields "Direct link to Dropped Fields")

The backend does not receive the following fields, and the response carries no signal that they were removed:

| Anthropic field                                                                                  | Why it is dropped                                                                                     |
| ------------------------------------------------------------------------------------------------ | ----------------------------------------------------------------------------------------------------- |
| `top_k`                                                                                          | OpenAI Chat Completions has no equivalent parameter.                                                  |
| `cache_control`                                                                                  | No equivalent. The converted request carries no caching directive.                                    |
| `citations`                                                                                      | The converter does not map citations in either direction.                                             |
| `thinking` and `redacted_thinking` blocks in message history                                     | OpenAI Chat Completions has no equivalent field. Ordinary text in the same assistant message is kept. |
| Anthropic built-in tools (`computer_`, `bash_`, `text_editor_`, `web_search`, `code_execution_`) | The converter has no mapping for these tools.                                                         |

#### Request Headers[​](#request-headers "Direct link to Request Headers")

If a request has an `x-api-key` header but no `Authorization` header, the converter sends the key as a bearer token in `Authorization`. It removes the original `x-api-key` header and headers whose names start with `anthropic-` or `x-stainless-`.

### Response Conversion[​](#response-conversion "Direct link to Response Conversion")

The converter reads completion fields only from `choices[0]`; any additional OpenAI choices are discarded. It maps top-level `usage` and `error` fields separately.

| OpenAI response field                              | Anthropic response                                                | Behavior                                                                                                                                                                                                                                                                                                                                                                        |
| -------------------------------------------------- | ----------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `message.content`                                  | A `text` content block                                            | Converted.                                                                                                                                                                                                                                                                                                                                                                      |
| `message.reasoning_content` or `message.reasoning` | A `thinking` content block                                        | Converted when the backend returns a non-empty string. A non-streaming block has an empty signature; see [Known Limitations](#known-limitations).                                                                                                                                                                                                                               |
| `message.tool_calls`                               | `tool_use` content blocks                                         | Converted. Sanitized tool names are restored when the original name is available.                                                                                                                                                                                                                                                                                               |
| `finish_reason`                                    | `stop_reason`                                                     | `stop` and `content_filter` become `end_turn`; `length` becomes `max_tokens`; `tool_calls` and `function_call` become `tool_use`. Any other value defaults to `end_turn`.                                                                                                                                                                                                       |
| `usage`                                            | `input_tokens`, `output_tokens`, and available cache-token fields | `completion_tokens` becomes `output_tokens`. Cache-read and cache-creation tokens are subtracted from `prompt_tokens` to produce `input_tokens`, then reported separately as `cache_read_input_tokens` and `cache_creation_input_tokens`. The nonnegative subtraction keeps the three Anthropic fields equal to the upstream prompt total without counting cached tokens twice. |
| `error`                                            | An Anthropic error object                                         | Converted when a normally parsed upstream response body contains an error object. HTTP 429, 5xx, and transport errors can bypass this conversion.                                                                                                                                                                                                                               |

For streaming responses, the converter emits Anthropic message and content-block events for OpenAI text, reasoning, and tool-call deltas. Initial usage values in `message_start` are zero; final token usage is emitted in `message_delta`.

The converter reads cache usage from the following OpenAI-compatible fields:

| Usage        | Upstream fields                                                                                                                                                                               |
| ------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Cache writes | Top-level `cache_creation_input_tokens`, or `prompt_tokens_details.cache_creation_tokens`, `prompt_tokens_details.cache_write_tokens`, or `prompt_tokens_details.cache_creation_input_tokens` |
| Cache reads  | `prompt_tokens_details.cached_tokens` or DeepSeek's `prompt_cache_hit_tokens`                                                                                                                 |

Clients that report streaming usage should read the final event and should not assume that cache-token fields are present.

### Release Compatibility[​](#release-compatibility "Direct link to Release Compatibility")

API7 Gateway 3.9.x and 3.10.x receive fixes independently. Check the column for the release line you run.

| Behavior                                                                                                                                            | API7 Gateway 3.9.x | API7 Gateway 3.10.x | APISIX           |
| --------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------ | ------------------- | ---------------- |
| An orphaned `tool_choice` is removed when every tool was dropped                                                                                    | 3.9.16 and later   | 3.10.2 and later    | 3.18.0 and later |
| A malformed backend tool call degrades instead of failing the response                                                                              | 3.9.16 and later   | 3.10.2 and later    | 3.18.0 and later |
| `message_start.content` is serialized as an array                                                                                                   | 3.9.16 and later   | 3.10.3 and later    | 3.18.0 and later |
| Anthropic's current structured-output shape is recognized                                                                                           | 3.9.16 and later   | 3.10.3 and later    | 3.18.0 and later |
| `thinking.type: adaptive` uses `output_config.effort`                                                                                               | 3.9.16 and later   | 3.10.3 and later    | 3.18.0 and later |
| `thinking.budget_tokens` uses the four-level mapping described below                                                                                | 3.9.16 and later   | 3.10.3 and later    | 3.18.0 and later |
| A user message with a single text block is sent as a content array, and an assistant message with several text blocks is concatenated into a string | 3.9.16 and later   | 3.10.3 and later    | 3.18.0 and later |
| Tool names in message history are rewritten consistently with declared tools                                                                        | 3.9.16 and later   | 3.10.3 and later    | 3.18.0 and later |
| `tool_result` messages are placed before ordinary text from the same user message                                                                   | 3.9.16 and later   | 3.10.3 and later    | 3.18.0 and later |
| Media is retained when a user message also contains `tool_result`                                                                                   | 3.9.16 and later   | 3.10.3 and later    | 3.18.0 and later |
| Converted usage subtracts cache reads and cache writes from `input_tokens` and reports both separately                                              | 3.9.22 and later   | 3.10.9 and later    | 3.20.0 and later |

These version differences affect structured output, message content, thinking effort, and tool history as follows:

#### Structured Output[​](#structured-output "Direct link to Structured Output")

API7 Gateway 3.9.15 and earlier, API7 Gateway 3.10.0 through 3.10.2, and APISIX 3.17.0 recognize only the converter's legacy expected shape. That shape is `output_config` or `output_format` carrying `type: json_schema` together with a `json_schema` field, or carrying `type: json` or `type: json_object`.

Anthropic's current shape carries the schema in `output_format.schema` or in `output_config.format`. In the 3.9.x line, API7 Gateway 3.9.16 and later recognize this shape. In the 3.10.x line, 3.10.3 and later recognize it. APISIX 3.18.0 and later recognize it as well. These releases normalize the schema and send `response_format` with strict mode enabled. On earlier releases, the backend receives no `response_format`, and the client receives no error.

#### Message Content Shape[​](#message-content-shape "Direct link to Message Content Shape")

On a release that predates the change, a user message carrying a single text block is sent as a plain string. An assistant message carrying several text blocks is sent as a content array. A backend that accepts only one of these shapes behaves differently across an upgrade.

#### Thinking Effort[​](#thinking-effort "Direct link to Thinking Effort")

The `budget_tokens` mapping changes across releases. The earlier mapping applies to APISIX 3.17.0 and API7 Gateway releases before 3.9.16 or 3.10.3 in their respective lines. The later mapping applies from APISIX 3.18.0 and from API7 Gateway 3.9.16 and 3.10.3 in their respective lines.

| `budget_tokens`    | Earlier releases | Later releases |
| ------------------ | ---------------- | -------------- |
| Below 1024         | `low`            | `minimal`      |
| 1024 through 2047  | `low`            | `low`          |
| 2048 through 4095  | `low`            | `medium`       |
| 4096 through 16383 | `medium`         | `high`         |
| 16384 or higher    | `high`           | `high`         |
| Not provided       | `medium`         | `minimal`      |

#### Tool History[​](#tool-history "Direct link to Tool History")

On an earlier release, a `tool_use` name in message history is not rewritten to match the declared tool name. Ordinary text can also precede `tool_result` messages from the same user message, and media in that message is discarded. A strict backend may reject the name or ordering mismatch.

APISIX 3.18.0 and later, API7 Gateway 3.9.16 and later, and API7 Gateway 3.10.3 and later handle these cases consistently. They rewrite history names, place tool messages first, and retain media.

### Known Limitations[​](#known-limitations "Direct link to Known Limitations")

The following limitations can affect converted requests and responses across the supported releases.

#### Streaming Can End Without a Terminating Event[​](#streaming-can-end-without-a-terminating-event "Direct link to Streaming Can End Without a Terminating Event")

When a backend closes the stream without a properly delimited final frame, the plugins do not emit the closing `message_delta` and `message_stop` events. A client that relies on `message_stop` may wait indefinitely or treat the stream as incomplete. This affects every release listed above. Set a client-side timeout and treat an unexpected end of the stream as a failure.

#### Converted `thinking` Blocks Do Not Carry a Valid Signature[​](#converted-thinking-blocks-do-not-carry-a-valid-signature "Direct link to converted-thinking-blocks-do-not-carry-a-valid-signature")

For a non-streaming response, the plugins set the block's `signature` to an empty string. For a streaming response, they emit thinking deltas without a signature. Clients that require a valid signature cannot replay either converted form as a signed Anthropic thinking block. A backend that instead embeds reasoning in ordinary message content produces a response where the reasoning appears as visible text.

#### Error Responses Are Not Consistently Anthropic-Shaped[​](#error-responses-are-not-consistently-anthropic-shaped "Direct link to Error Responses Are Not Consistently Anthropic-Shaped")

HTTP 429, 5xx, and transport timeout responses can bypass response conversion. Clients should be prepared to receive an upstream or gateway error body that does not follow the Anthropic error schema.

#### Backend Capabilities Are Not Validated[​](#backend-capabilities-are-not-validated "Direct link to Backend Capabilities Are Not Validated")

The plugins convert the request but do not check whether the backend model supports the result. A backend can return HTTP 200 while silently ignoring a capability, such as dropping an image, ignoring `response_format`, or returning no tool call.

Backend behavior varies by model and between dated snapshots of the same model name. Validate the specific models you plan to use rather than generalizing from a backend.

### Validate Backend Compatibility[​](#validate-backend-compatibility "Direct link to Validate Backend Compatibility")

Test the following converted inputs against each backend model because a backend can reject them even when the original Anthropic request is valid:

* **A named `tool_choice`.** The converter emits an object naming the function, or `"required"`. Some backends accept only `"auto"` while the model is reasoning. If a backend rejects the converted form, send `{"type": "auto"}` from the client.
* **`thinking` together with a small `max_tokens`.** `thinking` becomes `reasoning_effort`, which can make a backend reserve a reasoning budget. When that budget exceeds the converted `max_completion_tokens`, the backend rejects the request. Raise `max_tokens` when you enable `thinking`.
* **A `document` block.** The converter can only offer it to the backend as an image. A model that cannot read it may answer with invented content instead of reporting an error.
* **Mixed text, media, and `tool_result` content.** Earlier releases can put ordinary text before the converted tool message and discard media from the same user message. Test this shape if the backend validates tool-message ordering, or upgrade to a release that places tool messages first and retains media.

## Related Configuration[​](#related-configuration "Direct link to Related Configuration")

* [Configure `ai-proxy` to convert Anthropic requests](https://docs.api7.ai/hub/ai-proxy.md#convert-anthropic-requests-to-openai-compatible-backend).
* [Configure native Anthropic Messages pass-through](https://docs.api7.ai/hub/ai-proxy.md#native-anthropic-messages-api-pass-through).
* [Configure `ai-proxy-multi`](https://docs.api7.ai/hub/ai-proxy-multi.md).
* [Convert Anthropic Messages with API7 Gateway](https://docs.api7.ai/api7-gateway/ai-gateway/use-cases/protocol-conversion.md).
