Formal Verification of AI-Generated Code: What It Proves and What It Doesn’t

Written by

in

12 min read

Most of us check AI-written code by testing it. Feed the program some inputs, see whether the answers match what we expected, and treat that as proof it works. An experiment Anthropic published on October 22, 2026 puts a number on what that habit misses. Researchers had Claude mathematically verify code in 123 open-source libraries, and the attempt surfaced bugs that had survived trillions of test runs over several years.[1]

The technique is formal verification: instead of running a program on sample inputs, you prove mathematically that it obeys a stated rule for every input. It spent decades as the preserve of a small group of specialists, until writing proofs turned out to be the kind of labor large language models are good at.

A test only reports on the inputs it tried

To see this directly, I ran a small experiment. The code below stands in for a function that tracks how many bytes are left while unpacking a file. It subtracts the amount already read from the amount remaining.

function remainingBytes(remain: number, used: number): number {
  return (remain - used) >>> 0; // read the result as a 32-bit unsigned integer
}

>>> 0 tells JavaScript to read the result as a 32-bit unsigned integer. Unsigned means there is no room to store a negative number, so subtracting 1 from 0 rolls over to the largest value available, the way a car odometer would if you ran it backwards. Running it:

remainingBytes(1000, 400) = 600
remainingBytes(10, 10)    = 0
remainingBytes(10, 11)    = 4294967295

When the amount read exceeds what remains by just one, the answer comes back as roughly 4.29 billion. A program that trusts that number as “bytes you may safely read next” will reach outside its own memory and start writing over whatever sits there. Anthropic found bugs of exactly this shape, including one in decompression code where a count ran backwards past zero and the program wrote outside its allotted memory.[1]

Then I tested it. I generated a million random inputs in a realistic range: a total length between 1 and 1,000,000 bytes, and an amount read picked from inside that total.

Random testing, 1,000,000 runs (realistic input range)
Failures found: 0 / 1000000

The test did not get unlucky. Because the amount read was always drawn from inside the total, the case where used exceeds remain is never generated at all, so a trillion runs would return the same zero. Then I changed one thing, drawing the amount read independently of the total, and the result flipped.

Same test, wider generation range
Failures found: 507927 / 1000000

Same code, same number of runs, 507,927 failures. Checking every combination from 0 to 999 exhaustively gave a similar ratio.

Exhaustive check: all pairs 0-999 (1,000,000 combinations)
Failures found: 499500 / 1000000
First counterexample: remain=0, used=1 -> 4294967295
Failure rate: 50.0%

The bug sat across half the input space, and the first million runs found none of it. What the test measured was the range of inputs its author imagined, which tells you nothing about how the code behaves outside that range.[6]

The same code and the same number of test runs split between 0 and half a million failures depending on how inputs are generated
The same code and the same number of test runs split between 0 and half a million failures depending on how inputs are generated

Fuzzing (an automated technique that floods a program with endless mutated inputs looking for crashes) runs into the same wall. Fuzzers in practice cap their inputs at around 1 MB. One bug Anthropic found only triggered on inputs around 20 MB arranged in one rare layout. Another needed a 64 MB input and over 2 GB of memory. No amount of fuzzing time reaches those.[1]

Trying every possible input is not an option either. The function above takes two 32-bit integers, which works out to 18,446,744,073,709,551,616 combinations. At a billion checks per second that is 584 years of computing.[6] Functions that take more or larger inputs reach impossible totals faster still.

Three pieces: a spec, a proof, and a checker

The first piece is the specification, or spec: a mathematical statement of what “this program works correctly” even means. For the function above it would be something like “the result is never greater than remain.” Anthropic described writing the spec as the hardest gate. A PNG decoder has a correct answer defined by a published standard, but stating what it means for a text editor or a web browser to be correct is genuinely hard.[1]

The second piece is the proof: an argument that the spec holds for every input. Rather than feeding in inputs one at a time, you reason about conditions, showing that the 4.29 billion result requires used > remain and that the code rules that case out. The input space can run to 18 quintillion combinations while the argument takes a few lines.

The third piece is the proof checker, a program that walks the proof line by line and rejects it if there is a gap. That is what makes this workable with AI in the loop, because nobody has to read a long machine-written proof and decide whether to believe it. Interviewed by Latent Space, Clark Barrett described Lean this way: “What Lean does is it just checks that you didn’t make a mistake. So you can think of it as a very sophisticated checker for mathematics.”[3]

