diagramkit

What is an AI agent? The agent loop explained

An AI agent is a language model that can call tools, run in a loop by your app. On each trip around the loop, the model either asks for a tool or gives its answer. The app runs the tool, adds the result to the conversation, and sends everything back. The loop is what lets an agent handle questions nobody planned for, and it is also what makes it slower and more expensive than a single model call.

Chatbot, workflow or agent

The three get confused because they can use the same model. What differs is who decides the next step.

  • A chatbot answers from what it was sent. It has no way to look anything up.
  • A workflow is code that calls tools in a fixed order, then calls the model once with the results. The programmer decides every step.
  • An agent lets the model decide. It sees the tools it can use and picks the next call each trip, until it has enough to answer.

The agent loop, step by step

  1. The app sends the model the conversation so far and the definitions of the tools it can use.
  2. The model replies with either a tool call (a tool name and its inputs) or a final answer.
  3. If it is a tool call, the app runs the tool, such as looking up a customer's orders.
  4. The app adds the tool's result to the conversation and goes back to step 1.
  5. When the model replies with an answer instead, the loop ends and the answer goes to the user.

The agent loop: the model asks for a tool or answers, and the app runs each tool and sends everything back

The model never runs anything itself. It writes a request as text, and your app decides whether to run it. That is where you add limits, such as a cap on trips or an approval step before a tool changes data.

One real agent run, trip by trip

To see the loop working, I built a small order-support agent: about 60 lines of Python, three tools (find_customer, get_orders, get_tracking), five customers and six orders, on Claude Sonnet 5.5. Here is one real run for the question "[email protected] here. Which of my orders will arrive first?"

  1. The model asks for find_customer with the email. 652 input tokens, 1.11 seconds.
  2. With the customer ID back, it asks for get_orders. 722 tokens, 1.23 seconds.
  3. Lena has two orders, so it asks for both orders' tracking at once. 878 tokens, 1.18 seconds.
  4. It answers: "Your trail shoes (order 1060) should arrive first. Your headlamp and batteries (order 1061) are delayed by weather." 1,184 tokens, 1.40 seconds.

A real agent run: four trips to the model, with input tokens growing on every trip

The whole run took 4.96 seconds and 3,756 input tokens. Notice the input grows every trip. The model keeps no memory between calls, so the app resends the whole conversation each time, and each trip costs more than the last.

Agent, workflow or one model call, on the same 10 questions

I ran the same 10 order questions three ways, with the same data, system prompt, model and grader (a code check for the facts each answer needed).

  • Agent loop: the model chooses tool calls until it answers, with a cap of 8 model calls.
  • Fixed workflow: code pulls the email and any order number from the question, looks up the customer, takes the order named or else the newest one, gets its tracking, then calls the model once.
  • One model call: the question alone, no tools.

Correct answers, cost and time per question for an agent loop, a fixed workflow and one model call

Approach Correct of 10 Model calls per question Input tokens per question Cost per question Seconds per question
Agent loop 10 3.6 2,912 $0.0086 9.4
Fixed workflow 7 1 321 $0.0024 2.8
One model call 0 1 108 $0.0031 4.9

The single model call had no data, so it asked for an order number every time. The workflow missed three questions, all for the same reason: it only ever tracks one order. Asked "Which of my orders will arrive first?", it tracked the newest order and could not compare. Asked about a rain jacket that was the older order, it answered "the tracking information I have is for a different order."

The agent got all 10, because it chose its next step from what the last tool returned. When get_orders came back with two orders, it asked for both.

What the agent loop costs

Averaged over the 10 questions, the agent was 3.4 times slower than the workflow and cost 3.6 times as much. It used 9 times the input tokens, because every trip resends the conversation.

The workflow's misses could be fixed with more code, such as tracking every order. The agent handled them with no new code. That is the trade: an agent costs more per question and saves you writing a branch for every case.

When an agent loop doesn't stop

Nothing in the loop itself makes it end. The model decides when it has enough, and a model that keeps calling tools keeps costing money. Every agent needs a cap on trips. In this demo the cap was 8 model calls, and no question came close: most used 3 or 4.

When not to use an agent

Use a workflow when the steps are always the same: look up the customer, get the order, answer. It is faster, cheaper and easier to test. Use an agent when the next step depends on what the last one returned, such as how many orders a customer has or which one they mean.

A fair limit on these numbers: it is 10 questions, and the grader checks for required facts, so it can miss a right answer worded differently.

FAQ

What is the difference between an AI agent and a chatbot?

A chatbot answers from what it was sent. An agent can call tools to get more information or take actions, and it decides which tools to call, one trip at a time, until it can answer.

What is the difference between an agent and a workflow?

In a workflow, code decides every step and the model is called once at a fixed point. In an agent, the model decides the next step each trip. Workflows are cheaper and more predictable. Agents handle cases nobody wrote code for.

What are examples of AI agents?

Coding assistants that read files, run tests and edit code until the tests pass; support assistants that look up orders and accounts before answering; and research assistants that search, read pages and search again. Each one is a model choosing tool calls in a loop.

Filed under How AI works