Lesson 23 / 31
Agents: The Tool-Calling Loop
Understand how an agent decides, acts and observes.
Decide, act, observe, repeat
An agent is an LLM in a loop with tools. Each turn the model either returns a final answer or requests a tool call; your code runs the tool, appends the result as a message and calls the model again, until it answers or a limit is reached. That is all an agent is; the frameworks supply the loop, tool schemas, message formats and helpers. Use agents when the number and order of steps cannot be known in advance (for example research, debugging, multi-source questions). For fixed sequences, a plain chain is simpler, cheaper and more predictable. Always set a maximum number of iterations, a token and time budget, and log every tool call.
Models that choose actions
An agent loops between thinking and acting; workflows give you control over that loop.
The loop in pseudocode
Frameworks wrap this; knowing the loop helps you debug. Illustrative; not run here.
messages = [system, user_question]
for step in range(MAX_STEPS): # always cap the loop
reply = model.invoke(messages) # model sees tools via bind_tools
messages.append(reply)
if not reply.tool_calls: # no tool requested -> final answer
return reply.content
for call in reply.tool_calls:
result = run_tool(call["name"], call["args"]) # YOUR code validates and executes
messages.append(tool_message(call["id"], result))
return "Stopped: step limit reached"Quick check: What must every agent loop have?
- A maximum number of iterations and a budget
- An unlimited number of retries
- No logging
- Admin access to everything
Answer
A maximum number of iterations and a budget — Limits prevent runaway cost and endless loops.