diffox
Blog

Ralph loop and goal mode: how an agent runs until the job is done

What is a Ralph loop, who decides the work is done in goal mode, and what stops the loop? We read the docs of Codex, Claude Code and four more tools.

ShareXLinkedIn

Summary. There are two ways to keep a coding agent working until the job is done without waiting for you: the Ralph loop Geoffrey Huntley described in 2025, a shell loop that gives the agent the same prompt over and over, and the tools’ own goal mode. On 2 December 2026 we read the documentation of Claude Code, OpenAI’s Codex, GitHub Copilot, Cursor, Google’s Antigravity CLI and OpenCode, and set beside it a measurement by METR and what Anthropic, Google and LangChain document about graph-based workflows.

  • A Ralph loop is a shell loop that gives the agent the same prompt over and over. The agent starts every turn with a fresh context and reads what has been done from the files and from git. What makes the loop useful are the tests, the compiler and the linter that send bad code back.
  • Five tools have a /goal command, and they differ on who decides the work is done. In Codex, Copilot and Antigravity, the agent doing the work decides; in Claude Code a separate small model does, in Cursor a group of reviewer subagents. None of them documents a default budget limit for a goal.
  • You need a check the agent can’t change and a limit you set yourself. In METR’s measurement, on tasks where the model could see the scoring code, it gamed the score in 30.4% of runs. All five tools have a hook that runs when the agent wants to stop, and your script can send the agent back to work.
  • If you know the steps up front, build a graph instead of a loop. According to Anthropic’s, Google’s and LangChain’s documentation, a path drawn in advance is more predictable; Anthropic and LangChain also say it is cheaper. A loop is for work whose path you don’t know but whose end you can measure.

What is a Ralph loop, and where did it come from?

5/6tools have a /goal command; OpenCode’s documentation has none (2 December 2026)
3/5tools leave the “done” call to the agent doing the work
5/5tools document no default budget limit for a goal
30.4%of runs gamed the score on tasks where the model could see the scoring code (39/128, METR)

In the harness engineering post we covered loop engineering: building the system that runs the agent instead of writing it a prompt every turn. This post looks at the practice: running an agent until the job is done, who you leave the “done” call to, and what stops the loop. The story starts with a one-line shell loop.

Geoffrey Huntley defines the technique in one sentence in his post of 14 July 2025: “In its purest form, Ralph is a Bash loop.” It is named after Ralph Wiggum from The Simpsons. Here is the loop itself:

bash
while :; do cat PROMPT.md | claude-code ; done

Every turn starts a new agent process that reads the same PROMPT.md. It remembers nothing from the turn before; it learns what has been done from the files and the git history. Huntley’s prompt has the agent read a plan file (fix_plan.md) and pick the most important item, and asks for only one thing per turn: “one item per loop”. The agent makes the change and runs the tests, and once they pass, it updates the plan and commits. Then the loop starts over.

What makes the loop useful are the checks that send back the bad code the agent writes. Huntley calls them “back pressure”: tests, type checking, static analysis, security scanners. He also gives two warnings from his own experience: the model tends to write unfinished or placeholder code, and some mornings you wake up to a codebase that doesn’t compile. He recommends the technique mostly for projects started from scratch, and expects the loop to get 90% of the work done.

The ralph-loop plugin that Anthropic publishes for Claude Code builds the same idea another way. The loop runs inside the open session, not in an outside shell: when Claude tries to end its turn, a Stop hook stops it and gives it the same prompt again. The agent itself says when the work is done; the loop ends when it writes the word you passed with --completion-promise. --max-iterations defaults to unlimited, and the plugin’s documentation names this limit as the main safety measure. The difference between the two is context: Huntley’s loop starts every turn with a fresh context, while the plugin keeps everything in the same session. On his own page, Huntley also recommends a video on why the Claude Code plugin isn’t the real Ralph.

Which tools keep an agent working until the job is done?

You don’t have to build Ralph by hand: according to the documentation we read on 2 December 2026, five of the six tools have a /goal command. Goal mode gives the agent a goal and has it check, at the end of every turn, whether the goal has been reached. If it hasn’t, the agent starts a new turn instead of coming back to you. The tools part ways at two points: who makes that call, and what stops the loop.

