DEVSAIO

Use XML-style tags to structure prompts (great with Claude)

Wrapping instructions, documents and examples in tags makes long prompts clearer and answers more reliable.

DEVSAIO team · 1 min read
Share on X

When a prompt includes several things (instructions, a document, examples, user input), models can mix them up. Tags make the structure unambiguous. Anthropic recommends this for Claude, and it helps with other models too.

Example

text
You'll review a contract clause.

<instructions>
- List any obligations for the customer.
- Flag anything unusual compared with a standard SaaS agreement.
- Quote the exact text you're referring to.
</instructions>

<clause>
{clause_text}
</clause>

Put your answer in <obligations> and <flags> tags.

Then extract the parts in code:

python
import re
def tag(text, name):
    m = re.search(rf"<{name}>(.*?)</{name}>", text, re.S)
    return m.group(1).strip() if m else None

Why it helps

  • Separation: the model knows exactly where the document ends and your instructions begin.
  • Safety: user-provided text inside <user_input> tags is less likely to be confused with your instructions.
  • Parsing: asking for output inside tags makes it easy to extract sections.

Tips

  • Tag names don't need to be special; use clear ones like <document>, <example>, <question>.
  • Refer to tags in your instructions: “Using the text in <document>…”.
  • For multiple documents, number them: <doc id="1">.
Try it with one API key

Claude, GPT, Gemini, DeepSeek, image and video models through one OpenAI- and Anthropic-compatible endpoint. Prepaid, pay only for what you use.

Keep reading