Prompt Elements
Anatomy of a prompt
The Problem: You ask the AI "Write something good" and get a generic response. But when you ask precisely, you get exactly what you need. What's the difference?
The Solution: Think Like a Recipe
A good prompt is like a recipe. If you just say "make something tasty", the chef won't know what you want. But a recipe has clear structure: ingredients, portions, steps, and the expected result. A prompt works the same way — it is built from a handful of reusable parts, and each one nudges the model toward the answer you actually had in mind.
The Building Blocks of a Prompt
The role tells the model whose voice to use: "You are a senior security engineer" pushes it toward careful, domain-specific phrasing instead of a generic reply. The context supplies the background it cannot guess — your audience, the codebase, the goal — so the answer fits your real situation. The instruction is the single most important part: it states the concrete action to perform ("summarize", "rewrite", "classify"). Vague verbs produce vague output, so make the task explicit and unambiguous.
Three more parts sharpen the result. Examples (the few-shot technique) show the model a pattern to copy: one or two input→output pairs are often more powerful than a paragraph of description. The input data is the actual material to work on — the email to reply to, the text to translate — and is best separated from your instructions with clear delimiters like triple quotes or XML-style tags so the model never confuses data with commands. Finally, the output format and constraints pin down the shape and limits of the answer: "Return valid JSON with keys title and tags, under 50 words, no markdown." Constraints prevent rambling and make the response easy to parse downstream.
Compare a thin prompt with a structured one. Before: "Write about our product." — you get a bland, generic blurb. After: "You are a B2B copywriter. We sell time-tracking software to small agencies. Write a 40-word LinkedIn post highlighting the weekly-report feature; friendly tone, one emoji, end with a question." — now the output is on-brand, the right length, and ready to publish. You rarely need all six elements at once: a quick factual question needs just the instruction, while a complex generation task benefits from role, context, format, and examples together. The role in particular is often set once via a system prompt, which defines the model's behavior before the user's message even arrives.
Think of it like a recipe with key ingredients:
- 1. Role: who should the AI be? "You are an experienced Python developer"
- 2. Context: what's the background? "I'm building a REST API for an e-commerce site"
- 3. Task: what exactly should be done? "Write a function to validate email addresses"
- 4. Format: how should the output look? "Return as a Python function with docstring and type hints"
- 5. Constraints: what limitations are there? "No external libraries, Python 3.9+"
- 6. Examples: show what you want. "For input 'test@email.com' return True"
Pro Tip: The order matters! Put the most important info at the beginning and end of your prompt — models pay more attention to those parts. This is called the "primacy and recency effect" in cognitive science.
Where Is This Used?
Fun Fact: The key ingredients of a prompt are like building blocks — Role, Context, Task, Format, Constraints, and Examples. Mix and match them based on your needs!
Try It Yourself!
Click on prompt elements to toggle them on or off. Notice how adding each element makes the output more specific!
Toggle prompt elements on and off — see how it affects the model response quality. Try different combinations!
What the model should do. A clear task or command.
Additional information that helps the model understand the task better.
The data that the model should process.
A hint about the expected response format.
You are a professional translator. Preserve the style and tone of the original.
Translate the following text to Spanish.
Text: "Today is a beautiful day for a walk in the park."
Translation:
Build a prompt and click "Generate"
Context → Instruction → Input → Output Indicator. This order helps the model understand the task better.
Simple queries may contain only an instruction. Add other elements as the task becomes more complex.
Clear separation of prompt elements helps the model understand what is expected. Instruction answers "what to do", context — "how to do it", input — "what to work with", and output indicator guides the response format.
Frequently asked questions
What makes a good prompt?
A good prompt is built from a few reusable parts: role, context, instruction, examples (few-shot), input data, and an output format with constraints. You rarely need all of them at once — a quick factual question needs just the instruction, while a complex task benefits from role, context, format, and examples together.
Why does a prompt need a role?
The role tells the model whose voice to use: "You are a senior security engineer" pushes it toward careful, domain-specific phrasing instead of a generic reply. This immediately improves the relevance and tone of the answer. The role is often set once via a system prompt, which defines the model's behavior before the user's message arrives.
What is the difference between the instruction and the context in a prompt?
The instruction names the concrete action to perform ("summarize", "rewrite", "classify") and is the single most important part of the prompt. The context supplies background the model cannot guess — your audience, goal, and codebase. The instruction says what to do, the context says under what circumstances, and together they produce a precise answer that fits your situation.
How do you specify the output format in a prompt?
State the shape and limits of the answer directly, for example: "Return valid JSON with keys title and tags, under 50 words, no markdown." These constraints stop the model from rambling and make the response easy to parse downstream. Keep the actual input data separated from your instructions with clear delimiters like triple quotes or XML-style tags so the model never confuses data with commands.
Try it yourself
Interactive demo of this technique
Write a function to calculate the factorial of a number
def factorial(n):
if n == 0:
return 1
return n * factorial(n-1)
def factorial(n: int) -> int:
"""Calculate factorial of n.
Args:
n: Non-negative integer
Returns:
Factorial of n
Raises:
ValueError: If n is negative
"""
if n < 0:
raise ValueError("n must be non-negative")
if n == 0 or n == 1:
return 1
return n * factorial(n - 1)
Low temperature (0.1) for code ensures more consistent, documented, and safe results.
Create a free account to solve challenges
4 AI-verified challenges for this lesson
This lesson is part of a structured LLM course.
My Learning Path