What is the agent loop? Claude Code, Codex, Cursor and Grok
By Flavio
The agent loop is the cycle a coding agent repeats: look, act, check, and go again. How Claude Code names that cycle, and what the other tools document.
The agent loop is the cycle a coding agent repeats until the task is done or you stop it. It looks at the goal and the project, takes an action, reads the result, and decides again.
The agent loop in the free AI Fundamentals course is the lesson that owns the idea: perceive, decide, act, evaluate, and what a stuck loop looks like. This entry is the short definition, plus the words Claude Code, Codex, Cursor, and Grok Build use for that cycle.
How the agent loop works
A chat answer is one step. A loop is the tool calling the model again after a tool result comes back. The model does not run the command itself. The harness runs it and returns the output. That output is the next thing the model sees.
Claude Code’s glossary names the cycle: gather context, take action, verify results, and repeat. Each tool result informs the next step. You can interrupt it. Hooks, skills, and MCP attach to phases of that cycle, not to a separate product.
Agentic coding is the way of working this cycle enables. The loop is the mechanism. The session is the chat that contains many turns of it.
The agent loop in each tool
| Claude Code | Codex | Cursor | Grok Build | |
|---|---|---|---|---|
| Name in the docs | Agentic loop | Not named. The guide says the agent chooses next steps, uses tools, and adjusts | Not named. Agent keeps calling tools until the task ends or you steer it | Not named for the TUI. The API overview says you can put grok-4.7 in your own loop |
| The cycle they describe | Gather context, take action, verify, repeat | Plan, use tools, check results, adapt, inside your permissions | Instructions, tools, and a model. No cap on tool calls in one task | A coding agent in a session. /goal adds its own check after every round |
| How you stop a step | Interrupt the loop | Interrupt or stop. An instruction change does not undo a finished action | Enter queues. Cmd+Enter sends now. A follow-up can steer at the next tool call | Not documented as a loop interrupt. /goal pause pauses a goal |
Claude Code
The glossary defines the agentic loop as that four-part cycle, and a turn as one response: your message, then any number of tool calls, until Claude finishes. Stop hooks fire at the end of a turn. A session is many turns. The loop is what happens inside one.
Most extension points plug into a phase. A hook on PreToolUse runs before the action. A skill loads when the phase needs it. You can interrupt and redirect. See the glossary.
How Claude Code mods change the agent loop is the in-process version of that hook: a handler that can pause or rewrite an event before the rest of the loop continues.
OpenAI Codex
The agent guide does not define a term called the agent loop. It describes the same shape. You hand over a goal. The agent chooses next steps, uses tools, and adjusts from what it finds. You review, redirect, or pause.
One sentence there matters for the loop. Changing an instruction does not undo an action that already finished. If it edited a file, the file stays edited until you revert it. See Working with agents.
Cursor
The agent page does not say “agent loop”. It says Agent can complete coding tasks, run terminal commands, and edit code, with no limit on the number of tool calls in one task. That is a loop with no fixed length.
While it is working, Enter queues your next message. Cmd+Enter sends it immediately. A follow-up can also land at the next tool call, so the action already in progress is not cut off. In the CLI, Enter steers at a safe boundary, and Enter again interrupts the turn. See Cursor Agent.
Grok Build
The Build overview does not define a loop inside the TUI. It does say you can drop grok-4.7 into your own agent loop, IDE integration, or coding tool. That sentence is about the API model, not about a mode named loop in grok.
/goal is the documented multi-round run. After every round, Grok checks progress and either continues, stops to ask you, or treats the goal as done. A panel of reviewer subagents has to agree before a goal is marked complete. That is a loop with a judge. It is not the glossary definition above. See Grok Build and Modes and commands.
Details checked against each vendor’s documentation in November 2026.
A real example
A request with a check, so the verify step has something to look at:
The RSS feed keeps 30 items. Change the limit in src/pages/rss.xml.ts to 20. Run npm run build. If the build fails, stop and report the failure. Do not edit any other feed field.
In Claude Code that is one turn if the edit and the build finish before Claude stops talking, or several turns if you reply in between. The loop is the tool calls inside the turn: read the file, edit it, run npm run build, read the output.
The same words in Cursor can keep going across as many tool calls as the task takes. There is no documented cap. In Codex, a finished npm run build stays finished even if your next message says to skip the build. In Grok Build, a long version of this job is a /goal with a finish line the reviewers can check, such as npm run build passing and no other feed field changed.
When to use a loop
Use an agent, which means this cycle, when the next step depends on what the previous step found. A failing test, then a fix, then the test again, is the pattern. Give your coding agent a state machine is the version you write down when the states get too loose to leave inside the prompt.
Skip it when you want one answer and no edits. A read-only question does not need a shell. Cursor’s /ask and a plan-first pass exist for that. The course lesson is where the “stuck loop” case is spelled out: a goal with no way to tell you are done.
Pitfalls
“Always ask me first” inside the prompt is not a phase of the loop. Claude Code’s permission check, Codex’s approval policy, and Cursor’s run mode are. The model can be mid-tool-call when your chat message arrives. Cursor documents steering at the next tool boundary so the in-flight call is not thrown away.
A Stop hook does not mean the task is finished. Claude Code fires it when a turn ends. Events Logger can record those turns. A log of them counts responses, not merged pull requests.
Grok’s /goal looks like a rename of the basic loop. It adds reviewer subagents and a status you can pause. A normal session does not run that panel. The commands page keeps them separate.
Codex will not roll back a tool that already ran because you sent a better instruction. Revert the file, or say what the next step should undo.
Related on flaviocopes.com
- The agent loop is the course lesson: the cycle, and how a loop gets stuck.
- How Claude Code mods change the agent loop pauses a Bash call inside the cycle until you choose.
- Give your coding agent a state machine turns an open-ended loop into named states.
- The big players in AI coding is the map of the products that run this cycle.
- Events Logger is a place to record turn-end hooks. The hooks entry has the
Stopexample.
Related glossary entries
- Agentic coding: Agentic coding means directing an agent that reads your repo, edits files and runs commands. How that loop works in Claude Code, Codex, Cursor and Grok Build.
- Coding agent: A coding agent uses a model and tools to read a repo, edit files and run commands in a loop. How Claude Code, Codex, Cursor and Grok Build each do that job.
- Agent harness: An agent harness is the program around a model: files, a shell, permissions, and the loop. How Claude Code defines it, and what Codex, Cursor and Grok document.
- Hooks: Hooks run your own scripts when a coding agent calls a tool or stops. How hooks work in Claude Code, Codex, Cursor and Grok Build, with examples.
Want me to talk about your product? You can sponsor this site.