XML tags are a powerful tool for structuring prompts and guiding Claude’s responses. Claude is particularly familiar with prompts that have XML tags as Claude was exposed to such prompts during training. By wrapping key parts of your prompt (such as instructions, examples, or input data) in XML tags, you can help Claude better understand the context and generate more accurate outputs. This technique is especially useful when working with complex prompts or variable inputs.

Looking for more advanced techniques? Check out long context window tips to learn how XML tags can help you make the most of Claude’s extended context capabilities.

What are XML tags?

XML tags are angle-bracket tags like <tag></tag>. They come in pairs and consist of an opening tag, such as <tag>, and a closing tag marked by a /, such as </tag>. XML tags are used to wrap around content, like this: <tag>content</tag>.

Opening and closing XML tags should share exactly the same name. The tag name can be anything you like, as long as it’s wrapped in angle brackets, although we recommend naming your tags something contextually relevant to the content it’s wrapped around.

XML tags should always be referred to in pairs and never as just as the first half of a set (e.g., Using the document in <doc></doc> tags, answer this question. ).

XML tag names

There is no canonical best set of XML tag names that Claude performs particularly well with. For example, <doc> works just as well as <document>. The only time you need very specific XML tag names is in the case of function calling.

Why use XML tags?

There are several reasons why you might want to incorporate XML tags into your prompts:

  1. Improved accuracy: XML tags help Claude distinguish between different parts of your prompt, such as instructions, examples, and input data. This can lead to more precise parsing of your prompt and thus more relevant and accurate responses, particularly in domains like mathematics or code generation.
  2. Clearer structure: Just as headings and sections make documents easier for humans to follow, XML tags help Claude understand the hierarchy and relationships within your prompt.
  3. Easier post-processing: You can also ask Claude to use XML tags in its responses, making it simpler to extract key information programmatically.

How to use XML tags

You can use XML tags to structure and delineate parts of your prompt from one another, such as separating instructions from content, or examples from instructions.

UserPlease analyze this document and write a detailed summmary memo according to the instructions below, following the format given in the example:




Handling variable inputs

When working with prompt templates that include variable inputs, use XML tags to indicate where the variable content should be inserted, such as in the following example:

UserI will tell you the name of an animal. Please respond with the noise that animal makes.

As a general rule, you should ways separate your variable inputs from the rest of your prompt using XML tags. This makes it clear to Claude where the examples or data begin and end, leading to more accurate responses.

Requesting structured output

You can ask Claude to use XML tags in its responses to make the output easier to parse and process:

UserPlease extract the key details from the following email and return them in XML tags:

- Sender name in <sender></sender> tags
- Main topic in <topic></topic> tags
- Any deadlines or dates mentioned in <deadline></deadline> tags

From: John Smith
To: Jane Doe
Subject: Project X Update

Hi Jane,

I wanted to give you a quick update on Project X. We’ve made good progress this week and are on track to meet the initial milestones. However, we may need some additional resources to complete the final phase by the August 15th deadline.

Can we schedule a meeting next week to discuss the budget and timeline in more detail?


Claude’s response:

Assistant (Claude’s response)<sender>John Smith</sender>
<topic>Project X Update</topic>
<deadline>August 15th</deadline>

XML tags make it easier to retrieve targeted details from Claude’s response by allowing for programmatic extraction of content between specific tags.

When calling Claude via the API, you can pass closing XML tags (e.g., </json>) to the stop_sequences parameter to have Claude stop generating once it reaches the desired endpoint. This can save both money and time by eliminating any concluding remarks after the core response. The same is true of skipping Claude’s friendly preamble by prefilling Claude’s response with an opening XML tag.

XML best practices

To get the most out of XML tags, keep these tips in mind:

  • Use descriptive tag names that reflect the content they contain (e.g., <instructions>, <example>, <input>).
  • Be consistent with your tag names throughout your prompts.
  • Always include both the opening (<tag>) and closing (</tag>) tags, including when you reference them, such as Using the document in <doc></doc> tags, answer this question.
  • You can and should nest XML tags, although more than five layers of nesting may decrease performance depending on the complexity of the use case.

Additional resources

  • Prompt engineering techniques: Explore other strategies for optimizing your prompts and enhancing Claude’s performance.
  • Anthropic cookbook: Browse a collection of Jupyter notebooks featuring copy-able code snippets that demonstrate highly effective and advanced techniques, integrations, and implementations using Claude.
  • Prompt library: Get inspired by a curated selection of prompts for various tasks and use cases.