Skip to content

How an AI agent decides what to do next

A plain walk through the loop inside an AI agent: what it looks at, how it picks a step, and where its choices go wrong.

People who watch an agent at work often describe it as if it were thinking. It reads a message, pauses, checks something, then acts. What is actually happening is simpler and more mechanical than that, and understanding it helps you design better agents, judge vendors more sharply, and predict where things will go wrong.

The loop at the centre of every agent

Almost every agent runs the same basic cycle:

  1. Look. Gather what it knows so far: the goal, its instructions, the conversation or task, and the results of anything it has already done.
  2. Choose. Ask the language model: given all this, what is the best next step? The answer is either “use this tool with these details” or “I am finished, here is the result”.
  3. Act. The surrounding software runs the chosen tool, for example a stock lookup or a database search.
  4. Record. The result is added to what the agent knows, and the loop starts again.

That is it. There is no hidden plan being worked out in the background. Each step is a fresh decision made by reading everything gathered so far. This is why the quality of that “everything” matters so much.

What the model is actually looking at

When the agent chooses, the model sees a block of text that usually contains:

  • The standing instructions: who it works for, what it is for, what it must never do.
  • A description of each tool it may use, including what the tool does and what details it needs.
  • The task itself, such as a customer message or a document.
  • A record of the steps taken so far and what came back.

The model reads all of this and predicts the most sensible next move. It is very good at this when the instructions are clear and the tool descriptions are precise. It is poor at it when two tools sound similar, when instructions contradict each other, or when the record has grown so long that important early details get lost.

A practical example. A customer writes: “Need 20 packets of the red tea, deliver to Kurunegala, cash on delivery.” The agent looks, sees it has a stock tool, a delivery charge tool and an order draft tool. It chooses the stock tool first because it cannot confirm anything without knowing whether twenty packets exist. The stock tool says fifteen. The agent now looks again, with that new fact, and chooses to draft a reply offering fifteen now and five later, rather than creating the order. Each choice followed from the last result.

Where the choices go wrong

Knowing the loop tells you where the weak points are.

Vague tools

If one tool is called “search” and another is called “lookup”, the model has to guess which is which. Give each tool a plain name and a one-line description that says exactly when to use it and when not to.

Missing stop conditions

An agent without a clear definition of “done” may keep going: checking again, trying another tool, rephrasing. Set a maximum number of steps and tell it plainly what a finished result looks like.

Overconfidence after a bad result

If a tool returns an error or nothing at all, a poorly instructed agent may carry on as if it succeeded. Your instructions should say what to do when a tool fails, which is usually to stop and ask a person.

Too much history

On long tasks the record of past steps can crowd out the original instructions. Good agent design summarises older steps and keeps the key rules near the top.

Making decisions easier to trust

You cannot make an agent’s decisions perfect, but you can make them easier to check and harder to get badly wrong:

  • Ask for a short reason with each step. A single line such as “Checking stock because the order cannot be confirmed without it” makes the log readable for a manager.
  • Keep the toolset small. An agent with five well-described tools makes better choices than one with thirty. Add tools only when a real task needs them.
  • Put hard limits outside the model. If an order over a certain value needs approval, enforce that in the software around the agent, not only in its instructions. The model can forget or misread an instruction. The software cannot.
  • Test with awkward cases. Mixed-language messages, half-filled forms, requests for things you do not sell. These show you how the agent chooses when the path is not obvious.

What this loop does not do

An agent does not plan weeks ahead, weigh long-term strategy or understand your business the way a senior employee does. It makes one sensible local choice at a time based on the text in front of it. For many tasks, that is exactly enough. For decisions that need wider judgement, such as whether to extend credit to a struggling customer or how to respond to a complaint from a major buyer, the agent should prepare the facts and hand the choice to a person.

If you already have an agent running, open its log for a single task and read each step’s reason. If you cannot follow why it did what it did, the instructions or the tool descriptions need rewriting before anything else.

Tell us about the work that repeats.

Send a few lines about the task, the team and the systems involved. We reply within two working days with honest next steps, even if that means not working with us.