Uso eficiente de tokens em ferramentas
A partir do Claude Sonnet 3.7, Claude é capaz de chamar ferramentas de forma eficiente em tokens. As solicitações economizam em média 14% em tokens de saída, chegando a 70%, o que também reduz a latência. A redução exata de tokens e as melhorias de latência dependem da forma e tamanho geral da resposta.
O uso eficiente de tokens em ferramentas é um recurso beta que funciona apenas com Claude 3.7 Sonnet. Para usar este recurso beta, adicione o cabeçalho beta token-efficient-tools-2025-02-19 a uma solicitação de uso de ferramentas. Este cabeçalho não tem efeito em outros modelos Claude.
Todos os modelos Claude 4 suportam uso eficiente de tokens em ferramentas por padrão. Nenhum cabeçalho beta é necessário.
O uso eficiente de tokens em ferramentas atualmente não funciona com disable_parallel_tool_use.
Aqui está um exemplo de como usar ferramentas eficientes em tokens com a API no Claude Sonnet 3.7:
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'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)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);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());
}
}A solicitação acima deve, em média, usar menos tokens de entrada e saída do que uma solicitação normal. Para confirmar isso, tente fazer a mesma solicitação, mas remova token-efficient-tools-2025-02-19 da lista de cabeçalhos beta.
Para manter os benefícios do cache de prompt, use o cabeçalho beta consistentemente para solicitações que você gostaria de armazenar em cache. Se você usá-lo seletivamente, o cache de prompt falhará.