LLM Evaluation for Coding: How to Test AI-Generated Code Properly

LLM Evaluation for Coding: How to Test AI-Generated Code Properly

LLM evaluation for coding is how you find out whether an AI coding assistant actually writes code you can ship, rather than code that merely looks right. A model can produce elegant-looking code that fails its tests, leaks secrets, or breaks when the input changes. The central question is straightforward: does the generated code work, and is it safe to keep? You answer that by running the code, testing it against real tasks, and checking it the way you would check a colleague’s work.

Coding is one of the most demanding tests for a language model because code either runs or it does not. There is no partial credit for a function that almost compiles. At the same time, passing a test is not the whole story: code can pass its tests and still be slow, insecure, or unreadable. Good evaluation measures all of that, not just the pass rate.

Key takeaways

  • LLM evaluation for coding must run the generated code, not just read it.
  • Pass rates alone hide problems with security, performance and readability.
  • Test on realistic tasks from your own codebase, not only on textbook problems.
  • Use held-out tests the model has never seen to avoid inflated scores.
  • Combine automated tests, static analysis and human review for a full picture.

What is LLM evaluation for coding?

LLM evaluation for coding means giving a model a set of programming tasks, collecting the code it produces, and scoring that code against defined criteria. The tasks can range from writing a single function to fixing a bug in an existing file, or building a small feature from a written specification. Each task has a way of deciding whether the code is good enough.

The most common scoring method is execution-based: run the generated code against a set of tests and see whether it passes. This is objective and hard to game, which is why it is the basis of most coding benchmarks. But execution alone is not enough, because tests only check what they were written to check. A function can pass every test and still contain a security flaw, a performance trap, or a hidden assumption about its inputs.

Why coding evaluation is different

Code has a property that prose does not: it is executable. That makes evaluation more objective, because you can run the output and observe what happens, and more demanding, because a single misplaced character can turn working code into a crash. Fluency is a poor proxy for correctness in code.

Coding tasks also have many valid solutions. Two functions can both pass the tests while differing in speed, clarity and safety, so an evaluator that only checks pass or fail hides real differences. Serious coding evaluation adds checks for style, complexity and security on top of the test results.

There is also the problem of memorisation. Public coding problems and their solutions appear in training data, so a model may reproduce a known answer rather than solve the problem. Test on tasks the model could not have memorised: your own code, your own bugs, and your own specifications.

What to measure

Functional correctness

Functional correctness asks whether the code does what the task asked for. The standard way to measure it is to run hidden tests: tests the model never saw, covering normal cases, edge cases and error handling. A task with only one or two tests is easy to pass by luck, so each task should have a thorough test suite. Best for: comparing models on a fixed set of programming tasks, where the tests are the same for every candidate.

Test generation matters as much as the model: if your tests are weak, a bad solution can pass. Check boundaries, empty inputs, invalid inputs and large inputs, and include at least one test a naive solution would fail.

Code quality and readability

Code that works but cannot be read is a liability. Measure readability with a linter and a complexity check, and have a human reviewer read a sample. Look for clear naming, sensible structure and comments where the logic is not obvious.

Watch for over-engineering: models sometimes produce elaborate solutions to simple problems, with extra layers that add risk without adding value. A simple solution that passes the tests is usually better than a clever one that does the same job with three times the code.

Security

Generated code can contain security flaws: unsanitised inputs, hard-coded credentials, weak cryptography, or unsafe handling of user data. Run a static security scanner over every generated snippet, and include tasks that deliberately invite an insecure solution, such as building a database query from user input. A model that writes an injection-prone query has failed the task, even if the query returns the right rows. Our guide to building an LLM evaluation framework that actually guides decisions shows how to include security in the scoring rubric.

Performance

Correct code can still be too slow: a function that works on ten items but takes minutes on ten thousand is not production-ready. Measure performance on realistic input sizes, and include at least one task where the obvious solution is inefficient.

Following instructions and context

Coding assistants work inside a project, not in a vacuum. They need to follow the project’s conventions, use its existing helpers, and respect its style guide. Test this by giving the model a task inside a real repository, with the surrounding code visible, and scoring whether the generated code fits the codebase. A solution that ignores the project’s own utilities and reinvents them is a worse solution, even if it passes the tests.

Evaluation methods compared

Different methods suit different questions. Unit tests give an objective verdict on correctness. Static analysis catches style and security issues cheaply. Human review catches the judgement calls that tools miss. Use them together, with each method covering what the others cannot.

