Prompt Formatting: Structure and Markup
Briefly
Prompt formatting is a method to structure instructions for LLMs using sections, markdown, and XML tags to enhance the accuracy and stability of responses. Related concepts: prompt, system prompt, prompt template.
Why Formatting is Needed
LLMs are sensitive to the structure of input text. Poorly formatted prompts as a continuous stream of text increase the likelihood of missing instructions and reduce the quality of the response. Formatting addresses three tasks:
- Role separation: system instructions, user requests, and examples do not mix.
- Highlighting key elements: the model can distinguish instructions from data.
- Attention management: important parts of the prompt do not get lost in long context (see context-window).
Key Formatting Techniques
1. Sections and Headings
Use markdown headings for logical blocks:
# Task
Translate the text into Russian.
# Text
Hello, world!
2. XML Tags
XML tags allow for explicit marking of block boundaries. This is especially useful when the prompt is generated programmatically:
<instructions>
Extract names from the text.
</instructions>
<text>
Ivan and Maria came to the meeting.
</text>
3. Numbered Lists
For step-by-step instructions:
Follow in order:
1. Define the topic.
2. Create a plan.
3. Write the text.
4. Tables
For structured data:
| Field | Type | Description |
|-------|------|-------------|
| name | string | Name |
| age | int | Age |
Practical Recommendations
| Technique | When to Use | Example |
|---|---|---|
| Headings | Long prompts with multiple sections | ## Context, ## Question |
| XML Tags | Inserting user data | <user_input>...</user_input> |
| Lists | Step-by-step instructions | 1. ... 2. ... |
| Tables | Structured output | Table with columns |
Formatting Errors
- Role mixing: instructions and data in one paragraph.
- Excessive formatting: too many nested tags, the model gets 'confused'.
- Inconsistency: different structures in various prompts — the model cannot adapt.
Relation to Other Techniques
Formatting is the foundation for prompt engineering. It complements role prompting and prompt templates. For complex tasks, it is useful to use prompt chaining.