Lean is an open-source programming language built for writing proofs and having them checked.[2] Anthropic used Rocq, a comparable tool, for its code work.[1] These tools already sit inside services people use. AWS formally verifies Cedar, the system it uses to decide who is allowed to access what, in Lean.[2] Barrett said cvc5, the automated reasoning engine he co-created, is called billions of times a day inside Amazon’s running systems.[3]

Barrett put the difference from testing this way: “The main difference is really just, how do you know when you’re done. So if you’re testing, patching, scanning, using all these existing tools, they’re quite good. I mean, they find a ton of problems. They raise those issues. But you’re never quite sure, did I find all of them or is there one more lurking there that my tool can’t find?”[3]

What Anthropic actually ran

Working with the verification company Theorem, Anthropic targeted code registered with OSS-Fuzz, Google’s continuous fuzzing program for open-source projects. Rather than write specs by hand, the team converted each project’s existing fuzz test into one, encoding the rule “this test never crashes on any input,” with the input ceiling set at 64 MB.[1]

Over a few weeks Claude completed proofs for 239 entry points (the doors a project’s authors provide for testing to reach the code behind them) across 123 open-source libraries. The output came to roughly 180 million lines of Rocq, with a typical proof around 600,000 lines, covering programs whose compiled form ran to a median of 18,000 machine instructions. The proof is vastly longer than the code it certifies, and essentially none of it needs human review, because the checker is what reads it.[1]

The flow from spec to machine-written proof to automated checking, with the scale of Anthropic's experiment
The flow from spec to machine-written proof to automated checking, with the scale of Anthropic’s experiment

Hundreds of bugs turned up along the way. In one widely used PDF reader, a week of this work produced as many security bugs as the project typically sees reported in a year. In another application, a logic bug that had gone unnoticed for more than a decade came out.[1]

For a sense of what this used to cost, Anthropic pointed to seL4, an operating system kernel. Building it took two person-years, while formally verifying it took twenty, so verification cost ten times as much as writing the thing. That is why the technique stayed confined to places where lives are at stake, such as Airbus avionics code.[1]

The cost is coming down

A single proof can consume hundreds of millions of tokens, which is the unit models are billed in.[1] Anthropic offered two reasons to expect that bill to shrink.

Newer models need fewer tokens for the same work. On 36 small verification tasks drawn from real OSS-Fuzz code, Opus 5.5 proved half of them using about 40% fewer tokens than Opus 5. Each task got three independent attempts on a 5M-token budget, and the task counted as proved once the earliest of those attempts finished.[1]

Proofs can also be reused, since they are built recursively out of smaller proofs. When researchers gave several agents a shared workspace where they could copy each other’s proofs, the agents used 17% fewer tokens than working alone. Solo work cost more on 30 of the 34 completed proofs, and some agents finished tasks in the shared workspace that they had failed solo.[1]

Mathematicians have been accumulating proofs this way for years. Mathlib, Lean’s communal library, now holds over a million lines of mathematics written in machine-checkable form, each proof resting on the ones before it.[2]

Results clustered in 2026. Going by the news timeline kept by Lean FRO, the nonprofit that supports Lean’s development: in August Anthropic released results connected to the Riemann hypothesis along with Lean formalizations; in September it announced a Lean formalization of Fermat’s Last Theorem; in October OpenAI and Axiom each published Lean proofs on separate open problems, and &Truth came out of stealth with $70m in seed funding for Lean-based software verification.[5]

Axiom CEO Carina Hong connects the math results to the code results. On the Latent Space podcast she argued against treating formal verification purely as a filter for mistakes: “Verification to me is about scaling brilliance, compounding brilliance.” Her argument is that once a proof is machine-checked it can serve as ground truth for the next round of model training, which is a much more reliable teaching signal than scoring an answer on how plausible it looks. She named the limit in the same conversation: “Anything that can be specified can be proven. Humans are bad at specifying everything we want.”[4]

“Verified” does not mean “bug-free”

Anthropic gave the limitations their own section. A proof checker confirms that the proof satisfies the spec. It cannot tell you whether the spec matches reality. When a bug shows up in verified code, it means the spec or the model of the machine differed from the real world.[1]

