Model, loop, tools
A language model on its own only writes text. An agent is a program that runs a model in a loop and lets it use tools: functions that look things up or change things. The model decides; the program acts.
The program tells the model which tools exist: each name, what it does, and its inputs, written as a JSON schema (a description of the data). Ours has two: get_weather(city) and to_fahrenheit(c).
One run, step by step
The user asks: "How hot is it in Kuwait City, in °F?" The program starts a list of messages with it. This list, the context, is everything the model will see.
Line 2 sends the whole list to the model. It cannot look up the weather, so instead of an answer it replies with a tool call: structured text naming get_weather and its argument, city = "Kuwait City".
The model never runs code. Line 5 does: the program calls get_weather("Kuwait City"), which returns 38 °C (fixed demo data). Line 6 adds the call and its result to the list. The list grows.
The model keeps no memory between calls, so line 2 sends the whole list again, now three messages. It reads 38 °C and asks for one more tool: to_fahrenheit(c = 38).
The program runs to_fahrenheit(38): 38 × 9/5 + 32 = 100.4. The call and its result join the list, now five messages. Each trip round the loop adds what the agent did and what it saw.
On the third call the model has what it needs, so it replies with an answer, not a tool call. Lines 3 and 4 spot it and return it: "It is 100.4 °F in Kuwait City." Three model calls, two tool runs.
The pattern and its limits
This loop is a well-known agent pattern called ReAct: the model reasons about what to do (often in writing), acts by calling a tool, observes the result, and repeats until it can answer. Bigger agents run the same loop with more tools.
Tools act in the real world, so agents need limits. Line 1 stops after five turns, so the loop cannot run for ever. Give an agent only the tools it needs, and ask a person before risky actions.
What happens
;; step 1/10 · agent
language model text in -> text out
agent model + tools + a loop:
the model picks the next action
the program runs it
the result goes back to the model
repeat until the model answers
The model in the program is a scripted stand-in (a few if-statements), so the code runs offline and always gives the same answer. A real agent sends the same message list to a real model API, and the model writes the tool calls itself. get_weather returns fixed demo data (38 °C). A real tool would ask a weather service.