工具
令牌高效的工具使用
从 Claude Sonnet 3.7 开始,Claude 能够以令牌高效的方式调用工具。请求平均节省 14% 的输出令牌,最高可达 70%,这也降低了延迟。确切的令牌减少和延迟改进取决于整体响应形状和大小。
令牌高效的工具使用是一个测试版功能,仅适用于 Claude 3.7 Sonnet。要使用此测试版功能,请将测试版标头 token-efficient-tools-2025-02-19 添加到工具使用请求中。此标头对其他 Claude 模型没有影响。
所有 Claude 4 模型 默认支持令牌高效的工具使用。不需要测试版标头。
令牌高效的工具使用目前不适用于 disable_parallel_tool_use。
以下是如何在 Claude Sonnet 3.7 中通过 API 使用令牌高效工具的示例:
Shell
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'Python
import anthropic
client = anthropic.Anthropic()
response = client.beta.messages.create(
max_tokens=1024,
model="claude-3-7-sonnet-20250219",
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."
}],
betas=["token-efficient-tools-2025-02-19"]
)
print(response.usage)TypeScript
import Anthropic from '@anthropic-ai/sdk';
const anthropic = new Anthropic();
const message = await anthropic.beta.messages.create({
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."
}],
betas: ["token-efficient-tools-2025-02-19"]
});
console.log(message.usage);Java
import java.util.List;
import java.util.Map;
import com.anthropic.client.AnthropicClient;
import com.anthropic.client.okhttp.AnthropicOkHttpClient;
import com.anthropic.core.JsonValue;
import com.anthropic.models.beta.messages.BetaMessage;
import com.anthropic.models.beta.messages.BetaTool;
import com.anthropic.models.beta.messages.MessageCreateParams;
import static com.anthropic.models.beta.AnthropicBeta.TOKEN_EFFICIENT_TOOLS_2025_02_19;
public class TokenEfficientToolsExample {
public static void main(String[] args) {
AnthropicClient client = AnthropicOkHttpClient.fromEnv();
BetaTool.InputSchema schema = BetaTool.InputSchema.builder()
.properties(JsonValue.from(Map.of(
"location",
Map.of(
"type", "string",
"description", "The city and state, e.g. San Francisco, CA"
)
)))
.putAdditionalProperty("required", JsonValue.from(List.of("location")))
.build();
MessageCreateParams params = MessageCreateParams.builder()
.model("claude-3-7-sonnet-20250219")
.maxTokens(1024)
.betas(List.of(TOKEN_EFFICIENT_TOOLS_2025_02_19))
.addTool(BetaTool.builder()
.name("get_weather")
.description("Get the current weather in a given location")
.inputSchema(schema)
.build())
.addUserMessage("Tell me the weather in San Francisco.")
.build();
BetaMessage message = client.beta().messages().create(params);
System.out.println(message.usage());
}
}上述请求平均应该使用比普通请求更少的输入和输出令牌。要确认这一点,请尝试发出相同的请求,但从测试版标头列表中删除 token-efficient-tools-2025-02-19。
为了保持提示缓存的优势,请为您想要缓存的请求一致地使用测试版标头。如果您选择性地使用它,提示缓存将失败。