Skip to content

LLM Engineering·Lesson 49 of 67·advanced

Building an agent loop from first principles

An agent is the tool loop plus a termination policy, a budget and an approval gate — and the hardest part is deciding not to build one.


In this lesson · 22 min

  • →State what makes a loop an agent, and when one is justified
  • →Add a termination policy, a budget and a round ceiling
  • →Gate destructive tools behind an approval step
  • →Log every step so a run can be explained afterwards
  • →Manage a history that grows on every turn

The last lesson’s loop is already most of an agent. What is missing is everything that stops it: a budget, a ceiling, a way to finish, and a gate in front of anything irreversible.

Should you build one?

PythonPython not started

The minimal agent

A tool loop, plus a way to stop:

PythonPython not started

The finish tool is the termination policy. Without one, “done” is inferred from the model producing text instead of a tool call — which works, but gives you nothing structured to act on and no place to demand a citation or a confidence.

Budgets

Rounds are one ceiling; tokens are the one that maps to money:

PythonPython not started

Approval gates

Anything irreversible goes behind a gate. The gate lives in your code, never in the prompt:

PythonPython not started

Note that a declined call still returns a tool_result. The model needs to know it was refused — otherwise it waits for an answer that never comes, or tries again.

Observability

An agent that cannot be explained afterwards cannot be improved. Log every step as a structured record:

PythonPython not started

Two searches for nearly the same thing, visible immediately. That is the kind of waste a trace surfaces and a final answer hides.

The history problem

Every round resends everything, so an agent’s context grows steadily until it does not fit:

PythonPython not started

The first strategy is the one people skip and the one that pays most. A tool that returns a 20,000-token document body puts that body in every subsequent request; a tool that returns three relevant sentences and an id does not.

Putting it together

PythonPython not started

Try it yourself

Exercise 1

Write Budget with record(input_tokens, output_tokens), a spent_usd property at Opus 5 rates, and exceeded() returning a reason string or None.

Your turnPython not started
Exercise 2

Write gate(block, requires_approval, approved): run the tool normally, or return a declined tool_result with is_error when it needs approval it does not have.

Your turnPython not started

Check yourself

Why should a destructive tool be gated in code rather than forbidden in the system prompt?

Check yourself

An agent's cost grows faster than its number of rounds. Why?

Recap

  • An agent is the tool loop plus a termination policy. Most problems are workflows and should stay workflows.
  • Check complexity, value, viability and cost of error before building one; a “no” anywhere means a simpler tier.
  • Give it an explicit finish tool, and ceilings on rounds, tokens and spend — checked before each request.
  • Gate irreversible tools in code, and return the decline as a tool_result so the model knows.
  • Log every step as structured records; the trace is what makes a bad run explainable.
  • The history grows every round, so return less from each tool before reaching for summarisation.