Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 38 ยท Getting Started

The Anatomy of a Prompt

Break a prompt into parts: role, task, context, constraints, examples and output format. Build and test a reusable prompt builder in Python.

Strong prompts are rarely one sentence. They are assembled from a small set of parts, each doing a different job. Once you can name the parts, you can see which one is missing when an answer disappoints.

The six common parts

Not every prompt needs all six, but knowing the full list helps you decide what to include.

Part Question it answers Example
Role Who should the model act as? "You are a careful technical editor."
Task What exactly should it do? "Summarize the article below."
Context What does it need to know? "The readers are new hires."
Constraints What limits apply? "At most 80 words. No jargon."
Examples What does good look like? One or two sample input/output pairs
Output format How should the answer look? "Return a bulleted list."

The input data (the thing being summarized, classified or translated) is also part of the prompt. Keep it clearly separated from the instructions; the delimiters lesson shows how.

flowchart TB
  R["Role"] --> P["Final prompt"]
  T["Task"] --> P
  C["Context"] --> P
  K["Constraints"] --> P
  E["Examples"] --> P
  F["Output format"] --> P
  D["Input data"] --> P

A prompt is assembled from parts. Missing parts are not neutral: the model fills them with its own guesses.

A worked example

Here is a prompt with every part labelled in comments (the comments are for you; you would not send them).

TEXT
# Role
You are a support-team editor who writes for non-technical customers.

# Task
Rewrite the internal note below as a message to the customer.

# Context
The customer's invoice was charged twice because of a system error.
The duplicate charge has been refunded.

# Constraints
- Maximum 70 words
- Apologize once, do not blame the customer
- No internal ticket numbers

# Output format
Plain text, no greeting line, no signature.

# Input data
Internal note: "dup charge due to retry bug, refunded, ticket 88213"

Read it as the model would. Is anything ambiguous? Is there a decision left to chance? Each answered question is one fewer guess.

Build prompts from parts in code

In real software you rarely type one giant string. You keep the parts separate, so you can reuse, test and swap them. The builder below skips any part you do not provide.

Python A reusable prompt builder

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Try removing constraints or role and run again. Notice how the shape of the prompt changes. Each part is optional, but dropping one is a decision, not an accident.

A simple prompt linter

Because prompts are strings, you can check them with code. This stand-in linter uses keyword checks to flag missing parts. Real prompt quality needs human judgment and testing (chapter 6), but a cheap check catches silly omissions in a team's templates.

Python Linting a prompt for missing parts (heuristic)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A heuristic like this will have false positives and false negatives. Treat it as a smoke alarm, not a judge.

Order and placement

Where you put things matters a little:

  • Put the task or most important instruction early, and repeat a critical constraint at the end for long prompts.
  • Put long input data after the instructions, clearly fenced off, and put the question about it at the end.
  • Keep examples together and consistent in format.
  • If the model supports separate system and user messages, put stable rules in the system message and the changing request in the user message. Roles and system prompts covers this.

Do not over-stuff

More parts is not always better. A short, clear prompt for a simple task beats a ten-paragraph one that buries the request. Include a part when its absence would change the answer in a way you care about.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which part of a prompt tells the model what limits apply, such as length?

    Constraints state limits and rules such as word counts, tone restrictions and things to avoid.
  2. 2. What happens when you leave out part of a prompt?

    Missing parts are not neutral. The model picks something plausible, which may not be what you wanted.
  3. 3. Why build prompts from separate parts in code?

    Keeping role, constraints, examples and data separate makes prompts easier to maintain and test.
  4. 4. Where should long input data go relative to the instructions?

    Fencing the data off after the instructions avoids confusing data with commands.
  5. 5. Does a prompt always need all six parts?

    Simple tasks need simple prompts. Add parts for the decisions you do not want left to chance.

Summary

  • Prompts are assembled from role, task, context, constraints, examples, output format and input data.
  • A missing part is filled by the model's guess, so omit parts on purpose.
  • Keep parts separate in code so they can be reused and tested; cheap linters catch obvious omissions.
  • Put stable rules early (or in a system message) and fence off long input data.

Next: Writing Clear, Specific Instructions