ツール
トークン効率的なツール使用
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 を削除してください。
プロンプトキャッシングの利点を保つために、キャッシュしたいリクエストに対してベータヘッダを一貫して使用してください。選択的に使用すると、プロンプトキャッシングは失敗します。