找回密码
立即注册
搜索
热搜: Java Python Linux Go
发回帖 发新帖

2318

积分

0

好友

328

主题
发表于 10 小时前 | 查看: 0| 回复: 0

Claude品牌标志

当你的 MCP 工具越来越多,Claude 的上下文窗口却被工具定义吃掉大半?Anthropic 最新的 Tool Search 功能来了!

痛点:工具越多,上下文越少

如果你是 Claude Code 或 MCP 的重度用户,一定遇到过这个问题:

连接 5 个常用 MCP 服务器,工具定义就吃掉 55K+ tokens

MCP 服务器 工具数量 Token 消耗
GitHub 35 个工具 ~26K tokens
Slack 11 个工具 ~21K tokens
Sentry 5 个工具 ~3K tokens
Grafana 5 个工具 ~3K tokens
Splunk 2 个工具 ~2K tokens
合计 58 个工具 ~55K tokens

如果再加上 Jira(~17K tokens),你还没开始对话,上下文窗口就已经被占用了 100K+ tokens

Anthropic 内部测试显示,有些配置的工具定义甚至达到了惊人的 134K tokens

这不仅浪费了宝贵的上下文空间,还导致了另一个问题:工具选择准确率下降。当 Claude 面对几十个相似名称的工具时(比如 notification-send-user vs notification-send-channel),很容易选错。

解决方案:Tool Search 渐进式加载

Anthropic 在 2025 年底推出了 Tool Search Tool,核心思路是:

不再一次性加载所有工具定义,而是按需动态发现和加载。

工作原理

传统方式:
┌─────────────────────────────────────────┐
│启动时加载全部50+工具定义(~72K tokens)│
│+系统提示+对话历史│
│=上下文消耗~77K tokens(还没开始工作)│
└─────────────────────────────────────────┘

ToolSearch方式:
┌─────────────────────────────────────────┐
│启动时只加载ToolSearchTool(~500 tokens)│
│+用到时按需加载3-5个相关工具(~3K tokens)│
│=上下文消耗~8.7K tokens                 │
│=节省95%上下文空间!│
└─────────────────────────────────────────┘

实测效果

指标 优化前 优化后 提升
工具定义 Token 消耗 ~72K ~8.7K 88% 减少
Opus 4 工具选择准确率 49% 74% +25%
Opus 4.5 工具选择准确率 79.5% 88.1% +8.6%
每月 API 成本(1万请求/天) $13,500 $1,530 节省 $11,970

如何配置

1. API 调用方式(开发者)

在 API 调用中添加 defer_loading: true 标记:

{
  "tools":[
    {
      "type":"tool_search_tool_regex_20251119",
      "name":"tool_search_tool_regex"
    },
    {
      "name":"github.createPullRequest",
      "description":"Create a pull request",
      "input_schema":{...},
      "defer_loading":true
    }
  ]
}

2. MCP 服务器级别配置

可以对整个 MCP 服务器启用延迟加载,同时保留高频工具常驻:

{
  "type":"mcp_toolset",
  "mcp_server_name":"google-drive",
  "default_config":{"defer_loading":true},
  "configs":{
    "search_files":{
      "defer_loading":false
    }
  }
}

3. Claude Code 本地配置

~/.claude/settings.json 中配置:

{
  "mcpServers":{
    "filesystem":{
      "type":"stdio",
      "command":"npx",
      "args":["@modelcontextprotocol/server-filesystem"],
      "defer_loading":true
    },
    "github":{
      "type":"http",
      "url":"https://mcp.github.com/mcp",
      "defer_loading":true
    },
    "search":{
      "type":"stdio",
      "command":"npx",
      "args":["@modelcontextprotocol/server-web-search"],
      "defer_loading":false
    }
  },
  "env":{
    "ENABLE_TOOL_SEARCH":"true"
  }
}

最佳实践

  • 工具定义超过 10K tokens
  • 连接了 3 个以上 MCP 服务器
  • 拥有 10+ 个工具
  • 遇到工具选择不准确的问题

什么时候不需要

  • 工具库很小(< 10 个工具)
  • 所有工具每次会话都会用到
  • 工具定义本身很紧凑

优化工具描述

Tool Search 基于工具名称和描述进行匹配,清晰的描述能提高发现准确率:

// 推荐
{
  "name":"search_customer_orders",
  "description":"Search for customer orders by date range, status, or total amount. Returns order details including items, shipping, and payment info."
}

