Topic 2 of 4 · 5 min read
Autonomous loops for open-ended tasks
Goal: Identify the property of a task, such as unknown steps or environment feedback, that makes an autonomous loop necessary.
An agent is a loop in which the model chooses the next action, your code runs it, and the result goes back to the model, until the model says it is done or a limit stops it. Use one only when you cannot write the steps in advance.
Check yourself · 1 of 3
Which task most clearly needs an autonomous loop?
The loop
The mechanics are the same across Claude, ChatGPT, Gemini and open-weight models with tool calling. The model gets a goal and a list of tools. It replies with a tool request, such as run_tests or search_files. Your code runs the tool and appends the result to the conversation. The model reads it and picks the next move. The ReAct paper (2022) describes this reason-act-observe cycle, and most agents still use it.
Which tasks need it
Three properties, any one of which can justify a loop:
- The number of steps is unknown. "Find why this test fails" might take two file reads or twenty.
- The order depends on what earlier steps return. If the next step cannot be chosen until you have seen the last result, no fixed path can encode it.
- The environment pushes back. Tests fail, a page does not load, an API returns an error. The model must read that feedback and change course.
Coding agents such as Claude Code or Cursor fit all three: the task is open-ended, each file read changes the plan, and the compiler and test runner give real feedback.
Before and after
Weak framing: "Build an agent that writes our weekly status report." The report always pulls the same three sources and has the same sections. Steps are known and the order never changes. This is a workflow.
Better framing: "Given a failing CI job, find and fix the cause." The model does not know in advance which logs matter, which file is wrong, or how many attempts a fix takes. After each edit the test run says whether it worked. That is a task for a loop.
What a loop needs to be safe
- A stop condition: a maximum number of turns, a time limit and a budget.
- Tools with narrow permissions: read-only where possible, and approval before anything that changes or deletes data.
- A way to check progress: tests, a linter, a diff a person reviews.
- A log of every action, so you can replay what happened.
What goes wrong
- Wandering. Without a clear goal the model keeps exploring and burns turns.
- Repeating itself. It retries the same failing call with small changes. A turn limit and a rule to stop after repeated errors help.
- Confident wrong finish. The model says it is done when it is not. Check against something outside the model, such as a passing test.
- Using an agent where a workflow would do, which pays loop costs for a path that never varies.
Examples
Failing CI job
A job fails with a stack trace. The agent reads the trace, opens the file, edits a function, runs the test, and sees a different failure. Next it reads a config file it did not know about. The path is different on every run and each test result steers the next step, which is the case for a loop.
Weekly sales summary
Every Monday: query the sales table, ask a model to write three bullet points, post them to a channel. The steps and their order never change and there is no feedback to react to. A workflow does this at a fraction of the cost and with the same output shape every week.
Key points
- An agent is a loop: the model picks an action, code runs it, and the result goes back to the model.
- Use a loop when the number of steps is unknown, the next step depends on the last result, or the environment gives feedback.
- A task with the same steps in the same order every time is a workflow, however much language it involves.
- Every loop needs a turn limit, a budget, narrow tool permissions and a log of actions.
- Check the model's claim of being done against something outside the model, such as a test.
Common mistakes
- Choosing an agent because the task involves a lot of text, when the steps are actually fixed.
- Starting a loop with no turn limit, budget or stop condition.
- Accepting 'done' from the model without a test or other external check.
- Giving the loop write or delete access to systems when read-only tools would do.
Check yourself · 2 of 3
In an agent loop, what happens right after the model asks to run a tool?
Check yourself · 3 of 3
You are designing a research agent. Which control prevents it from running for hours on a vague goal?
Practice items, not counted towards your mastery.
Start here
- Building effective agents (Anthropic) (opens in a new tab)ArticleFree
Defines workflows versus agents and says to find the simplest solution first; the source of this skill's framing.
- A practical guide to building agents (OpenAI) (opens in a new tab)DocsFree
A second vendor's view on when a use case justifies an agent and how to design the loop.
- Workflows and agents (LangGraph docs) (opens in a new tab)DocsFree
Code-level side-by-side of fixed workflows and agent loops, with runnable examples.
More for this topic
- ReAct: Synergizing Reasoning and Acting in Language Models (opens in a new tab)PaperFree
The original description of the reason-act-observe loop.
- Tool use overview (Claude docs) (opens in a new tab)DocsFree
Shows the request, run, return cycle in a real API.