Appearance
Anatomy of a Prompt
A prompt is not one thing. It's a composition of instructions, context, input data, output format, and examples, each serving a different purpose. Understanding the anatomy lets you debug why a prompt fails and which part to fix. This lesson dissects a prompt into its components and explains how each one affects the model's output.

What you'll learn
- A prompt has five components: system instructions, user message, context data, output format, and examples
- The primacy/recency effect means the model pays most attention to the beginning and end of the prompt
- API-level separation (system vs user) is structural defense against injection, use it, don't concatenate
The problem
You write a prompt that works. Then you add one more instruction and it stops working. You don't know why because you treated the prompt as a blob of text instead of a composition of components with different effects on the model's attention.
The five components
Here is a complete example of a prompt broken down into its five core structural components.
python
# A well-structured prompt with all five components
system = """You are a code reviewer. You find bugs, security issues, and style problems.
Be specific. Reference line numbers. Suggest fixes. # ← Instructions + Role
## Rules
- Never approve code that logs secrets.
- Flag missing type hints.
- Suggest tests for untested logic.""" # ← Constraints
user_message = """Review the following code:
This section injects the user's source code directly into the review context.
```python
{code}
This code block injects the user's raw source code into the prompt.
```""" # ← Input data + context
output_format = """Return your review as JSON:
{
"bugs": [{"line": int, "severity": "high|medium|low", "description": str, "fix": str}],
"style_issues": [...],
"summary": str
}""" # ← Output format
examples = """Example output:
{"bugs": [], "style_issues": [{"line": 3, "severity": "low", "description": "Missing type hint", "fix": "Add -> str"}], "summary": "Minor style issues only"}""" # ← Few-shot exampleThe primacy/recency effect
Models pay disproportionate attention to the beginning and end of the prompt. Instructions at the start and the output format at the end are the most reliably followed. Information in the middle is more likely to be missed or de-prioritized.
python
# Optimal ordering for attention:
# 1. System prompt (role, rules, constraints) , highest priority
# 2. Critical instructions, mentioned early
# 3. Context/data, middle (lower attention)
# 4. Output format, end (high attention from recency)
# 5. Examples, end (reinforce format)Build it: a prompt template
You can use Python's built-in string templating to build reusable prompt structures.
python
from string import Template
CODE_REVIEW_PROMPT = Template("""System: $system
User: Review the following code:
```python
$codeOutput format: $output_format
Example: $example""")
## What goes wrong
| Mistake | How you notice it | The fix |
|---|---|---|
| Instructions buried in the middle | Model ignores or partially follows instructions | Move critical instructions to the system prompt or the beginning of the user message |
| Output format contradicts examples | Model produces mix of formats | Make examples match the output format exactly. Test with and without examples |
| System prompt concatenated with user input | User can override system instructions | Pass system separately via the API. Never concatenate: `system + user_input` |
| Too many components | Model gets confused, output is inconsistent | Simplify. Start with system + user + output format. Add examples only if needed |
## Confirm it worked
Verify your prompt's effectiveness by asserting that the output matches your expected schema.
```python
# Test that the model follows the output format
response = call_llm(system=system, message=user_message.format(code="print('hello')"))
import json
result = json.loads(response)
assert "bugs" in result and "style_issues" in result and "summary" in result