Advanced8 min readPrompting Techniques

October 22, 2024

Use XML tags to create clear, structured prompts for complex AI agent systems.

XML Structured Prompts

XML structured prompts use XML-like tags to organize complex instructions, making them clearer for AI models—especially when building agents or handling multi-part tasks.

What Are XML Structured Prompts?

XML structured prompts wrap different parts of your prompt in tags, similar to HTML or XML markup:

<task>Analyze customer feedback</task>
<data>
  [Customer feedback text here]
</data>
<requirements>
  - Identify sentiment
  - Extract key themes
  - Suggest improvements
</requirements>

This structure helps AI models clearly distinguish between different parts of your prompt.

Why Use XML Structure?

Clarity

Tags make it obvious what each section represents.

Complex Tasks

Essential for multi-step or multi-part prompts.

Agent Systems

Critical for AI agents that need structured instructions.

Consistency

Reduces ambiguity in how prompts are interpreted.

Debugging

Easier to identify which part of a prompt isn't working.

Basic Structure

Simple Example

<instruction>
  Summarize the following article
</instruction>

<article>
  [Article text here]
</article>

<format>
  - 3 bullet points
  - 50 words maximum
  - Focus on key takeaways
</format>

Multi-Part Task

<role>Senior Data Analyst</role>

<context>
  We're analyzing Q4 sales data for strategic planning
</context>

<data>
  [Sales data here]
</data>

<tasks>
  <task priority="high">
    Identify top 3 performing products
  </task>
  <task priority="medium">
    Compare with Q3 results
  </task>
  <task priority="low">
    Suggest areas for improvement
  </task>
</tasks>

<output_format>
  Present as an executive summary with charts descriptions
</output_format>

Common Tags and Patterns

Information Tags

<context>Background information</context>
<data>Raw data or content to process</data>
<examples>Sample inputs/outputs</examples>
<constraints>Limitations or requirements</constraints>

Instruction Tags

<role>Who the AI should act as</role>
<task>What to accomplish</task>
<steps>How to approach it</steps>
<goals>Desired outcomes</goals>

Output Tags

<format>How to structure output</format>
<tone>Voice and style to use</tone>
<length>Word/character limits</length>
<requirements>Must-have elements</requirements>

Practical Applications

AI Agent Instructions

<agent_config>
  <name>Customer Support Agent</name>
  <personality>Helpful, patient, professional</personality>
  
  <capabilities>
    <capability>Answer product questions</capability>
    <capability>Troubleshoot issues</capability>
    <capability>Escalate to human when needed</capability>
  </capabilities>
  
  <constraints>
    <constraint>Never make promises about features</constraint>
    <constraint>Always verify customer identity for account changes</constraint>
    <constraint>Escalate refund requests over $500</constraint>
  </constraints>
  
  <knowledge_base>
    [Product documentation here]
  </knowledge_base>
</agent_config>

Content Generation

<content_brief>
  <type>Blog Post</type>
  <topic>AI in Healthcare</topic>
  
  <audience>
    <primary>Healthcare administrators</primary>
    <knowledge_level>Intermediate</knowledge_level>
  </audience>
  
  <structure>
    <section name="introduction" length="150 words"></section>
    <section name="current_state" length="300 words"></section>
    <section name="future_trends" length="300 words"></section>
    <section name="conclusion" length="150 words"></section>
  </structure>
  
  <seo_requirements>
    <keyword>AI healthcare</keyword>
    <keyword>medical AI</keyword>
    <meta_description_length>155</meta_description_length>
  </seo_requirements>
</content_brief>

Data Processing

<data_processing_task>
  <input_data format="json">
    [Raw JSON data]
  </input_data>
  
  <transformations>
    <transform>
      <step>1</step>
      <action>Filter records where status = 'active'</action>
    </transform>
    <transform>
      <step>2</step>
      <action>Group by category</action>
    </transform>
    <transform>
      <step>3</step>
      <action>Calculate average price per category</action>
    </transform>
  </transformations>
  
  <output_format>
    <type>Table</type>
    <columns>Category, Count, Average Price</columns>
    <sort_by>Average Price descending</sort_by>
  </output_format>
</data_processing_task>

Best Practices

1. Use Meaningful Tag Names

Poor: <part1>, <thing>, <stuff> Good: <context>, <requirements>, <data>

