02 Building the agentLesson 1 of 812 minBeginnerHelps to have seen:Structured output
Tools and the loop
- + customer
- + LLM
The diagram shows: Customer (in the store chat), The conversation sent to the model (on each call) and LLM (answers or asks).
Highlighted: LLM.
Customer sends to The conversation sent to the model carrying "where is 4821?".
LLM sends to The conversation sent to the model.
Added: customer. Added: LLM.
Step 1 of 9: The model cannot reach the store system
- What a tool needs for the model to use it
- Who decides and who runs the call
- How the turns form the loop, and why the conversation grows
- Why the loop needs useful errors and a cap in code
- Why a tool that changes something needs a unique key
Conversations with the right tool calls (name and arguments) and average number of turns, across the store's 50 test conversations
Coming soon: I will measure this on the example agent and publish the number with the report.
The whole lesson as textEvery step, to read at once, search or print.
The model cannot reach the store system
The customer asks about order 4821. The model has no access to the store orders: all it does is write the next piece of text.
Without help, it either apologizes or writes a status that looks true, as in the lesson What an LLM is, and why it makes things up. In this lesson it gets a way to ask for the data, and then to act.
To act in the world, the model needs someone to carry things out for it.
Name, description and format
Along with the conversation, your code sends the list of . Each one has a name, a description and the format of its arguments, in a .
The model does not see the function's code. It decides to use buscar_pedido only from what it reads there. Anthropic's documentation says the description is, by far, what weighs most on the tool's performance.
The tool description is a prompt. Write it so the model knows when to use it.
The model asks, and the call ends
Instead of answering the customer, the model returns a request: "call buscar_pedido with numero 4821". In the Claude API, the response comes with stop_reason "tool_use" and an id for the request.
Here the call ends. The model ran nothing: it only wrote, in an agreed format, what it wants someone to run.
The model decides which tool and with which arguments. It does not run it.
Your code runs it and hands it back
Your code reads the request, actually runs buscar_pedido and puts the result in the conversation as a tool_result, linked to the request by the id. Then it makes a new call to the model.
This is where control lives: before running, the code can check the arguments, deny the call or ask a person for approval.
The model chooses. The code runs, and that is why the code can say no.
Ask, run, read and repeat
With the result, the model decides again. It mentions the delay and offers to cancel, because it is past 5 days. The customer accepts, it asks for criar_reembolso and, on the next turn, confirms.
Each turn is a new call with the whole conversation, and that is why it grows. The ends when the model answers without asking for a tool.
An agent is this cycle: decide, ask, get the result and decide again.
The loop that does not stop
The customer asks about 4999, which does not exist. The lookup returns only "error". The model tries again, with the same number, and again. Each turn is a whole call, and the customer waits.
Two defenses, both outside the model: the error must say what happened and what to do, and the turn cap lives in your code. Past the cap, it stops and calls a person.
An error the model can use, and a turn cap in code.
The refund that went out twice
The code calls criar_reembolso, but the response gets lost on the network. It does not know if it worked and tries again. If the tool does not recognize the repeat, the customer gets paid twice and the store pays twice.
Every tool that changes something (creates, pays, cancels) needs a unique key per operation. This is called .
A tool that reads can repeat. A tool that changes needs a unique key.
A tool designed for whoever reads it
What the source says: Anthropic describes a tool as a contract between deterministic code and a non-deterministic agent. In the text's example, a Slack thread, the concise response used 72 tokens; the detailed one, 206.
Our reading: the tool result goes into the conversation and comes back in every following call. Return only what helps decide.
Design the tool for whoever reads it: the model, turn after turn.
The model asks, the code runs
The agent looks things up and acts through tools. The model asks by name, guided by the description; your code checks, runs and returns the result to the conversation.
Each turn carries the whole conversation. The useful error, the turn cap and the unique key live in the code. The question that remains: when is it worth letting the model choose the path?
Questions in this lesson
- How does the model decide to use buscar_pedido?
- By reading the tool's name, description and schema. The model only sees the definition. The description works as a prompt.
- Where can you deny a dangerous call, like a large refund?
- In the code that runs the tool. The model only asks. The code runs it and can check, deny or ask for approval.
- The lookup returns only "error" and the agent keeps trying. What fixes it?
- An error that says what to do and a turn cap in code. The error must be useful for the next decision, and the limit must be enforced outside the model.
- The criar_reembolso response got lost and the code tries again. What prevents a duplicate refund?
- A unique key per refund, which the tool recognizes. The code is what repeats. The tool must recognize the same operation.