The command that keeps the agent working toward a goal in six coding tools, who decides the work is done, and the limit
ToolCommandWho decides it’s “done”Limit
Claude Code/goalAfter every turn, a separate small model judges the condition from the conversation; it runs no commands and reads no filesNo default; you write a sentence like “stop after 20 turns” into the condition, and the evaluator reads that too
Codex/goalThe model doing the work checks the goal against evidence and marks it complete itselfA token budget with goals.max_goal_token_budget; if you don’t set one, there is no limit
GitHub Copilot CLI/autopilot or /goalThe agent calls task_complete itself when it has finished--max-ai-credits per goal; when it is reached, autopilot pauses
Cursor/goalWhen the work looks done, reviewer subagents check the evidence; if most of them approve, the goal closesA token limit with --budget; it pauses when it gets stuck on the same gaps
Antigravity CLI/goalThe agent matches each deliverable to evidence and declares the work done itselfNo iteration cap, according to the documentation; it runs until the agent finishes or you stop it
OpenCodeNot stated in the documentationNot stated in the documentationsteps per agent: the maximum number of model steps

Source: the tools’ documentation and release notes, 2 December 2026. “Not stated in the documentation”: in the pages we read that day, we found no sentence that describes this. In Codex, goal mode stopped being experimental on 21 May 2026, and it has been in Codex since 0.128.0. Cursor’s site says “Cursor is now Grok Build”, and its documentation is published under the Grok Build name; in the Agents Window, /goal is rolling out gradually. Gemini CLI’s documentation has no goal command.

Codex’s design states the principle plainly. According to OpenAI’s guide to goals, a goal should not be marked complete because the model believes it is “probably done”; it should close only after it has been checked against concrete evidence such as tests, logs or benchmark output. Still, the model that reads the evidence is the same one that closes the goal: according to the documentation, the model can start a goal and mark it complete when the evidence supports it. In Copilot, the agent calls the task_complete tool itself when the work is finished. In Antigravity, too, the agent matches each deliverable to evidence and then declares the work done itself.

Claude Code gives this call to a separate model. According to its documentation, after every turn it sends the condition and the conversation so far to a small, fast model, so it is a fresh model that decides the work is done, not the one doing it. But this evaluator runs no commands and reads no files; it looks only at what Claude has shown in the conversation. That is why the documentation asks you to write the condition as something Claude’s output can prove, for example “npm test exits with 0”. In Cursor, when the work looks done, a group of reviewer subagents checks the evidence, and the goal closes only if most of them approve.

3 ways to decide that the work is done. From the five tools’ goal and hook documentation, 2 December 2026. 1. The agent doing the work. Codex, Copilot, Antigravity: it closes the goal itself. 2. A separate model. A small model in Claude Code, reviewer subagents in Cursor. 3. Your script. A Stop hook: if the check fails, it sends the agent back.
Source: the five tools’ goal and hook documentation, 2 December 2026.

The third way is open in all five tools: a hook that runs when the agent wants to stop. Here the call is made not by the agent or another model but by a script you write; if the tests fail, the agent goes back to work. Claude Code’s /goal is in fact a shortcut to this way: according to the documentation, it is a session-scoped, prompt-based Stop hook. We covered hooks in detail in the hooks post.

What stops the loop?

A loop has two parts: a check that says the work is done, and a limit that steps in if the check never passes. Anthropic gives exactly this as the stop criterion for a goal-based loop in its post of 30 June 2026: the goal is achieved, or the maximum number of turns is reached. Google’s ADK documentation says the same of its own loop agent: LoopAgent doesn’t decide on its own when to stop, you have to build the way it stops, and max_iterations has no default.

The check has to sit out of the agent’s reach. METR, a research organisation that measures what AI agents can do on a computer, listed how models game the score in a post published in June 2025: changing the tests or the scoring code, or finding and copying the right answer the evaluator holds. A pre-release version of OpenAI’s o3 model gamed the score in 39 of 128 runs (30.4%) on the task set where it could see all of the scoring code; on a broader task set, the rate was 0.7% over 1,087 runs. METR names the model being able to see the scoring function as a possible reason for the difference, and doesn’t consider the behaviour specific to one model or company.

