Utilizzo efficiente dei token per gli strumenti
A partire da Claude Sonnet 3.7, Claude è in grado di chiamare gli strumenti in modo efficiente dal punto di vista dei token. Le richieste risparmiano in media il 14% dei token di output, fino al 70%, il che riduce anche la latenza. La riduzione esatta dei token e i miglioramenti della latenza dipendono dalla forma e dalle dimensioni complessive della risposta.
L'utilizzo efficiente dei token per gli strumenti è una funzionalità beta che funziona solo con Claude 3.7 Sonnet. Per utilizzare questa funzionalità beta, aggiungi l'intestazione beta token-efficient-tools-2025-02-19 a una richiesta di utilizzo dello strumento. Questa intestazione non ha alcun effetto su altri modelli Claude.
Tutti i modelli Claude 4 supportano l'utilizzo efficiente dei token per gli strumenti per impostazione predefinita. Non è necessaria alcuna intestazione beta.
L'utilizzo efficiente dei token per gli strumenti attualmente non funziona con disable_parallel_tool_use.
Ecco un esempio di come utilizzare gli strumenti efficienti dai token con l'API in 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());
}
}La richiesta di cui sopra dovrebbe, in media, utilizzare meno token di input e output rispetto a una richiesta normale. Per confermarlo, prova a fare la stessa richiesta ma rimuovi token-efficient-tools-2025-02-19 dall'elenco delle intestazioni beta.
Per mantenere i vantaggi della memorizzazione nella cache dei prompt, utilizza l'intestazione beta in modo coerente per le richieste che desideri memorizzare nella cache. Se la utilizzi selettivamente, la memorizzazione nella cache dei prompt avrà esito negativo.