Emrehan Görgeç
Agentic design patterns · Lesson 5 of 7All lessons

The agent loop

From fixed paths to a loop

The patterns so far follow paths you designed. An agent is different: the model decides what to do next, one step at a time, based on what it has seen.

This is the loop from the first lesson, now with the pieces from Part 1: tools to act, retrieval to look things up, context to remember, and evaluation to check.

1. Decide, act, observe

ONE STEP OF THE LOOP
Goal: “Find out why the nightly report is empty.”
  1. Decide“Check the job logs first.”
  2. Actread_logs(job="nightly_report")
  3. Observe“The query returned 0 rows after a schema change.”
  4. Repeat or finishA next step, an answer, or a question
Each observation goes back into the context and shapes the next decision.

2. Define when it stops

An agent needs clear endings: the goal is reached, it is blocked and needs help, or a limit is hit. Without them, it can loop, repeat itself, or keep spending.

3. Set hard limits in code

Limit the number of steps, the total cost, and the time. These limits live in your code, not in the prompt, so the model cannot talk its way past them.

4. Make progress visible

Log each decision, tool call, and result. When something goes wrong, the trace shows where the reasoning went off course.

When is it useful?

For open ended tasks where the path depends on what is found along the way, such as debugging, research, or multi step changes. Start with a workflow when you can. Use an agent when you cannot.