Give a model tools: the loop from scratch
Write the loop that lets a model call your functions, with no framework, so you know what every framework is doing for you.
-
Describe two tools to the model: list_dir and read_file
A tool is a name, a sentence and a JSON schema. The model never runs anything: it only asks.
Run this to check
uv run --with openai agent.py --once "Which files are in this folder?"You pass when
The script prints a tool call the model asked for (list_dir with a path) and stops. Nothing is executed yet.
Files you write
- agent.py
-
Run the tool the model asked for and send the result back
Run this to check
uv run --with openai agent.py --once "Which files are in this folder?"You pass when
Now the answer lists the real files in the folder.
Files you write
- agent.py
-
Loop until the model stops asking for tools, with a limit of 8 turns
The turn limit is your first safety control. Without it a confused model loops until your budget is gone.
Run this to check
uv run --with openai agent.py "Which file in this folder has the most lines, and how many?"You pass when
It lists the folder, reads several files, then names the right file. Check the number with wc -l.
Files you write
- agent.py
-
Print one line per step: who spoke, which tool, how many tokens
Run this to check
uv run --with openai agent.py --trace "How many Python files are here?"You pass when
A numbered trace prints before the answer, and reading a file that does not exist shows up as a tool error the model recovers from, not a crash.
Files you write
- agent.py