Loading...
    • 开发者指南
    • API 参考
    • MCP
    • 资源
    • 发布说明
    Search...
    ⌘K

    第一步

    Claude 简介快速开始

    模型与定价

    模型概览选择模型Claude 4.5 的新功能迁移到 Claude 4.5模型弃用定价

    使用 Claude 构建

    功能概览使用 Messages API上下文窗口提示词最佳实践

    功能

    提示词缓存上下文编辑扩展思考流式消息批处理引用多语言支持Token 计数嵌入向量视觉PDF 支持Files API搜索结果Google Sheets 插件

    工具

    概述如何实现工具使用令牌高效的工具使用细粒度工具流式传输Bash 工具代码执行工具计算机使用工具文本编辑器工具Web fetch 工具网络搜索工具记忆工具

    代理技能

    概述在 API 中开始使用 Agent Skills技能创作最佳实践通过 API 使用 Agent Skills

    Agent SDK

    概览TypeScript SDKPython SDK

    指南

    流式输入处理权限会话管理托管 Agent SDK修改系统提示词SDK 中的 MCP自定义工具SDK 中的子代理SDK 中的斜杠命令SDK 中的代理技能跟踪成本和使用情况待办事项列表SDK 中的插件

    API 中的 MCP

    MCP 连接器远程 MCP 服务器

    Claude 在第三方平台上

    Amazon BedrockVertex AI

    提示词工程

    概述提示词生成器使用提示模板提示词改进器保持清晰和直接使用示例(多示例提示)让 Claude 思考(思维链)使用XML标签给Claude分配角色(系统提示)预填充 Claude 的响应链式复杂提示长文本技巧扩展思考技巧

    测试与评估

    定义成功标准开发测试用例使用评估工具减少延迟

    加强防护措施

    减少幻觉提高输出一致性缓解越狱handle-streaming-refusals减少提示词泄露保持Claude的角色特征

    管理和监控

    Admin API 概述使用量和成本 APIClaude Code 分析 API
    Console
    工具

    网络搜索工具

    网络搜索工具使 Claude 能够直接访问实时网络内容,允许它使用超出其知识截断日期的最新信息来回答问题。Claude 会自动在其答案中引用搜索结果中的来源。

    请通过我们的反馈表单分享您对网络搜索工具的使用体验。

    支持的模型

    网络搜索可用于:

    • Claude Sonnet 4.5 (claude-sonnet-4-5-20250929)
    • Claude Sonnet 4 (claude-sonnet-4-20250514)
    • Claude Sonnet 3.7 (已弃用) (claude-3-7-sonnet-20250219)
    • Claude Haiku 4.5 (claude-haiku-4-5-20251001)
    • Claude Haiku 3.5 (claude-3-5-haiku-latest)
    • Claude Opus 4.1 (claude-opus-4-1-20250805)
    • Claude Opus 4 (claude-opus-4-20250514)

    网络搜索工作原理

    当您在 API 请求中添加网络搜索工具时:

    1. Claude 根据提示决定何时进行搜索。
    2. API 执行搜索并向 Claude 提供结果。此过程可能在单个请求中重复多次。
    3. 在其回合结束时,Claude 提供带有引用来源的最终响应。

    如何使用网络搜索

    您组织的管理员必须在控制台中启用网络搜索。

    在您的 API 请求中提供网络搜索工具:

    Shell
    curl https://api.anthropic.com/v1/messages \
        --header "x-api-key: $ANTHROPIC_API_KEY" \
        --header "anthropic-version: 2023-06-01" \
        --header "content-type: application/json" \
        --data '{
            "model": "claude-sonnet-4-5",
            "max_tokens": 1024,
            "messages": [
                {
                    "role": "user",
                    "content": "What's the weather in NYC?"
                }
            ],
            "tools": [{
                "type": "web_search_20250305",
                "name": "web_search",
                "max_uses": 5
            }]
        }'
    Python
    import anthropic
    
    client = anthropic.Anthropic()
    
    response = client.messages.create(
        model="claude-sonnet-4-5",
        max_tokens=1024,
        messages=[
            {
                "role": "user",
                "content": "What's the weather in NYC?"
            }
        ],
        tools=[{
            "type": "web_search_20250305",
            "name": "web_search",
            "max_uses": 5
        }]
    )
    print(response)
    TypeScript
    import { Anthropic } from '@anthropic-ai/sdk';
    
    const anthropic = new Anthropic();
    
    async function main() {
      const response = await anthropic.messages.create({
        model: "claude-sonnet-4-5",
        max_tokens: 1024,
        messages: [
          {
            role: "user",
            content: "What's the weather in NYC?"
          }
        ],
        tools: [{
          type: "web_search_20250305",
          name: "web_search",
          max_uses: 5
        }]
      });
    
      console.log(response);
    }
    
    main().catch(console.error);

    工具定义

    网络搜索工具支持以下参数:

    JSON
    {
      "type": "web_search_20250305",
      "name": "web_search",
    
      // 可选:限制每个请求的搜索次数
      "max_uses": 5,
    
      // 可选:仅包含来自这些域的结果
      "allowed_domains": ["example.com", "trusteddomain.org"],
    
      // 可选:永远不包含来自这些域的结果
      "blocked_domains": ["untrustedsource.com"],
    
      // 可选:本地化搜索结果
      "user_location": {
        "type": "approximate",
        "city": "San Francisco",
        "region": "California",
        "country": "US",
        "timezone": "America/Los_Angeles"
      }
    }

    最大使用次数

    max_uses 参数限制执行的搜索次数。如果 Claude 尝试的搜索次数超过允许的次数,web_search_tool_result 将是一个包含 max_uses_exceeded 错误代码的错误。

    域名过滤

    使用域名过滤器时:

    • 域名不应包含 HTTP/HTTPS 方案(使用 example.com 而不是 https://example.com)
    • 子域名会自动包含(example.com 涵盖 docs.example.com)
    • 支持子路径(example.com/blog)
    • 您可以使用 allowed_domains 或 blocked_domains,但不能在同一请求中同时使用两者。

    请求级别的域名限制必须与在控制台中配置的组织级别域名限制兼容。请求级别的域名只能进一步限制域名,不能覆盖或超出组织级别列表。如果您的请求包含与组织设置冲突的域名,API 将返回验证错误。

    本地化

    user_location 参数允许您根据用户的位置本地化搜索结果。

    • type:位置类型(必须为 approximate)
    • city:城市名称
    • region:地区或州
    • country:国家
    • timezone:IANA 时区 ID。

    响应

    以下是响应结构的示例:

    {
      "role": "assistant",
      "content": [
        // 1. Claude 的搜索决定
        {
          "type": "text",
          "text": "I'll search for when Claude Shannon was born."
        },
        // 2. 使用的搜索查询
        {
          "type": "server_tool_use",
          "id": "srvtoolu_01WYG3ziw53XMcoyKL4XcZmE",
          "name": "web_search",
          "input": {
            "query": "claude shannon birth date"
          }
        },
        // 3. 搜索结果
        {
          "type": "web_search_tool_result",
          "tool_use_id": "srvtoolu_01WYG3ziw53XMcoyKL4XcZmE",
          "content": [
            {
              "type": "web_search_result",
              "url": "https://en.wikipedia.org/wiki/Claude_Shannon",
              "title": "Claude Shannon - Wikipedia",
              "encrypted_content": "EqgfCioIARgBIiQ3YTAwMjY1Mi1mZjM5LTQ1NGUtODgxNC1kNjNjNTk1ZWI3Y...",
              "page_age": "April 30, 2025"
            }
          ]
        },
        {
          "text": "Based on the search results, ",
          "type": "text"
        },
        // 4. Claude 的带有引用的响应
        {
          "text": "Claude Shannon was born on April 30, 1916, in Petoskey, Michigan",
          "type": "text",
          "citations": [
            {
              "type": "web_search_result_location",
              "url": "https://en.wikipedia.org/wiki/Claude_Shannon",
              "title": "Claude Shannon - Wikipedia",
              "encrypted_index": "Eo8BCioIAhgBIiQyYjQ0OWJmZi1lNm..",
              "cited_text": "Claude Elwood Shannon (April 30, 1916 – February 24, 2001) was an American mathematician, electrical engineer, computer scientist, cryptographer and i..."
            }
          ]
        }
      ],
      "id": "msg_a930390d3a",
      "usage": {
        "input_tokens": 6039,
        "output_tokens": 931,
        "server_tool_use": {
          "web_search_requests": 1
        }
      },
      "stop_reason": "end_turn"
    }

    搜索结果

    搜索结果包括:

    • url:源页面的 URL
    • title:源页面的标题
    • page_age:网站最后更新的时间
    • encrypted_content:加密内容,必须在多轮对话中传回以用于引用

    引用

    引用始终为网络搜索启用,每个 web_search_result_location 包括:

    • url:引用来源的 URL
    • title:引用来源的标题
    • encrypted_index:必须为多轮对话传回的参考。
    • cited_text:最多 150 个字符的引用内容

    网络搜索引用字段 cited_text、title 和 url 不计入输入或输出令牌使用量。

    当直接向最终用户显示 API 输出时,必须包含对原始来源的引用。如果您对 API 输出进行修改,包括在显示给最终用户之前重新处理和/或将其与您自己的材料结合,请根据与您的法律团队的协商适当显示引用。

    错误

    当网络搜索工具遇到错误(例如达到速率限制)时,Claude API 仍然返回 200(成功)响应。错误在响应体中使用以下结构表示:

    {
      "type": "web_search_tool_result",
      "tool_use_id": "servertoolu_a93jad",
      "content": {
        "type": "web_search_tool_result_error",
        "error_code": "max_uses_exceeded"
      }
    }

    这些是可能的错误代码:

    • too_many_requests:超过速率限制
    • invalid_input:无效的搜索查询参数
    • max_uses_exceeded:超过最大网络搜索工具使用次数
    • query_too_long:查询超过最大长度
    • unavailable:发生内部错误

    pause_turn 停止原因

    响应可能包括 pause_turn 停止原因,这表示 API 暂停了长时间运行的回合。您可以在后续请求中按原样提供响应以让 Claude 继续其回合,或者如果您希望中断对话,可以修改内容。

    提示缓存

    网络搜索与提示缓存配合使用。要启用提示缓存,请在您的请求中添加至少一个 cache_control 断点。系统将在执行工具时自动缓存到最后一个 web_search_tool_result 块。

    对于多轮对话,在最后一个 web_search_tool_result 块上或之后设置 cache_control 断点以重用缓存内容。

    例如,为多轮对话使用提示缓存和网络搜索:

    import anthropic
    
    client = anthropic.Anthropic()
    
    # 第一个请求,带有网络搜索和缓存断点
    messages = [
        {
            "role": "user",
            "content": "What's the current weather in San Francisco today?"
        }
    ]
    
    response1 = client.messages.create(
        model="claude-sonnet-4-5",
        max_tokens=1024,
        messages=messages,
        tools=[{
            "type": "web_search_20250305",
            "name": "web_search",
            "user_location": {
                "type": "approximate",
                "city": "San Francisco",
                "region": "California",
                "country": "US",
                "timezone": "America/Los_Angeles"
            }
        }]
    )
    
    # 将 Claude 的响应添加到对话中
    messages.append({
        "role": "assistant",
        "content": response1.content
    })
    
    # 第二个请求,在搜索结果后有缓存断点
    messages.append({
        "role": "user",
        "content": "Should I expect rain later this week?",
        "cache_control": {"type": "ephemeral"}  # 缓存到此点
    })
    
    response2 = client.messages.create(
        model="claude-sonnet-4-5",
        max_tokens=1024,
        messages=messages,
        tools=[{
            "type": "web_search_20250305",
            "name": "web_search",
            "user_location": {
                "type": "approximate",
                "city": "San Francisco",
                "region": "California",
                "country": "US",
                "timezone": "America/Los_Angeles"
            }
        }]
    )
    # 第二个响应将受益于缓存的搜索结果
    # 同时仍然能够在需要时执行新搜索
    print(f"Cache read tokens: {response2.usage.get('cache_read_input_tokens', 0)}")

    流式传输

    启用流式传输后,您将接收搜索事件作为流的一部分。搜索执行时会有一个暂停:

    event: message_start
    data: {"type": "message_start", "message": {"id": "msg_abc123", "type": "message"}}
    
    event: content_block_start
    data: {"type": "content_block_start", "index": 0, "content_block": {"type": "text", "text": ""}}
    
    // Claude 的搜索决定
    
    event: content_block_start
    data: {"type": "content_block_start", "index": 1, "content_block": {"type": "server_tool_use", "id": "srvtoolu_xyz789", "name": "web_search"}}
    
    // 搜索查询流式传输
    event: content_block_delta
    data: {"type": "content_block_delta", "index": 1, "delta": {"type": "input_json_delta", "partial_json": "{\"query\":\"latest quantum computing breakthroughs 2025\"}"}}
    
    // 搜索执行时暂停
    
    // 搜索结果流式传输
    event: content_block_start
    data: {"type": "content_block_start", "index": 2, "content_block": {"type": "web_search_tool_result", "tool_use_id": "srvtoolu_xyz789", "content": [{"type": "web_search_result", "title": "Quantum Computing Breakthroughs in 2025", "url": "https://example.com"}]}}
    
    // Claude 的带有引用的响应(本示例中省略)

    批量请求

    您可以在消息批处理 API中包含网络搜索工具。通过消息批处理 API 的网络搜索工具调用的定价与常规消息 API 请求中的定价相同。

    使用情况和定价

    Web search usage is charged in addition to token usage:

    "usage": {
      "input_tokens": 105,
      "output_tokens": 6039,
      "cache_read_input_tokens": 7123,
      "cache_creation_input_tokens": 7345,
      "server_tool_use": {
        "web_search_requests": 1
      }
    }
    

    Web search is available on the Claude API for $10 per 1,000 searches, plus standard token costs for search-generated content. Web search results retrieved throughout a conversation are counted as input tokens, in search iterations executed during a single turn and in subsequent conversation turns.

    Each web search counts as one use, regardless of the number of results returned. If an error occurs during web search, the web search will not be billed.

      © 2025 ANTHROPIC PBC

      Products

      • Claude
      • Claude Code
      • Max plan
      • Team plan
      • Enterprise plan
      • Download app
      • Pricing
      • Log in

      Features

      • Claude and Slack
      • Claude in Excel

      Models

      • Opus
      • Sonnet
      • Haiku

      Solutions

      • AI agents
      • Code modernization
      • Coding
      • Customer support
      • Education
      • Financial services
      • Government
      • Life sciences

      Claude Developer Platform

      • Overview
      • Developer docs
      • Pricing
      • Amazon Bedrock
      • Google Cloud’s Vertex AI
      • Console login

      Learn

      • Blog
      • Catalog
      • Courses
      • Use cases
      • Connectors
      • Customer stories
      • Engineering at Anthropic
      • Events
      • Powered by Claude
      • Service partners
      • Startups program

      Company

      • Anthropic
      • Careers
      • Economic Futures
      • Research
      • News
      • Responsible Scaling Policy
      • Security and compliance
      • Transparency

      Help and security

      • Availability
      • Status
      • Support center

      Terms and policies

      • Privacy policy
      • Responsible disclosure policy
      • Terms of service: Commercial
      • Terms of service: Consumer
      • Usage policy

      Products

      • Claude
      • Claude Code
      • Max plan
      • Team plan
      • Enterprise plan
      • Download app
      • Pricing
      • Log in

      Features

      • Claude and Slack
      • Claude in Excel

      Models

      • Opus
      • Sonnet
      • Haiku

      Solutions

      • AI agents
      • Code modernization
      • Coding
      • Customer support
      • Education
      • Financial services
      • Government
      • Life sciences

      Claude Developer Platform

      • Overview
      • Developer docs
      • Pricing
      • Amazon Bedrock
      • Google Cloud’s Vertex AI
      • Console login

      Learn

      • Blog
      • Catalog
      • Courses
      • Use cases
      • Connectors
      • Customer stories
      • Engineering at Anthropic
      • Events
      • Powered by Claude
      • Service partners
      • Startups program

      Company

      • Anthropic
      • Careers
      • Economic Futures
      • Research
      • News
      • Responsible Scaling Policy
      • Security and compliance
      • Transparency

      Help and security

      • Availability
      • Status
      • Support center

      Terms and policies

      • Privacy policy
      • Responsible disclosure policy
      • Terms of service: Commercial
      • Terms of service: Consumer
      • Usage policy
      © 2025 ANTHROPIC PBC