AI engineering path · Shipping with confidence · Topic 10 of 12

Agents: loops, plans and stopping

Not started

"Agent" is used loosely. A useful distinction, drawn for example in Anthropic's guide to building agents, is between **workflows**, where your code fixes the sequence of model calls and tools, and **agents**, where the model decides at each step what to do next, which tool to use and when it is finished. Mechanically an agent is the tool-use loop from the previous topics, run with more freedom and more steps: observe results, choose an action, act, repeat.

Why it matters: Agents can handle open-ended tasks that no fixed pipeline could, at the price of more cost, more latency and more ways to fail. Knowing when not to build one matters as much as knowing how.

  1. 1An agent is a tool loop that decides its own path

    The model looks at results so far, picks the next action, and repeats until it judges the task done.

  2. 2Prefer a workflow when the steps are known

    If you can write the steps down in advance, a fixed chain of model calls is cheaper, faster and easier to test.

  3. 3Stopping conditions are not optional

    Cap steps, tokens, time and cost; detect repeated actions; stop when the goal check passes or a human is needed.

  4. 4Make it observable and resumable

    Log every step, persist state, and require approval before irreversible actions.

1

How it works

The explanation, with examples you can run.

Start without one

The same guide recommends finding the simplest solution that works and adding complexity only when it clearly helps, which often means no agent at all. In order of increasing complexity:

  1. One well-prompted call, maybe with retrieval.
  2. A workflow: a fixed chain (classify, then draft, then check), routing (a cheap classifier picks which prompt handles the request), or parallel calls whose results are combined. Each step can be tested on its own.
  3. An agent: when you cannot know in advance how many steps a task needs or which tools, such as investigating a bug across a codebase or researching a question across many sources.

Agents trade cost and latency for flexibility, and their failures compound: a small mistake in step two can send steps three to twenty in the wrong direction.

Planning and the loop

Many agents interleave reasoning and acting (the ReAct pattern): the model states what it is trying to find out, calls a tool, reads the result, updates its plan. Some write an explicit plan or to-do list first and revise it as they go; keeping that plan in the context helps on long tasks. Either way, your code owns the loop and the budget.

Stopping conditions

The model deciding it is done is one way to stop, and not the only one you need. The example runs a fake agent that gets stuck repeating itself and shows the guards that end the run.

⌘/Ctrl + Enter
OutputNot run yet

Press Run to see what your code prints, in order.

Runs in this browser, in an isolated frame with no access to this page. console.log, info, warn and error are shown; code runs as a classic script, so use an async function for await.

A practical set of limits:

  • Step, token, time and cost caps per run, enforced by your code.
  • No-progress detection: repeated identical calls, or several steps without new information.
  • A goal check where one exists, such as tests passing, the form validating, or every required field being filled.
  • Human checkpoints: before irreversible or external actions (payments, emails, deletes, deploys), and when the agent is stuck or unsure.
  • A clean failure: when a limit is hit, return what was found and why it stopped, rather than a made-up final answer.

Running agents in production

  • Least privilege. Give the agent only the tools and permissions the task needs, scoped to the current user. Prefer read-only tools; gate writes.
  • Trace everything. Store each step's input, the model's decision, tool calls and results. Agent bugs are only debuggable from traces, and good traces become eval cases.
  • Persist state. A long run is a workflow with slow, failure-prone steps; checkpoint after each step so a crash or a provider outage resumes instead of starting over.
  • Evaluate the whole task and the steps. Measure task success on a set of realistic tasks, and also cost, number of steps and how often it needed a human.
  • Manage the context. Long runs fill the context window with tool results. Summarise or drop old results, and keep the goal and plan visible.
2

Check yourself

3 questions. Get them all right to finish this topic.

  1. 1.Every support email must be classified, then answered from the policy, then checked for tone. Agent or workflow?
  2. 2.An agent has called the same search tool with the same query five times in a row. What should a well-built loop do?
  3. 3.Your agent can send emails on the user's behalf. Which control matters most?

0 of 3 right so far. Get all three to finish this topic.