Lesson 3

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.

Prompt Structure — Order Matters!

🔀 Drag blocks to change prompt order. Models pay more attention to the beginning and end!

Prompt structure:
  • 📋
    Context
    You are a Python programming assistant
    #1
  • 🎯
    Task
    Write an array sorting function
    #2
  • 📝
    Output format
    Return only code without explanations
    #3
  • 💡
    Examples
    Example: [3,1,2] → [1,2,3]
    #4
  • ⚠️
    Constraints
    Don't use built-in sort()
    #5
Final prompt:
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()
Model attention (approximate):
StartMiddleEnd
Good practices
  • Context and role — at the start
  • Main task — right after context
  • Output format — closer to the end
Avoid
  • Task at the very end
  • Constraints at the start
  • Examples without context
Key Insight

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).
💡 How to Use Templates
  • 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

Technique Comparison
Demo Mode
Pre-recorded responses
TaskBeginnerWriting

Structuring a prompt — flat text vs. clear markup with delimiters

Without technique
Without technique
Prompt
Write a product description for an online store. Product: wireless headphones. Price $79.99. Bluetooth 5.3, noise cancellation, 30 hours battery. For the product page need: title, 3 bullet points of benefits, short description under 100 words. Friendly tone.
Response

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.

Tokens:55/38
Time:250ms
Quality:
With Context Structure
With technique
Prompt
<role>You are a copywriter for an electronics online store</role> <product> Name: SoundWave Pro Type: wireless headphones Price: $79.99 Specs: Bluetooth 5.3, active noise cancellation, 30h battery </product> <format> 1. Title (up to 10 words, with emotion) 2. 3 benefit bullet points (each up to 15 words) 3. Description (up to 100 words, friendly tone) </format> <constraints> - Do not mention competitors - Include price in description </constraints>
Response

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 separate role, data, format, and constraints — the model does not get confused
🧠Flat text mixes everything together — the model loses the response structure
Structured prompt = predictable and controllable output
Tokens:95/110
Time:420ms
Quality:
Why this works

XML tags and clear sections in the prompt are like markup for the model. It knows exactly where the data, format, and constraints are.

1 / 2

Lesson Quiz

1 of 3

1.Why is section ordering important in prompt design?

Practice Challenges

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