Skip to content

Anthropic Compatible API ​

One API Pro uses the relay/adaptor/anthropic package to translate OpenAI-style Chat Completions requests (/v1/chat/completions) into Anthropic Messages, and to consume Anthropic SSE streaming events. This page describes the Anthropic request / response and streaming-event conventions so clients can talk to Claude-style models through the gateway.

The native /v1/messages endpoint is currently served by /v1/chat/completions: when a claude-* model is requested, the body is sent upstream in Anthropic Messages form, and the response is normalized into OpenAI Chat Completions shape. The Anthropic wire format documented below is the form the adapter speaks to the upstream.

Endpoint index ​

EndpointMethodAuthDescription
/v1/chat/completions (model=claude-*)POSTBearer TokenOpenAI Chat Completions entry, hits the anthropic adapter
/v1/modelsGETBearer TokenModel list (OpenAI-compatible)

1. Request Format (Anthropic Messages) ​

Internally the adapter converts OpenAI Chat Completions requests into the Anthropic Messages shape below (relay/adaptor/anthropic/main.go::ConvertRequest).

Request body:

json
{
  "model": "claude-3-5-sonnet",
  "messages": [
    { "role": "user", "content": "Hello" }
  ],
  "system": "You are a helpful assistant.",
  "max_tokens": 4096,
  "temperature": 0.7,
  "top_p": 0.9,
  "top_k": 40,
  "stop_sequences": ["\n\nHuman:"],
  "stream": false,
  "tools": [
    {
      "name": "get_weather",
      "description": "Get current weather",
      "input_schema": {
        "type": "object",
        "properties": { "city": { "type": "string" } },
        "required": ["city"]
      }
    }
  ],
  "tool_choice": { "type": "auto" }
}

Fields:

FieldTypeRequiredDescription
modelstringyesModel name. Common: claude-3-5-sonnet, claude-3-opus, etc.
messagesarrayyesConversation history, see Message below
systemstringnoSystem prompt. A role=system entry in messages is folded into this field.
max_tokensintnoMax output tokens. Filled with 4096 when omitted.
temperaturefloatnoSampling temperature
top_pfloatnoNucleus sampling
top_kintnoTop-K sampling
stop_sequencesarray<string>noCustom stop sequences
streamboolnoWhether to stream the response
toolsarraynoTool list, each a Tool
tool_choiceobject/stringnoTool choice; OpenAI function shape or string auto / any

Message:

json
{ "role": "user", "content": [{ "type": "text", "text": "Hello" }] }
FieldTypeDescription
rolestringuser / assistant. OpenAI's tool role is mapped to user + tool_result.
contentarrayContent blocks (Content)

Content:

FieldTypeDescription
typestringtext / image / tool_use / tool_result
textstringText payload (type=text or type=tool_result)
sourceobjectImage source (type=image); {type:"base64", media_type:"image/png", data:"..."}
idstringtool_use id
namestringtool_use tool name
inputobjecttool_use arguments
tool_use_idstringMatching tool_use.id for a tool_result
contentstringText payload of a tool_result

Tool:

FieldTypeDescription
namestringTool name
descriptionstringTool description
input_schemaobjectJSON Schema for the tool arguments; contains type / properties / required

2. Response Format (non-streaming) ​

json
{
  "id": "msg_01XYZ",
  "type": "message",
  "role": "assistant",
  "content": [
    { "type": "text", "text": "Hello! How can I help you today?" }
  ],
  "model": "claude-3-5-sonnet",
  "stop_reason": "end_turn",
  "stop_sequence": null,
  "usage": {
    "input_tokens": 12,
    "output_tokens": 18,
    "cache_read_input_tokens": 0,
    "cache_creation_input_tokens": 0
  },
  "error": { "type": "", "message": "" }
}

Fields:

FieldTypeDescription
idstringMessage ID
typestringAlways message
rolestringAlways assistant
contentarrayContent blocks (same shape as above)
modelstringActual model used
stop_reasonstringend_turn / stop_sequence / max_tokens / tool_use
stop_sequencestring/nullStop sequence hit, if any
usage.input_tokensintInput tokens
usage.output_tokensintOutput tokens
usage.cache_read_input_tokensintCache read tokens
usage.cache_creation_input_tokensintCache write tokens
errorobjectError object; empty on success

The adapter maps stop_reason into OpenAI finish_reason: end_turn / stop_sequence → stop, max_tokens → length, tool_use → tool_calls. The response is returned on /v1/chat/completions in OpenAI chat.completion shape.

3. Streaming Response (SSE) ​

SSE event order follows the Anthropic Messages streaming spec; clients dispatch by the event prefix:

event: message_start
data: {"type":"message_start","message":{...full message...}}

event: content_block_start
data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}

event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Hello"}}

event: content_block_stop
data: {"type":"content_block_stop","index":0}

event: message_delta
data: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":18}}

event: message_stop
data: {"type":"message_stop"}
EventKey fieldsDescription
message_startmessage.{id,model,role,usage}Message metadata; usage is cumulative.
content_block_startcontent_block.{type,text/id/name/input}Opens a content block (text or tool_use).
content_block_deltadelta.{type,text/partial_json}Incremental content; input_json_delta carries partial JSON for tool args.
content_block_stop—Closes the current content block.
message_deltadelta.{stop_reason,stop_sequence}, usageCumulative usage (max-merged to avoid double counting).
message_stop—Closes the whole message.

At /v1/chat/completions, One API Pro normalizes these events into OpenAI chat.completion.chunk shape so OpenAI-SDK clients can consume them directly.

4. Auth and Billing ​

  • Auth: Bearer Token (i.e. sk-xxxxxxxx issued by /api/token/), identical to the OpenAI-compatible endpoints.
  • Billing: Computed from OpenAI-side prompt_tokens + completion_tokens; cache_read_input_tokens / cache_creation_input_tokens are folded into prompt_tokens by model.ClaudeUsage2OpenAI.
  • Retry / Fallback: Same as the OpenAI-compatible endpoints (config.RetryTimes, ErrorNext), driven by the generic relay/handler retry chain.

5. Error Response (from upstream) ​

json
{
  "type": "error",
  "error": {
    "type": "invalid_request_error",
    "message": "messages: must be non-empty"
  }
}

After relaying through /v1/chat/completions, the final body uses the standard OpenAI error shape:

json
{
  "error": {
    "message": "<msg> (request id: <uuid>)",
    "type": "one_api_error",
    "param": "",
    "code": "<upstream error type>"
  }
}