← Blog

How Do You Verify That AI-Generated Code Follows Every Business Rule?

By Jeet Patel7 min read

When an AI writes your code, how do you know it followed every rule? We give every rule a code, and every code a test.

In short: In our AI development process, every business rule and validation gets a unique, traceable code such as BILL-SUBS-CANCEL-BR-003. Each code appears in the specification, in a “Test Hints” table mapping it to an expected outcome, and in the QA team’s test cases. That chain lets us answer a question most AI-assisted teams can’t: which rules were implemented, and how do we know? On one billing module, it supported over 700 test cases, all passing before release.

The question nobody can answer

Ask a team that uses AI coding tools a simple question: “Your billing system has a rule that a coupon can’t apply to the current cycle. Where is that rule tested?”

Most can’t answer quickly. The rule might be in a ticket, a chat thread, a developer’s memory or nowhere at all. The AI may or may not have implemented it. A test may or may not exist.

This is an old problem. Requirements traceability is a long-standing practice in regulated industries. But AI makes it sharper. When code is produced quickly by an agent, the gap between “what we meant” and “what was built” can grow just as quickly, and a confident-looking implementation hides it well.

We wanted every rule to be named, visible, built and tested, with a straight line connecting the four.

Step 1: give every rule a code

In our specifications, rules aren’t paragraphs of prose. They’re individual, numbered statements, each with a unique code. There are two kinds:

  • Validations (VAL): checks on input and state that happen before anything changes. “The subscription must be in ACTIVE status.”
  • Business rules (BR): the behaviour the system must follow. “A cancellation takes effect at the end of the current billing period, never immediately.”

The code is derived from the document’s location, so it’s predictable and unique across the whole repository:

Path segmentRuleExample
ModuleFirst 4 lettersbilling → BILL
FeatureFirst 4 letterssubscriptions → SUBS
Sub-featureFirst 4 letterscancellation → CANC
FlowFile name, up to 6 letterscancel.md → CANCEL

Together they produce codes like BILL-SUBS-CANC-CANCEL-BR-001 and BILL-SUBS-CANC-CANCEL-VAL-002. Anyone who sees a code can find the document it came from, and the code never collides with another.

Step 2: map each code to a test hint

Every specification includes a Test Hints table. Each row takes one rule or validation code, describes what to test, and states the expected outcome, including the HTTP status where relevant.

CodeTest caseExpected outcome
...-VAL-001Cancel a subscription that is already cancelled409, “subscription not active”
...-BR-001Cancel mid-cycleStatus stays active until the period ends
...-BR-002Cancel with an unpaid invoice400, invoice must be settled first

(Illustrative example, not from a real system.)

The table is written at the same time as the rules, by the same process. If a rule exists, its test hint exists. A rule without a test hint is visible immediately as a gap.

Step 3: let QA build from the specification

Our QA team doesn’t reverse-engineer behaviour from a finished feature. They write test cases from the specification while development is still under way, using the business rules, validations and test hints as their starting point.

This changes the shape of the work:

  • Testing starts in parallel with development, not after it.
  • Test cases cover what the spec says, including the edge cases and failure paths that are easy to forget.
  • Coverage gaps are easier to see. A rule with no matching test case stands out.
  • Results are explainable. A failed test points back to a specific rule in the specification.
01 · The traceability chain
The traceability chainSpecification feeds Test hints and AI-built code. Test hints feed QA test cases. Code and QA tests converge on Test run. All rules covered and passing? No returns to Specification; yes leads to Release.SpecificationBR-001 · VAL-001Test hintsexpected outcomesAI-built codeQA test casesTest runAll rules coveredand passing?noyesRelease
Figure 1. The traceability chain. A rule can be followed from the specification to its test result.

Step 4: close the loop when something fails

When a test fails, the code tells us where to look. There are only three possibilities:

  1. The code is wrong. The AI didn’t implement the rule as specified. Fix the code.
  2. The specification is wrong or incomplete. The rule was never stated clearly. Fix the spec first, then the code.
  3. The test is wrong. The test misread the rule. Fix the test.

The second case is the most valuable. It means we learned something about the system that nobody had written down, and the fix improves the context for every future feature that touches the same module.

02 · Closing the failure loop
Test failure processTest fails leads to Where is the error? Spec leads to Fix the spec, add or clarify the rule, then Fix the code. Code leads directly to Fix the code. Test leads to Fix the test. Both fixes lead to Re-run tests.Test failsWhere isthe error?speccodetestFix the specadd or clarify the ruleFix the codeFix the testRe-run tests
Figure 2. Failures feed back into the specification, so the same gap doesn’t appear twice.

What this gave us on one module

For the billing module we’ve described in this series:

  • About 60 user stories, each with its own specification.
  • Over 60 context documents, almost all with coded rules and validations, and most with a Test Hints table.
  • Over 700 test cases written and run, all passing before release.

We can’t claim traceability alone produced that result. The precise specifications, the review steps and a disciplined QA team all played a part. But traceability is what made the result checkable.

What we haven’t solved yet

We want to be honest about the gaps.

  • The link to automated tests isn’t enforced yet. Our coding agent writes unit tests, but it isn’t yet required to write one test per rule code. That’s the next step: make the agent read the Test Hints table and prove coverage.
  • Some test cases are still manual. End-to-end flows are tested on staging by people, not by an automated suite.
  • QA test cases use their own IDs. Today our test cases are written from the specification but carry their own identifiers, so the link back to a rule code is made by reading, not by a shared key. Tagging every test case with its rule code would make coverage checkable automatically.

Why this matters more with AI

When people write code, traceability is a good practice. When AI writes code, it becomes a control.

An AI agent can produce a lot of plausible code quickly. Rule codes and test hints turn “it looks right” into “every stated rule has a test, and every test passes”. That’s the difference between trusting the AI and verifying it.

The forward deployed angle

Forward deployed teams are accountable for outcomes, not just code. Rule codes and test hints let us show a client exactly which of their business rules were built and how each one was verified. That’s a far stronger handover than “it works on staging”. We cover this in more depth in our article on forward deployed engineering in the age of AI agents.

Key takeaways

  • Write rules as individual, numbered statements, not prose.
  • Derive codes from the document path so they’re predictable and unique.
  • Put a Test Hints table in every specification, mapping each code to an expected outcome.
  • Let QA write test cases from the specification while development is under way, and tag them with the rule codes.
  • When a test fails, decide whether the code, the spec or the test is wrong, and fix the spec when it’s the spec.

Read how an AI agent interviews engineers to write the specifications that these rule codes come from.

Frequently asked questions

What is requirements traceability in AI development?

It’s the ability to follow each requirement from its specification to the code that implements it and the test that verifies it. With AI writing the code, traceability is how a team confirms the AI followed every rule.

What’s the difference between a validation and a business rule?

A validation checks input or state before anything changes, such as “the subscription must be active”. A business rule describes how the system must behave, such as “cancellation takes effect at the end of the billing period”.

Why derive rule codes from the file path?

It makes codes predictable and guarantees they’re unique. Anyone who sees a code can find the specification it came from without searching.

Does this replace automated testing?

No. It organizes testing. The codes tell you what must be tested; automated and manual tests do the testing.