2. Keep Hierarchy Simple

Don't over-nest. Aim for 2-3 levels maximum.

Too Complex:

<outer><middle><inner><deepest>Content</deepest></inner></middle></outer>

Better:

<section type="requirements">Content</section>

3. Be Consistent

Use the same tags the same way across prompts.

4. Close All Tags

Always close tags properly:

<tag>Content</tag><tag>Content        ❌

5. Use Attributes for Metadata

<task priority="high" category="analysis">
  Analyze market trends
</task>

Advanced Patterns

Conditional Logic

<workflow>
  <step id="1">
    <action>Check user subscription level</action>
    <if condition="premium">
      <then>Provide detailed analysis</then>
      <else>Provide basic summary</else>
    </if>
  </step>
</workflow>

Templates with Variables

<email_template>
  <subject>{{customer_name}}, here's your {{product_name}} update</subject>
  
  <body>
    <greeting>Hi {{customer_name}},</greeting>
    <content>
      Your {{product_name}} subscription {{status}}
    </content>
    <cta action="{{cta_action}}">{{cta_text}}</cta>
  </body>
</email_template>

<variables>
  <var name="customer_name">John Smith</var>
  <var name="product_name">Premium Plan</var>
  <var name="status">has been renewed</var>
  <var name="cta_action">view_account</var>
  <var name="cta_text">View Your Account</var>
</variables>

Multi-Agent Systems

<agent_system>
  <agent name="researcher">
    <role>Research and gather information</role>
    <outputs_to>analyzer</outputs_to>
  </agent>
  
  <agent name="analyzer">
    <role>Analyze researched information</role>
    <inputs_from>researcher</inputs_from>
    <outputs_to>writer</outputs_to>
  </agent>
  
  <agent name="writer">
    <role>Create final content</role>
    <inputs_from>analyzer</inputs_from>
    <outputs_to>user</outputs_to>
  </agent>
</agent_system>

Real-World Example: Code Review Agent

<code_review_agent>
  <config>
    <language>Python</language>
    <strictness>medium</strictness>
    <focus_areas>
      <area>Security vulnerabilities</area>
      <area>Performance issues</area>
      <area>Code style</area>
      <area>Best practices</area>
    </focus_areas>
  </config>
  
  <code_to_review>
    [Python code here]
  </code_to_review>
  
  <review_criteria>
    <security weight="high">
      Check for SQL injection, XSS, insecure dependencies
    </security>
    <performance weight="medium">
      Identify inefficient loops, unnecessary operations
    </performance>
    <style weight="low">
      Follow PEP 8 guidelines
    </style>
  </review_criteria>
  
  <output_structure>
    <summary>Overall assessment</summary>
    <issues>
      <issue severity="critical|high|medium|low">
        <description>What's wrong</description>
        <location>Line numbers</location>
        <recommendation>How to fix</recommendation>
      </issue>
    </issues>
    <score>Overall code quality score 1-10</score>
  </output_structure>
</code_review_agent>

When to Use XML Prompts

Ideal For:

  • Complex, multi-part tasks
  • AI agent configurations
  • Systems with multiple steps
  • Clear separation of concerns needed
  • Templates and workflows

Not Necessary For:

  • Simple, single-task prompts
  • Quick questions
  • Straightforward requests

Tools and Frameworks

Many AI frameworks support XML-structured prompts:

  • Anthropic Claude: Excellent XML understanding
  • LangChain: Supports structured prompts
  • Custom AI Agents: Essential for complex systems

Tips for Success

  1. Start Simple: Begin with basic tags before complex structures
  2. Test Incrementally: Add structure gradually
  3. Document Your Tags: Keep a reference of what each tag means
  4. Validate Structure: Ensure tags are properly formatted
  5. Iterate: Refine based on results

Conclusion

XML structured prompts bring clarity and organization to complex AI interactions. By wrapping different parts of your prompt in meaningful tags, you create:

  • Clearer instructions
  • Better AI understanding
  • Easier debugging
  • More consistent results
  • Scalable systems

Start using XML structure when your prompts grow complex, and you'll find AI responses become more reliable and aligned with your intentions.

Next Steps:

  • Try adding basic tags to a complex prompt
  • Build a simple agent with XML configuration
  • Create reusable XML templates for common tasks