For the student
This is the whole idea of an agent in one short file: a tool, a model and a loop. The file is m01_first_agent/agent.py in the companion project, about forty lines of code plus comments. The model in this lesson is a scripted stand-in, so nothing is sent to a hosted model: the file runs on any laptop with no key, no network and no cost. The loop is the real thing, and module 2 puts a real model call in the stand-in's place.
The three parts of the file
- The tool:
calculator(operation, a, b) is an ordinary Python function. It can add, subtract, multiply and divide - The dictionary
TOOLS maps a tool's name to its function: {"calculator": calculator}. The model asks for a tool by name, and the program looks the name up and calls the function - The model:
ScriptedModel in m01_first_agent/scripted_model.py. It follows fixed rules to find the operation words and the numbers in the question. It understands nothing, and it answers only sums - Each time it is asked, the model returns one of two things: a
ToolCall (a tool name and arguments) or a FinalAnswer (text for the user) - The loop:
run_agent(question, model, max_steps=MAX_STEPS)
The loop, in its smallest form
- Ask the model for the next step, giving it the question and every tool result so far
- If it is a tool call, run the tool, keep the result, and go round again
- If it is a final answer, stop and return its text
- Print every step, so the printout is a run you can read as in lesson 1.4
Run it on three questions
uv run python -m m01_first_agent.agent "What is 12 times 7?" prints these five lines:Question: What is 12 times 7?Step 1: call calculator with {'operation': 'multiply', 'a': 12, 'b': 7}Step 1: result 84Step 2: final answerAnswer: The answer is 84."add 12 and 7, then multiply by 3" makes two tool calls, with results 19 and 57, and answers at step 3: The answer is 57."What is 90 divided by 4?" makes one tool call and answers The answer is 22.5.- Quick count: the tool calls are the step number of the
final answer line minus one. This holds while each step asks for one tool
The step limit and errors
MAX_STEPS = 5. The loop is a for loop over range(1, max_steps + 1), never while True- At the limit, the function returns
Stopped: no final answer after 5 steps. - With no limit, a model that never gives a final answer never stops, and with a hosted model every trip is a paid model call
- A tool error does not crash the run. It goes back to the model as a result that starts with
error:. Ask "What is 5 divided by 0?" and the printout shows Step 1: result error: cannot divide 5 and 0 - The tests in
tests/m01_first_agent/test_agent.py check the limit with a model that never finishes, named NeverDoneModel
Try it
- Add a second tool that returns today's date, then ask a question that needs both tools, such as
"What is 12 times 7, and what is the date today?" - Step 1, in
agent.py: write a function today() with no arguments that returns date.today().isoformat() (import date from datetime), and add it to TOOLS under the name "today" - Step 2, in
scripted_model.py: the stand-in knows nothing about dates, so adding the tool is not enough. Teach it one new rule in next_step: when the question contains the word today and the sums are done, return ToolCall(tool="today", arguments={}). When the date has come back, return a FinalAnswer that holds both results. The sums are done when len(results) == len(plan), and the new rule goes above the line that returns the final answer for sums - A real model would need no new rule: it reads the tool's description and chooses. That is the difference between a stand-in and a model, and you meet it in module 2
- Check yourself: the printout shows one
call calculator line, one call today line, and Step 3: final answer. A question without the word today still prints what it printed before