Skip to main content
Code is the ideal domain for agentic systems because verification is cheap and objective. When a human reviews prose, they bring subjective judgment—is this paragraph clear? Is the argument convincing? When a test suite runs against code, it returns pass or fail. This binary signal creates tight feedback loops that drive rapid improvement. A code agent can generate, test, discover failures, fix, and test again in seconds. No human judgment required until the tests pass. This chapter builds a code agent that uses verification as its primary feedback signal. The agent takes specifications, plans implementations, writes code, runs tests, and iterates until either the tests pass or it exhausts its retry budget. Along the way, it learns patterns that improve future coding tasks.

13.1 Why Code Is Special

Several properties make code uniquely suitable for agentic automation. Verifiable correctness. Tests provide ground truth. Code either passes or it doesn’t. This eliminates the ambiguity that plagues other domains. A research report might be “good enough” by various subjective standards, but code has clear success criteria. Structured feedback. Test failures aren’t just binary signals—they explain what went wrong. “Expected 5, got 4 on line 42” tells the agent exactly where to look. Compiler errors pinpoint syntax problems. Type checkers identify interface mismatches. This structured feedback guides iteration effectively. Incremental verification. Code can be tested at multiple granularities. Unit tests verify individual functions. Integration tests verify component interactions. End-to-end tests verify complete workflows. This hierarchy allows agents to build confidence incrementally. Rich context. Codebases contain extensive documentation through types, comments, tests, and the code itself. An agent can read existing patterns and match them. Function signatures specify contracts. Test cases demonstrate expected behavior. This context grounds generation in reality. These properties make code agents highly effective. The challenge isn’t whether agents can write code—they demonstrably can. The challenge is building systems that write code reliably, handle edge cases gracefully, and improve over time.

13.2 System Architecture

The code agent has four main components organized around the verification loop.
The Planner analyzes the specification and codebase context to create an implementation plan. It identifies which files need changes, what the approach should be, and what edge cases to consider. The Implementer writes code according to the plan. It generates or modifies files, following codebase conventions and respecting existing patterns. The Verifier runs tests and other checks against the implementation. It captures pass/fail status plus detailed error information. The Error Analyzer processes verification failures to understand what went wrong. It transforms raw error output into actionable guidance for the next implementation attempt. The loop continues until verification passes or the retry budget exhausts.

13.3 The Codebase Artifact

Code agents operate on a codebase—a semantic artifact with structure, conventions, and relationships.
To let the agent navigate and modify the project safely, the codebase artifact exposes operations for reading, writing, and searching files while enforcing repository conventions:

13.4 The Verification Loop

The heart of the code agent is the verification loop—generate, test, analyze errors, retry.

13.5 Error Analysis as Gradient

The quality of error analysis determines how effectively the agent improves between attempts. Raw error output is often verbose and confusing. Good error analysis extracts the actionable signal.

13.6 Learning from Code Tasks

The code agent improves by learning from both successes and failures. Patterns that lead to passing tests get reinforced; patterns that cause failures get flagged. On later tasks, the agent injects promptGuidance.do as explicit “best practices” in the system prompt (for example, “prefer pure functions and dependency injection”), and includes promptGuidance.avoid as explicit warnings (for example, “do not mutate shared global state in tests”).

13.7 Handling Stuck States

Not every implementation succeeds. The agent must recognize when it’s stuck and handle failure gracefully.

Key Takeaways

  • Code is ideal for agentic automation because verification is cheap and objective
  • The verification loop—generate, test, analyze, retry—drives rapid improvement
  • Structured error feedback (type errors, test failures) provides clear guidance
  • Error analysis transforms raw output into actionable fix suggestions
  • Learning from both successes and failures improves future implementations
  • Recognizing stuck states and escalating gracefully prevents infinite loops

Transition

Chapter 13 showed verification-driven development where tests provide an objective feedback signal and the agent can rely on pass/fail results plus structured error messages. Chapter 14: Customer System addresses a different challenge—handling human-facing interactions where feedback is subjective, quality is harder to measure, and the system must balance automation with appropriate escalation to humans.