Two such gaps are documented. The proofs assume a chip built to the RISC-V design, running a program that handles one task at a time. In practice people run this software on the x86 and ARM chips found in ordinary computers and phones, assembled with other components in ways the proof did not model. And what was proven is “no crash on any input under 64 MB,” as judged by a monitoring tool that does not catch every category of memory error. Not every entry point was verified, and verifying all of them still would not cover a whole library.[1]

A footnote addresses reward hacking, where a model finds a shortcut that satisfies the letter of its goal. Here, rewriting a program to do nothing at all would make “never crashes” trivial to prove. The team docked points whenever Claude modified the code, awarded credit for showing that a given fix changed the behavior of as few inputs as possible, and logged and audited every change.[1]

So when you hear that something passed formal verification, three questions follow: what was used as the spec, what environment was assumed, and how much of the code the claim covers. In Anthropic’s framing, the remaining bugs have not vanished; you now know where to look for them.[1]

What this is worth if you don’t write code

  • Hear “we verified it” as two different claims. “We ran it several times and it looked fine” and “we showed it holds in every case” carry different guarantees. The first is only true of the cases that were run.
  • Writing down what a correct result looks like, before handing off the work, makes checking possible. The most expensive step in formal verification is writing the spec, and delegating to AI has the same shape. If you cannot state the conditions a good result must meet, you have no standard to check the output against.
  • Trying to produce a counterexample teaches you more than confirming success. In the experiment above, what hid the bug was a test that only generated sensible inputs. Looking for the case that breaks beats looking for the case that works.
  • When a security notice says something is “memory safe,” check the scope. The real content is which guarantee was made under which assumptions.

Edsger Dijkstra’s old line still sets the terms: program testing can be used to show the presence of bugs, but never to show their absence.[7] Formal verification does show absence, within the bounds of a stated spec. The price is the work of writing that spec plus the token bill, and the guarantee holds only as far as the spec and its assumptions match reality. In 2026 that price fell. Work that once took twenty person-years can absorb models and tokens instead, and because nobody has to read the proof, AI-written volume is usable as-is.

Related reading: the trust problem with AI-written code, how code review became the bottleneck, and measuring agent reliability.

Sources


Notes

  1. Anthropic Frontier Red Team, “LLMs are making formal software verification feasible at a much larger scale” (2026-10-22). Source for 239 entry points across 123 libraries, ~180 million lines of Rocq, typical proof ~600,000 lines, median binary of 18,000 instructions, the 1 MB fuzzer input cap, hundreds of millions of tokens per proof, Opus 5.5 using 40% fewer tokens across 36 tasks, 17% savings from a shared workspace, seL4 at two versus twenty person-years, and the limitations and reward-hacking notes. Original ↩
  2. Lean official site. Lean’s definition as “an open-source programming language and proof assistant,” Mathlib at over a million lines, and AWS verifying Cedar in Lean. Original ↩
  3. Richard MacManus, “The War Against Slop and an Emerging Weapon to Fight it: Formal Verification,” Latent Space (2026-11-20). Clark Barrett’s quotes and the cvc5 usage figure at Amazon. Original ↩
  4. Latent Space Podcast, “Scaling Past Informal AI — Carina Hong, Axiom Math.” Source for both Hong quotes. Original ↩
  5. Lean FRO 2026 news timeline, Lean official site. August Anthropic Riemann zeta results, September Fermat’s Last Theorem formalization, October OpenAI and Axiom results and &Truth’s $70m seed round. Original ↩
  6. AI Signal experiment (2026-11-21, `scripts/tmp-verify-demo.ts`). 0 failures across 1,000,000 random runs, 507,927 with a widened generation range, 499,500 of 1,000,000 (50.0%) on exhaustive check with first counterexample remain=0, used=1, and 584 years to check 18,446,744,073,709,551,616 combinations at a billion per second. The outputs above are reproduced as printed. ↩
  7. Edsger W. Dijkstra, *Notes on Structured Programming*. The wording quoted here is the one most commonly cited from the text; sources differ on whether to date it to the notes as circulated in 1970 or the 1972 Academic Press volume, so no year is given above. A closely related formulation appears in Dijkstra’s EWD303. ↩

Source: https://www.anthropic.com/research/formal-verification