AI-Assisted Engineering

Can AI Really Understand Code? Exploring Code Intelligence

OpenAI's Codex paper reports that its model solves a meaningful share of hand-written programming problems from their descriptions, and far more when it can try many times. Does that mean AI understands code? We look at what code models can and cannot do, how they are evaluated and what 'understanding' should mean for developers.

By EthicalHat Research · · 3 min read
Infographic: What the Codex paper measured on HumanEval

"Understanding" is a loaded word. When a model writes a working function from a one-line description, it is tempting to say it understands code. When the same model confidently produces a function that fails on an obvious edge case, the word feels wrong.

This summer brought the most detailed public evidence yet on how well AI models handle code. In July, OpenAI published Evaluating Large Language Models Trained on Code, the paper behind Codex, the model powering GitHub Copilot. In August, OpenAI opened a private beta of the Codex API. It is a good moment to ask what these models can actually do.

How code models are evaluated

Early work evaluated generated code by how similar it looked to a reference solution, using metrics borrowed from machine translation. That is a poor measure: two very different programs can both be correct, and a program that looks almost right can be completely wrong.

The Codex paper introduced HumanEval, a benchmark of 164 hand-written Python problems, each with a function signature, a docstring and unit tests. A solution counts only if it passes the tests, a measure called functional correctness.

python
def below_zero(operations: list[int]) -> bool:
    """You're given a list of deposit and withdrawal operations on a bank account that
    starts with zero balance. Return True if at any point the balance falls below zero,
    otherwise return False.
    >>> below_zero([1, 2, 3])
    False
    >>> below_zero([1, 2, -4, 5])
    True
    """

What the results show

  • The largest Codex model tested (12 billion parameters) solved 28.8% of problems on its first attempt (pass@1).
  • Given 100 attempts per problem, at least one passed the tests for 70.2% of problems (pass@100).
  • GPT-3, not trained specifically on code, solved almost none.
  • Performance improved steadily with model size.

The gap between pass@1 and pass@100 matters. The model often can produce a correct solution but does not reliably produce it first. Where tests exist, generating many candidates and running them becomes a practical strategy.

What models seem to capture

Research on models such as CodeBERT, GraphCodeBERT and the newly released CodeT5, alongside Codex, suggests code models learn a great deal:

  • Syntax and idioms of many languages.
  • API usage patterns, such as which calls usually follow which.
  • Correspondences between natural language and code: docstrings, comments and variable names.
  • Some semantics: data flow, simple invariants and common algorithms.

That is enough to write plausible code for common tasks, translate between languages, summarise functions and search code by meaning.

Where "understanding" breaks down

The Codex paper is candid about limitations:

  • Long specifications. Performance drops sharply as the number of operations described in a docstring grows. Chaining several requirements together is hard.
  • Variable binding. Models confuse which operation applies to which variable when descriptions get complex.
  • Misalignment. When the surrounding code contains bugs, the model tends to produce buggy code too. It imitates context rather than trying to be correct.
  • Security and bias. It can generate insecure code and reflect biases in its training data.

In other words, code models are extraordinarily good pattern engines. They do not build a reliable mental model of what a program must do and check their work against it.

flowchart LR
  subgraph Strong["Strong today"]
    A[Common idioms]
    B[API recall]
    C[Code ↔ language mapping]
  end
  subgraph Weak["Weak today"]
    D[Multi-step specifications]
    E[Reasoning about edge cases]
    F[Project-wide intent]
  end

What this means for developers

  • Verification beats inspection. A generated function that passes good tests is far more trustworthy than one that merely looks right. Code models make tests more valuable, not less.
  • Short, clear specifications work best. Break problems into small, well-described functions.
  • Sampling plus testing is powerful. For well-tested tasks, generating several candidates and keeping the one that passes can work surprisingly well.
  • Human understanding remains essential for design, edge cases, security and anything that is not already a common pattern.

So, does AI understand code?

It depends on the definition. Code models capture enough structure and meaning to be genuinely useful, far beyond autocomplete. They do not yet reason about programs the way a careful engineer does, and they cannot tell when they are wrong. The practical stance is to use them as fast, fallible collaborators, and to let tests and reviews decide what ships.

Our secure SDLC team uses AI code tools with test-driven verification, and our secure code review service helps teams review AI-assisted code effectively.

EthicalHat Research

The EthicalHat research team: penetration testers, SOC analysts and engineers writing about security, software engineering and AI.

Want an expert opinion on your own systems?

Email info@ethicalhat.com or call +1-844-838-4422. Our security engineers are happy to talk it through.