// 不推荐
{
  "name":"query_db_orders",
  "description":"Execute order query"
}

系统提示引导

在系统提示中告诉 Claude 可用的工具类别:

You have access to tools for Slack messaging, Google Drive file management,
Jira ticket tracking, and GitHub repository operations.
Use the tool search to find specific capabilities.

配套功能:Programmatic Tool Calling

除了 Tool Search,Anthropic 还推出了 Programmatic Tool Calling——让 Claude 用代码而非自然语言来编排工具调用。

场景示例

任务:查找 Q3 超出差旅预算的团队成员

传统方式:

  1. 获取团队成员 → 20 人
  2. 为每人获取费用明细 → 20 次工具调用,每次返回 50-100 条记录
  3. 获取预算限额
  4. 结果:2000+ 条费用记录全部进入上下文(50KB+)

Programmatic Tool Calling:

team = await get_team_members("engineering")
budgets = {level: await get_budget_by_level(level)
           for level in set(m["level"] for m in team)}

expenses = await asyncio.gather(*[
    get_expenses(m["id"], "Q3") for m in team
])

exceeded = [
    {"name": m["name"], "spent": sum(e["amount"] for e in exp),
     "limit": budgets[m["level"]]["travel_limit"]}
    for m, exp in zip(team, expenses)
    if sum(e["amount"] for e in exp) > budgets[m["level"]]["travel_limit"]
]
print(json.dumps(exceeded))

结果:Claude 只看到最终的 2-3 个超标人员信息(~1KB),而不是 2000+ 条原始数据!

效果

指标 传统方式 Programmatic Tool Calling
Token 消耗 43,588 27,297 (-37%)
知识检索准确率 25.6% 28.5%
GIA 基准测试 46.5% 51.2%

更进一步:Tool Use Examples

JSON Schema 只能定义结构,无法表达使用模式。通过提供示例,Claude 能学会:

  • 日期格式应该用 YYYY-MM-DD
  • 用户 ID 格式是 USR-XXXXX
  • 什么情况下需要填写完整信息,什么情况下只需要标题
{
  "name":"create_ticket",
  "input_schema":{...},
  "input_examples":[
    {
      "title":"Login page returns 500 error",
      "priority":"critical",
      "labels":["bug","authentication","production"],
      "reporter":{"id":"USR-12345","name":"Jane Smith"},
      "escalation":{"level":2,"notify_manager":true,"sla_hours":4}
    },
  {
   "title":"Add dark mode support",
   "labels":["feature-request","ui"]
  },
{
  "title":"Update API documentation"
  }
]
}

内部测试显示,Tool Use Examples 将复杂参数处理的准确率从 72% 提升到 90%

总结

功能 解决的问题 效果
Tool Search 工具定义占用过多上下文 Token 节省 85-95%
Programmatic Tool Calling 中间结果污染上下文 Token 节省 37%,准确率提升
Tool Use Examples 参数填写不准确 准确率从 72% → 90%

三者可以组合使用:

  • Tool Search 确保找到正确的工具
  • Programmatic Tool Calling 确保高效执行
  • Tool Use Examples 确保参数正确

快速开始

client.beta.messages.create(
    betas=["advanced-tool-use-2025-11-20"],
    model="claude-sonnet-4-5-20250929",
    max_tokens=4096,
    tools=[
      {"type":"tool_search_tool_regex_20251119","name":"tool_search_tool_regex"},
      {"type":"code_execution_20250825","name":"code_execution"},
      # 你的工具,带上 defer_loading、allowed_callers、input_examples
    ]
)

参考资料

  • Anthropic 官方博客:Advanced Tool Use^1
  • Tool Search Tool 官方文档^2
  • Programmatic Tool Calling Cookbook^3

希望这篇关于 人工智能 Agent 工具链优化的深度解析对你有帮助。想了解更多类似的技术实战干货,欢迎访问云栈社区进行交流探讨。




上一篇:Node.js 多个LTS版本曝高危安全漏洞,需紧急升级至安全版本
下一篇:单片机开发必备:10款提升效率的VS Code插件推荐
您需要登录后才可以回帖 登录 | 立即注册

手机版|小黑屋|网站地图|云栈社区 ( 苏ICP备2022046150号-2 )

GMT+8, 2026-1-18 16:28 , Processed in 0.307803 second(s), 42 queries , Gzip On.

Powered by Discuz! X3.5

© 2025-2026 云栈社区.

快速回复 返回顶部 返回列表