What it is
Code generation is using AI to write, fix, and explain computer code. You describe what you want in ordinary words, such as "a spreadsheet formula that adds up this column only when the status says Paid", and the AI replies with the code that does it, usually with a short explanation of how it works.
Code is just text written in a strict language that computers can follow. Spreadsheet formulas are code. The HTML that lays out a web page is code. So is a Python script that renames a folder of photos, or the snippet a website builder asks you to paste in to add a booking button. Because a language model learns by reading enormous amounts of text, and the public internet contains billions of lines of code alongside explanations of that code, these models turned out to be surprisingly good at writing it. They learned the patterns of programming languages the same way they learned the patterns of English.
In practice, code generation covers several jobs:
- Drafting. Writing a new formula, script, function (a small named block of code that does one job), or web page from a description.
- Fixing. Pasting in code that doesn't work, along with the error message, and asking what's wrong.
- Explaining. Pasting in code you didn't write, such as a formula a colleague left in a shared spreadsheet, and asking what it does line by line.
- Translating. Turning code from one language into another, or turning an Excel formula into the Google Sheets version.
- Testing. Writing tests, which are small extra pieces of code that check whether the main code gives the right answers.
You can do all of this in an ordinary chatbot. Programmers also use code assistants built into the software they write code in, which suggest the next line as they type, much like predictive text on a phone. When the AI goes further and plans a task, edits many files, runs the code, and fixes its own errors without you pasting anything back and forth, that's a different element: Coding Agents. This article is about the conversation between you and the AI about a piece of code: you ask, it writes or explains, and you decide what to do with the result.
The key thing to understand is that the AI doesn't run your code when it writes it, unless the tool you're using has that feature switched on. It predicts what correct code probably looks like. Most of the time that prediction is very good. Sometimes it's confidently wrong, in the same way a chatbot can confidently invent a fact. The difference with code is that you can usually find out quickly, because code either does the job or it doesn't.
Why it matters
It opens programming to people who never learned it. For decades, anyone who wanted a custom formula, a small automation, or a tweak to their website had two options: learn to code, or pay someone who had. Code generation adds a third. A novelist can get a script that counts words per chapter across forty files. A shop owner can get a formula that flags orders older than a week. A podcaster can get a snippet that adds a subscribe button to a site. These are jobs that were too small to hire a developer for and too fiddly to learn from scratch, and they're now a five-minute conversation.
It changed how professional software gets written. On 29 October 2024, Google's chief executive Sundar Pichai told investors that "more than a quarter of all new code at Google is generated by AI, then reviewed and accepted by engineers." Notice the second half of that sentence. Even at a company full of expert programmers, the AI drafts and a person checks. That's the pattern this whole article keeps returning to.
It makes unfamiliar code less scary. Explanation may be the most underrated use. Inherited a spreadsheet full of nested formulas nobody understands? Bought a website theme with settings that require pasting in code? Asking an AI "explain this to me as if I've never programmed" turns a wall of symbols into a plain description, and that often matters more than writing anything new.
The speed gains are real but not automatic. Early studies found large speed-ups, and later ones found that experienced people on complicated projects could actually be slowed down. Both are covered in the examples below. The honest summary is that code generation helps most with small, well-described, self-contained jobs, and helps least when the job depends on a lot of knowledge the AI can't see.
The risks are real too. AI-written code can contain security holes, invented features, and references to software that doesn't exist. For a formula in your own spreadsheet, the worst case is a wrong number you'll probably notice. For code that handles customer data or payments, the worst case is much worse. Knowing where that line sits is part of using code generation well.
Real-world examples
GitHub Copilot and the 55 percent study
On 29 June 2021, GitHub, the website where millions of programmers store and share code, announced a technical preview of GitHub Copilot, which it called an "AI pair programmer". It was built on OpenAI Codex, a model OpenAI had trained specifically on code. Copilot sat inside the programmer's editor and suggested whole lines or entire functions as they typed. It was the first time many working developers used AI code generation every day, and it set the template that most coding assistants still follow.
In September 2022, GitHub published a controlled experiment. It recruited 95 professional developers and asked them to write a small web server in JavaScript, one of the most common programming languages. Half used Copilot and half didn't. The Copilot group finished in an average of 1 hour 11 minutes, against 2 hours 41 minutes for the others: 55 percent faster. They were also slightly more likely to finish at all, 78 percent against 70 percent. The researchers later wrote the study up as a paper, published in February 2023.
It's worth being clear about what that experiment measured. The task was small, well defined, and started from nothing, which is exactly the kind of job AI handles best. It was also run by the company selling the product. The result was real, but it described one kind of work, not all of it.
The METR study: slower, but feeling faster
In July 2025, METR, a non-profit research group that studies what AI systems can do, published a study that pointed the other way. It recruited 16 experienced developers who each worked on large open-source projects they already knew well (open source means the code is public and anyone can contribute). Across 246 real tasks from those projects, a random draw decided whether each task could be done with AI tools, mainly the Cursor code editor with Anthropic's Claude models at the time, or without them.
With AI, the developers took 19 percent longer. The more striking finding was about perception. Before starting, they expected AI to make them about 24 percent faster, and even after finishing they believed it had made them about 20 percent faster. They felt quicker while actually being slower, probably because reviewing, correcting, and re-prompting the AI took more time than they noticed.
METR was careful to say this was a snapshot of early-2025 tools and of one particular situation: experts on large, familiar projects with high standards. It didn't claim the result applies to beginners, to small tasks, or to later tools. In February 2026 it said it was redesigning its experiment, partly because many developers now refused to work without AI at all, and that developers were likely sped up more by early-2026 tools, though its new data was too biased to say by how much. The lasting lesson isn't "AI makes coding slower". It's that how fast you feel is not a measurement. If speed matters to you, time it.
Vibe coding
On 2 February 2025, Andrej Karpathy, a founding member of OpenAI and former head of AI at Tesla, posted on X about a new way of working he called "vibe coding". He described it as a style where you "fully give in to the vibes, embrace exponentials, and forget that the code even exists": you describe what you want, accept what the AI writes without reading it closely, and paste any error message back in until things work.
The phrase caught on far beyond programmers, because it named something non-programmers had started doing: building small apps, games, and tools purely by talking to an AI. Merriam-Webster listed it as a slang and trending term in March 2025, and Collins English Dictionary made it its Word of the Year for 2025. Karpathy later called the original post a throwaway thought he fired off, which shows how much the idea had been waiting for a name.
Vibe coding is a genuinely good way to make a personal tool, a prototype, or a toy. It's a risky way to build something other people will trust with their data, because nobody has read the code. Several vibe-coded apps in 2025 turned out to expose their users' personal information, which is the security problem in the next example showing up in real life.
Security flaws and invented packages
In August 2021, weeks after Copilot's preview launched, researchers at New York University and the University of Calgary led by Hammond Pearce released a paper titled "Asleep at the Keyboard?". They set Copilot 89 security-sensitive scenarios, such as handling passwords or database queries, and examined the 1,689 programs it produced. About 40 percent contained a known type of security weakness. The AI had learned from public code, and a lot of public code is insecure.
A newer risk appeared as more people started installing whatever the AI suggested. Programmers often reuse ready-made building blocks called packages, downloaded by name from public libraries. A study by researchers at the University of Texas at San Antonio and colleagues, first released in June 2024 and presented at the USENIX Security Symposium in 2025, generated 576,000 code samples from 16 models and found that the models regularly recommended packages that don't exist, a form of hallucination. Commercial models did so for at least 5.2 percent of packages on average and open-source models for 21.7 percent, producing more than 205,000 unique made-up names. Worse, the same fake names came back again and again when the prompts were repeated.
That repetition makes an attack possible: a criminal can register a commonly hallucinated name and fill it with malicious code, then wait for someone to install it because an AI told them to. In April 2025, Seth Larson, a security developer at the Python Software Foundation, gave this a name: "slopsquatting". If an AI tells you to install something, check that it's a real, widely used package before you do.
An everyday case: the invoice tracker
Here's an illustrative scenario. Imagine Dana, an illustrator who tracks commissions in a spreadsheet. She wants a column that shows "OVERDUE" when an invoice is unpaid and more than 30 days old. She asks a chatbot, pastes in her column headings, and gets a formula back with a two-sentence explanation. It works on the first three rows. Then she notices one invoice marked "paid" in lower case still shows as overdue. She tells the chatbot, and it adjusts the formula to ignore capitalisation. Total time: ten minutes, including the check that caught the bug. The AI wrote the formula; Dana's test on real rows made it right.
Try it yourself
You need any AI chatbot (ChatGPT, Claude, Gemini, Copilot or similar; free plans could do this at the time of writing, but features and limits change, so check your tool's current terms) and either a spreadsheet program or a computer with a web browser. Allow about thirty minutes. Both exercises are safe: nothing gets installed and nothing leaves your computer.
Exercise one: a spreadsheet formula
- Open a spreadsheet in Excel, Google Sheets, LibreOffice, or Apple Numbers. In row 1 type the headings
Client,Amount,Date sent,Status. Fill in six made-up rows, and make sure some statuses sayPaid, some sayUnpaid, one sayspaidin lower case, and one is blank. - Tell the chatbot exactly which program you're using, because formulas differ between them. Copy and paste this, filling in the brackets:
I use [Google Sheets / Excel / LibreOffice / Numbers].
My columns are: A = Client, B = Amount, C = Date sent, D = Status.
Data starts in row 2.
Write a formula for column E that shows "OVERDUE" when the status
is not "Paid" and the date sent is more than 30 days before today.
Otherwise leave the cell empty.
Explain the formula in plain English, one part at a time.
Tell me which unusual cases might break it.
- Paste the formula into cell E2 and copy it down the column.
- Check it. Work out by hand which rows should say OVERDUE, then compare. Look hardest at the tricky rows: the lower-case "paid", the blank status, and a date exactly 30 days ago. If any row is wrong, tell the chatbot which row, what you expected, and what you got. That one sentence of feedback fixes most problems.
Exercise two: a tiny web page
- Paste this prompt:
Write a single HTML file, with no external files or links, for a
one-page "link in bio" site for a [your job, e.g. watercolour painter].
Include my name as a heading, a two-line description, and three
buttons linking to example.com. Use a calm colour scheme.
Put all the styling inside the file. Add short comments explaining
each section so a beginner can edit it.
- Copy the code. Open a plain text editor (Notepad on Windows, TextEdit on a Mac set to plain text via Format, Make Plain Text), paste it in, and save it as
test.htmlon your desktop. - Double-click the file. It opens in your web browser, as a page only you can see.
- Check it. Does it look right on a narrow window? Drag the browser edge in to find out. Do the buttons go where they should? Then ask the AI to make one change, such as "make the buttons rounder", and repeat. Read the comments it added, and try changing a colour yourself. That's how explaining turns into understanding.
Exercise three: ask for an explanation
Find any formula or snippet you use but don't understand, perhaps in a shared spreadsheet or a website setting, and paste it in with this prompt. Don't include passwords, keys, or anyone's personal data.
Explain what this code does, line by line, for someone who has
never programmed. Then tell me one thing that could go wrong with it.
[paste the code here]
Common mistakes and misunderstandings
Assuming it works because it looks right. AI-written code is fluent and neatly formatted even when it's wrong. The only proof is running it on real examples, including awkward ones, and comparing the result with what you expected. The Dana scenario above and the Copilot security study are both reminders that "looks professional" and "is correct" are different things.
Not saying which tool or version you use. An Excel formula may not work in Google Sheets. Code written for one website builder won't work in another. The AI will guess if you don't tell it, and it often guesses the most common option, which may not be yours.
Installing whatever it suggests. If the AI says "first install this package" or "add this plugin", check that the thing exists, is widely used, and comes from where you'd expect. Invented package names are common, and slopsquatting means some of them may now be traps.
Pasting secrets into the chat. Code often contains passwords, API keys (secret codes that let software use a paid service), or customer data. Remove them before asking for help. Replace them with placeholders like YOUR_KEY_HERE.
Trusting your own sense of speed. The METR study found experienced developers felt faster while being slower. If you're deciding whether AI saves you time on a real project, measure it rather than going by feel.
Vibe coding things that hold other people's data. Accepting code without reading it is fine for a personal tool. It's not fine for a sign-up form, a shop, or anything that stores names, emails, or payments. For those, have someone who can read code review it, or use an established product instead.
Asking for everything at once. "Build me a membership site" produces a big, shaky answer. "Write the formula for column E" produces a small, checkable one. Break the job into steps, test each one, and then move on; the same idea as prompt chaining.
Thinking it replaces understanding. You don't need to become a programmer, but asking the AI to explain what it wrote, and reading that explanation, is what lets you fix the next problem yourself. People who read the explanations get better fast. People who skip them stay dependent.
Confusing code generation with coding agents. A chatbot that writes a formula for you to paste is code generation. A tool that edits your files and runs commands on your computer by itself is an agent, and it needs more care about what it's allowed to touch.
Related elements
- Coding Agents: the next step up, where the AI edits files, runs code, and fixes its own errors.
- Language Model: the kind of AI that writes code by predicting text, the same way it writes prose.
- Prompt: clear, specific requests are what make generated code fit your situation.
- Hallucination: invented functions and package names are hallucinations in code form.
- Iterative Refinement: the loop of test, report the problem, and try again that fixes most code.
- Prompt Chaining: breaking a big coding job into small steps you can check one at a time.
- Context Window: how much of your code and instructions the AI can see at once, which limits what it can get right.
- Data Analysis: a close cousin, where AI writes formulas and scripts to make sense of your spreadsheets.
- API: the connections code often uses to talk to other services, and a common thing to ask AI to help write.
- Human in the Loop: why a person should always check AI-written code before it touches anything important.