The Build Loop Only Works If It Knows How to Stop
A build loop is safe when checks run inside the sandbox and the only exits are a pull request with evidence or a handoff to a person.

An AI coding agent build loop implements one ready step in a sandbox and has two exits: a pull request with evidence, or a handoff when the budget runs out.
A build agent that retries until someone notices the bill will ship confident code and a surprise invoice. The AI coding agent build loop is only a factory stage when a checker the agent does not control can pass it, and a budget can force a handoff to a person.
In this article: Build is narrower than the demo. The loop has three phases and two exits, four elements that stop specific failures, a runtime that starts as a GitHub Action, and a sandbox that breaks the lethal trifecta. A fictional workspace export shows an agent refusing a third attempt.
Everyone pictures this stage: the agent writes code, runs tests, and opens a pull request at lunch. It also has the most public evidence that the pattern works at real scale.
It is easy to get expensive. An unbounded agent retries forever, burns money, and opens pull requests that fail CI for reasons it could have caught. Ambient secrets plus a prompt-injected issue are an incident waiting for a trigger. Vague tickets produce confident, wrong code.
By the time work arrives, the ticket is ready, admission has attached a workflow and a budget, and the work graph has split the ticket into steps. Build implements the step, proves it, stays inside the budget, and stops cleanly. It does not call review. It leaves a pull request or a written question. A person stays at the merge gate.
What Build is allowed to touch

Build's loop has five parts. The Part column names each part, and the Build column says what that part is for this stage.
- Trigger. Build runs on a ready claim, or on a review correction to the change.
- Work. It implements in an isolated workspace, builds, tests, and opens a pull request linked to the ticket.
- Independent check. CI runs first, then review, then a person decides the merge.
- Memory. It recalls past fixes nearby, known failure patterns, and how this ticket was solved.
- Stop condition. It stops when checks pass and the pull request is open, or when the budget runs out and a person gets the question.
Shrinks for the human: watching sessions and checking each change by hand. Grows for the human: a goal a machine can check, the checker, and the stop rule.
Build consumes only ready work. An agent that accepts any ticket also accepts the vague ones, and it will not complain. The output is a change plus test results and known gaps. An honest "this works for workspaces under 10,000 rows; I did not test larger" beats a green check that hides the gap.
The agent must never touch production, never merge, never edit tests and code together to force agreement, and never widen the ticket. Scope questions are written back for a person.
The build loop, two exits
Waiting for CI after the pull request exists spends a full cycle on failures the sandbox could have caught. Run the checks inside the loop, before the pull request.
The three phases are gather context, act, and verify. Without verify, the agent reports success because nothing contradicted it. A verifier returns a fact, a pass or a fail, not an opinion.

Feedback sensors are linters, type checkers, unit tests, and the tests named in the validation plan. Build and test are one step in one ephemeral environment. MCP servers reach what built-in tools cannot: service logs, rows after a migration, a browser compared with the spec wireframe. Without them, a person checks by hand. The fallback is a Mechanical Turk: it looks automated, and a person is doing the tedious part.
Exactly two exits exist. Checks pass, and the agent opens a pull request with results. Or the budget runs out, and the assigned engineer gets the question. No third exit opens a failing pull request "for visibility" or keeps going past the limit.
Four elements, four failures


