Verification Loop

Pattern: A named solution to a recurring problem.

Understand This First

• Agent – the verification loop is the agent’s primary quality assurance mechanism.

• Tool – the agent needs tools to run tests and read results.

Context

At the agentic level, the verification loop is the cycle of change, test, inspect, and iterate that makes agentic coding reliable. It’s the mechanism by which an agent confirms that its changes actually work, not through confidence, but through evidence.

The verification loop is what separates agentic coding from “generate and hope.” A model generates plausible code, but plausible isn’t correct. The loop closes the gap by running tests, checking output, and feeding results back to the agent for correction.

Problem

How do you ensure that agent-generated changes actually work, when the agent’s default output is optimized for plausibility rather than correctness?

An agent that writes code without verifying it is like a developer who never runs their tests. The code might be right. It often is. But when it isn’t, the errors compound: the next change builds on a broken foundation, and the agent doesn’t notice because it isn’t checking.

Forces

• Agent confidence doesn’t correlate with correctness. The model sounds equally sure about right and wrong code.

• Fast iteration is one of the agent’s strengths, making verify-and-retry cheap.

• Test infrastructure must exist for verification to work. The loop is only as good as the checks it runs.

• Verification scope must be calibrated. Running the full test suite after every small change is wasteful; running nothing is reckless.

Solution

Build verification into the agent’s workflow as a mandatory step, not an optional one. The basic loop is:

1. Change. The agent modifies code based on the task or the previous iteration’s feedback.

2. Test. The agent runs relevant tests, linters, type checks, or other automated checks.

3. Inspect. The agent reads the results. If everything passes, the task may be complete. If something fails, the agent analyzes the failure.

4. Iterate. The agent uses the failure information to make a corrective change and returns to step 2.

Steps 2-4 are what the agent does naturally when given access to test tools and trained to use them. Most capable agents, when told “fix this and make sure the tests pass,” will automatically run tests, read failures, and iterate. Your job is to ensure the infrastructure exists and the agent knows how to invoke it.

Verification works at multiple granularities. Unit tests catch functional errors quickly. Type checkers catch structural errors. Linters catch style violations and common mistakes. Integration tests catch issues at boundaries. A good verification loop uses the fastest checks first and escalates to slower, broader checks as the change stabilizes.

Warning: Don’t trust agent-generated tests as your only verification. An agent can write code and tests that agree with each other while both being wrong. Use existing tests, human-written tests, and manual inspection as anchors. See Smell (AI Smell) for more on this failure mode.

How It Plays Out

An agent is asked to add input validation to an API endpoint. It writes the validation logic, runs the existing test suite, and discovers that two tests fail because they were sending invalid input that the old code silently accepted. The agent examines the tests, determines they should be updated to send valid input, makes the corrections, reruns the suite, and all tests pass. Without the verification loop, the validation would have shipped alongside broken tests.

A developer configures their agent’s harness to automatically run type checks after every file save. The agent writes a function that returns string | null but the caller expects string. The type checker catches the mismatch immediately, and the agent adds a null check before moving on. The bug never reaches a test; it was caught at the fastest verification level.

Here’s what a verification loop looks like in practice. A developer asks the agent to add rate limiting to an API endpoint. The agent writes the implementation, then runs the tests:

Developer prompt:
  "Add rate limiting to POST /api/upload. Limit to 10 requests
  per minute per API key. Run the tests after you make the change."

Agent writes rate_limiter.py, updates the route handler,
then runs the test suite:

  $ pytest tests/api/test_upload.py -v
  FAILED test_upload_returns_201 - AssertionError: 429 != 201
  FAILED test_upload_large_file - AssertionError: 429 != 201
  3 passed, 2 failed

Agent analyzes the failures:
  "Two existing tests are failing because they don't include an
  API key in their request headers. My rate limiter rejects
  keyless requests with 429. The tests need updating — they
  should include a valid test API key."

Agent fixes the test fixtures to include X-API-Key headers,
then reruns:

  $ pytest tests/api/test_upload.py -v
  5 passed, 0 failed

Agent reports:
  "Rate limiting is in place. Two existing tests needed updated
  fixtures because they were sending unauthenticated requests
  that the rate limiter now rejects. All tests pass."

Without the loop, the agent would have shipped the rate limiter alongside two broken tests. The loop caught the interaction between new code and old assumptions, and the agent fixed both sides before reporting success.

Example Prompt: “Add input validation to the /register endpoint. After writing the code, run the full test suite. If any test fails, read the failure output and fix the issue. Repeat until all tests pass.”

