文本
创建消息
位于 /v1/messages 的 Anthropic 兼容 Messages 端点。
POST
/
messages
创建消息
const options = {
method: 'POST',
headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'},
body: JSON.stringify({
model: 'anthropic/claude-sonnet-4',
max_tokens: 512,
stream: false,
messages: [{role: 'user', content: 'What time is it in America/New_York right now?'}],
tools: [{type: 'gateway:datetime', parameters: {timezone: 'America/New_York'}}],
tool_choice: {type: 'auto'}
})
};
fetch('https://api.phaseo.app/v1/messages', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));import requests
url = "https://api.phaseo.app/v1/messages"
payload = {
"model": "anthropic/claude-sonnet-4",
"max_tokens": 512,
"stream": False,
"messages": [
{
"role": "user",
"content": "What time is it in America/New_York right now?"
}
],
"tools": [
{
"type": "gateway:datetime",
"parameters": { "timezone": "America/New_York" }
}
],
"tool_choice": { "type": "auto" }
}
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)curl --request POST \
--url https://api.phaseo.app/v1/messages \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"model": "anthropic/claude-sonnet-4",
"max_tokens": 512,
"stream": false,
"messages": [
{
"role": "user",
"content": "What time is it in America/New_York right now?"
}
],
"tools": [
{
"type": "gateway:datetime",
"parameters": {
"timezone": "America/New_York"
}
}
],
"tool_choice": {
"type": "auto"
}
}
'package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.phaseo.app/v1/messages"
payload := strings.NewReader("{\n \"model\": \"anthropic/claude-sonnet-4\",\n \"max_tokens\": 512,\n \"stream\": false,\n \"messages\": [\n {\n \"role\": \"user\",\n \"content\": \"What time is it in America/New_York right now?\"\n }\n ],\n \"tools\": [\n {\n \"type\": \"gateway:datetime\",\n \"parameters\": {\n \"timezone\": \"America/New_York\"\n }\n }\n ],\n \"tool_choice\": {\n \"type\": \"auto\"\n }\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.phaseo.app/v1/messages")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"model\": \"anthropic/claude-sonnet-4\",\n \"max_tokens\": 512,\n \"stream\": false,\n \"messages\": [\n {\n \"role\": \"user\",\n \"content\": \"What time is it in America/New_York right now?\"\n }\n ],\n \"tools\": [\n {\n \"type\": \"gateway:datetime\",\n \"parameters\": {\n \"timezone\": \"America/New_York\"\n }\n }\n ],\n \"tool_choice\": {\n \"type\": \"auto\"\n }\n}")
.asString();<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.phaseo.app/v1/messages",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'model' => 'anthropic/claude-sonnet-4',
'max_tokens' => 512,
'stream' => false,
'messages' => [
[
'role' => 'user',
'content' => 'What time is it in America/New_York right now?'
]
],
'tools' => [
[
'type' => 'gateway:datetime',
'parameters' => [
'timezone' => 'America/New_York'
]
]
],
'tool_choice' => [
'type' => 'auto'
]
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}require 'uri'
require 'net/http'
url = URI("https://api.phaseo.app/v1/messages")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"model\": \"anthropic/claude-sonnet-4\",\n \"max_tokens\": 512,\n \"stream\": false,\n \"messages\": [\n {\n \"role\": \"user\",\n \"content\": \"What time is it in America/New_York right now?\"\n }\n ],\n \"tools\": [\n {\n \"type\": \"gateway:datetime\",\n \"parameters\": {\n \"timezone\": \"America/New_York\"\n }\n }\n ],\n \"tool_choice\": {\n \"type\": \"auto\"\n }\n}"
response = http.request(request)
puts response.read_body{
"id": "<string>",
"type": "<string>",
"role": "assistant",
"model": "<string>",
"content": [
{
"type": "text",
"text": "<string>",
"cache_control": {
"type": "<string>",
"ttl": "<string>",
"scope": "<string>"
},
"source": {
"type": "<string>",
"media_type": "<string>",
"data": "<string>",
"url": "<string>"
},
"id": "<string>",
"name": "<string>",
"input": {},
"tool_use_id": "<string>",
"content": "<string>"
}
],
"stop_reason": "<string>",
"stop_sequence": "<string>",
"usage": {
"input_tokens": 123,
"cache_read_input_tokens": 123,
"cache_creation_input_tokens": 123,
"cache_creation": {
"ephemeral_5m_input_tokens": 123,
"ephemeral_1h_input_tokens": 123
},
"output_tokens": 123,
"cost": 0.001234567,
"cost_details": {
"upstream_inference_cost": null
}
},
"context_management": {
"applied_edits": [
{}
]
}
}将 Anthropic Messages API 请求体发送到
系统消息可以是字符串或文本块。它必须紧跟在用户轮次之后(或紧跟在以服务器工具结果结尾的助手轮次之后),并且必须是最后一条消息,或者后面紧跟一个助手轮次。放在其他位置会返回
服务器工具调用会计入
/v1/messages,即可为 Phaseo 路由的任何模型获得 Anthropic 格式的响应。支持 Anthropic 的工具调用字段,X-Phaseo-Strictness 标头控制如何处理不支持的参数。
设置 stream: true 可接收 Anthropic 格式的服务器发送事件(message_start、content_block_*、message_delta、message_stop)。流式传输在工具循环中同样可用。
对话中途的系统消息
在messages 中添加一条 role: "system" 消息,作为从对话中该位置开始生效的指令。顶层 system 字段保持不变,因此缓存的前缀仍能命中。
curl https://api.phaseo.app/v1/messages \
-H "Authorization: Bearer $PHASEO_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "anthropic/claude-opus-5.5",
"max_tokens": 512,
"system": "You are a code review assistant.",
"messages": [
{ "role": "user", "content": "Review process() in utils.py." },
{ "role": "assistant", "content": "The loop is fine; the regex is compiled on every call." },
{ "role": "user", "content": "Now review the code that calls it." },
{ "role": "system", "content": "From now on, include type annotations in every suggestion." }
]
}'
400。不支持 clear_at、output_config 以及 tool_addition 或 tool_removal 块。
| 模型 | 如何接收系统消息 |
|---|---|
| Claude Fable 5 和 5.1、Mythos 5 和 5.1、Opus 4.8、Opus 5 和 5.5、Sonnet 5.5、Haiku 5.5 | 保留在原位置 |
| 其他 Claude 模型(包括 Sonnet 5),以及 Microsoft Foundry 上的所有 Claude 模型 | 合并到顶层系统提示中 |
| 其他提供商的模型 | 作为系统消息,与发送到 Chat Completions 的系统消息相同 |
使用服务器工具
在tools 中添加一个 phaseo: 工具,模型调用它时由 Phaseo 运行:Phaseo 会在上游将其改写为 Anthropic 的工具格式,并在服务器端继续工具循环。/v1/messages 接受除 phaseo:apply_patch 以外的所有服务器工具;服务器工具指南列出了这些工具。
curl https://api.phaseo.app/v1/messages \
-H "Authorization: Bearer $PHASEO_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "anthropic/claude-sonnet-5.5",
"max_tokens": 512,
"messages": [
{ "role": "user", "content": "What time is it in America/New_York right now?" }
],
"tools": [
{ "type": "phaseo:datetime", "parameters": { "timezones": ["America/New_York"] } }
],
"tool_choice": { "type": "auto" }
}'
usage.server_tool_use。旧的 gateway:datetime 类型仍然可用。设置 stream: true 时,Phaseo 会自行运行工具轮次,然后将后续回答作为单个流发送给你。
使用 Anthropic 原生网页搜索
当路由到的 Anthropic 模型和提供商支持原生网页搜索时,可直接传入 Anthropic 的工具定义,例如type: "web_search_20250305"。执行前,提供商路由仍会筛选支持 web_search_options 的提供商。
curl https://api.phaseo.app/v1/messages \
-H "Authorization: Bearer $PHASEO_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "anthropic/claude-sonnet-5.5",
"max_tokens": 512,
"messages": [
{ "role": "user", "content": "Find the latest Anthropic web search guidance." }
],
"tools": [
{
"type": "web_search_20250305",
"name": "web_search",
"max_uses": 3,
"allowed_domains": ["docs.anthropic.com"]
}
],
"tool_choice": { "type": "auto" }
}'
请求体
application/json
以 null 发送的可选顶层字段会被视为未设置,与省略该字段相同。
在 model 之后按顺序尝试的可选回退模型。model 为必填项,且始终是主模型。当某次尝试以 429、500、502、503 或 504 失败时,会尝试下一项;其他错误按原样返回。最多接受 25 个模型 ID 或预设;重复项以及与 model 相同的项会被忽略。每次尝试都会作为独立请求进行验证、记录和计费,响应中的 model 字段和路由追踪会显示实际处理请求的模型。流式请求仅在发送第一个字节之前回退,视频和音乐任务仅在提交时回退。Batch、realtime 和 council 请求会拒绝 models。动态路由上设置的回退优先,且 phaseo/auto 不能与 models 一起使用。
Required array length:
1 - 25 elementsMinimum string length:
1示例:
[
"anthropic/claude-sonnet-5-5",
"google/gemini-2.5-flash"
]
此请求使用的网关插件。web 为任意模型添加网页搜索,web-fetch 添加网页读取;response-healing 修复非流式结构化响应中格式错误的 JSON。工作区和预设的默认值会与此列表合并。未知的插件 id 返回 400 unknown_gateway_plugin。
向请求添加 phaseo:web_search 服务器工具,让任意模型都能搜索网页。请求的运行方式以及搜索的计数和计费,与使用 phaseo:web_search 工具完全相同。如果请求已包含网页搜索工具,插件不会添加任何内容。其他选项会被忽略。
- Option 1
- Option 2
- Option 3
Show child attributes
Show child attributes
响应
消息响应
最后修改于 2026年11月5日
⌘I
创建消息
const options = {
method: 'POST',
headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'},
body: JSON.stringify({
model: 'anthropic/claude-sonnet-4',
max_tokens: 512,
stream: false,
messages: [{role: 'user', content: 'What time is it in America/New_York right now?'}],
tools: [{type: 'gateway:datetime', parameters: {timezone: 'America/New_York'}}],
tool_choice: {type: 'auto'}
})
};
fetch('https://api.phaseo.app/v1/messages', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));import requests
url = "https://api.phaseo.app/v1/messages"
payload = {
"model": "anthropic/claude-sonnet-4",
"max_tokens": 512,
"stream": False,
"messages": [
{
"role": "user",
"content": "What time is it in America/New_York right now?"
}
],
"tools": [
{
"type": "gateway:datetime",
"parameters": { "timezone": "America/New_York" }
}
],
"tool_choice": { "type": "auto" }
}
headers = {
"Authorization": "Bearer <token>",
"Content-Type": "application/json"
}
response = requests.post(url, json=payload, headers=headers)
print(response.text)curl --request POST \
--url https://api.phaseo.app/v1/messages \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"model": "anthropic/claude-sonnet-4",
"max_tokens": 512,
"stream": false,
"messages": [
{
"role": "user",
"content": "What time is it in America/New_York right now?"
}
],
"tools": [
{
"type": "gateway:datetime",
"parameters": {
"timezone": "America/New_York"
}
}
],
"tool_choice": {
"type": "auto"
}
}
'package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.phaseo.app/v1/messages"
payload := strings.NewReader("{\n \"model\": \"anthropic/claude-sonnet-4\",\n \"max_tokens\": 512,\n \"stream\": false,\n \"messages\": [\n {\n \"role\": \"user\",\n \"content\": \"What time is it in America/New_York right now?\"\n }\n ],\n \"tools\": [\n {\n \"type\": \"gateway:datetime\",\n \"parameters\": {\n \"timezone\": \"America/New_York\"\n }\n }\n ],\n \"tool_choice\": {\n \"type\": \"auto\"\n }\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.phaseo.app/v1/messages")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"model\": \"anthropic/claude-sonnet-4\",\n \"max_tokens\": 512,\n \"stream\": false,\n \"messages\": [\n {\n \"role\": \"user\",\n \"content\": \"What time is it in America/New_York right now?\"\n }\n ],\n \"tools\": [\n {\n \"type\": \"gateway:datetime\",\n \"parameters\": {\n \"timezone\": \"America/New_York\"\n }\n }\n ],\n \"tool_choice\": {\n \"type\": \"auto\"\n }\n}")
.asString();<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.phaseo.app/v1/messages",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'model' => 'anthropic/claude-sonnet-4',
'max_tokens' => 512,
'stream' => false,
'messages' => [
[
'role' => 'user',
'content' => 'What time is it in America/New_York right now?'
]
],
'tools' => [
[
'type' => 'gateway:datetime',
'parameters' => [
'timezone' => 'America/New_York'
]
]
],
'tool_choice' => [
'type' => 'auto'
]
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}require 'uri'
require 'net/http'
url = URI("https://api.phaseo.app/v1/messages")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"model\": \"anthropic/claude-sonnet-4\",\n \"max_tokens\": 512,\n \"stream\": false,\n \"messages\": [\n {\n \"role\": \"user\",\n \"content\": \"What time is it in America/New_York right now?\"\n }\n ],\n \"tools\": [\n {\n \"type\": \"gateway:datetime\",\n \"parameters\": {\n \"timezone\": \"America/New_York\"\n }\n }\n ],\n \"tool_choice\": {\n \"type\": \"auto\"\n }\n}"
response = http.request(request)
puts response.read_body{
"id": "<string>",
"type": "<string>",
"role": "assistant",
"model": "<string>",
"content": [
{
"type": "text",
"text": "<string>",
"cache_control": {
"type": "<string>",
"ttl": "<string>",
"scope": "<string>"
},
"source": {
"type": "<string>",
"media_type": "<string>",
"data": "<string>",
"url": "<string>"
},
"id": "<string>",
"name": "<string>",
"input": {},
"tool_use_id": "<string>",
"content": "<string>"
}
],
"stop_reason": "<string>",
"stop_sequence": "<string>",
"usage": {
"input_tokens": 123,
"cache_read_input_tokens": 123,
"cache_creation_input_tokens": 123,
"cache_creation": {
"ephemeral_5m_input_tokens": 123,
"ephemeral_1h_input_tokens": 123
},
"output_tokens": 123,
"cost": 0.001234567,
"cost_details": {
"upstream_inference_cost": null
}
},
"context_management": {
"applied_edits": [
{}
]
}
}