AI Агенты и Tool Use · Урок 2
Function calling в Claude
Полный цикл tool_use в Claude на текущих моделях: блоки tool_use и tool_result, параллельные вызовы, режимы tool_choice и их ограничения, structured outputs и strict tool use для строгого JSON, tool runner из SDK.
Цикл tool_use в Claude
Claude возвращает stop_reason: "tool_use", а в content появляется один или несколько блоков tool_use с id, name, input. Вы выполняете функции и продолжаете тот же диалог: добавляете ответ ассистента целиком и сообщение role: "user" с блоками tool_result.
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 # текст и thinking пропускаем
try:
output = run_weather(block.input["city"]) # ваш код
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 дней полного доступа ко всему каталогу. Карта не нужна.


