Tool Use
- Python
- JavaScript
- curl
import anthropic
client = anthropic.Anthropic(api_key="your-api-key", base_url="https://aisupermarket.work")
tools = [
{
"name": "get_weather",
"description": "Get the weather for a specified city",
"input_schema": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "City name"}
},
"required": ["city"]
}
}
]
response = client.messages.create(
model="claude-opus-4-7",
max_tokens=1024,
tools=tools,
messages=[{"role": "user", "content": "What is the weather in Guangzhou today?"}]
)
for block in response.content:
if block.type == "tool_use":
print(f"Tool call: {block.name}, args: {block.input}")
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({ apiKey: "your-api-key", baseURL: "https://aisupermarket.work" });
const tools = [
{
name: "get_weather",
description: "Get the weather for a specified city",
input_schema: {
type: "object",
properties: {
city: { type: "string", description: "City name" },
},
required: ["city"],
},
},
];
const response = await client.messages.create({
model: "claude-opus-4-7",
max_tokens: 1024,
tools,
messages: [{ role: "user", content: "What is the weather in Guangzhou today?" }],
});
for (const block of response.content) {
if (block.type === "tool_use") {
console.log(`Tool call: ${block.name}, args:`, block.input);
}
}
curl https://aisupermarket.work/v1/messages \
-H "x-api-key: your-api-key" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{
"model": "claude-opus-4-7",
"max_tokens": 1024,
"tools": [{
"name": "get_weather",
"description": "Get the weather for a specified city",
"input_schema": {
"type": "object",
"properties": {"city": {"type": "string", "description": "City name"}},
"required": ["city"]
}
}],
"messages": [{"role": "user", "content": "What is the weather in Guangzhou today?"}]
}'
Complete Tool Calling Loop
- Python
- JavaScript
- curl
import anthropic
client = anthropic.Anthropic(api_key="your-api-key", base_url="https://aisupermarket.work")
def get_weather(city: str) -> str:
return f"{city} is sunny today, 25°C"
tools = [
{
"name": "get_weather",
"description": "Get the weather for a specified city",
"input_schema": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"]
}
}
]
messages = [{"role": "user", "content": "What is the weather in Beijing?"}]
while True:
response = client.messages.create(
model="claude-opus-4-7",
max_tokens=1024,
tools=tools,
messages=messages
)
if response.stop_reason == "end_turn":
print(response.content[0].text)
break
tool_results = []
for block in response.content:
if block.type == "tool_use":
result = get_weather(**block.input)
tool_results.append({
"type": "tool_result",
"tool_use_id": block.id,
"content": result
})
messages.append({"role": "assistant", "content": response.content})
messages.append({"role": "user", "content": tool_results})
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({ apiKey: "your-api-key", baseURL: "https://aisupermarket.work" });
function getWeather(city) {
return `${city} is sunny today, 25°C`;
}
const tools = [
{
name: "get_weather",
description: "Get the weather for a specified city",
input_schema: {
type: "object",
properties: { city: { type: "string" } },
required: ["city"],
},
},
];
const messages = [{ role: "user", content: "What is the weather in Beijing?" }];
while (true) {
const response = await client.messages.create({
model: "claude-opus-4-7",
max_tokens: 1024,
tools,
messages,
});
if (response.stop_reason === "end_turn") {
console.log(response.content[0].text);
break;
}
const toolResults = [];
for (const block of response.content) {
if (block.type === "tool_use") {
const result = getWeather(block.input.city);
toolResults.push({ type: "tool_result", tool_use_id: block.id, content: result });
}
}
messages.push({ role: "assistant", content: response.content });
messages.push({ role: "user", content: toolResults });
}
# Step 1: send a request; the model returns tool_use
curl https://aisupermarket.work/v1/messages \
-H "x-api-key: your-api-key" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{
"model": "claude-opus-4-7",
"max_tokens": 1024,
"tools": [{"name": "get_weather", "description": "Get the weather for a specified city", "input_schema": {"type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"]}}],
"messages": [{"role": "user", "content": "What is the weather in Beijing?"}]
}'
# Step 2: continue the conversation with tool_result
curl https://aisupermarket.work/v1/messages \
-H "x-api-key: your-api-key" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{
"model": "claude-opus-4-7",
"max_tokens": 1024,
"tools": [{"name": "get_weather", "description": "Get the weather for a specified city", "input_schema": {"type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"]}}],
"messages": [
{"role": "user", "content": "What is the weather in Beijing?"},
{"role": "assistant", "content": [{"type": "tool_use", "id": "tool_123", "name": "get_weather", "input": {"city": "Beijing"}}]},
{"role": "user", "content": [{"type": "tool_result", "tool_use_id": "tool_123", "content": "Beijing is sunny today, 25°C"}]}
]
}'