> ## Documentation Index
> Fetch the complete documentation index at: https://docs.codeflare.cc/llms.txt
> Use this file to discover all available pages before exploring further.

# Anthropic Messages

> 配置 Messages 请求头与原生消息格式。

## 基础调用

入口为 `POST /v1/messages`，模型需要支持 `anthropic-messages`。传入 `x-api-key`、`anthropic-version`，并显式设置 `max_tokens`。

Python 示例需要先执行 `python -m pip install anthropic`。SDK 的 Base URL 使用网关根地址，不包含尾部 `/v1`。

<CodeGroup>
  ```bash cURL theme={null}
  curl "https://YOUR-REGION.example/v1/messages" \
    -H "x-api-key: $CODEFLARE_API_KEY" \
    -H 'anthropic-version: 2023-06-01' \
    -H 'Content-Type: application/json' \
    -d '{"model":"MODEL_ID","max_tokens":256,"messages":[{"role":"user","content":"Hello"}]}'
  ```

  ```python Python theme={null}
  from anthropic import Anthropic
  import os

  client = Anthropic(
      api_key=os.environ["CODEFLARE_API_KEY"],
      base_url="https://YOUR-REGION.example", max_retries=0,
  )
  with client.messages.stream(
      model="MODEL_ID", max_tokens=256,
      messages=[{"role": "user", "content": "Hello"}],
  ) as stream:
      for text in stream.text_stream:
          print(text, end="", flush=True)
  ```
</CodeGroup>

## 流式与工具

设置 `stream: true` 可使用 Messages 的原生事件；Anthropic SDK 的 `messages.stream` 提供文本流。工具调用与多模态输入必须由所选模型和渠道支持，不能把 OpenAI 的事件直接当作 Messages 解析。

`POST /v1/messages/count_tokens` 需要渠道的 `count_tokens` 能力，token 计数不收生成费用。它不保证随后请求一定可被准入。

## Claude Code

客户端配置见 [Claude Code](/zh/tools/claude-code)。此入口不提供 Responses 的响应保存与 HTTP 续接接口。中断后先检查平台请求日志，避免重复生成。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.