Anthropic Messages access

Use the website address to call the compatible Messages interface.

HTTP request

Call POST /v1/messages with a valid credential to avoid passing in multiple different keys at the same time:

curl --fail-with-body https://dunduntoken.com/v1/messages \
  -H "x-api-key: $DUNDUN_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "Content-Type: application/json" \
  -d '{"model":"MODEL_ID","max_tokens":256,"messages":[{"role":"user","content":"只回复 OK"}]}'

Structural differences

System instructions use the top level system, and normal history is placed in messages. The response’s content is an array of content blocks, which may contain text, tools, or other types. Read stop_reason to determine whether it is finished or needs to execute the tool.

SDK base address

Anthropic clients usually fill in https://dunduntoken.com, and the client spells /v1/messages. Models must be accessible through the Messages protocol. Output caps, thinking, cache hints, and tool fields should be validated against the current model.

Limitations

API compatible access and Anthropic official subscription are different services. This site does not promise that all official management, file, counting or experimental interfaces will be available; the token counting route for Messages is not currently open.

Request fields and responses

Field Description
model Supports precise IDs for Messages
max_tokens Output upper limit set according to the allowed range of the model
system Top-level system directives that support the use of text or content blocks per model
messages User and assistant history
stream Enable SSE output for this protocol

Simplified response:

{
  "id": "EXAMPLE_MESSAGE_ID",
  "type": "message",
  "role": "assistant",
  "content": [
    {
      "type": "text",
      "text": "OK"
    }
  ],
  "stop_reason": "end_turn"
}

Traverse content without assuming that the first block is always text. tool_use needs to return tool_result according to the same call ID; when the output upper limit is reached, check whether the complete result is obtained.

Python SDK

After installing anthropic, use the existing environment variables:

import os
from anthropic import Anthropic

client = Anthropic(api_key=os.environ["DUNDUN_API_KEY"], base_url="https://dunduntoken.com")
message = client.messages.create(
    model="MODEL_ID", max_tokens=256,
    messages=[{"role": "user", "content": "只回复 OK"}],
)
for block in message.content:
    if block.type == "text":
        print(block.text)

The production application adds timeout, exception and request ID records to avoid repeated retries between the SDK and the business.

Verification sequence and troubleshooting

Text first, then streaming, and finally tools and thinking. 401 Check credentials; 404 Check if /v1/v1/messages appears; Move the system prompt to the top level when the system role is wrong. The new fields in the official SDK do not mean that all models are supported. First, remove unnecessary beta and experimental options.