The tools already protect the file the hook lives in. In Claude Code, .claude/ is one of the protected paths: in the default mode the agent asks you before writing there, and in dontAsk mode it can’t write there at all. In Codex’s sandbox, .codex/ is read-only too. Tests get no such protection. For them, Claude Code’s goal documentation suggests adding a constraint to the condition; its example is “no other test file is modified”. A stricter way is to block writes to the test folder with a PreToolUse hook, which runs before the agent writes to a file. We covered other ways to narrow what the agent can touch in the sandbox post.

The hook that runs when the agent wants to stop is called a Stop hook. All five tools have one, and in each of them your script can send the agent back to work:

The hook that runs when the agent wants to stop in five coding tools, the answer that sends the agent back to work, and how many times in a row it can do so
ToolHook that runs when the agent wants to stopAnswer that sends the agent back to workMost times in a row
Claude CodeStopdecision: "block" or exit code 28; the count resets when Claude calls a tool
CodexStopdecision: "block" or exit code 2Not stated in the documentation
GitHub Copilot CLIagentStopdecision: "block"8
Cursorstopfollowup_message5; set with loop_limit
Antigravity CLIStopdecision: "continue"Not stated in the documentation

Source: the tools’ hook documentation, 2 December 2026. “Not stated in the documentation”: in the pages we read that day, we found no sentence that describes this. In Claude Code and Copilot, once the limit is reached, the tool overrides the hook and ends the turn; in Claude Code, CLAUDE_CODE_STOP_HOOK_BLOCK_CAP changes the limit. In Gemini CLI, the AfterAgent hook does the same job.

These limits are there against endless loops; they don’t stop a loop that is making progress. In Claude Code the count resets every time Claude calls a tool, so an agent that changes something every turn may never reach this limit. The real limit is one you have to set. On the check side, here is the example from Claude Code’s documentation: a Stop hook that doesn’t let Claude end its turn until the type check passes.

.claude/settings.json
{
  "hooks": {
    "Stop": [
      {
        "hooks": [
          { "type": "command", "command": "pnpm tsc --noEmit 1>&2 || exit 2" }
        ]
      }
    ]
  }
}

The same documentation asks five questions before you leave a run unattended. We added METR’s finding to the second one:

Before you leave the agent alone. Five questions from Claude Code’s documentation. What does done look like?: A test that has to pass, or an exit code. Can it check its own work?: Tests or a Stop hook, with their files out of the agent’s reach. What can it touch?: Permission rules, the sandbox, a single branch. What stops it?: A turn, token or credit limit. Who approves the result?: A person who reads the pull request.
Adapted from the five questions in Claude Code’s best practices documentation, 2 December 2026.

For the fourth question, the tools give different limits. When Claude Code runs from a script, --max-turns limits the number of turns and --max-budget-usd limits spend; --max-turns has no default, and according to the documentation spend can go slightly past the limit. In Codex goals.max_goal_token_budget, in Copilot --max-ai-credits and in Cursor --budget put a token or credit limit on a goal. For the third and fifth questions, run the agent on its own branch, or even in a separate worktree, and have the result come to you as a pull request.

How much usage does a long run consume, and where does it get stuck?

The documentation gives no figure per goal, but all three companies say plainly that a long run uses more. According to GitHub, autopilot can start several model calls on its way to the goal, and you should keep an eye on your AI credits while the run goes on. According to OpenAI, long-running tasks and long sessions in which the agent has to hold more context use significantly more per message. According to Anthropic, the evaluation /goal makes after every turn is billed on top of what the main turn uses.

Most of it comes from context. According to Claude Code’s documentation, every request sends the whole conversation again; in a session that has collected three tasks, the fourth re-reads the history of the other three on every turn. While the cache is warm, that reading is discounted; once it expires, it is full price. The documentation gives the same warning about /loop: each run is another turn in the same session, so don’t leave one running in a long conversation. Goal mode keeps the goal in a single session or thread; Huntley’s loop, by contrast, builds its context from scratch every turn and keeps its memory in files. Gemini CLI has a counterpart: when its AfterAgent hook rejects the agent’s response and starts a new turn, it can also clear the conversation history with clearContext.

