Understanding AI agents
Trace
user Which room is the workshop in? developer Use the organizer’s note. model read_workshop_note() harness Run the Python function tool workshop.txt → Map Room model Today’s workshop is in the Map Room.
Start with a model, give it one tool, and build the loop in Python.
Alexander Dess · 4:49 · English captions
10-minute narration cut
Start with the model
A language model uses patterns learned during training and the words you send it to produce a reply. Ask what to bring to a writing workshop and it can suggest a notebook. But which room did the organizer book this morning? That fact lives in a file on my laptop.
Try the packing question first. Set OPENAI_API_KEY in your terminal, then send this request.
curl -fsS https://api.openai.com/v1/responses \
-H "Authorization: Bearer $OPENAI_API_KEY" \
--json '{
"model": "gpt-6.2-sol",
"store": false,
"input": "What should I bring to a writing workshop?"
}'The response contains the model’s answer in output.
To ask for a shorter answer, replace input with two messages. The app supplies the developer instruction. The reader supplies the user question.
"input": [
{"role": "developer", "content": "Answer in exactly three words."},
{"role": "user", "content": "What should I bring to a writing workshop?"}
]In the rehearsal the reply was “Draft, notebook, pen.” The instruction changed the shape of the answer. Where in either request did we tell the model the room?
Give it one tool
In chat.py, Python keeps a terminal prompt open. Each line you type becomes a fresh API request. Ask which room today’s Trailnotes workshop is in and the app’s instructions tell the model to ask for the organizer’s note when it needs it.
A tool is a function the model can ask the program to run. In agent.py, we give it a function called read_workshop_note. The model sees its name, its description and the fact that it takes no arguments. A packing question can still get a direct answer. For the room question, the model can request the function.
Build the loop in Python
Python sends the question to the model. If the reply contains a tool call, Python runs the function and sends its result back. When the model gives an answer, the loop ends.
if not calls:
return response.output_text
call = calls[0]
contents = read_workshop_note()
context.extend(response.output)
context.append({
"type": "function_call_output",
"call_id": call.call_id,
"output": contents,
})The function opens demo_project/workshop.txt and reads its text. Then it adds the model’s earlier output and the function result to the next request. The matching call ID ties the text to the particular request for the file.
On the next pass, the model has a sentence from the organizer. It can answer “Garden Room.” The file supplies the room name.
Change one line on disk
Leave the question and the code alone. Open the text file and change Garden Room to Map Room. Save it, then run the agent again.
uv run --python 3.12 agent.pyThe answer changes to “Map Room.” Only the file changed. This is the version shown in the opening replay.
Run the demo
To try both paths, unzip the demo, open its folder in a terminal, and set your OpenAI API key. With uv installed, run uv run --python 3.12 chat.py for the chat or uv run --python 3.12 agent.py for the room check.
Further reading · OpenAI’s function calling guide · Andrej Karpathy on how he uses LLMs