D4 · Prompt Engineering › Cluster 4 · Structure with XML Tags

4.5 · Long-Context Structuring

Docs at top · query at bottom · quote first

CONCEPT Three rules for large, data-rich prompts

When you work with large documents or data-rich inputs — roughly 20K+ tokens — the official long-context guidance boils down to three moves that stack on everything in this cluster:

RuleWhat to doWhy
Longform data at the TOPPlace long documents near the top of the prompt, above your query, instructions, and examples.Improves performance across all models; queries at the end can improve response quality by up to ~30% in tests, especially on complex multi-document inputs.
Wrap each doc with metadataPut each document in <document> tags with <document_content> and <source> (and other metadata) subtags.Gives structure Claude can navigate and cite (builds on 4.4 nesting).
Quote first, then answerAsk Claude to pull relevant quotes into <quotes> before doing the task.Helps Claude cut through the noise of the surrounding text and ground its answer.
🔑 Query-at-the-bottom is the headline: long docs on top, your actual question last. It's the opposite of how people often paste ("here's my question… now here's a wall of text").

CONCEPT Grounding responses in quotes

For long-document tasks, instruct Claude to first find and quote the relevant passages — placed in <quotes> tags — and only then produce its answer (e.g. in <info> or <answer> tags). Quoting first forces Claude to locate its evidence in the noise before reasoning over it, which improves accuracy and gives you a traceable link from claim back to source.

DIAGRAM Long-context prompt order

flowchart TD T["TOP of prompt"] --> DOCS["🟢 <documents>
each <document> with
<source> + <document_content>
(the 20K+ token bulk)"] DOCS --> STEP["<instructions>
1) put quotes in <quotes>
2) then answer in <info>"] STEP --> Q["🟢 <query>
the actual question — LAST"] Q --> B["BOTTOM of prompt"] classDef ok fill:#d4edda,stroke:#28a745,color:#155724; classDef step fill:#e7f0ff,stroke:#4a90d9,color:#123; class DOCS,Q ok; class STEP step;

CODE Docs at top, query at bottom, quote-first

Mirrors the official quote-extraction pattern: documents (with sources) first, then instructions that ask for quotes in <quotes> before the diagnostic answer in <info>, with the query at the end.

You are an AI physician's assistant that helps doctors diagnose illnesses.

<documents>
  <document index="1">
    <source>patient_symptoms.txt</source>
    <document_content>
      {{PATIENT_SYMPTOMS}}
    </document_content>
  </document>
  <document index="2">
    <source>patient_records.txt</source>
    <document_content>
      {{PATIENT_RECORDS}}
    </document_content>
  </document>
</documents>

<instructions>
Find quotes from the patient records that are relevant to the reported symptoms
and place them in <quotes> tags. Then, based on those quotes, list the
information that would help the doctor diagnose, in <info> tags.
</instructions>

<query>
What are the most likely diagnoses to investigate first?
</query>

Assembled in a Messages API call, the token-heavy documents lead the user turn and the query trails it:

import anthropic
client = anthropic.Anthropic()

user_content = (
    docs_block          # <documents>…</documents> — 20K+ tokens, goes FIRST
    + instructions_block
    + query_block       # the question — goes LAST
)
msg = client.messages.create(
    model="claude-opus-4-8",
    max_tokens=2048,
    messages=[{"role": "user", "content": user_content}],
)

TRADE-OFFS Query placement & quote-first

✅ Docs-top / query-bottom / quote-first

  • Up to ~30% better quality on complex multi-doc inputs
  • Quotes give a traceable evidence trail
  • Metadata tags let Claude cite each source

⚠️ Costs to weigh

  • Quote-first adds output tokens and a little latency
  • Very large prompts cost more — pair with prompt caching for the static doc block
  • Order matters most at scale; short prompts see little difference
For repeated queries over the same big corpus, keep the document block stable at the top and cache it — the query at the bottom changes while the cached prefix stays put.

EXAM TRAPS

Trap: Putting the question first and the long document after it. → For long context, documents go at the TOP, the query at the BOTTOM. Query-at-the-end can lift quality up to ~30%.
Trap: "The 20K guidance means never exceed 20K tokens." → No — ~20K+ is the threshold at which this structuring matters, not a limit. It's advice for large inputs.
Trap: Dumping raw documents with no tags or metadata. → Wrap each in <document> with <source> and <document_content> so Claude can navigate and cite them.
Trap: Skipping the quote-first step on a big corpus. → Asking Claude to quote relevant parts first cuts through noise and grounds the answer; it's a core long-context technique.

PRACTICE

Q1 · Where do long documents go, and where does the query go?

Long documents (~20K+ tokens) go near the top of the prompt, above the instructions and examples; the query goes at the bottom. Query-at-the-end can improve response quality by up to ~30% on complex multi-document inputs.

Q2 · What is "quote-first" and why use it?

Ask Claude to first extract the relevant passages into <quotes> tags before answering. It helps Claude cut through the noise of long documents and grounds the final answer in specific evidence.

Q3 · How should each document be wrapped in a long-context prompt?

In <document> tags (inside a <documents> container), each with a <source> and a <document_content> subtag (plus an index/other metadata) — so the structure is navigable and citable.