Pananglitan (Python, usa ka manual loop; gisulayan gamit ang stand-in sa API)
import anthropic
client = anthropic.Anthropic() # reads ANTHROPIC_API_KEY
TOOLS = [{
"name": "check_stock",
"description": "Check how many units of a product are in stock. Use the exact product name, for example 'blue mug'.",
"input_schema": {
"type": "object",
"properties": {"item": {"type": "string", "description": "Product name"}},
"required": ["item"],
},
}]
STOCK = {"blue mug": 12, "red mug": 0}
def check_stock(item):
return str(STOCK[item.lower()])
messages = [{"role": "user", "content": "Do we have blue mugs and red mugs?"}]
while True:
response = client.messages.create(
model="claude-opus-5-5", max_tokens=1024, tools=TOOLS, messages=messages
)
if response.stop_reason != "tool_use":
break
messages.append({"role": "assistant", "content": response.content})
results = []
for block in response.content:
if block.type == "tool_use":
results.append({
"type": "tool_result",
"tool_use_id": block.id,
"content": check_stock(**block.input),
})
messages.append({"role": "user", "content": results})
print("".join(b.text for b in response.content if b.type == "text"))
Kini nga loop wala pay limit ug wala pay error handling. Ang Lessons 3 ug 5 mag-ayo sa duha.
ClaudeStuck? Copy a ready-made prompt for Claude
Copy this, open Claude, paste it, and Claude will walk you through the lesson step by step.
For a grown-up: Claude is for adults (18+). Copy this, paste it into Claude, and use the answer to explain the lesson to your child.
I'm learning "Multi-Step Agents: mga tool, memorya ug pagkasaligan" on The Future Market. Lesson 1: "Abliha ang loop (ang manual agent loop)".
What the lesson teaches:
- Kung gusto ni Claude nga mogamit og client tool, ang tubag adunay stop_reason nga tool_use ug usa o labaw pa nga tool_use blocks. Ang matag block adunay id, name ug input (ang mga argumento).
- Ang imong code ang mopadagan sa tool, dayon mopadala og bag-ong user message nga naay sulod nga tool_result blocks. Ang matag resulta nagdala sa tool_use_id sa hangyo nga gitubag niini, dugang ang content ug opsyonal nga is_error.
- Mga lagda sa format gikan sa docs: ang mga tool result kinahanglan mosunod dayon human sa assistant message nga nangayo niini, ug sa user message ang mga tool_result blocks kinahanglan nga una. Ang bisan unsang text mosunod human niini.
- Makahangyo si Claude og daghang tool sa usa ka turno. Tubaga ang matag tool_use id nga adunay motakdo nga tool_result.
- Matapos ang loop kung ang stop_reason lahi sa tool_use (pananglitan end_turn).
- Ang mga tool result kanunay nagdala og sulod nga dili nimo makontrol, sama sa web pages o emails. Ilha kini nga dili kasaligan: mahimong naay sulod nga mga panudlo nga gituyo aron limbongan si Claude (prompt injection).
- Ngano nga manu-manong himoon? Mas simple ang tool runner, apan gisulti sa docs sa Anthropic nga gamiton ang manual loop kung kinahanglan nimo ang human-in-the-loop approval, custom logging o conditional execution. Mao gyud kana ang gidugang niini nga course.
The exercise I'm working on:
Padagana ang loop gamit ang pangutana nga nagkinahanglan sa duha ka mug. I-print ang len(response.content) sa unang turno aron makita kung gihangyo ba ni Claude ang duha ka tool sa usa ka higayon. Dayon isulat ang duha ka sentence nga nagpasabot kung ngano nga ang matag tool_use id nagkinahanglan og tool_result.
I'm stuck. Please:
1. Explain the main idea in plain, simple words with one everyday example.
2. Walk me through the exercise one small step at a time. Ask me what I've done so far before giving the next step.
3. Don't just give me the final answer. Help me do it myself, and check my work when I share it.
I'm a parent helping my child (about 8 years old) with a lesson called "Abliha ang loop (ang manual agent loop)" from the course "Multi-Step Agents: mga tool, memorya ug pagkasaligan".
Here's what the lesson says:
Sa course 1.4 usa ka robot ang naghimo sa mga mandu-mando ug resulta ra ang imong nakita. Karon ikaw na ang naghupot sa clipboard. Moingon si Claude "palihog check sa stock room," moadto ka ug mo-check, isulat nimo ang tubag sa clipboard ug ihatag balik. Dayon si Claude ang mohukom kung unsay sunod nga pangutan-on. Mao kana nga paghatag ug pagbalik ang tibuok agent.
The activity:
Padagana ang loop gamit ang pangutana nga nagkinahanglan sa duha ka mug. I-print ang len(response.content) sa unang turno aron makita kung gihangyo ba ni Claude ang duha ka tool sa usa ka higayon. Dayon isulat ang duha ka sentence nga nagpasabot kung ngano nga ang matag tool_use id nagkinahanglan og tool_result.
Please explain this so an 8-year-old understands: short sentences, a fun everyday example, and no hard words.
Then give me 3 simple questions I can ask my child to check they understood, and a way to do the activity together, one tiny step at a time.