AI Agents & Tool Use · Урок 2
Function calling in Claude
The full tool_use loop in Claude on current models: tool_use and tool_result blocks, parallel calls, tool_choice modes and their limits, structured outputs and strict tool use for strict JSON, and the SDK tool runner.
ENМатеріали курсів поки англійською. Інтерфейс, ШІ-наставник і перевірка завдань — українською.
The tool_use loop in Claude
Claude returns stop_reason: "tool_use", and one or more tool_use blocks appear in content with id, name, input. You run the functions and continue the same conversation: append the full assistant response and a role: "user" message with tool_result blocks.
import anthropic
client = anthropic.Anthropic()
tools = [
{
"name": "get_weather",
"description": "Get current weather for a city. Use when the user asks about weather.",
"input_schema": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "City name"},
"units": {"type": "string", "enum": ["celsius", "fahrenheit"]}
},
"required": ["city"]
}
}
]
messages = [{"role": "user", "content": "What's the weather in Paris?"}]
response = client.messages.create(
model="claude-sonnet-5-5",
max_tokens=4096,
tools=tools,
messages=messages,
)
while response.stop_reason == "tool_use":
results = []
for block in response.content:
if block.type != "tool_use":
continue # skip text and thinking blocks
try:
output = run_weather(block.input["city"]) # your code
results.append({"type": "tool_result", "tool_use_id": block.id, "content": output})
except Exception as e:
results.append({"type": "tool_result", "tool_use_id": block.id,
"content": f"Error: {e}", "is_error": True})
Далі урок відкривається після безкоштовної реєстрації
Реєстрація через email або Google відкриває перші два уроки кожного курсу назавжди і 7 днів повного доступу до всього каталогу. Картка не потрібна.