Each of the four elements replaces a habit that fails. The Element column names the element. Replaces says what it takes the place of. Failure without it names what goes wrong when the element is missing.
- A machine-checkable stop. It replaces the agent saying it is done. Without it, you get unbounded iteration, or a false claim of done.
- A separate checker. It replaces the maker grading itself. Without it, the agent passes itself.
- Persisted state. It replaces starting over after a crash or compaction. Without it, progress is lost and work is repeated.
- A budget with escalation. It replaces hoping someone notices. Without it, you get a bill instead of a handoff.
A mining study of 36,710 repositories found autonomous loops in 217 of 256 inspected, about 0.59%. Those files mostly recorded triggers. Goals appeared in 55, budgets in 101, and verifiers in 76.
Start at 90 minutes per attempt and at most two attempts, plus a separate money limit. Stripe caps CI rounds at two on one of the largest published fleets, for the same reason: past that, the agent is guessing, and each guess is billed.
A stop records what was tried, what broke, and the open question. Scope goes back on the ticket. Everything else goes to the engineer. The stages share that note. They do not call each other.
The model classifies. Code grades.
The model may report which acceptance criteria a change meets. Ordinary code turns that report into pass, retry, or escalate. "Stop after three tries" in a prompt is a suggestion. A counter is a rule.

Done needs a receipt. "Tests passed" is not evidence. Code must show that the checks passed, on this exact tree, after the last edit. Missing evidence is a failure. One row is not enough: an agent can pass by writing one trivial test and deleting the rest. Read tests, coverage, lint, types, and the acceptance criteria.
Stall and strict improvement. If two attempts fail the same set, the last one changed nothing. Stop. A new attempt replaces the current one only when its failures are a strict subset of the old ones. Otherwise the loop trades one gap for another until the money is gone.
The chat is not the memory. The model is stateless on every call. This gap is the session illusion. The plan, what passed, and what is left live on disk. A failed check rolls back to the last known-good commit, not to an earlier message.
AlphaCodium ran GPT-4 through reason, tests, code, and another pass on the failures. On the CodeContests validation set, accuracy rose from 19% on one well-designed prompt to 44%. Same model, different loop.
The ticket implementer
A loop engineering workshop makes Build the second lab. Lab one is a ticket enhancer: is this ticket a contract a machine can check? Lab two is the ticket implementer: did the work meet it?
The implementer takes only ready tickets and lets a harness check each criterion with evidence. A sample CRM walks a ticket with gaps, enhancer comments, a human "looks good to me," and a pull request with evidence. The fourth lab fixes broken pull requests alone and must know when to stop. Each lab is its own folder. The Claude Agent SDK and LangChain Deep Agents ports, across nine runtimes, show the same loop. Code: reliable-agentic-lab.
Start as a GitHub Action
Every loop starts as a plugin on a GitHub Action. A ready label fires the workflow. Claude Code runs headless. The result lands on the issue or as a pull request. No new infrastructure. GitHub is the audit trail.


The runtime ladder has three steps. The Step column names each one in order. Runs as says what the loop runs on at that step. Graduate when names the signal to move past that step.
- 1. GitHub Action. The loop runs as a Claude Code plugin on a repository event. Move on when time, memory, tools, or control flow exceed a CI runner.
- 2. Container and webhook. The loop runs on the Claude Agent SDK or LangChain Deep Agents in a cloud sandbox. Move on when state must last hours or days, or survive a restart.
- 3. Durable runtime. The loop runs on Temporal, or on a managed agent platform. Its graduation note says the container stage has shown you why, and only that reason earns this step.
The same loop runs from a poll, a cron job, a GitHub Action, or a webhook. Identity, shared context across repositories, MCP access, and a durable runtime matter later. None blocks the first Action. Microsoft Foundry and Amazon Bedrock AgentCore are examples, not a required platform.
Break the lethal trifecta
Simon Willison's lethal trifecta is access to private data, exposure to untrusted content, and a way to communicate externally. Any two can be managed. All three lets one prompt injection exfiltrate whatever the agent can read.
A CI build agent reads issues, comments, and branches from people you do not control, runs in your repository, and can reach the network unless you stop it. Researchers spent 2026 documenting that attack. It is not theoretical. Break the combination in the sandbox. Do not ask the model to notice.
- Ephemeral sandboxes. A fresh environment, destroyed afterward. Not a laptop, and not a runner with leftover state.
- No ambient secrets. No production credentials, no long-lived keys. Use short-lived OIDC tokens where the provider allows them.
- Restricted network. Registries and the services tests need. Nothing else.
- Its own identity. A GitHub App with minimum permissions, never a borrowed human token. It cannot approve or merge.
- Untrusted ticket text. A ready ticket is still data, not instructions.
- No agent-triggers-agent chain. Block bot actors unless you allow them, or one loop starts a billing spiral in another.
What it remembers
A blank run spends the budget rediscovering a known flaky test. Long sessions push opening constraints to the middle of the window, where models attend least, and near the limit some models skip steps and claim they are done. Use a fresh session per step, files for state, and a written handoff. Do not compact and hope.
Before the edit, recall nearby fixes, known failures, and the decision records on the ticket. Afterward, record the solution on the ticket and the pull request. OKF plugins are one example: System Architecture Capture says what is running, and Project Knowledge Capture holds why. A person reviews shared memory. A wrong "fix" teaches every later run the wrong lesson.
What the evidence shows
Build has more public evidence than any stage except Maintain. Nearly all of it is self-reported.

