Testing AI-Generated API Code: Check It Like Lean Checks Proofs

Lean accepted an AI-written proof without trusting its author. Use the same split for your API: a frozen spec, agent-written code, and contract tests the agent runs but can't edit.

Ashley Goolam

Ashley Goolam

23 November 2026

Testing AI-Generated API Code: Check It Like Lean Checks Proofs

Apidog for Enterprise

On-Premises Deploy

SSO & RBAC

SOC 2 Compliant

Explore Apidog Enterprise

Testing AI-generated API code works best when you copy how mathematicians now check AI-written proofs: fix the statement before the AI starts, let the AI write whatever it wants, and accept the result only if a small, independent checker passes it. For an API, the statement is your OpenAPI spec, and the checker is a set of contract tests your coding agent can run but can’t edit.

This guide shows which parts you keep trusted, the steps that turn a spec into a merge gate, and the Apidog CLI commands your agent runs to check its own work.

What does testing AI-generated API code mean?

Testing AI-generated API code means checking an endpoint a coding agent wrote against a contract the agent didn’t write. You compare real responses from a running build with the status codes, fields and types in your API spec. The agent may fix its code until the checks pass, but it never changes the spec or the checks.

That last rule is the whole idea. Tests an agent writes for its own code confirm its own assumptions: if it misread the requirement, its tests misread it too, and everything goes green.

Why a Lean proof is the right model

On November 20, 2026, Levent Alpöge, a member of Anthropic’s technical staff, said he had proved the Riemann hypothesis working with Claude. As phys.org reported, he released the argument only as Lean code, and said the method also covers the broader generalized Riemann hypothesis, the version about Dirichlet L-functions.

Nobody can read the result line by line. Kevin Buzzard of Imperial College London told Al Jazeera it’s “17 million lines of Lean code.” Mathematicians expect weeks of work to understand it, and a Stanford postdoc has independently submitted a competing proof. The claim still got serious attention on day one for one reason: Lean checks every step with what its maintainers call a “minimal trusted kernel,” and the Lean FRO’s timeline lists the result as a Lean-verified proof.

You face a smaller version of this every week. Your agent writes 400 lines of handler and serializer code in minutes, and you can’t review all of it with care. So don’t trust the author. Trust a checker.

Map the proof checker to your API

Lean splits the work into an untrusted part that can be as clever and buggy as it likes, and a small trusted part that says yes or no. Your API workflow needs the same split.

Lean Your API workflow Who owns it
Theorem statement OpenAPI spec for the endpoint You, frozen before the agent starts
Proof (tactics, terms) Handler, models, migrations The coding agent
Kernel Contract tests generated from the spec Shared tool, read-only to the agent
#print axioms List of skipped or disabled checks Printed on every run
Accepted / rejected Pass / fail report in CI The merge gate

A mistake only matters if it gets past the trusted part. A wrong field name in the handler gets rejected. A wrong field name in the spec doesn’t, which is why a person writes and freezes the spec.

Schema: you own the frozen spec, the agent owns the handler and can't edit the spec, and contract tests built from the spec gate the merge

How to verify an AI-written endpoint before merge

  1. Freeze the spec first. Write or update the OpenAPI definition for the endpoint, including every response code you care about (200, 400, 404, 409). Merge it before any code exists. This is your theorem statement.
  2. Hand the agent the spec, not a description. “Implement POST /objects as specified” leaves less room for guessing than “add an endpoint that creates objects.”
  3. Generate the checks from the spec. The contract tests should come from the OpenAPI document, not from the code the agent just wrote. A schema assertion on the 200 response fails the moment the code drops a required field or returns a string where the spec says integer.
  4. Run them against a real build. Start the service, point the tests at it, and record the report. A mock that shares the agent’s assumptions proves nothing.
  5. Treat a failure as a rejection. The agent’s job is to change its code until the checks pass. It doesn’t get to loosen an assertion or edit the spec to match what it built.
  6. Gate the merge in CI. The same run goes into your pipeline with a JUnit report, so a red check blocks the pull request whether or not anyone reads the diff.

Step 5 is where teams slip. An agent’s fastest route to green is often to change the test: the API version of a proof that skips the hard step.

Sequence: the agent reads the spec with Apidog CLI, runs contract tests against the real build, fixes its handler until they pass, then CI reruns them

What contract tests catch that the agent’s own tests miss

An agent that writes the code and its unit tests tests what it built, not what you asked for. Spec-driven contract tests catch these:

None of these need a clever test, just one whose expectations come from somewhere other than the code. To throw generated edge-case inputs at the same spec, see our Apidog vs Schemathesis comparison.

Block the shortcuts: the API version of sorry

Lean has an escape hatch called sorry: it skips a step, and Lean flags every declaration that uses it. Agents find the same escape hatches in test suites:

