# Anthropic 兼容接口 (/docs/cloud/services/model/guides/anthropic-compatible)



Anthropic SDK 使用 `https://api.deepraxis.ai/anthropic`。SDK 会追加 `/v1/messages`，基础地址不要再加 `/v1`。请求按同协议转发，可用能力取决于模型的声明。

## 兼容覆盖什么 [#兼容覆盖什么]

`/anthropic/v1/models` 只列 Anthropic 兼容模型。不要假设其他协议目录里的 ID 在这里也可用。未声明的操作返回 `404`（`anthropic.messages is not offered for …`）；换地址不会把不支持的模型能力转换出来。

## 字段差异去哪里核对 [#字段差异去哪里核对]

| 应用使用的内容                           | 需要核对的差异                                                                                                    |
| --------------------------------- | ---------------------------------------------------------------------------------------------------------- |
| `thinking`、`output_config.effort` | 思考选项取决于模型；部分模型不能关闭思考，见 [Messages](/docs/api/compatible/anthropic/createMessage)                            |
| 缓存控制、工具、元数据、beta 或 workspace 请求头  | [Messages 字段支持](/docs/api/compatible/anthropic/createMessage)，不要假设所有官方扩展可用                                 |
| `count_tokens`                    | 带 `x-praxis-token-count: estimated` 的校准估算，见[统计 token](/docs/api/compatible/anthropic/countTokens)          |
| 模型限额与能力                           | [模型列表](/docs/api/compatible/anthropic/listModels)，未知限额可能为 `null`                                           |
| 文件上传、过期时间                         | [上传文件](/docs/api/compatible/anthropic/createFile)                                                          |
| 费用                                | [Messages 费用处理](/docs/cookbook/migration/from-anthropic#行为差异)与[生成记录](/docs/api/native/model/getGeneration) |

未知顶层字段可传到同协议上游，由上游决定是否接受；已声明字段会校验。成功响应不代表所有选项都已执行。操作支持见 [API 列表](/docs/api/compatible/anthropic)，缺失操作的处理见[迁移替代方案](/docs/cookbook/migration/from-anthropic#不支持的操作与替代)。

## 1. 准备 [#1-准备]

需要 Python 3.10+、[uv](https://docs.astral.sh/uv/getting-started/installation/) 和一个 Praxis Key。在[控制台 → API Keys](https://console.deepraxis.ai/keys)创建 Key，将它安全注入当前终端的 `PRAXIS_API_KEY` 环境变量；不要把 Key 写进代码或日志。保留原服务的配置与 Key，供回滚使用。以下命令在 macOS / Linux 的 Bash 中执行。

```bash
test -n "${PRAXIS_API_KEY:-}"
```

期望：退出码 0；退出码 1 表示变量未设置，先设置再继续。

## 2. 验证 Messages 与 token 统计 [#2-验证-messages-与-token-统计]

```bash
unset HTTP_PROXY HTTPS_PROXY ALL_PROXY http_proxy https_proxy all_proxy
uv run -q --no-project --with openai --with anthropic python - <<'PY'
import os
from anthropic import Anthropic

client = Anthropic(
    base_url="https://api.deepraxis.ai/anthropic",
    api_key=os.environ["PRAXIS_API_KEY"],
    timeout=90.0,
    max_retries=0,
)
models = client.models.list()
assert "deepseek-flash" in {m.id for m in models.data}
raw = client.messages.with_raw_response.create(
    model="deepseek-flash",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Reply OK."}],
)
reply = raw.parse()
text = "".join(block.text for block in reply.content if block.type == "text")
print("HTTP", raw.status_code)
print("X-Request-Id:", raw.headers.get("x-request-id"))
print("X-Praxis-Generation-Id:", raw.headers.get("x-praxis-generation-id"))
print(text)
assert raw.status_code == 200 and text and text.strip()
assert raw.headers.get("x-request-id")
count = client.messages.with_raw_response.count_tokens(
    model="deepseek-flash",
    messages=[{"role": "user", "content": "Reply OK."}],
)
print("count_tokens:", count.parse().input_tokens)
print("X-Request-Id:", count.headers.get("x-request-id"))
print("x-praxis-token-count:", count.headers.get("x-praxis-token-count"))
assert count.status_code == 200
assert count.headers.get("x-praxis-token-count") == "estimated"
PY
```

期望：退出码 0，输出 `HTTP 200`、非空正文（通常为 `OK.`）和 `X-Request-Id`。断言同时检查模型存在与真实回复；不是只检查能否连接。保留请求 ID 便于排查，不要记录 Key。示例会产生少量调用费用。

期望：即使第一块是思考，仍能从 `type == "text"` 的块取得正文。第二次调用输出 token 估算与 `estimated`；不能把估算当成计费用量。

若返回 `400`（`cannot disable thinking`），移除关闭思考的要求或改选允许关闭的模型；若返回 `anthropic.messages is not offered for …`，选择 Anthropic 兼容且支持该操作的模型。已有应用回滚时恢复原地址、Key、模型与思考选项，再运行原 Messages 请求。

[API 参考 →](/docs/api/compatible/anthropic) · [Cookbook：从 Anthropic 迁移 →](/docs/cookbook/migration/from-anthropic)
