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. It's like writing a good business letter — header, context, request, closing — each part serves a purpose. The structure typically starts with a system prompt, and can include few-shot examples to guide the model.
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!
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