Method How it works Main strength Main weakness
Hidden unit tests Generated code runs against tests the model never saw Objective verdict on correctness Only checks what the tests cover
Static analysis Linters and scanners check style, complexity and security Fast, cheap, catches common flaws Misses logic errors and bad design
Human review Developers read and rate the generated code Catches design and readability problems Slow and varies between reviewers
LLM-as-judge A model reviews the code against a rubric Scales to large test sets May miss the same bugs as the model tested

One common approach is to generate several solutions per task and count how often at least one passes, but this flatters models that need many attempts. In real use a developer usually takes the first suggestion, so also measure the first-attempt pass rate, and report both numbers clearly.

Developer running automated tests on AI-generated code at a desk
Running the code is the only reliable test. A solution that looks right on screen can still fail the moment it meets real data.

Building a coding test set

Start with tasks from your own work: real bugs, feature requests and refactoring jobs reflect the code your team actually writes. For each task, write a clear specification, provide the relevant context, and prepare hidden tests that check the behaviour you care about.

Include a mix of difficulty: easy tasks such as writing a utility function, medium tasks such as fixing a bug in an existing file, and hard tasks such as implementing a feature across several files. A test set of only easy tasks will make every model look good.

Cover the languages and frameworks your team uses. A model that is strong in one language may be weak in another, and a general benchmark score will not tell you which, so test in every language your team relies on.

Keep tasks stable between rounds so scores are comparable, but refresh the set over time: retire tasks that have become too easy and add new ones based on recent failures. For the pipeline that runs these tests, see our guide to building an LLM evaluation pipeline that actually catches failures.

Common failure modes in generated code

The most common failure is a plausible but wrong solution: code that compiles, runs, and returns the wrong answer for some inputs. These failures are dangerous because they look correct at a glance. Hidden tests with edge cases are the main defence, and they need to be genuinely tricky, not just a repeat of the examples in the task.

Hallucinated APIs are another frequent problem: a model may invent a function, a library method or a configuration option that does not exist. Test with the real library versions your project uses, and treat a call to a non-existent API as a hard failure.

Security flaws are the failures with the highest stakes. Generated code has been known to build database queries by concatenating user input, store passwords in plain text, and disable certificate checks to make a connection work. None of these show up in a functional test, so run a security scanner on every round and include security in the score.

Running coding evaluation as a routine

Run the full test set before adopting a new model or a new version, and a smaller subset whenever the prompt or tooling changes. Record results alongside the model version and date, so you can compare like with like and notice changes before your team does.

Review the failures, not just the score. Group them by type: logic errors, hallucinated APIs, security issues, style problems. The pattern tells you what to fix, whether that is a prompt change, a better tool, or a decision that a model is not ready for a particular task.

Coding evaluation checklist

  • Tasks come from your own codebase, not only from public problem sets.
  • Every task has hidden tests covering normal, edge and invalid inputs.
  • First-attempt pass rate is measured alongside best-of-several pass rate.
  • A linter and a security scanner run on every generated snippet.
  • Performance is tested on realistic input sizes.
  • Human reviewers rate a sample of code for readability and fit.
  • Tasks cover the languages and frameworks the team actually uses.
  • Results are recorded with the model version and date for comparison.
  • Failures are grouped by type and reviewed after each round.
  • The test set is refreshed regularly with new tasks from real work.

Frequently asked questions

What is the best way to measure coding ability in an LLM?

Run the generated code against hidden tests on realistic tasks. Pass rates on public problem sets are a useful starting point, but they overstate ability when the model has seen the problems before, so add tasks from your own codebase.

Is a high pass rate enough to trust generated code?

No. Passing tests does not prove the code is secure, fast or maintainable. Add static analysis, a security scan and a human review of a sample before you trust a model with production code.

How do I stop the model from memorising my test set?

Use tasks from private work that was never published, and refresh the set regularly. Do not reuse the same tasks for both tuning the prompt and measuring the result, because tuning against the test set inflates the score.

Should I evaluate with one attempt or several?

Measure both. Best-of-several shows what the model can find with retries, while first-attempt pass rate shows what a developer will actually get. Report the two numbers separately and be clear about which one you mean.

How do I evaluate code review and debugging, not just code writing?

Give the model real code to review or a real bug to fix, and score the result against a known-good answer. For reviews, check whether it found the actual defects; for debugging, check whether the fix works and introduced no new problems.

Can an LLM judge another model’s code?

Yes, for ranking and triage, but with limits: a judge model can miss the same subtle bugs as the model it is judging, so use it to prioritise human review, not as a replacement for running the tests.

Latest articles

Scroll to Top