Making AI-Generated Code Reliable: An Engineering Practice for Building an Evidence Loop

    AI can generate code faster than engineers can review it. This article explains how to build an evidence loop based on clear acceptance criteria, independent automated evaluation, end-to-end testing, property-based testing, and accumulated engineering rules. It also covers common failure modes and practical steps for improving the reliability and scalability of AI-assisted software development.

    1. Why AI-Generated Code Needs an Evidence Loop

    AI generates code far faster than humans can review it. If engineers must verify every line manually, one person can supervise only one or two AI agents at a time. To scale output, the bottleneck must shift from who writes the code to how the code is verified.

    An evidence loop consists of three parts:

    1. Clear acceptance criteria
    2. AI-generated implementation
    3. An independent tool that evaluates the result against those criteria

    The evaluation result is then fed back to the AI to drive the next round of changes.

    The key requirement is that the evaluator must be independent of the code generator. If the same session that generated the code also writes the tests, the AI can easily “prove” that the implementation is correct according to its own interpretation.

    An internal test shared publicly by one team found that, for structured file generation tasks, success rates increased by approximately 10 percentage points when an independent evaluation tool checked the output against predefined criteria. The more difficult the task, the more significant the improvement. This suggests that independent evaluation becomes increasingly valuable for complex work.

    2. End-to-End Testing: The First Testing Investment to Make

    If resources are sufficient to build only one type of test, end-to-end testing should usually come first. These tests simulate a complete user workflow, such as adding a product to a shopping cart, submitting an order, and reaching the payment-success page.

    Because end-to-end tests cover the entire path, a workflow either passes as a whole or fails at a specific step. This makes it difficult to conceal problems through isolated fixes.

    The difference between end-to-end tests and unit tests is especially clear in their failure information. When a unit test times out, it may report only that the operation exceeded 30 seconds. An end-to-end test can save screenshots and a complete execution trace, directly showing why the checkout button remained disabled.

    For AI agents that need to locate and fix problems, this quality of diagnostic information directly affects repair efficiency.

    In practice, teams can take the following steps:

    • Give the AI access to a real execution environment. For example, use browser automation to test web applications, a mobile emulator to test mobile apps, desktop automation tools to test desktop software, or a locally running backend service for integration testing.
    • Maintain a feature map that lists real user workflows, keyboard shortcuts, and expected behavior. The feature map serves both as an implementation guide for the AI and as the basis for evaluation.

    A representative scenario might look like this:

    1. scenario: Checkout flow
    2. given the shopping cart contains one item
    3. when the user clicks "Proceed to checkout" and submits the order
    4. then the page navigates to the payment-success page
    5. and save screenshots and logs for every step

    3. Property-Based Testing: Covering Boundaries Humans May Miss

    Most tests cover cases that developers can think of in advance. Property-based testing takes the opposite approach: define an invariant that must never be violated, then use tools to generate large numbers of random inputs in search of counterexamples.

    Because random inputs are not shaped by human assumptions, they can reveal extreme cases that manually designed tests overlook.

    Common properties include:

    1. Serializing and then deserializing data must fully restore the original data.
    2. An account balance must never become negative.
    3. Running migration or data-cleaning logic once or twice must produce exactly the same result, satisfying idempotence.
    4. Given the same input, optimized code and the previous implementation must produce the same output.

    The fourth property is particularly useful during refactoring. When an old specification no longer accurately reflects production behavior, comparing the behavior of the old and new implementations across many random inputs can expose hidden differences.

    A property-based rule can be expressed as follows:

    1. property: serialization round-trip consistency
    2. for any input object x:
    3. deserialize(serialize(x)) == x

    4. Two Common Failure Modes and How to Address Them

    Evidence loops often encounter two practical problems.

    Unstable tests. If the same test passes and fails intermittently, the AI may learn to retry until it gets a passing result instead of fixing the underlying cause.

    To address this, isolate flaky tests, record the failure rate of each test, mark tests that exceed a defined threshold for repair, and do not count them as valid evidence of success.

    Slow test execution. When tests take too long, the AI runs fewer investigation cycles in batches, making problems harder to locate.

    A layered execution strategy can help:

    • Run fast checks after every change.
    • Run the complete end-to-end suite before merging.
    • Parallelize test execution wherever possible.

    5. Turn Repeated Mistakes into Rules

    If the AI repeatedly makes the same mistake—for example, exposing sensitive user information in logs—the rule should be added to the project’s policy or specification files. A hook check should also be added before submission or execution.

    This allows similar problems in future tasks to be blocked automatically instead of relying on repeated manual reminders.

    Rule accumulation is an ongoing process. Whenever a new error pattern is discovered, the team should determine whether it can be converted into a rule or automated check. Over time, the project’s policy files become a repository of accumulated engineering knowledge.

    The cost of generating tests has also fallen significantly. AI can now produce a set of tests, benchmarks, or technical specifications within seconds. Once these tests are written, they become repeatable programs whose execution costs are far lower than ongoing manual verification. This makes them worth building early.

    6. Practical Recommendations

    • Start by building end-to-end tests for one critical business workflow. Expand coverage gradually after the approach is proven.
    • Keep tests separate from the implementation. The evaluation logic should not be written by the same session that generated the code.
    • Track three metrics: task success rate, test stability rate, and average time to identify a problem. Use these metrics to determine whether the system is working.
    • Prioritize property-based testing for high-risk modules involving funds, permissions, and data processing.

    Conclusion

    The reliability of AI-generated code depends on the verification system, not just on generation speed. The more complete the evidence loop, the more AI agents a single engineer can supervise effectively.

    Teams should focus on acceptance criteria that can be evaluated automatically and consistently. By making tests a stable source of constraints and evidence, organizations can scale AI-assisted development without allowing faster code generation to outpace verification.