Intermediate Technique

XML Tags & Delimiters

Use structured formatting to clearly separate instructions from content

What is XML Tags & Delimiters?

XML tags and delimiters are a structured way to organize your prompts by separating different components into distinct sections. Using tags like <instruction>, <context>, and <input> helps the AI understand exactly what each part of your prompt is supposed to do.

This technique is especially powerful with LLMs trained on code or XML-like structures, as they recognize these patterns as explicit boundaries.

When to Use It

Best For:

  • • Long, complex prompts
  • • Multi-part tasks
  • • Document processing
  • • Code generation tasks

Not Ideal For:

  • • Simple, one-line queries
  • • Quick questions
  • • Casual conversations
  • • Time-sensitive tasks

Examples

Example 1: Document Processing
Prompt
<instruction> Extract the key information from the article below. </instruction> <context> This article discusses climate change impacts on coastal cities. </context> <input> [Article content here...] </input>
Output
Review this function for issues:
def get_user(id): return db.query(id)
Example 2: Code Generation
Prompt
<task> Write a Python function with documentation. </task> <requirements> - Use type hints - Include docstring - Handle edge cases </requirements> <input> Create a function to calculate factorial. </input>
Output
def factorial(n: int) -> int: """Calculate the factorial of a non-negative integer. Args: n: A non-negative integer Returns: The factorial of n Raises: ValueError: If n is negative """ if n < 0: raise ValueError("n must be non-negative") if n == 0 or n == 1: return 1 return n * factorial(n - 1)

Best Practices

  • 01
    Use consistent tag names

    Stick to standard tags like <instruction>, <context>, <input>, <output>

  • 02
    Close all tags

    Always use closing tags (</tag>) to maintain proper structure

  • 03
    Nest logically

    Group related content under parent tags for clarity

  • 04
    Add comments for complex prompts

    Use <!-- comments --> to explain sections to future readers

XML Tags vs Plain Text

While plain text prompts work for simple tasks, XML tags provide clarity for complex prompts. They help the model distinguish between instructions and content, reducing confusion and improving output quality. This becomes especially important when building AI applications or chains.