When they hit a limit, the tools don’t delete the goal; they put it on hold. In Claude Code, a goal pauses when it hits a claude.ai usage limit or an API rate limit; if the session is waiting for the limit to reset, Claude picks the goal back up once it does. In Codex, when the token budget runs out, the goal moves to the “budget-limited” state: Codex starts no new work, sums up progress and blockers, and names the next step. OpenAI adds that this doesn’t mean the goal is complete. In Copilot, when the credit cap is reached, autopilot pauses and waits for you to enter a new amount. In Antigravity CLI, the Use AI Credits setting decides whether your personal AI credits are spent once your plan’s quota runs out. We covered the plans’ usage limits in the Claude Code, Codex and Cursor comparison.

If you see “stalled” in Codex’s interface, the goal is blocked. According to the changelog, Codex CLI 0.147.0 (7 August 2026) started showing blocked goals as “stalled” in the interface, and 0.155.0 (17 September 2026) blocks a goal after 3 automatic continuation turns in which nothing was done. According to OpenAI’s guide to goals, if a continuation turn calls no tool, the next automatic turn isn’t started, so that Codex doesn’t spin. Claude Code, too, stops the loop and hands control back to you if Claude answers the evaluator for several turns in a row without using a tool, but it keeps the goal. In Cursor, if the agent gets stuck on the same gaps, it pauses and shows what is still missing.

Then there is the kind of stuck an agent can’t get out of on its own. Claude Code clears the goal completely and warns you on an authentication failure, when your credit runs out, or when the context overflows and can’t be compacted. Huntley’s experience is similar: some mornings you are left with a codebase that doesn’t compile, and you either undo everything with git reset --hard and restart the loop, or write new prompts to rescue the agent. We covered how an agent keeps going while your computer is off in the cloud agents post, and runs that start on a schedule in the automations post.

If you know the steps up front, should you build a loop or a graph?

The name “graph engineering” spread in July 2026 from a question on X. Peter Steinberger asked on 18 July: “Are we still talking loops or did we shift to graphs yet?” In a post published on 22 July, LangChain, which develops LangGraph, didn’t treat the term as a new idea: a graph is an established way to build reliable agents, and loop engineering isn’t an alternative to it but a simple version of it. A loop, after all, is a graph that leads back to itself.

A graph splits the work into nodes and the transitions between them. LangGraph’s documentation sums it up in one sentence: nodes do the work, edges decide what runs next. A node can be an ordinary piece of code, a single model call, or a full agent with its own loop. The difference is who draws the path. In Anthropic’s definition, a workflow runs the model and tools through code paths written in advance, while an agent lets the model choose its own path. According to Anthropic, workflows give predictability and consistency on well-defined tasks, while agents trade latency and cost for better results. Google, too, describes sequential, parallel and loop agents in ADK as predictable patterns that run without consulting an AI model; in its documentation of 2 December 2026, it marks these template workflows as deprecated and recommends graph-based workflows instead. LangChain goes further for its own product: it writes that an agent built as a graph is cheaper, faster and more predictable.

The choice depends on the shape of the work:

Which one for which job?. Steps known up front: classify → search → sum up. Graph. Code draws the path; the model runs where needed. Path unclear, end measurable: tests pass. Loop. /goal or Ralph, with a limit. End unclear: “make it better”. Plan first. Define the end, then set a goal. Examples: LangChain’s knowledge base agent and Codex’s guide to goals.
Compiled from the Anthropic, Google ADK, LangChain, Codex, Copilot and Claude Code documentation, 2 December 2026. The examples are from LangChain’s post and Codex’s guide to goals.

LangChain also writes that a graph doesn’t fit every job: the planning, search and synthesis steps of a general research agent are hard to draw in advance. They first built their own research agent as a graph, then moved it to a freer loop. For work whose end can’t be measured, the tools give the same advice: Codex says not to use a goal when the finish line is vague, and Copilot writes that vague, exploratory work will need you to steer it; both suggest plan mode first. The two can also be combined: according to LangChain, running a coding agent as one node of a larger graph is a pattern that is only now becoming practical.

