Menu

How to Write AI Prompts: 6 Parts with Examples

A good prompt is a short brief: a role, a task, the context the model cannot guess, the material to work on, the shape of the answer and the limits. Each part fixes a different way an answer goes wrong.

Every prompt below is editable: change it, then open it in ChatGPT, Claude or another AI app.

A prompt is a brief. The model reads only what you wrote, so every fact it needs and every preference you have must be on the page. When an answer comes back generic, the cause is almost always something the prompt left out rather than something it said badly.

Most useful prompts are built from the same six parts. The block below has all six. Switch parts off to see the prompt without them, fill in your own language, then open the result in ChatGPT or Claude and compare.

Explain an error message
Fill in
Parts
You are a patient programming tutor for people in their first month of coding.
Explain what this Python error means and how to fix it.
I am following a beginner course. I know variables, strings, lists and for loops. I have not learned classes or exceptions yet.
My code:age = 30 print("Age: " + age)The error:TypeError: can only concatenate str (not "int") to str
Answer in three short parts with bold labels: what the error means, why my code caused it, and the fixed code.
Use plain words and no concept I have not learned yet. Keep the whole answer under 120 words.
Try it
Example replyReplies vary between models and runs.

What it means: + can join two strings, or add two numbers, but it cannot join a string to a number. Python does not guess which one you meant, so it stops.

Why your code caused it: "Age: " is a string and age holds the number 30. The line asks Python to glue them together with +.

The fixed code: turn the number into text first with str():

age = 30
print("Age: " + str(age))

This prints Age: 30.

The six parts

Role tells the model whose voice and judgement to use. "A patient tutor for beginners" and "a senior engineer reviewing production code" give very different answers to the same question. A role is most useful when it implies an audience and a standard; see role prompting for where it helps and where it does not.

Task is the one sentence that says what to do. Lead with a verb: explain, rewrite, compare, list, fix, summarize. If you cannot write the task in one sentence, the prompt probably holds two tasks, and one of them usually gets a thinner answer.

Context is everything the model cannot guess: who you are, what you already know, what the result is for, and what you have already tried. In the example, "I have not learned classes or exceptions yet" is the line that stops the model from answering with try and except.

Input is the material to work on: the code, the email, the table, the error. Paste it whole. A summary of your code is a description of what you think it does, and the bug usually lives in the gap between the two.

Format says what the answer should look like: a list, a table, three labelled parts, JSON, a single line. Without it the model picks a shape for you, and chat models tend to pick long ones. Structured output goes further when a program has to read the answer.

Constraints are the limits: length, vocabulary, things to avoid, things that must stay unchanged. A number ("under 120 words") works better than an adjective ("short"), because "short" means different things to different readers, and the model is one of them.

Vague versus specific

Here is the same request written two ways. Switch between the tabs and compare the replies.

Write a function that sorts a list.
Try it
Example replyReplies vary between models and runs.

Here is a simple sorting function in Python:

def sort_list(items):
    return sorted(items)

You can call it like sort_list([3, 1, 2]), which returns [1, 2, 3].

The vague prompt got a correct answer to a question nobody asked. It had to guess the language, the data, the order and whether mutating the input was acceptable, and it guessed the most common case each time. The specific prompt answered all four in advance, so there was nothing left to guess.

Habits that make prompts better

  • Say the task early, and again after long material. In a short prompt, lead with the task so the background that follows reads with a purpose. When you paste a long document, put the document first and the question at the end, restating the key constraints there, so the instruction sits next to the point where the model starts writing.
  • Say what to do, not only what to avoid. "Write in plain English for a 12 year old" gives the model a target. "Don't be too technical" only rules out one direction and leaves the rest open.
  • Separate instructions from material. When you paste an email or a file, mark where it starts and ends so the model does not mistake a sentence inside it for an instruction. Delimiters and XML tags covers the options.
  • Show one example of the output when the format is unusual. An example shows the shape faster than a paragraph describes it. This is few-shot prompting.
  • Iterate on the prompt, not in the chat. If the first answer is wrong, find the missing context or constraint, fix the prompt and run it again. A long chain of corrections works, but it leaves you with a result and no prompt you can reuse.

A checklist before you send

  1. Can I state the task in one sentence that starts with a verb?
  2. Would a stranger know who the answer is for and what I already know?
  3. Did I paste the real material, not a description of it?
  4. Did I say how long the answer should be and what shape it should take?
  5. Is there anything the answer must not change or must not use?

Five yes answers do not guarantee a perfect reply, but a no is the most likely reason for a bad one.

Frequently Asked Questions

What makes a good AI prompt?

A good prompt states the task in one clear sentence, gives the context the model cannot guess (who you are, what you already know, what the result is for), includes the material to work on, and says what the answer should look like: length, structure and tone. Most weak prompts fail on context and format, not on wording.

How long should a prompt be?

As long as the job needs and no longer. A one-line question is fine for a one-line answer. A task with a specific audience, input and output shape usually takes four to eight sentences. Length that restates the same instruction three times does not help; length that adds missing context does.

Should I be polite to ChatGPT or Claude?

Politeness words neither help nor hurt much. What changes the answer is information: the goal, the audience, the constraints and an example of the output you want. Write the way you would brief a capable colleague who has never seen your project.

Is there a template for writing prompts?

A reliable order is role, task, context, input, format, constraints. Not every prompt needs all six. Start with the task, add context the model cannot guess, then add format and constraints when the first answer comes back too long, too vague or in the wrong shape. See prompt templates for reusable versions with blanks to fill in.

Why does the same prompt give different answers each time?

Chat models pick each next word by sampling from a probability distribution, so two runs can differ. A more specific prompt narrows the space of good answers, which makes runs more alike. The temperature setting controls how much randomness the sampling uses.

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED