代理 Anthropic 请求
Anthropic 提供了一个与 OpenAI 兼容的 API,使你能够使用熟悉的 OpenAI API 格式访问 Claude 模型。
本指南将介绍如何使用 ai-proxy 插件将 APISIX 与 Anthropic 集成。当 provider 设置为 anthropic 时,无需配置自定义端点。
前提条件
获取 Anthropic API Key
按照 Anthropic API 文档 创建账户并生成 API Key。你也可以选择将 API Key 保存为环境变量:
export ANTHROPIC_API_KEY=sk-ant-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx # 替换为你的 API key
创建指向 Anthropic 的路由
按照如下方式创建一个启用 ai-proxy 插件的路由:
- Admin API
- ADC
- Ingress Controller
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT -d '{
"id": "anthropic-chat",
"uri": "/anything",
"plugins": {
"ai-proxy": {
// Annotate 1
"provider": "anthropic",
"auth": {
"header": {
// Annotate 2
"Authorization": "Bearer '"$ANTHROPIC_API_KEY"'"
}
},
// Annotate 3
"options": {
"model": "claude-sonnet-4-5"
}
}
}
}'
❶ 将 provider 设置为 anthropic。
❷ 使用 Authorization 请求头附加 Anthropic API Key。
❸ 设置 Anthropic 支持的模型,例如 claude-sonnet-4-5。
services:
- name: Anthropic Service
routes:
- uris:
- /anything
name: anthropic-chat
plugins:
ai-proxy:
# Annotate 1
provider: anthropic
auth:
header:
# Annotate 2
Authorization: "Bearer sk-ant-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
options:
# Annotate 3
model: claude-sonnet-4-5
❶ 将 provider 设置为 anthropic。
❷ 使用 Authorization 请求头附加 Anthropic API Key。
❸ 设置 Anthropic 支持的模型,例如 claude-sonnet-4-5。
将配置同步到 APISIX:
adc sync -f adc.yaml
创建一个 Kubernetes 清单文件来配置路由:
- Gateway API
- APISIX CRD
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: ingress-apisix
name: ai-proxy-plugin-config
spec:
plugins:
- name: ai-proxy
config:
# Annotate 1
provider: anthropic
auth:
header:
# Annotate 2
Authorization: "Bearer sk-ant-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
options:
# Annotate 3
model: claude-sonnet-4-5
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: ingress-apisix
name: anthropic-chat
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: Exact
value: /anything
filters:
- type: ExtensionRef
extensionRef:
group: apisix.apache.org
kind: PluginConfig
name: ai-proxy-plugin-config
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: ingress-apisix
name: anthropic-route
spec:
ingressClassName: apisix
http:
- name: anthropic-route
match:
paths:
- /anything
plugins:
- name: ai-proxy
enable: true
config:
# Annotate 1
provider: anthropic
auth:
header:
# Annotate 2
Authorization: "Bearer sk-ant-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
options:
# Annotate 3
model: claude-sonnet-4-5
❶ 将 provider 设置为 anthropic。
❷ 使用 Authorization 请求头附加 Anthropic API Key。
❸ 设置 Anthropic 支持的模型,例如 claude-sonnet-4-5。
将配置应用到集群:
kubectl apply -f anthropic-route.yaml
验证
向该路由发送如下请求:
curl "http://127.0.0.1:9080/anything" -X POST \
-H "Content-Type: application/json" \
-d '{"messages": [{"role": "user", "content": "Hello"}]}'
你应当会收到类似如下的响应:
{
"request": {
"messages": [
{
"role": "user",
"content": "Hello"
}
]
},
"response": {
"id": "msg_01HUQ8fAR1XvJ9PodefrZixW",
"object": "chat.completion",
"created": 1770029318,
"model": "claude-sonnet-4-5-20250929",
"choices": [
{
"index": 0,
"finish_reason": "stop",
"message": {
"role": "assistant",
"content": "Hello! How can I help you today?"
}
}
],
"usage": {
"prompt_tokens": 8,
"completion_tokens": 12,
"total_tokens": 20
}
}
}
下一步
现在你已经学会如何将 APISIX 与 Anthropic 集成。更多详细信息请参阅:
如果你希望使用流式响应,请在请求中启用 streaming,并使用 proxy-buffering 插件禁用 NGINX 的 proxy_buffering,以避免服务器发送事件(SSE)被缓冲。