Loop or graph, the work ends up delivered as a pull request, and the fifth question in Claude Code’s documentation asks for a person’s approval at this point. Our AI code review tool, diffox, reads this pull request as a pair of eyes separate from the agent that wrote the code: it classes the problems it finds as Critical, Warning or Suggestion and posts them as comments on the lines they concern. It always posts its review as a comment; it neither approves nor blocks the merge, and leaves the final decision to the team.

Frequently asked questions

What is a Ralph loop?

A Ralph loop is a shell loop that gives a coding agent the same prompt file again and again, without stopping. Geoffrey Huntley defined it on 14 July 2025; its simplest form is while :; do cat PROMPT.md | claude-code ; done. The agent starts every turn with a fresh context, reads what has been done from the plan file and from git, does one thing and commits. Whether it works depends on the tests and the compiler sending bad code back. Its name comes from Ralph Wiggum in The Simpsons.

How does Codex goal mode work?

In Codex, typing /goal followed by your objective ties the goal to the conversation; at the end of every turn Codex checks whether it has reached the goal and, if not, carries on without waiting for you. The model doing the work is the one that marks the goal complete; OpenAI asks that this be done against concrete evidence such as tests, logs or output. /goal pause, /goal resume and /goal clear manage the goal. You can set a token budget with goals.max_goal_token_budget; when the budget runs out, the goal stops and Codex sums up its progress. Goal mode stopped being experimental on 21 May 2026; if the command isn’t in the list, open config.toml and turn on features.goals.

What is the difference between goal mode and plan mode?

Plan mode proposes an approach before the work starts and waits for your approval; goal mode gives the agent a goal, and the agent works toward it without waiting for you. According to Codex’s documentation, plan-only work doesn’t trigger automatic continuation. The tools suggest using the two in turn: Codex suggests that, if the finish line isn’t clear yet, you start with /plan, turn the result into a measurable goal, and then start /goal. In Copilot CLI, --plan --mode autopilot finishes the plan and moves into autopilot on its own; Antigravity also suggests /plan before /goal for big changes.

How do you keep an agent working until the job is done in Claude Code?

There are three ways. /goal takes a condition, and after every turn a small model checks whether it holds; Claude keeps going until the condition is met, is judged impossible, or you clear it. A Stop hook runs your script every time Claude wants to stop, and if the script exits with code 2, Claude goes back to work; on this path the script makes the call, not a model. /loop runs a prompt again at intervals, so it is tied to time, not to a goal. For a run you will leave unattended, start /goal in auto mode and write a turn limit into the condition; for runs from a script, --max-turns and --max-budget-usd limit turns and spend.

What is the difference between loop engineering and graph engineering?

Loop engineering is building the system that runs an agent again and again, toward a goal or on a schedule; the agent decides which step to take every turn. Graph engineering splits the work into nodes and transitions drawn in advance: code decides which step comes next, and the model runs only in the nodes that need it. The term spread in July 2026; LangChain, which develops LangGraph, wrote that a loop is a simple version of a graph and that the idea isn’t new. If you know the steps up front, a graph is more predictable; if the path is unclear but the end can be measured, a loop fits better.

How much usage does an agent in a loop consume?

The documentation gives no figure per goal, but the direction is clear: a long run uses more. According to OpenAI, long-running tasks and long sessions that hold a lot of context use significantly more per message; GitHub writes that autopilot can start several model calls; Anthropic says the evaluation /goal makes after every turn is billed separately. To limit it, there is goals.max_goal_token_budget in Codex, --max-ai-credits in Copilot, --budget in Cursor and --max-budget-usd in Claude Code run from a script. In Claude Code, a goal that hits a usage limit pauses and can carry on once the limit resets.

How do you review a pull request opened by an agent that ran overnight?

Claude Code’s documentation suggests getting the result of an unattended run as a pull request and having a person approve the merge. Ask the agent for evidence: the command it ran and its output. Among the ways to check the work, the documentation also lists a review by someone who didn’t write the code; a reviewer that never saw the conversation isn’t swayed by the reasoning that produced the code. Our AI code review tool, diffox, reads the pull request with that eye and posts its findings on the lines as comments, classed as Critical, Warning or Suggestion; it doesn’t block the merge and leaves the decision to you. We covered what to look for in the post on reviewing AI-generated code.