Shortcut What it looks like in a diff How you block it
Skipped test skip, only, a commented-out case Fail CI if the count of skipped checks goes up
Loosened assertion toEqual becomes toBeDefined, schema check removed Keep contract tests outside the repo the agent edits
Edited spec A required field moves to optional in the same PR Spec changes need a separate, human-reviewed PR
Mocked dependency The test calls a stub that returns the spec’s example Run the contract suite against a real build only
Schema: a code-only pull request goes to contract tests and merge; one that edits the spec or adds skipped checks goes to human review

The simplest rule: a pull request may change code or the contract, never both. Tell the agent this up front, in its instructions file (CLAUDE.md, AGENTS.md or your editor’s rules file): “Never edit the API spec or existing test cases to make a check pass. If a check looks wrong, stop and explain why.”

Let your coding agent run the checker with Apidog CLI

Let the agent run the checker itself, from its terminal, with Apidog CLI. Your spec lives in an Apidog project, the test cases are built from it, and the agent reads both but only writes code.

Set it up once:

npm install -g apidog-cli@latest
apidog auth login --with-token <YOUR_TOKEN>
apidog skill install

The skill teaches your agent the CLI, so it reads each payload’s schema instead of guessing. If your spec lives in your repo, import it once with apidog import --project <projectId> --format openapi --file ./openapi.yaml.

Then give it one prompt:

In Apidog project <projectId>, read the spec for POST /objects. Don't edit the
endpoint spec or any existing test case. Create one test case that sends a valid
body and fails unless the response is a 200 matching the response schema. Run
every test case on that endpoint against my local environment, then fix my
handler, not the tests, until they pass.

The agent reads the contract first, then builds and runs the check:

apidog endpoint list --project <projectId>
apidog endpoint get <endpointId> --project <projectId>
apidog cli-schema get test-case-create
apidog cli-schema validate test-case-create --file case.json
apidog test-case create --project <projectId> --file case.json
apidog test-case run --project <projectId> --endpoint <endpointId> -e <envId> -r cli,junit
Apidog CLI on a sandbox project listing endpoints including POST /objects, finding no test cases on it, then the test-case run command

cli-schema validate checks the agent’s payload before it writes anything. The -r cli,junit flag gives the agent a readable result and gives CI a JUnit file. Put the same apidog run or apidog test-case run line in your pipeline to make it the merge gate; the Apidog CLI CI/CD guide has ready-made GitHub Actions and GitLab jobs. New to the CLI inside an agent? Start with how to use the Apidog CLI in Claude Code.

If several agents are writing endpoints in parallel and your team reviews what they ship, Sharkly gives you a board where you assign those tasks to the agents running on your own machines and review each result in one place. It’s free to start.

What this does not prove

Don’t oversell the comparison. Lean’s kernel proves a statement for every possible input. Passing 40 contract tests means the endpoint kept its promises on 40 requests, not on all of them.

You also only get out what the spec puts in. If it forgets a required field or the 409 case, the checker can’t catch the gap.

What you do get is the property that made the Riemann claim worth taking seriously on day one: the code’s author, human or AI, no longer has to be trusted. You trust a small checker you understand, built from a statement you wrote. For a deeper look at the contract side, read our guide to API contract testing, and for the broader case for testing agent output, see AI writes your API code. Who tests it?

FAQ

Can I let the AI write the tests too? Yes, if the tests come from the spec and the agent can’t change them afterward. The risk isn’t who types the test. It’s a test whose expectations come from the agent’s own code.

Is a contract test the same as a formal proof? No. A proof covers every input; a contract test covers the requests you send. Contract tests give you the same trust split as Lean (untrusted author, small trusted checker) without the guarantee for all inputs.

What if the spec is wrong? Fix it in its own pull request that a person reviews, then let the agent rerun its code against the new contract. Never fix the spec and the code in the same change.

Which coding agents can run Apidog CLI? Any agent that can run shell commands, including Claude Code, Codex, OpenCode and Cursor. apidog skill install adds the instructions it needs.

Does this replace code review? No. The checker covers the contract, so your review can focus on design, security and whether the spec asked for the right thing.

Explore more

Best AI Model for Math 2026: Claude vs GPT-6 vs Gemini 4

Best AI Model for Math 2026: Claude vs GPT-6 vs Gemini 4

Claude Fable 5.2, GPT-6.2 Astra and Gemini 4 compared on published math benchmarks: MathArena, HLE-Diamond, LeanLean and RiemannBench, with cost per problem and a way to test them on your own problems.

23 November 2026

Claude API Math: How to Write Formal Proofs You Can Check in Lean

Claude API Math: How to Write Formal Proofs You Can Check in Lean

Send a math problem to the Claude API, get a Lean 4 proof back, and let Lean check every step. A step-by-step guide with curl, a Python retry loop and effort settings.

23 November 2026

Did Claude Prove the Riemann Hypothesis? What Actually Happened

Did Claude Prove the Riemann Hypothesis? What Actually Happened

Levent Alpöge says he proved the Riemann Hypothesis working with Claude, and the Lean community certified the code. Here's what was claimed, what Lean checked and what's still open.

23 November 2026

Practice API Design-first in Apidog

Discover an easier way to build and use APIs

Testing AI-Generated API Code: Check It Like Lean Checks Proofs