Create message (Anthropic format)
使用 Anthropic Messages API 规范创建模型消息响应。OmniCortex 深度兼容 Anthropic 官方客户端库(Python SDK / TypeScript SDK),支持通过修改 base_url 实现零成本无感迁移,原生覆盖多模态输入、深度思考推理(Extended Thinking)、提示词缓存(Prompt Caching)、内置 Agent 工具集、MCP 服务器编排以及多通道跨模型熔断降级(fallbacks)。
POST/anthropic/v1/messages
请求头 (Headers)
x-api-keystringheader Anthropic SDK 默认鉴权请求头。支持直接传入 Anthropic 官方 API Key 或 OmniCortex 虚拟密钥(以
sk-bf- 开头)。 anthropic-versionstringheaderdefault: 2023-06-01 Anthropic API 协议版本标识,默认为
2023-06-01。 Authorizationstringheader Bearer Token 形式的通用鉴权凭证(备选),格式为
Bearer sk-bf-<token>。 x-bf-vkstringheader OmniCortex 虚拟密钥专属请求头,用于精细化组织治理、分流路由与配额限流控制。
Content-Typestringheaderrequired 请求体媒体格式,固定为
application/json。 anthropic-betastringheader 启用特定 Anthropic Beta 实验特性时传递的标识,多个特性可用逗号分隔(例如
prompt-caching-2024-07-31、output-128k-2025-02-19)。 x-bf-asyncstringheader 设为
true 时将请求提交为后台离线异步任务,网关将立即返回任务 ID(不兼容流式传输)。 x-bf-async-idstringheader 轮询先前已提交的异步任务执行结果时,传入对应的异步任务 ID。
必填参数 (Required Body Parameters)
modelstringbodyrequired 目标模型标识。支持 Anthropic 官方模型名(如
claude-3-7-sonnet-20250219、claude-3-5-sonnet-20241022、claude-3-5-haiku-20241022)或 provider/model 统一路由别名(如 anthropic/claude-3-7-sonnet)。 max_tokensintegerbodyrequired 单次响应允许生成的最大 token 数量上限。
messagesarraybodyrequired 对话上下文消息对象列表。每条消息必须包含:
role(string, 必需): 角色,仅支持user(用户)或assistant(模型助手)。content(string | array, 必需): 消息内容,支持纯文本字符串或结构化内容块列表(包含text、image、document、tool_use、tool_result、thinking等分块)。
系统提示词与提示词缓存 (System & Caching)
systemstring | arraybody 系统级指令提示词(System Prompt)。支持传入单个字符串或富文本内容块数组。当传入数组时,可针对特定长文本段落声明
cache_control: {"type": "ephemeral"} 实现提示词缓存。 cache_controlobjectbody 全局缓存控制指令,针对整条请求生效。
高可用与调度路由 (Routing & Reliability)
fallbacksarraybody 备选模型容灾列表,每个元素格式为
provider/model 或模型名。当主模型遭遇速率限制(429)或上游不可用(5xx)时,网关将无感依序降级至备选模型。 采样与生成控制 (Sampling & Generation)
streambooleanbodydefault: false 是否使用 Server-Sent Events (SSE) 流式返回响应分块。
temperaturenumberbodydefault: 1 采样温度,介于 0 到 1 之间。较高的值会使输出更具发散性与创意,较低的值则使输出更集中且具确定性。
top_pnumberbody 核采样(Nucleus sampling)阈值,介于 0 到 1 之间。建议不要与
temperature 同时调整。 top_kintegerbody 仅从每个采样步骤中概率最高的前 k 个候选 token 中采样,有助于抑制模型生成低概率的离群词汇。
stop_sequencesarraybody 自定义停止词序列列表。一旦模型输出命中任一停止词,将立即结束当前轮次的生成。
metadataobjectbody 附加到请求的元数据对象,例如
user_id,用于追踪终端用户行为或在日志中关联链路。 深度思考推理 (Extended Thinking)
thinkingobjectbody Claude 3.7 / 3.5 专属深度思考配置:
type(string, 必需): 思考模式开关,可选enabled或disabled。budget_tokens(integer, 必需): 允许模型在正式回复前消耗的思考推理 token 预算上限(必须小于max_tokens)。
工具调用与代理扩展 (Tools & MCP)
toolsarraybody 模型可调用的工具定义列表。原生支持 Anthropic 自定义函数工具及官方内置工具:
custom: 用户自定义函数工具(包含name、description以及符合 JSON Schema 规范的input_schema)。- 内置环境工具:包含
computer_20250124(计算机界面操控)、bash_20250124(终端执行)、text_editor_20250124(文件读写)与web_search_20250305(网络搜索)。 - 各工具均支持配置
cache_control: {"type": "ephemeral"}。
tool_choiceobjectbody 控制模型调用工具的行为策略:
type: 可选auto(模型自主决策)、any(强制必须调用工具)、tool(强制调用指定工具)、none(禁用工具)。name: 当type为tool时必填,指定具体的工具名称。disable_parallel_tool_use(boolean): 是否禁用单轮多工具并行调用。
mcp_serversarraybody Model Context Protocol (MCP) 服务器动态连接配置,包含
name、url、authorization_token 与白名单工具过滤规则 tool_configuration。 output_formatobjectbody 指定模型输出的结构化格式约束(支持 JSON Schema 严格校验)。
Create message (Anthropic format)
HTTP / SDK
bash
curl --request POST \
--url http://localhost:8080/anthropic/v1/messages \
--header 'x-api-key: sk-bf-your-virtual-key' \
--header 'anthropic-version: 2023-06-01' \
--header 'Content-Type: application/json' \
--data '{
"model": "deepseek-v4-flash",
"max_tokens": 2048,
"system": "You are a helpful and concise AI assistant.",
"messages": [
{
"role": "user",
"content": "Introduce yourself in one sentence."
}
],
"thinking": {
"type": "enabled",
"budget_tokens": 1024
},
"temperature": 1
}'python
import anthropic
# 零成本接入:仅需修改 base_url 与 api_key
client = anthropic.Anthropic(
base_url="http://localhost:8080/anthropic",
api_key="sk-bf-your-virtual-key"
)
message = client.messages.create(
model="claude-3-7-sonnet-20250219",
max_tokens=2048,
system="You are a professional systems architect specializing in high-throughput AI infrastructure.",
messages=[
{
"role": "user",
"content": "Compare the streaming characteristics of Server-Sent Events vs WebSockets in AI gateways."
}
],
thinking={
"type": "enabled",
"budget_tokens": 1024
},
extra_body={
"fallbacks": ["openai/gpt-4o"]
}
)
print(message.content)typescript
import Anthropic from '@anthropic-ai/sdk';
const anthropic = new Anthropic({
baseURL: 'http://localhost:8080/anthropic',
apiKey: 'sk-bf-your-virtual-key',
});
async function main() {
const message = await anthropic.messages.create({
model: 'claude-3-7-sonnet-20250219',
max_tokens: 2048,
messages: [
{
role: 'user',
content: 'Compare the streaming characteristics of Server-Sent Events vs WebSockets in AI gateways.',
},
],
// @ts-ignore - 传入 OmniCortex 专属跨模型容灾参数
fallbacks: ['openai/gpt-4o'],
});
console.log(message.content);
}
main();Response
json
{
"id": "msg_01XFDUDYJgAACzvnptvVoYEL",
"type": "message",
"role": "assistant",
"content": [
{
"type": "thinking",
"thinking": "The user is asking for a comparison of Server-Sent Events (SSE) and WebSockets specifically in the context of AI gateways. I should evaluate them across connection model, overhead, unidirectional vs bidirectional requirements, proxy friendliness, and reconnection semantics..."
},
{
"type": "text",
"text": "In modern AI gateway architectures, Server-Sent Events (SSE) is the predominant choice for text generation because LLM inference is fundamentally unidirectional (one prompt prompt produces a stream of tokens). SSE operates over standard HTTP/1.1 or HTTP/2, making it fully compatible with existing reverse proxies, edge caches, and corporate firewalls without specialized WebSocket protocol upgrades."
}
],
"model": "claude-3-7-sonnet-20250219",
"stop_reason": "end_turn",
"stop_sequence": null,
"usage": {
"input_tokens": 32,
"output_tokens": 88,
"cache_creation_input_tokens": 0,
"cache_read_input_tokens": 0
}
}json
{
"type": "error",
"error": {
"type": "invalid_request_error",
"message": "max_tokens: Field required"
}
}json
{
"type": "error",
"error": {
"type": "api_error",
"message": "Upstream provider error: All fallback targets failed or timed out"
}
}