"content": [
{"type": "text",
"text": "Hello! How can I help you today?"}
],
"stop_reason": "end_turn",
"usage": {"input_tokens": 10, "output_tokens": 13}
content holds the answer. stop_reason says why it stopped. usage is what you pay for.
Step 2
Tell it your name, then ask
Two pastes. Each has one message.
# paste 1"messages": [ {"role": "user", "content": "my name is Sam"}]# paste 2"messages": [ {"role": "user", "content": "what is my name?"}]
Step 2 · The repliesExample, trimmed
It forgets you immediately
# paste 1
"text": "Nice to meet you, Sam! What can I help you with today?"
# paste 2
"text": "I don't know your name. ... you haven'tmentioned it in our conversation."
The model keeps nothing between calls. Each request stands alone.
Step 3
Send the history, so it remembers
You write its earlier reply yourself.
"messages": [ {"role": "user", "content": "my name is Sam"}, {"role": "assistant", "content": "Nice to meet you, Sam."}, {"role": "user", "content": "what is my name?"}]
Step 3 · The replyExample, trimmed
“Memory” is the list you send
"text": "Your name is Sam. You told me just a moment ago."
You resend the whole conversation every turn, and you pay for all of it every time. That’s why long conversations get slow and expensive.
Step 4
Add a system prompt
Who it is and how it behaves, outside the messages.
"model": "claude-opus-5-5","max_tokens": 1024,"system": "Talk like a pirate. Keep every answer to one sentence.","messages": [ {"role": "user", "content": "my name is Sam"}, {"role": "assistant", "content": "Nice..."}, {"role": "user", "content": "what is my name?"}]
Step 4 · The replyExample, trimmed
Same conversation, new behaviour
"text": "Arr, yer name be Sam, matey, and beggin' yerpardon fer forgettin' me pirate tongue in melast reply!"
It apologized for a reply you wrote. Steps 1–4 are a chatbot: it talks, remembers and follows instructions. It can’t act.
Step 5
Ask for something it can’t do
Tell it to look at files. Then ask about one.
"system": "Look at files before answering questions about them.","messages": [ {"role": "user", "content": "what is in notes.txt?"}]
Step 5 · The replyExample, trimmed
It knows what to do. It can’t do it.
"text": "... I don't have a way to open files on yourcomputer or system, so I can't tell you what's in it.",
"stop_reason": "end_turn"
This is where most chatbots stop.
Step 6
Give it a tool
A name, a description and the arguments it takes.
The model reads the description to decide when to use it.
"system": "Look at files before ...","tools": [{ "name": "read_file", "description": "Read a text file and return its contents.", "input_schema": { "type": "object", "properties": { "path": {"type": "string"}}, "required": ["path"]}}],"messages": [ ...same question... ]
A tool request is just another block. The model proposes. You decide what happens next.
Step 7
Send back the result
Add its request to the history, then your result with the same id.
You are the code now. Nobody read a file. You wrote what it “contained.”
"system": ..., "tools": [ ...read_file... ],"messages": [ {"role": "user", "content": "what is in notes.txt?"}, {"role": "assistant", "content": [ {"type": "tool_use", "id": "toolu_01", "name": "read_file", "input": {"path": "notes.txt"}}]}, {"role": "user", "content": [ {"type": "tool_result", "tool_use_id": "toolu_01", "content": "Rehearse the talk. Check the Wi-Fi. Bring an HDMI adapter."}]}]
Step 7 · The replyExample, trimmed
It answers from what you sent
"text": "notes.txt is a short to-do list with three items:1. Rehearse the talk. 2. Check the Wi-Fi.3. Bring an HDMI adapter.",
"stop_reason": "end_turn"
You just did one turn of the loop by hand. That’s what an agent is. It also believes whatever a tool returns.
Step 8
Say no
Same request. This time you decline instead of running the tool.
"messages": [ {"role": "user", "content": "what is in notes.txt?"}, {"role": "assistant", "content": [ {"type": "tool_use", "id": "toolu_01", "name": "read_file", "input": {"path": "notes.txt"}}]}, {"role": "user", "content": [ {"type": "tool_result", "tool_use_id": "toolu_01", "content": "The user declined to share this file."}]}]
Step 8 · The replyExample, trimmed
“No” is an answer it can work with
"text": "I couldn't read notes.txt because access to thatfile was declined, so I can't tell you what's in it."
A declined action isn’t an error. The step people skip, and the one that matters most.
What you just did
You were the loop
01Send the messages.
02Read stop_reason. If it’s end_turn, you’re done.
03If it’s tool_use, ask a person, then run the tool or say no.
04Add the request and the result to the messages. Go to 01.
Do that in code, with a limit on rounds, and you’ve built an agent.
Next
Let code do the pasting
The same four rules, in bash, with a real read_file.
curl/step1.sh to step8.sh build it up a few lines at a time. About 60 lines in all.
for ((i = 1; i <= 10; i++)); do ask; remember [ "$(stop_reason)" = tool_use ] || break file=$(tool_arg path) read -r -p "allow read_file $file? " ok if [ "$ok" = y ]; then read_file "$file" | answer else echo "The user declined." | answer fidone
What you just built
Three things in one loop
1–4
A chatbot
It talks, remembers the conversation and follows instructions.
5–7
An agent
It asks for actions, reads the results and decides what to do next.
8
An agent with human approval
It stops, and a person approves what matters.
Autonomous execution, not autonomous approval.
Next · from AlwaysBuilt
Route before you loop
Not every request needs every tool, the longest instructions and the biggest model.
Add one small, cheap call first that decides what kind of request this is. Then run the same loop with only what that request needs.
kind=$(classify "$msg") # one small callcase "$kind" in question) fresh read-only.json ;; change) fresh read-write.json ;;esac# each file sets its own model,# tools and instructions# ...then the same loop
What AlwaysBuilt’s router taught me
The router is the easy part to get wrong
Route on what it is
Not on keywords
“Build a Jira clone with auth” went to the backend route on 26 of 26 turns, because of the word “auth.” No design guidance ever shipped. Each route now says what it’s for, what it’s not for, and gives examples.
Test it like code
Real requests are the test cases
Every misroute became a test. I changed the route descriptions until every case was classified correctly, and the tests keep it that way.
Rules go in code
The model classifies. Code decides.
Anything that changes the page gets at least the mid-tier model, whatever the classifier says. If the classifier is down, it says so instead of guessing.
Next: the same loop in Python
Same loop, real code
The messages you pastedbecome a list: messages = []
Each pastebecomes client.messages.create(...)
The result you wrotebecomes a tool_result your code appends
while True: reply = client.messages.create( model="claude-opus-5-5", max_tokens=4096, tools=TOOLS, messages=messages) messages.append({"role": "assistant", "content": reply.content}) if reply.stop_reason != "tool_use": break messages.append(run_tools(reply))
Try it this week
Be the loop once
01Paste all eight. It takes ten minutes.
02In step 7, send back something untrue. Watch it believe you.
03Run step8.sh, and let code do the pasting on a real file.
An agent is a loop, a function and a human who says yes.