AI code generation

On this page 7

AI code generation is the use of large language models trained on source code to produce new program code from a natural-language request, a partial implementation or the surrounding project context. Among AI applications in software work, it ranges from single-line suggestions inside an editor to multi-file changes proposed by an agent that can read a repository, run tests and revise its own output.

The technique changes where developer effort goes rather than removing it. Writing boilerplate becomes faster, while specifying intent, reviewing output and verifying behavior become the dominant tasks. Teams that treat generated code as an untrusted draft gain the most; teams that accept it unread inherit its defects.

How AI code generation produces code

AI code generation works by predicting the next token of code, conditioned on a prompt that the tool assembles from the request and the available context. The model has learned the statistical structure of programming languages from large corpora of public and licensed code, so it can reproduce idioms, library calls and common patterns with high fluency.

The quality of the result depends heavily on context assembly. A tool typically gathers the current file, open tabs, related symbols, type signatures and sometimes retrieved snippets from the wider repository, then fits them into the model’s context window, which in current systems ranges from tens of thousands to over a million tokens. Code that the model cannot see is code it will guess about.

The table below shows the main stages of a typical generation pipeline.

StageWhat happensMain influence on quality
Context gatheringCollects code, types and documentationRelevance of included files
Prompt constructionCombines instructions, context and requestClarity of the stated intent
DecodingModel samples tokens until a stop conditionTemperature and model capability
Post-processingFilters, formats and deduplicates suggestionsSyntax checks and policy filters
Verification loopCompiles, lints or runs tests, then retriesCoverage of the test suite
Stages of a code generation pipeline

Interaction modes and their scope

AI code generation appears in several interaction modes, which differ mainly in how much code is produced per request and how much autonomy the tool has. Code completion is the narrowest mode, while agentic coding is the broadest and the hardest to review.

ModeTypical output sizeReview burden
Inline completionOne line to one functionLow, reviewed while typing
Chat-based generationFunctions, classes or scriptsModerate, pasted deliberately
Edit by instructionTargeted changes across a fileModerate, shown as a diff
Agentic codingMulti-file changes with tool useHigh, needs full code review
Natural-language to querySQL or configuration snippetsModerate, depends on schema access
Common interaction modes

Related tasks often have their own names. Code translation converts code between languages, automated program repair targets known defects, AI test generation produces tests, and AI code review comments on existing changes. All of them use the same underlying models, but each benefits from different context and evaluation methods.

How generated code is evaluated

Generated code is evaluated by functional correctness, meaning whether it passes tests, rather than by textual similarity to a reference solution. The widely used pass@k metric estimates the probability that at least one of k sampled solutions passes all unit tests for a problem.

Benchmarks have moved from short algorithmic puzzles toward realistic repository tasks. Results on simple function-level benchmarks are now near saturation for leading models, so they say little about performance on real codebases. Scores also suffer from benchmark contamination when test problems appear in training data.

ApproachWhat it measuresMain limitation
Function-level benchmarksSolving isolated, specified problemsSaturated and often contaminated
Repository-level benchmarksResolving real issues in real projectsCostly to run, narrow language mix
Internal acceptance rateShare of suggestions developers keepKept code is not always correct
Defect and rework trackingBugs and reverts linked to generated codeSlow feedback, attribution is hard
Evaluation approaches compared

For an organization, the most meaningful signal is downstream quality: change failure rate, review time and the volume of rework, measured before and after adoption. Acceptance rate alone is misleading, because developers often accept a suggestion and then rewrite most of it.

Where AI code generation helps most

AI code generation delivers the clearest gains on well-specified, pattern-heavy work where correctness is easy to check. It is weaker on tasks that require deep domain knowledge, novel algorithms or decisions about system architecture.

Task typeTypical fitReason
Boilerplate and scaffoldingStrongHighly repetitive, easy to verify
Unit tests for existing codeStrongBehavior is visible in the source
Unfamiliar library usageGoodCommon APIs are well represented
Data transformation scriptsGoodOutput can be checked on samples
Security-sensitive logicWeakSubtle flaws pass casual review
Architecture decisionsWeakNeeds context beyond the codebase
Fit of AI code generation by task type

Practical guidance follows from this pattern. Provide explicit specifications and examples in the prompt, keep requests small enough to review in one sitting, and pair generation with automated tests and static analysis. A strong test suite turns the tool from a guesser into a system that can check its own work.

Risks, limitations and common mistakes

The central risk of AI code generation is plausible but wrong code. Models produce output that compiles, reads cleanly and follows conventions, which lowers the reviewer’s guard even when the logic is incorrect.

  • Hallucinated APIs and packages: the model invents functions or dependency names; attackers can register such names, a risk known as package hallucination or slopsquatting.
  • Insecure patterns: generated code may reproduce injection flaws, weak cryptography or missing input validation found in training data.
  • Outdated knowledge: suggestions may target deprecated library versions released before the model’s training cutoff.
  • License and provenance questions: near-verbatim reproduction of licensed code is rare but possible, so many tools offer duplicate-detection filters.
  • Data exposure: prompts can carry proprietary code or secrets to an external service unless the deployment is configured to prevent retention.
  • Skill erosion and review fatigue: large generated diffs encourage approval without understanding, especially in vibe coding workflows.

The common mistake behind most incidents is treating generation as completion of the task. The task is complete only when the code has been read, tested and understood by someone accountable for it.

A non-technical view

AI code generation is like having a very fast assistant who has read millions of programs. You describe what you need, and the assistant drafts the code in seconds.

The draft is often good, but the assistant does not truly know your project and sometimes makes confident mistakes. A programmer still has to check the work, test it and decide whether it is right. The speed is real, but so is the need for review.

A concrete example

A backend team at a mid-sized logistics company needs a new export endpoint that writes shipment records as CSV, with filters for date range and carrier. A developer asks a coding assistant inside the editor to add the endpoint, following the style of three existing export endpoints that the tool includes as context.

Within a minute the assistant proposes the route handler, a query function and twelve unit tests. The tests pass, but review shows that the date filter ignores time zones and that the query loads all rows into memory before writing the file. The developer asks for a streaming version, adds a time zone test case and fixes the filter by hand.

The finished change ships in about half a day instead of two days. The team records that the generated tests caught one regression later, while the two defects found in review would have reached production without a careful reader.