# Code Generation and Completion

> How AI code completion and code generation are not the same. How to prompt each one, and why code that runs on the first try still has to be read.

**Course:** [Prompt Engineering](https://learn.benibix.com/learn/ai-working-with-ai)  
**Time:** 11 min

**After this lesson you can:**

- Tell code completion from code generation
- Write a code request with inputs, outputs and tests
- Read suggested code before you keep it

## How AI suggests and writes code

AI code generation writes program code from a plain request. AI code completion suggests the next part of the code as you type, right in the editor. In a 2022 test by GitHub, 95 developers wrote the same small program. Those using Copilot finished 55% faster, in about 71 minutes against 161.

Completion is the quieter of the two. It reads the file and the code around your cursor, then offers anything from the rest of a line to a whole function. You press a key to take it, or keep typing to skip it. It suggests, and it never runs anything.

Generation starts from words. You describe what the code should do, and the model writes it: one function, a test or a small app. The prompt rules from the rest of this course apply. Say the language, the inputs, the outputs and what should happen when something goes wrong.

For completion, the prompt is the code itself. A clear function name, a short comment saying what it does and the types of its inputs all steer the guess. A function called f with no comment gets a guess. One called is_weekend, taking a date, gets the right two lines.

Generated code often runs on the first try. That is not the same as being right. It can miss a rare case, use an old way of doing things or call a library function that does not exist. Running it once proves it runs once.

Ask for tests with the code. A prompt that says write the function and five tests, including an empty input, gives you a way to check the work. Read the tests first. If they test the wrong thing, the code will pass the wrong thing.

Read every line you keep. Suggestions look right more often than they are right, and the speed makes it easy to stop reading. The time saved on typing is only saved if you do not spend it later hunting for the bug.

```text
Write a Python function is_palindrome(word) that returns True
if the word reads the same both ways, ignoring capitals.
Add five tests, including an empty string and "Anna".
```
A code request names the language, the input and the output. It also asks for the tests.

![An editor with one line of typed code in full colour and two suggested lines in grey below it, with a note that one key press accepts them.](https://learn.benibix.com/diagrams/pr-code-completion.svg)

### Example: Emma adds a weekend check

Emma types the first line of an is_weekend function, and completion offers two more lines. She presses Tab to take them.

Then she asks a chat model for five tests. The Sunday test fails, because the suggestion counted the days of the week the wrong way. Two minutes of reading would have caught it.

**Words in this lesson:** [AI Code Completion](https://learn.benibix.com/term/ai/ai-code-completion), [Code Generation (AI)](https://learn.benibix.com/term/ai/code-generation), [AI Coding Assistant](https://learn.benibix.com/term/ai/ai-coding-assistant)

> **The one thing to remember.** Completion suggests the next lines while you type. Generation writes code from a request. Both write code you still have to read.

**Question:** Is AI-written code right if it runs on the first try?

**Answer:** Not always: it can still miss cases or be unsafe. Running once proves it runs once. Tests and a careful read show whether it does the right thing on the cases that matter.

---

Source: https://learn.benibix.com/learn/ai-working-with-ai/code-generation-and-completion
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-20
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
