Skip to Content
模型用法Claude联网搜索

Claude 联网搜索

本页说明如何让 Claude 通过 web_search 工具联网搜索。模型 ID、接口地址与供应商说明见 Claude 系列概览。

要点Sonnet 5.5 联网搜索使用 Anthropic 原生 Messages 接口,并指定 Anthropic 供应商。其他型号请在模型页查看是否标注 Web Search。

在 tools 中加入官方的 web_search 服务端工具,模型会自行发起搜索并在回答中引用来源:

claude_web_search.py
import os import anthropic client = anthropic.Anthropic( base_url="https://api.ofox.ai/anthropic", api_key=os.environ["OFOX_API_KEY"], default_headers={"X-OfoxAI-Provider-Type": "anthropic"}, ) message = client.messages.create( model="anthropic/claude-sonnet-5.5", max_tokens=192, tools=[{"type": "web_search_20250305", "name": "web_search", "max_uses": 1, "allowed_domains": ["python.org"]}], messages=[{"role": "user", "content": "使用联网搜索在 python.org 上查找 Python 官方下载页,只搜索一次,只返回网址。"}], ) print("".join(b.text for b in message.content if b.type == "text")) print("搜索次数:", message.usage.server_tool_use.web_search_requests)
  • 响应的 content 中依次出现 server_tool_use(搜索关键词)、web_search_tool_result(搜索结果)与 text(回答)块。
  • usage.server_tool_use.web_search_requests 为实际搜索次数,搜索按次计费,单价见模型页 。
  • max_uses 限制单次请求的搜索次数。搜索结果也可能计入输入 token 费用。

其他报错见常见报错。

最后更新于