1for step in range(5):2 reply = llm(messages)3 if "answer" in reply:4 return reply["answer"]5 result = call_tool(reply)6 messages += [reply, result]get_weatherto_fahrenheitYOUlanguage modelusercalltoolcalltool?
Gold mat: the agent program · teal block: the language modelCode card: the agent loop · blocks at the back: the tools · cells in front: the messagesEnvelope: a message · blue ? = question, violet gear = tool call, khaki page = result, green tick = answerThe gold mat is the agent: a program wrapped round a language model. At the back, the agent loop in Python and two tools. In the middle, the model. In front, the message list (the context), which grows by two messages each time a tool runs. The question goes into the list. The model reads the whole list and asks for get_weather; the program runs it (38 °C) and adds the result. The model asks for to_fahrenheit; the program runs it (100.4). On its third call the model answers, and the answer travels back to you. 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.

Model, loop, tools

  1. 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.

One run, step by step

The pattern and its limits

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.