Lesson 2 of 15 · Foundations
Anatomy of a Good Prompt
The parts of a good prompt: task, context, input data, examples, output format and constraints, how each changes the answer, and building prompts in Python.
- 7 min read
- Beginner
- Reviewed
- The six building blocks of a good prompt
- What each part changes in the answer
- A sensible order for the parts, especially with long documents
- How to assemble a prompt from parts in code
Six building blocks
Almost every good prompt is made from a few of these parts. You rarely need all of them, but knowing them helps you spot what is missing.
| Part | What it does | Example |
|---|---|---|
| Task | Says what to do, with a clear verb | "Write the follow-up email for this meeting." |
| Context | Explains the situation and purpose | "I run a small bakery. This is a new flour supplier." |
| Input data | The material to work on | Meeting notes, a document, a spreadsheet, code |
| Examples | Show what a good output looks like | "Here is an email we sent last month that worked well." |
| Output format | Shape, length and structure of the answer | "A short email with a subject line, under 120 words." |
| Constraints | Rules, tone and things to avoid | "Friendly but professional. Do not invent prices." |
Watch each part change the answer
Prompt
Explain this error. IndentationError: expected an indented block
Example response
An IndentationError occurs when Python's parser encounters a code block that lacks the required indentation. Python uses indentation to define the structure of code blocks such as functions, loops, conditionals and classes, unlike many languages that use braces...
Correct but written for nobody in particular, and long.
Put the parts in a sensible order
There is no single correct order, but this one works well for most prompts:
- Context first, so everything after it is read in the right light.
- Input data, especially anything long, clearly separated.
- The task, close to the end, so it is fresh when the model starts writing.
- Format and constraints last.
The program below assembles a prompt from parts, in that order. Long input is wrapped in tags so it is clearly separate from the instructions (more on that in a later lesson).
def build_prompt(task, context="", data="", output_format="", constraints=""):
parts = []
if context:
parts.append(context)
if data:
# Long input goes first, wrapped in tags so it is clearly separate
parts.append("<notes>\n" + data + "\n</notes>")
parts.append(task)
if output_format:
parts.append("Format: " + output_format)
if constraints:
parts.append("Rules: " + constraints)
return "\n\n".join(parts)
prompt = build_prompt(
task="Write the follow-up email for this meeting.",
context="I run a small bakery. These are my notes from a meeting with a new flour supplier.",
data="- price 18% lower than current supplier\n- delivery Tue and Fri\n- trial order of 50 kg agreed\n- they need our VAT number",
output_format="A short email with a subject line, under 120 words.",
constraints="Friendly but professional. Do not invent prices or dates.",
)
print(prompt)Change the constraints or output_format and run it again. Writing prompts as parts like this makes it easy to see what you have and what is missing.
Common mistakes
The pieces of a prompt arrived in a muddle. Print them in a sensible order: background first, then the input, then the task, then the format.
pieces = {
"format": "Answer in 2 sentences.",
"task": "Explain what this error means and how to fix it.",
"context": "I am learning Python and just wrote my first loop.",
"input": "IndentationError: expected an indented block",
}
# Print the pieces in this order: context, input, task, format
# (one per line)
You want a model to answer a question about a 40-page report. Where should the report go?
Summary
- Prompts are built from task, context, input data, examples, output format and constraints.
- The task should be a clear action, not a topic.
- Each added part removes a guess and makes the answer more specific.
- A good default order: context, input, task, format. Put long documents first and the question last.
Next: how to be clear and specific, the most important prompting skill.
Finished reading? Tick it off to track your progress.