GenAIWiki
beginner

Claude XML Tags for Long-Context Prompts

How to use Claude XML tags to structure instructions, examples, documents, sources, and user input in long-context prompts.
ClaudeXML tagsprompt engineeringlong contextfew-shot prompting

9 min read

Updated todayVerified recentlyInformation score 92

Key insights

Concrete technical or product signals.

  • Claude tags are prompt delimiters, not magic syntax.
  • They are most useful when prompts combine documents, examples, instructions, and variable input.
  • Tags help search queries like 'Claude tag' resolve to practical prompt-structure guidance.

Use cases

Where this shines in production.

  • Structuring multi-document legal, research, or support prompts.
  • Separating examples from live user input.
  • Improving maintainability of reusable Claude prompt templates.

Limitations & trade-offs

What to watch for.

  • XML tags do not replace schema validation for application outputs.
  • Tags do not neutralize prompt injection inside untrusted content.

Claude XML Tags for Long-Context Prompts

Claude XML tags are a prompt-structure convention, not a separate API feature. Anthropic recommends them because they make complex prompts easier for Claude to parse when instructions, examples, documents, tool rules, and user input are mixed together.

Use descriptive tags

Prefer tags that describe the role of the content:

<instructions>
Summarize the documents using only cited evidence.
</instructions>

<documents>
  <document index="1">
    <source>pricing-policy.md</source>
    <document_content>...</document_content>
  </document>
</documents>

<user_question>
Which plan should we recommend?
</user_question>

Put long documents before the final question

For long-context work, place the large documents near the top, then put instructions and the specific query after the evidence. Use document-level tags and source metadata so citations and comparisons are easier to produce.

Tag examples explicitly

When using few-shot prompting, wrap the full example set in <examples> and each example in <example>. This helps Claude distinguish examples from the live user input.

Do not treat tags as a security boundary

Tags reduce ambiguity, but they do not make untrusted content safe. A malicious webpage inside <document_content> can still contain instructions. Pair tags with retrieval trust labels, tool permissions, and approval gates.

Sources