Claude 联网搜索 API 的官方格式。
{
"error": {
"type": "authentication_error",
"message": "Invalid API key"
}
}| 参数 | 类型 | 必填 | 默认值 | 范围 | 描述 |
|---|---|---|---|---|---|
model | string | 是 | claude-haiku-4-5-20251001 | - | 模型标识符。此模型使用 claude-haiku-4-5-20251001 |
messages | array | 是 | - | - | 对话的输入消息。每条消息必须包含 role(user/assistant)和 content(字符串或内容块数组) |
max_tokens | integer | 是 | - | 1-64000 | 生成的最大 token 数量。模型可能会在此限制之前停止 |
tools | array | 是 | - | - | 工具对象数组。对于联网搜索,必须包含类型为 web_search_20250305 的 web_search 工具配置 |
temperature | number | 否 | 1.0 | 0.0-1.0 | 控制输出的随机性。对于分析任务使用较低的值(接近 0.0),对于创意任务使用较高的值(接近 1.0)。注意:即使在 0.0 时,结果也不是完全确定性的 |
top_p | number | 否 | - | 0.0-1.0 | 核采样阈值。通过仅考虑累积概率达到 top_p 的 token 来控制多样性。建议仅高级用户使用。不要与 temperature 一起使用 |
top_k | integer | 否 | - | >0 | 仅从每个 token 的前 K 个选项中采样。移除低概率响应。建议仅高级用户使用 |
stream | boolean | 否 | false | - | 是否使用服务器发送事件逐步流式传输响应 |
stop_sequences | array | 否 | - | 最多 8191 个序列 | 导致模型停止生成的自定义文本序列。每个序列必须包含非空白字符 |
messages 数组中的每个消息对象应具有以下结构:| 字段 | 类型 | 必填 | 范围 | 描述 |
|---|---|---|---|---|
role | string | 是 | - | 消息的角色。可以是:user 或 assistant |
content | string/array | 是 | - | 消息的内容。对于纯文本消息可以是简单字符串,对于多模态内容可以是内容块数组 |
| 字段 | 类型 | 必填 | 范围 | 描述 |
|---|---|---|---|---|
type | string | 是 | - | 必须是 text |
text | string | 是 | - | 文本内容 |
tools 数组应包含一个联网搜索工具对象:| 字段 | 类型 | 必填 | 范围 | 描述 |
|---|---|---|---|---|
type | string | 是 | - | 工具类型。联网搜索必须是 web_search_20250305 |
name | string | 是 | - | 工具名称。使用 web_search |
max_uses | integer | 否 | 1-10 | 单个请求中联网搜索工具最多可使用的次数 |
allowed_domains | array | 否 | - | 仅包含来自这些域名的搜索结果。不能与 blocked_domains 同时使用 |
blocked_domains | array | 否 | - | 永不包含来自这些域名的搜索结果。不能与 allowed_domains 同时使用 |
user_location | object | 否 | - | 根据用户位置本地化搜索结果 |
max_uses 参数限制执行的搜索次数。如果 Claude 尝试的搜索次数超过允许值,web_search_tool_result 将返回带有 max_uses_exceeded 错误码的错误。example.com 而不是 https://example.com)example.com 包含 docs.example.com)docs.example.com 仅返回来自该子域名的结果,不包含 example.com 或 api.example.com)example.com/blog)allowed_domains 或 blocked_domains,但不能在同一个请求中同时使用user_location 对象允许您根据用户位置本地化搜索结果:| 字段 | 类型 | 必填 | 范围 | 描述 |
|---|---|---|---|---|
type | string | 是 | - | 位置类型。必须是 approximate |
city | string | 是 | - | 城市名称(例如:San Francisco) |
region | string | 是 | - | 地区或州(例如:California) |
country | string | 是 | - | 国家代码(例如:US) |
timezone | string | 是 | - | IANA 时区 ID(例如:America/Los_Angeles) |
{
"type": "web_search_20250305",
"name": "web_search",
"max_uses": 5,
"allowed_domains": ["example.com", "trusteddomain.org"],
"user_location": {
"type": "approximate",
"city": "San Francisco",
"region": "California",
"country": "US",
"timezone": "America/Los_Angeles"
}
}| 错误码 | 错误名称 | 描述 |
|---|---|---|
| 401 | Unauthorized | API 密钥缺失或无效 |
| 403 | Forbidden | 您的 API 密钥没有访问此资源的权限,或请求操作的余额不足 |
| 429 | Too Many Requests | 您已超出速率限制 |
| 500 | Internal server error | 发生内部服务器错误 |
| 503 | Content policy violation | 内容因安全问题被阻止(实际状态码为 400) |