Monday AI Insight. One practical prompting lesson for RAG and long-document apps: how you package the context is part of the prompt. Retrieved chunks dumped in as a JSON array, or glued together with blank lines, make the model work harder to tell where one source ends, which source said what, and where your instructions start.

The problem: one big blob of context

A common RAG prompt looks like this:

Answer the question using these documents: [{"id": 1, "title": "Refund policy", "content": "..."}, {"id": 2, "title": "Billing FAQ", "content": "..."}, ...] Question: Can I get a refund after 30 days? 

It works in a demo. Then the context grows to dozens of chunks, some chunks contain quotes, code or their own JSON, and answers start mixing sources, citing the wrong document, or treating text inside a document as an instruction.

What the model vendors actually recommend

OpenAI, GPT-4.1 Prompting Guide (Delimiters section). For adding a large number of documents or files to the input context, OpenAI reports from its long context testing:

  • XML performed well, e.g. <doc id='1' title='The Fox'>The quick brown fox jumps over the lazy dog</doc>
  • A pipe format proposed by Lee et al. also performed well, e.g. ID: 1 | TITLE: The Fox | CONTENT: The quick brown fox jumps over the lazy dog
  • JSON performed particularly poorly, e.g. [{'id': 1, 'title': 'The Fox', 'content': 'The quick brown fox jumped over the lazy dog'}]

The same guide adds a caveat worth remembering: if the documents you retrieve contain lots of XML themselves, an XML delimiter will likely be less effective. Pick a delimiter that stands out from your content.

Anthropic, Claude prompting best practices. XML tags help Claude parse prompts that mix instructions, context, examples and variable inputs. Use consistent, descriptive tag names and nest them when content has a hierarchy. For multiple documents, Anthropic's example wraps each one in <document index="n"> with <source> and <document_content> subtags inside a <documents> block.

Where to put the question

The two vendors give slightly different advice on placement, so know which model you're on:

  • Anthropic (long inputs, 20k+ tokens): put the long documents at the top, above your query, instructions and examples. Anthropic says queries at the end can improve response quality by up to 30% in its tests, especially with complex multi-document inputs.
  • OpenAI (GPT-4.1 guide): in long context, place your instructions at both the beginning and the end of the provided context; OpenAI found that worked better than only above or only below. If you only state them once, above the context worked better than below.

A layout that satisfies both: short instructions first, then the tagged documents, then the question and a one-line restatement of the key rules.

The pattern

You answer questions using only the documents below. If they don't contain the answer, say so. <documents> <document index="1"> <source>refund-policy.md</source> <document_content> ... </document_content> </document> <document index="2"> <source>billing-faq.md</source> <document_content> ... </document_content> </document> </documents> First, copy the sentences that answer the question into <quotes> tags with their document index. Then answer in <answer> tags, citing the index for each claim. Question: Can I get a refund after 30 days? 

A small Python helper:

from html import escape def format_docs(chunks): parts = ["<documents>"] for i, c in enumerate(chunks, 1): parts.append(f' <document index="{i}">') parts.append(f" <source>{escape(c['source'])}</source>") parts.append(f" <document_content>\n{c['text']}\n </document_content>") parts.append(" </document>") parts.append("</documents>") return "\n".join(parts) 

Two details in that pattern come straight from Anthropic's guidance:

  • Metadata in tags. The source (file name, URL, date) sits next to the content, so the model can cite it and you can check the citation.
  • Quotes first. For long-document tasks, Anthropic suggests asking the model to quote the relevant parts first, then do the task. This helps it focus on the relevant content and ignore the rest. It also gives you something to verify: if a quote isn't in the document, you've caught a bad answer before your user does.

What tags don't do

Tags are structure, not security. Wrapping a retrieved web page in <document> does not stop instructions hidden inside it from being followed. Treat that as a separate problem (I wrote about it in my prompt-injection posts, linked below). Tags make the boundary clear; your architecture has to enforce it.

Checklist

  1. Replace the JSON array of chunks with one tagged block per document (XML, or the ID | TITLE | CONTENT format).
  2. Put source metadata inside each block so answers can cite it.
  3. If your documents are full of XML or HTML, choose a different delimiter.
  4. Place the question per your model's guide: after the documents for Claude; instructions before and after for GPT-4.1-style guidance.
  5. Ask for quotes first on long-document tasks, and check that each quote exists.
  6. Measure: run your eval set before and after the formatting change. Vendor results are a starting point, not a guarantee on your data.

Takeaway: your model reads the context exactly as you packaged it. Give every document a clear start, end and source, and put the question where your model expects it.

What do you use to separate retrieved docs in your prompts today: JSON, XML, markdown, or just blank lines?

References