Three organizations have published numbers for their build fleets. The Organization column names the company. What runs describes the agent setup. Reported scale gives the volume each one reports. Human gate says who approves the result.
- Stripe (Minions). Stripe runs one-shot agents in pre-warmed devboxes, started from Slack, with at most two CI rounds. It reports over 1,300 merged pull requests a week with no human-written code, and a person reviews every pull request.
- Spotify (Honk). Spotify runs a background agent for fleet-wide changes. It reports 650+ merged pull requests a month, under human review.
- OpenAI. OpenAI runs an internal fleet with checked-in execution plans. It reports about 3.5 pull requests per engineer per day, under human review.
All three keep human review.
Each fleet bounds the agent, isolates it, and keeps production access away from an unbounded run. Tools you can swap include Claude Code and its GitHub Action, OpenAI Codex, the Copilot coding agent, Goose, OpenHands, and the Claude Agent SDK for container loops. A pipeline the agent improvises cannot be reproduced by a team of two or three. Keep a script.
Do not start with Build. Stand up review and a ticket reviewer first, then maintenance, then this stage on internal tickets in one repository: a CI action, small bug fixes with a reproduction. Move to features only when merge rate and change failure rate hold or improve.
A fictional workspace export
Claimed work is the query layer, the CSV writer, the tests, and large-workspace validation, with the time-zone fix already ahead of the writer.

In a fresh sandbox the agent recalls the tenant-isolation decision and the access-check pull request. Admins can export, non-admins get a 403, and every query filters by workspace. Those checks pass on the first try.
The large-workspace check times out at 200,000 rows. Streaming helps and is still slow, and the next idea touches the shared database client, which the ticket never named.
The agent does not take a third attempt, and it opens no pull request. The note covers what works, what it tried, the numbers, and whether large exports belong here. The owner makes large exports a background job on a new ticket and trims this one to a size limit. The agent rebuilds and opens a pull request with evidence. A person made both calls.
Do this today
- Run tests, types, lint, coverage, and the acceptance criteria in the sandbox before any pull request.
- Allow two exits. A receipt opens the pull request. A spent budget, a repeated failure set, or a non-strict change writes a handoff and stops.
- Cap attempts at 90 minutes and two tries, with a money limit. Missing evidence is failure.
- Ship the first loop as a GitHub Action: fresh sandbox, no production secrets, a tight network, an identity that cannot merge.
- Keep a real build script. Do not let the agent improvise it, edit the checks, or widen the ticket.
A good stop is a handoff
The factory version is one ready step, no production secrets, a receipt from code, and a person at the merge. On failure, the agent writes what it tried and gives the question back. Published fleets, Stripe's Minions among them, keep human review, a retry cap, and an isolated environment. An AI coding agent build loop that cannot stop has not started that pattern.
Where does your build agent stop today: at a limit, or when someone notices the bill?
This is Part 6 of "AI Software Factory Series," a 17-part guide to building an AI software factory with human gates.