Prompt Structure
Order matters
The Problem: You write prompts that seem clear to you, but AI gives unexpected results. Is there a better way to structure prompts for consistency?
The Solution: Anatomy of a Good Letter
Prompt structure is about organizing the components of your prompt in a clear, logical way so the model can tell instructions apart from data, examples apart from the actual task, and the question apart from the context. It's like writing a good business letter — header, context, request, closing — where each part serves a single purpose. A well-structured prompt usually opens with a system prompt that sets the role and rules, then supplies background context, then the explicit task, then any input data, and finally the desired output format. Optionally it includes a few few-shot examples to demonstrate the behavior you want.
How it works
A language model reads your entire prompt as one stream of tokens and predicts the continuation. It has no separate "instructions" channel — everything is text. Structure is how you compensate for that: explicit delimiters (Markdown headings like ### Instructions, or XML-style tags like <context>…</context>) create unambiguous boundaries so the model knows where the reference document ends and your question begins. This matters most for tasks that mix instructions with long pasted content, where an unstructured prompt invites the model to treat your data as commands — the root of many prompt injection problems. Position also carries weight: instructions placed at the very start or the very end of a long prompt tend to be followed more reliably than ones buried in the middle, a tendency often called the "lost in the middle" effect.
When to use it and what to watch for
Reach for deliberate structure whenever a prompt grows past a couple of sentences, gets reused as a template, or feeds a downstream system that needs a predictable format. The main tradeoff is verbosity: every delimiter and label consumes context budget, so don't wrap a one-line question in five XML tags. The common pitfall is implicitness — assuming the model shares context you only have in your head. Worked example: instead of "Summarize this and make it short" followed by a wall of text, write ### Task "Summarize the support ticket below in 2 bullet points," then ### Ticket with the pasted text inside a delimited block, then ### Format "Plain bullets, no preamble." The model now knows exactly what to do, what to read, and how to answer — and you can swap the ticket text without rewriting the instructions.
Think of it like a well-structured letter:
- 1. Context/Background: What situation is the AI responding to?
- 2. Instruction/Task: What exactly should the AI do?
- 3. Input Data: What material should it work with?
- 4. Output Format: How should the response be structured?
Common Structure Patterns
- Delimiters: Use ### or XML tags to separate sections
- Placeholders: Mark variable content with brackets
- Order matters: Instructions at start/end get more attention
- Be explicit: Don't assume AI understands implicit context
Fun Fact: Studies show that using XML-style tags (like <context> and <instruction>) can improve task completion by 10-15% compared to plain text because it helps the model parse the prompt structure!
Try It Yourself!
Use the interactive example below to experiment with different prompt structures and see how organization affects AI outputs.
🔀 Drag blocks to change prompt order. Models pay more attention to the beginning and end!
- 📋#1ContextYou are a Python programming assistant
- 🎯#2TaskWrite an array sorting function
- 📝#3Output formatReturn only code without explanations
- 💡#4ExamplesExample: [3,1,2] → [1,2,3]
- ⚠️#5ConstraintsDon't use built-in sort()
You are a Python programming assistant Write an array sorting function Return only code without explanations Example: [3,1,2] → [1,2,3] Don't use built-in sort()
- • Context and role — at the start
- • Main task — right after context
- • Output format — closer to the end
- • Task at the very end
- • Constraints at the start
- • Examples without context
Transformers pay more attention to the start and end of context ("Lost in the Middle" effect). Place important information in these positions for better results.
Optimal Prompt Structure:
📝Template 1: Basic Structure
# Role You are a web development expert # Context I have a React project with TypeScript # Task Create a reusable component to display a product list # Output format Return ready-to-use code with TypeScript types and comments # Constraints Don't use third-party libraries, only React
🎯Template 2: For Complex Tasks
# Role and Expertise You are an experienced Senior Backend Developer. Your expertise: microservices architecture, database optimization, API design. # Task Context User: Startup with growing API load Goal: Optimize performance and reduce latency Constraints: Infrastructure budget is limited # Input Data Current architecture: Node.js monolith + PostgreSQL Load: 1000 requests/sec during peak hours Average response latency: 300ms # Instructions 1. First analyze current bottlenecks 2. Then propose specific optimization solutions 3. Finally estimate approximate costs and implementation timeline # Response Format Answer in the format: - Analysis: [identified issues] - Recommendations: [top 3 solutions with priorities] - Next steps: [2-week action plan] # Examples (few-shot) Input: "API responds slowly under high load" Output: "Analysis: Main issue is lack of caching and N+1 database queries. Recommendations: 1) Implement Redis for caching popular requests 2) Optimize SQL queries using JOIN 3) Add pagination for large lists..."
🔢Element Ordering Rule
# Prompt Element Ordering Rule:
1. System instructions (at the beginning)
→ Role, expertise, communication style
2. Context and examples (in the middle)
→ Background information, few-shot examples
3. User question (at the end!)
→ Specific task — closer to model output
Why? Models better account for information from the start and end of context ("Lost in the Middle" effect).- • Choose the appropriate template based on task complexity
- • Replace examples in [...] with your data
- • For simple questions, the basic template is sufficient
- • For expert tasks, use the complex template with few-shot examples
- • Remember: user question always at the end!
Frequently asked questions
How should I structure a prompt?
Order the parts logically: a system prompt with the role and rules, then background context, then the explicit task, then input data, and finally the desired output format. Separate sections with clear delimiters — Markdown headings (### Task) or XML-style tags (<context>…</context>) — so the model can tell instructions apart from data.
Why use XML tags in a prompt?
XML-style tags like <context> and <instruction> create unambiguous boundaries between parts of a prompt. This is especially useful when instructions are mixed with long pasted text: without clear boundaries the model may treat your data as commands. Explicit markup helps the model parse the structure and follow the task more reliably.
What are the main parts of a good prompt?
Typical blocks are: role/rules (system prompt), context or background, the specific task instruction, the input data to work with, and the output format. Optionally you add a few few-shot examples to demonstrate the behavior you want. Not every prompt needs all blocks — a short question only needs the task and the format.
Why doesn't the model follow my prompt instructions?
Common causes are implicitness (assuming context the model doesn't have) and poor structure. It helps to explicitly separate instructions from data with delimiters and to place key directives at the start or end of the prompt: instructions buried in the middle of a long prompt are followed less reliably due to the 'lost in the middle' effect.
Try it yourself
Interactive demo of this technique
Structuring a prompt — flat text vs. clear markup with delimiters
Wireless headphones — great choice! With Bluetooth 5.3 and noise cancellation they are perfect for music, work, and travel. 30 hours battery life. Price: $79.99.
Dive into Sound — SoundWave Pro Changes the Game
- Active noise cancellation: your music, nothing else
- 30 hours without charging: enough for a whole week of commutes
- Bluetooth 5.3: instant connection to any device
SoundWave Pro is made for those who value quality and freedom. Enjoy your favorite tracks in crystal-clear sound while noise cancellation blocks out the world. 30 hours of battery means you can forget about charging for days. All this for just $79.99.
XML tags and clear sections in the prompt are like markup for the model. It knows exactly where the data, format, and constraints are.
Create a free account to solve challenges
3 AI-verified challenges for this lesson
This lesson is part of a structured LLM course.
My Learning Path