What an agent loop is (and isn't): state, action, stop
DEV Community

What an agent loop is (and isn't): state, action, stop

An agent loop is the cycle in which a language model decides an action, that action runs, and the result feeds back in as context for the next decision, until a stopping condition is met. That repetition-decide, act, observe, repeat-is what separates an agent from a chat. A chat answers each message once; an agent runs that cycle on its own, many times, until the task is done. This is the first post in a series on how AI agents work under the hood, and it starts with the piece that gives the whole thing its name: the loop. TL;DR - An agent loop has three pieces: a state (what the agent knows so far), an action (what it does each turn), and a stopping condition (when it decides it's done). - The difference from a chat isn't the model, it's who runs the cycle: in a chat you run it (read, run, ask again); in an agent the program does. - "Build me an app" is a prompt for a chat and a goal for an agent. Same sentence, two different architectures. What an agent loop is A language model, on its own, does one thing: it takes text and produces text. One input, one inference, one output. It doesn't run anything, doesn't remember what came before, doesn't check whether it got it right. Each call is independent and ends the moment the model stops writing. An agent wraps that inference in a cycle. The model's output is no longer the final answer but a decision: "run this command," "read this file," "search for this." The program executes that decision, captures the result, and hands it back to the model as new context. Then the model decides again, with more information than before. That turn repeats until the task is done. The new piece isn't the model, it's the cycle around it. The same model that would answer once in a chat gets called ten or fifty times inside a loop, accumulating context at each step. That's why an agent can solve multi-step tasks a chat can only describe: not because the model is more capable, but because its output feeds back instead of ending. The three components: state, action, and stopping condition Every agent loop, however simple or complex, comes down to three pieces. If you understand these three, you understand the whole pattern. State. It's everything the agent knows at this point: the goal, the history of actions it has already taken, and the results it got. The state grows on every turn, because each action and its result get added to the context. The first entry in the state is the goal; the last is what it just observed. Action. It's what the agent does on each iteration. Almost always it's calling a tool: running a command, reading or writing a file, doing a search, calling an API. One detail that matters and that the series will keep repeating: the model doesn't execute anything. The model chooses the action; the program runs it. That separation is what makes the loop safe and debuggable. Stopping condition. It's the rule that cuts the cycle off. It can be the model emitting a "done" action, a verifiable goal being met (the tests pass, the file exists), or an iteration cap as a safety net. Without a clear stopping condition, an agent either doesn't finish or doesn't know it already has. The fastest way to pin down these roles is to see who handles each one: | Component | What it is | Who handles it | |---|---|---| | State | The goal plus the history of actions and results | The program accumulates it | | Action | The decision of what to do this turn | The model chooses it, the program runs it | | Stopping condition | The rule that decides when to cut off | The program checks it | The split in that last column is the center of the pattern: the model provides the judgment (what to do now), and the program provides the execution and the control (doing it and deciding when to stop). Confusing those two roles is the cause of most agents that misbehave. Keep reading That is the first half. The full walkthrough - with the rest of the implementation, the trade-offs and the things that only show up in production - is on my blog: Read the full post on ramonchancay.me โ†’ Originally published at www.ramonchancay.me/blog/what-is-an-agent-loop. Top comments (0)

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.