Structured Prompts and Long Context

Use XML-style tags, clear sections and good document placement so the model never confuses instructions with data.

Beginner · about 3 hours · difficulty 2/5

Learn first: Clear Instructions and System Prompts

As prompts grow to include documents, examples, tool results and history, structure keeps the model oriented and makes your prompts maintainable.

Techniques

  • Tags as delimiters: wrap each piece of content, for example <document>, <example>, <user_question>. Claude is notably responsive to XML-style tags, and they help other models too. Refer to the tags by name in instructions ("using only the text in the document tags...").
  • Consistent naming: pick tag names and stick with them across prompts.
  • Documents first, question last: for long context, put large documents near the top and the question and instructions at the end.
  • Metadata per document: include a source, title and date inside each document block so the model can cite.
  • Quote extraction: for long documents, ask the model to first pull relevant quotes, then answer from the quotes.

Why this also matters for security

Clear separation between trusted instructions and untrusted data (web pages, emails, user uploads) is the first, though not sufficient, line of defence against prompt injection, covered in depth in the safety section. Tell the model that content inside data tags is information, never instructions.

Good enough to move on

You can build a prompt template that combines multiple documents, examples and a user question with clean tags, and the model answers with correct source references.

Key concepts

  • XML-style tags as delimiters
  • Document placement in long context
  • Per-document metadata
  • Quote-then-answer
  • Separating instructions from untrusted data

Mini project

Write a prompt template that answers questions over three pasted policy documents using tags, forces the model to quote the relevant passages first, and returns an answer with document references.

Check yourself

Why does wrapping an uploaded email in tags help, and why is it still not a complete defence against a malicious email?

Free resources