ChatGPT to NotionChatGPT to Notion

What are XML tags?

on 17 days ago

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 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.

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:

As a general rule, you should always 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:

Claude's response: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., ) 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.

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.