Example (Python, a manual loop; tested with a stand-in for the 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"))
This loop has no limit and no error handling yet. Lessons 3 and 5 fix both.
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: tools, memory and reliability" on The Future Market. Lesson 1: "Open the loop (the manual agent loop)".
What the lesson teaches:
- When Claude wants to use a client tool, the response has stop_reason of tool_use and one or more tool_use blocks. Each block has an id, a name and an input (the arguments).
- Your code runs the tool, then sends a new user message that holds tool_result blocks. Each result carries the tool_use_id of the request it answers, plus content and an optional is_error.
- Formatting rules from the docs: the tool results must come right after the assistant message that asked for them, and in the user message the tool_result blocks must come first. Any text goes after them.
- Claude can ask for several tools in one turn. Answer every tool_use id with a matching tool_result.
- The loop ends when stop_reason is something other than tool_use (for example end_turn).
- Tool results often carry content from outside your control, such as web pages or emails. Treat it as untrusted: it may contain instructions meant to trick Claude (prompt injection).
- Why do this by hand? The tool runner is simpler, but Anthropic's docs say to use the manual loop when you need human-in-the-loop approval, custom logging or conditional execution. That is exactly what this course adds.
The exercise I'm working on:
Run the loop with a question that needs both mugs. Print len(response.content) on the first turn to see whether Claude asked for both tools at once. Then write two sentences explaining why every tool_use id needs a 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 "Open the loop (the manual agent loop)" from the course "Multi-Step Agents: tools, memory and reliability".
Here's what the lesson says:
In course 1.4 a robot did the errands and you only saw the result. Now you hold the clipboard. Claude says "please check the stock room," you go and check, you write the answer on the clipboard and hand it back. Then Claude decides what to ask next. That back and forth is the whole agent.
The activity:
Run the loop with a question that needs both mugs. Print len(response.content) on the first turn to see whether Claude asked for both tools at once. Then write two sentences explaining why every tool_use id needs a 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.