Where to Put Long Documents in a Prompt: Documents First, Question Last
For prompts that include long documents, a good default is to place the documents first and your instruction or question after them, at the end. Anthropic's long-context guidance recommends this ordering, and it reflects how models tend to behave: the text closest to where generation begins often has the strongest influence. Test it on your own task, but start here.
The default layout
- Role or high-level setup (optional, short).
- The long documents, each wrapped in tags with an index and a source.
- The question or task, stated clearly.
- Output format instructions.
If you also use a system prompt, keep the stable role and rules there, and put the documents in the user message. That keeps the system prompt short and cacheable, while the documents change from request to request.
Why the question goes last
When the model reads a very long input and then meets the question, it can attend to the relevant passages with the question fresh in mind. If the question comes first, followed by 50,000 tokens of text, the question can feel distant by the time the model starts answering. Many teams report better results from the ordering above, particularly with several documents and a complex query. The effect varies by model and task, so it is worth checking with a small test, not just assuming.
Several documents
With multiple documents, give each a stable index and source so the model can refer to them. Put the most important or authoritative documents carefully; do not assume the middle of a very long list gets the same attention as the start and end.
A reusable builder
This function assembles a prompt from a list of documents and a question. It escapes angle brackets in document text so a stray tag cannot break the structure.
import html as htmllib
def build_prompt(documents: list[dict], question: str, output_format: str = "") -> str:
parts = ["<documents>"]
for i, doc in enumerate(documents, 1):
body = htmllib.escape(doc["text"], quote=False)
parts.append(
f'<document index="{i}">\n'
f"<source>{htmllib.escape(doc['source'], quote=False)}</source>\n"
f"<content>\n{body}\n</content>\n"
f"</document>"
)
parts.append("</documents>")
parts.append("")
parts.append(f"<question>\n{question}\n</question>")
if output_format:
parts.append(f"<output_format>\n{output_format}\n</output_format>")
return "\n".join(parts)
docs = [
{"source": "handbook.md", "text": "Employees accrue 1.5 vacation days per month."},
{"source": "faq.md", "text": "Unused vacation days carry over for 12 months."},
]
prompt = build_prompt(
docs,
"How many vacation days do I get in a year, and what happens to unused days?",
"Answer in two sentences and cite the source file for each fact.",
)
print(prompt)
Running this prints the documents in tags followed by the question and the format instructions. Send the string as the user message. Expect an answer such as 18 days a year (12 months at 1.5 days) with carry-over for 12 months, citing the two files.
Ask for quotes on very long inputs
For long inputs, it helps to ask the model first to pull out the passages relevant to the question and then answer from them. This focuses attention and also gives you evidence you can check. Post 30 covers this technique in depth.
When not to include everything
Placement fixes only part of the problem. If a 200-page manual is in the prompt and only two pages matter, you are still paying for and diluting attention across the other 198. Consider retrieval to select pages (the RAG track covers the pipeline) or the just-in-time approach in post 39. Long-context prompting is best when the task really needs most of the material, such as summarising a whole report or comparing two contracts.
Be careful with caching
If the same documents are reused across many requests, put them in a stable position at the start of the message and mark the end of the document block as a cache breakpoint. The question that changes each time then goes after the cached part. Post 44 explains how to structure prompts for cache hits.
Checklist
- Documents wrapped in tags, with index and source.
- Question and task after the documents.
- Stable material before volatile material.
- A quick test comparing your ordering against the alternative on ten real questions.
Comments
No comments yet. Have a question or a tip? Be the first to share it.