Skip to main content
Version: 3.9.x

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

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

  • Install Docker.

  • Install cURL to send requests to the services for validation.

  • Have a running API7 Gateway instance.

  • Create a token from the Dashboard 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, then save its ID to an environment variable:

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

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

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 for the complete field mappings, release boundaries, and known limitations.

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.

curl -k "https://localhost:7443/apisix/admin/routes?gateway_group_id={gateway_group_id}" -X PUT \
-H "X-API-KEY: ${API_KEY}" \
--data-binary @- <<EOF
{
"id": "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.

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

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