Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

How to write a good prompt for code

After this lesson: I can write a request that gets a usable answer.

55 min lesson2 commandsAI coding agent lab: use opencode to explain and fix code, and check its work

A good prompt gives what the assistant lacks

A good prompt is a request that gives a coding assistant the goal, the facts it cannot see and the limits the answer must keep. It matters because the assistant fills every gap with a guess, and a guess that looks fine is hard to spot.

Compare "make it better" with "in shop.js, only change asPounds so that it shows two decimal places". The second can be checked.

By the end of this lesson

Note

I can write a request that gets a usable answer.

Key words

WordWhat it means
Requestthe words you give an assistant to say what you want
Constrainta limit the answer must keep
Examplea sample input with the output you want
Scopehow much you ask for at one time
Iterateto ask again with a correction

The cells students fill in are left blank in this preview.

A request has five parts

Say the goal: what should exist when the work is done. Say where: the file or the function. Say the constraints: what must not change, and what style to follow.

Give an example: one input and the output you want. Say how you will check: for example, that a test must pass.

Worked example: a weak request made strong

PartWeak requestStrong request
GoalMake it betterShow prices with two decimal places
WhereIn the codeThe asPounds function in shop.js
ConstraintNothing saidChange nothing else, keep the tests as they are
ExampleNothing saidasPounds(5) should give £0.05
CheckNothing saidThe tests must still pass

The cells students fill in are left blank in this preview.

Send a vague request

Command
docker exec -w /home/node/project agent opencode run "Tell me about this project."

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Send a specific request

Command
docker exec -w /home/node/project agent opencode run "In shop.js, list each function with its parameters, one per line, and say in one short sentence what it is for. Do not change any file."

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Compare what the two requests got back

Both requests only ask a question, so nothing in the project changes. The words you get back will differ from a classmate's and from your own next try.

Look at the shape instead. Which answer is easier to check against the file? Which tells you more than you could see for yourself? The specific request names a file, a format and a limit.

Ask for one small step at a time

The more you ask for at once, the more the assistant decides for you. Ask for one function, read it, then ask for the next.

When an answer is wrong, do not start again. Iterate: say what is wrong and what you want instead. "The total ignores the last item" is far more useful than "that is wrong".

A common mistake

Warning

Many people think a longer prompt is a better prompt. Length is not the point: facts are. Add the file, the limit and an example, and cut every sentence that adds none of them.

Say what shape the answer should take

You can ask for an answer in a form you can check. Examples are "one function only", "a list, one item per line" and "the changed lines and nothing else".

A small, shaped answer is quick to read and quick to run. A long essay of code is hard to review, which is the next lesson.

About this lab and its source: AI coding agent lab: use opencode to explain and fix code, and check its work

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.