Consequences

The verification loop makes agentic coding reliable. It catches errors while the agent still has the context to fix them, reducing the chance that broken code reaches code review or production. It also builds a healthy habit: treat agent output as a hypothesis to be tested, not a fact to be trusted.

The cost is infrastructure. You need tests, linters, type checkers, and a way for the agent to invoke them. Projects with weak test coverage get less benefit from the verification loop because there are fewer checks to run. This creates a virtuous cycle: the more you invest in test infrastructure, the more productive your agents become.

Complements: Structured Outputs — Validation failures on structured output are the most common signal that drives the loop's next iteration.

Contrasts with: Evaluation Gate — A verification loop is the agent correcting itself inside a task; an evaluation gate is an external release barrier.

Contrasts with: LLM-as-Judge — A verification loop runs inside a single agent's context; an LLM-as-Judge call deliberately separates the grader from the grader's subject.

Contrasts with: Retry Budget — A verification loop acts on a failure signal to improve the next attempt; a retry budget decides whether the same attempt is even worth repeating.

Contrasts with: Risk Spike — Contrasts with Risk Spike.

Depended on by: Dark Factory — The verification loop is what replaces human review at the code layer when the factory runs lights-out.

Depends on: Agent — The verification loop is the agent's primary quality assurance mechanism.

Depends on: Tool — The agent needs tools to run tests and read results.

Enabled by: Feedback Sensor — The verification loop is the process that consumes sensor output and drives correction.

Enabled by: Feedforward — Verification loops consume both feedforward and feedback signals — the starting context plus the runtime evidence.

Enabled by: Shift-Left Feedback — Tighter feedback loops make the verification loop faster and cheaper.

Enabled by: Spec-Driven Development — The spec supplies the criteria the verification loop checks against.

Enables: Eval — Evals are verification loops applied to the agent's overall performance.

Extended by: Evaluator-Driven Code Search — Evaluator-Driven Code Search extends this pattern.

Informed by: Programming Language Selection — Programming Language Selection informs this.

Prevents: Vibe Coding — Systematic verification is the antidote to accepting code without checking it.

Reduced by: Preframing — Preframing reduces this.

Refined by: Human in the Loop — Some verification steps require human judgment.

Refined by: Steering Loop — The verification loop describes the change-test-inspect cycle; the steering loop is the complete closed-loop system that includes feedforward, feedback, and escalation.

Refines: Feedback Loop — The agent-specific feedback loop: generate, test, read results, regenerate.

Related: Agent Trace — The inner act-observe-correct loop emits the spans a trace is built from.

Related: Happy Path — Agents retry off the happy path until they find it again.

Related: Printf Debugging — Agents use printf debugging as part of their verify step: insert prints, run, read output, fix.

Related: Reasoning Effort — Low reasoning effort paired with a strong verification loop often beats high effort alone.

Related: Test Pyramid — The pyramid shapes what an agent sees in each verification cycle.

Specializes: Belt-and-Suspenders — An independent verification pass over an agent output is Belt-and-Suspenders applied to generation.

Supported by: Test Impact Analysis — A tight selected slice is what makes an agent's verification loop fast enough to iterate against.

Supported by: Test-Driven Development — A failing test gives an agent a concrete exit condition to loop against.

Used by: Background Agent — Background Agent relies on this pattern.

Used by: Code Review — Code review is the human-mediated verification step in a change workflow.

Used by: Loop Engineering — Loop engineering makes the independent-verify step its quality gate; without an executable done-check there is no loop, only a runaway.

Used by: Pipeline Synthesis — Pipeline Synthesis relies on this pattern.

Uses: Plan Mode — Planning produces expectations that verification can check against.

Uses: Smell (AI Smell) — AI smell detection is a form of verification that automated tools can't yet perform.

Sources

• Norbert Wiener formalized the feedback loop as a general principle of control in Cybernetics: or Control and Communication in the Animal and the Machine (1948). The verification loop’s core structure (act, observe the result, correct) is a direct instance of Wiener’s cybernetic cycle applied to software construction.

• Kent Beck codified the tight test-feedback cycle in Test-Driven Development: By Example (2003). The verification loop’s change-test-inspect-iterate rhythm is a generalization of Beck’s red-green-refactor, extended from human developers to autonomous agents.

• The application of closed-loop verification to LLM-generated code emerged as a community practice among agentic coding practitioners in 2023-2024, as teams discovered that treating model output as a hypothesis to be tested, not a result to be trusted, was essential for reliability.