# Convert Anthropic Messages to OpenAI Chat Completions

API7 AI Gateway can convert Anthropic Messages API requests to the OpenAI Chat Completions API format. This enables teams to use the Anthropic SDK with an OpenAI-compatible backend without changing application code.

## Overview[​](#overview "Direct link to Overview")

Teams often adopt different LLM SDKs for different services. Switching providers typically requires rewriting API integration code. API7 AI Gateway solves this by converting between protocols at the gateway layer:

* **Anthropic SDK to OpenAI backend**: Send Anthropic-format requests; the gateway converts and forwards to an OpenAI-compatible provider.
* **Transparent response conversion**: Responses from the OpenAI backend are automatically converted back to Anthropic format.

The gateway recognizes an Anthropic Messages request when the request URI ends in `/v1/messages` and handles the protocol conversion automatically.

note

The request URI can have a custom prefix, but it must keep the `/v1/messages` suffix. A provider that supports Anthropic Messages receives the request in its original format. Conversion applies when the selected provider supports OpenAI Chat Completions instead.

## Prerequisites[​](#prerequisites "Direct link to Prerequisites")

* Install [Docker](https://docs.docker.com/get-docker/).

* Install [cURL](https://curl.se/) to send requests to the services for validation.

* Have a running API7 Gateway instance.

* Create a [token from the Dashboard](https://docs.api7.ai/api7-gateway/reference/obtain-dashboard-token.md) and save it to an environment variable:

  ```
  export API_KEY=your-dashboard-token   # replace with your Dashboard token
  ```

* Replace `{gateway_group_id}` with your gateway group ID. Use `default` if you are following the quickstart.

* If you are following the Admin API examples, create or reuse a service in API7 Gateway. If you do not have one yet, follow [Create or Reuse a Service](https://docs.api7.ai/api7-gateway/ai-gateway/get-started.md#create-or-reuse-a-service), then save its ID to an environment variable:

  ```
  export SERVICE_ID=your-service-id         # replace with your service ID
  ```

## How Protocol Conversion Works[​](#how-protocol-conversion-works "Direct link to How Protocol Conversion Works")

The conversion pipeline:

1. **Auto-detection**: When the request URI ends in `/v1/messages`, the gateway identifies it as Anthropic Messages.
2. **Request conversion**: Anthropic fields are mapped to OpenAI equivalents (system prompts, messages, tool definitions, parameters).
3. **Response conversion**: OpenAI response fields are mapped back to Anthropic format (content blocks, stop reasons, usage).
4. **Streaming support**: OpenAI SSE stream chunks are converted to Anthropic SSE events (`message_start`, `content_block_delta`, `message_delta`, `message_stop`).

## Conversion Compatibility[​](#conversion-compatibility "Direct link to Conversion Compatibility")

The conversion supports a subset of the Anthropic Messages API. Some fields are transformed or approximated, and unsupported fields can be discarded without an error. See [Protocol Reference](https://docs.api7.ai/hub/ai-proxy-protocol-reference.md#anthropic-to-openai-conversion) for the complete field mappings, release boundaries, and known limitations.

## Configure Protocol Conversion[​](#configure-protocol-conversion "Direct link to Configure Protocol Conversion")

Route Anthropic-format requests to an OpenAI backend. No explicit protocol configuration is needed because the `/v1/messages` URI suffix identifies the request as Anthropic Messages.

* Admin API
* ADC

```
curl -k "https://localhost:7443/apisix/admin/routes/protocol-conversion?gateway_group_id={gateway_group_id}" -X PUT \

  -H "X-API-KEY: ${API_KEY}" \

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

  --data-binary @- <<EOF

  {

    "name": "protocol-conversion",

    "service_id": "$SERVICE_ID",

    "paths": ["/v1/messages"],

    "plugins": {

      "ai-proxy": {

        "provider": "openai",

        "auth": {

          "header": {

            "Authorization": "Bearer $OPENAI_API_KEY"

          }

        },

        "options": {

          "model": "gpt-4o"

        }

      }

    }

  }

EOF
```

❶ Set the URI to `/v1/messages` — the Anthropic Messages API endpoint. The gateway auto-detects this as Anthropic protocol.

❷ Set the backend provider to `openai`. The gateway converts Anthropic requests to OpenAI format before forwarding.

❸ Set the backend model. Clients send Anthropic-format requests, but the actual inference runs on this OpenAI model.

adc.yaml

```
services:

  - name: Protocol Conversion

    routes:

      - uris:

          - /v1/messages

        name: protocol-conversion

        plugins:

          ai-proxy:

            provider: openai

            auth:

              header:

                Authorization: "Bearer ${OPENAI_API_KEY}"

            options:

              model: gpt-4o

    labels:

      docs-example: protocol-conversion
```

❶ Set the URI to `/v1/messages` — the Anthropic Messages API endpoint. The gateway auto-detects this as Anthropic protocol.

❷ Set the backend provider to `openai`. The gateway converts Anthropic requests to OpenAI format before forwarding.

❸ Set the backend model. Clients send Anthropic-format requests, but the actual inference runs on this OpenAI model.

Preview the changes owned by this example and confirm that the diff contains no unintended updates or deletions:

```
adc diff -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=protocol-conversion
```

Synchronize the reviewed configuration:

```
adc sync -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=protocol-conversion
```

## Example: Use Anthropic SDK with OpenAI Backend[​](#example-use-anthropic-sdk-with-openai-backend "Direct link to Example: Use Anthropic SDK with OpenAI Backend")

Send an Anthropic-format request to the route:

```
curl "http://127.0.0.1:9080/v1/messages" -X POST \

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

  -H "anthropic-version: 2023-06-01" \

  -d '{

    "model": "gpt-4o",

    "max_tokens": 1024,

    "system": "You are a helpful assistant.",

    "messages": [

      { "role": "user", "content": "What is an API gateway?" }

    ]

  }'
```

You should receive a response in Anthropic format:

```
{

  "id": "msg_abc123",

  "type": "message",

  "role": "assistant",

  "content": [

    {

      "type": "text",

      "text": "An API gateway is a server that acts as a single entry point for API requests, handling routing, authentication, rate limiting, and other cross-cutting concerns for backend services."

    }

  ],

  "model": "gpt-4o",

  "stop_reason": "end_turn",

  "usage": {

    "input_tokens": 18,

    "output_tokens": 35

  }

}
```

The response follows the Anthropic format (`type: "message"`, `content` blocks, `stop_reason`, `input_tokens`/`output_tokens`) even though the actual inference ran on OpenAI's GPT-4o.

## Example: Tool Calling Across Protocols[​](#example-tool-calling-across-protocols "Direct link to Example: Tool Calling Across Protocols")

Send an Anthropic-format request with tool definitions:

```
curl "http://127.0.0.1:9080/v1/messages" -X POST \

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

  -H "anthropic-version: 2023-06-01" \

  -d '{

    "model": "gpt-4o",

    "max_tokens": 1024,

    "tools": [

      {

        "name": "get_weather",

        "description": "Get weather for a location",

        "input_schema": {

          "type": "object",

          "properties": {

            "location": { "type": "string" }

          },

          "required": ["location"]

        }

      }

    ],

    "messages": [

      { "role": "user", "content": "What is the weather in San Francisco?" }

    ]

  }'
```

The gateway converts Anthropic tool definitions to OpenAI function format, forwards to OpenAI, then converts the response back to Anthropic `tool_use` blocks.

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

* [Connect to Anthropic Claude](https://docs.api7.ai/api7-gateway/ai-gateway/llm-providers/anthropic.md) — Route directly to Anthropic without conversion.
* [Multi-LLM Routing and Fallback](https://docs.api7.ai/api7-gateway/ai-gateway/use-cases/multi-llm-routing-and-fallback.md) — Combine protocol conversion with multi-model routing.
* Review field mappings, release boundaries, and limitations in [Protocol Reference](https://docs.api7.ai/hub/ai-proxy-protocol-reference.md#anthropic-to-openai-conversion).
* For the full configuration reference, see [`ai-proxy`](https://docs.api7.ai/hub/ai-proxy.md).
