Claude 内置联网搜索 (web_search_20250305)
POST /v1/messages
说明
概述
Anthropic Claude 通过 web_search_20250305 服务端工具提供原生联网搜索 + 引用面板。OpenAI-Hub 完全透传 Anthropic 协议;当渠道不支持原生搜索时,会自动回退到 web_search_baidu 替代实现,对客户端完全无感。
支持模型:claude-sonnet-4、claude-opus-4、claude-haiku-4-5 等所有 Claude 4.x 系列。
请求示例
1. 最小用法
curl https://api.openai-hub.com/v1/messages \
-H "x-api-key: $API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-sonnet-4",
"max_tokens": 1024,
"messages": [
{ "role": "user", "content": "巴菲特 2026 致股东信里提到的核心观点?" }
],
"tools": [
{ "type": "web_search_20250305", "name": "web_search" }
]
}'
2. 限制次数 + 域名白名单 + 地理位置
curl https://api.openai-hub.com/v1/messages \
-H "x-api-key: $API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-sonnet-4",
"max_tokens": 2048,
"stream": true,
"messages": [
{ "role": "user", "content": "比较 ClickHouse 和 Doris 最新版本性能" }
],
"tools": [
{
"type": "web_search_20250305",
"name": "web_search",
"max_uses": 3,
"allowed_domains": ["clickhouse.com", "doris.apache.org", "github.com"],
"user_location": {
"type": "approximate",
"country": "CN"
}
}
]
}'
字段说明
| 字段 | 类型 | 默认 | 说明 |
|---|---|---|---|
type |
string | — | 固定 web_search_20250305 |
name |
string | web_search |
自定义工具名(影响 SSE 块里的回显) |
max_uses |
int | 5 | 单次对话最多调用搜索的次数;模型自主决策 |
allowed_domains |
string[] | — | 域名白名单;与 blocked_domains 互斥 |
blocked_domains |
string[] | — | 域名黑名单 |
user_location |
object | — | 地理位置提示 |
user_location.type |
string | — | 固定 approximate |
user_location.country |
string | — | ISO 3166-1 alpha-2 |
user_location.city |
string | — | 城市英文名 |
user_location.region |
string | — | 省/州英文名 |
响应(非流式)
{
"id": "msg_xxx",
"model": "claude-sonnet-4",
"type": "message",
"role": "assistant",
"content": [
{
"type": "server_tool_use",
"id": "srvtoolu_xxx",
"name": "web_search",
"input": { "query": "Buffett 2026 letter to shareholders" }
},
{
"type": "web_search_tool_result",
"tool_use_id": "srvtoolu_xxx",
"content": [
{
"type": "web_search_result",
"title": "Berkshire Hathaway 2026 Annual Letter",
"url": "https://www.berkshirehathaway.com/letters/2026.html",
"encrypted_content": "<base64...>"
}
]
},
{
"type": "text",
"text": "根据巴菲特 2026 致股东信,核心观点有三..."
}
],
"stop_reason": "end_turn",
"usage": {
"input_tokens": 120,
"output_tokens": 380,
"server_tool_use": { "web_search_requests": 1 }
}
}
流式输出
event: message_start
event: content_block_start # type=server_tool_use
event: content_block_delta # input_json_delta,逐步输出 query JSON
event: content_block_stop
event: content_block_start # type=web_search_tool_result
event: content_block_stop
event: content_block_start # type=text
event: content_block_delta # text_delta 模型回答
...
event: message_delta # 含 usage.server_tool_use.web_search_requests
event: message_stop
模型自主多轮
max_uses > 1 时,模型可能:
- 第一次搜索结果不够 → 自己再发一次更精确的查询
- 直到累计达到
max_uses或得到满意结果
每次搜索都会单独 emit 一组 server_tool_use + web_search_tool_result 块(block index 递增)。
渠道兼容自动回退
如果当前命中的渠道不是官方 Anthropic(例如自部署的 Claude 兼容代理),OpenAI-Hub 会自动用 web_search_baidu 替代实现:
- SSE 块结构完全一致(
server_tool_use+web_search_tool_result) encrypted_content留空字符串- 客户端引用面板正常渲染
请求头
| 名称 | 位置 | 类型 | 必填 | 说明 |
|---|---|---|---|---|
| Authorization | header | string | 否 |
响应
| 字段 | 类型 | 必填 | 说明 |
|---|