从Claude Sonnet 3.7开始,Claude能够以高效令牌的方式调用工具。请求平均节省14%的输出令牌,最高可达70%,这也减少了延迟。确切的令牌减少和延迟改进取决于整体响应的形状和大小。
高效令牌工具使用是Claude 3.7中的测试版功能。要使用此测试版功能,只需在工具使用请求中添加测试版标头token-efficient-tools-2025-02-19所有Claude 4模型默认支持高效令牌工具使用。不需要测试版标头,但token-efficient-tools-2025-02-19标头不会破坏API请求。
高效令牌工具使用目前不适用于disable_parallel_tool_use
以下是如何在Claude Sonnet 3.7中使用API的高效令牌工具的示例:
curl https://api.anthropic.com/v1/messages \
  -H "content-type: application/json" \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "anthropic-beta: token-efficient-tools-2025-02-19" \
  -d '{
    "model": "claude-3-7-sonnet-20250219",
    "max_tokens": 1024,
    "tools": [
      {
        "name": "get_weather",
        "description": "Get the current weather in a given location",
        "input_schema": {
          "type": "object",
          "properties": {
            "location": {
              "type": "string",
              "description": "The city and state, e.g. San Francisco, CA"
            }
          },
          "required": [
            "location"
          ]
        }
      }
    ],
    "messages": [
      {
        "role": "user",
        "content": "Tell me the weather in San Francisco."
      }
    ]
  }' | jq '.usage'
上述请求平均应该比正常请求使用更少的输入和输出令牌。要确认这一点,请尝试发出相同的请求,但从测试版标头列表中删除token-efficient-tools-2025-02-19
为了保持提示缓存的好处,请对您想要缓存的请求一致地使用测试版标头。如果您有选择性地使用它,提示缓存将失败。