D5 · Context & Reliability › Cluster 4 · Long Context & Retrieval

4.1 · Long-Context Prompting (Placement)

Docs top, query bottom, quote first

CONCEPT Where you put the documents changes the answer

When a prompt carries large documents or data-rich inputs (~20K+ tokens), placement is a first-class lever, not a formatting detail. Anthropic's long-context guidance is three concrete rules, and the exam rewards knowing all three:

RuleWhat to doWhy it works
Longform data at the topPlace long documents and inputs near the top of the prompt, above the query, instructions, and examples."Improves performance across all models."
Query at the endPut the question after the documents.Can improve response quality by up to 30% in tests, especially with complex, multi-document inputs.
Quote firstAsk Claude to quote the relevant passages before doing the task.Helps Claude "cut through the noise" of the rest of the document.
🔑 The ~30% quality lift is specifically the query-at-the-end effect — long docs on top, the question underneath them. It is not a caching or retrieval trick; it is pure ordering.

CONCEPT Wrap each document in tags with metadata

For multi-document prompts, wrap each document in <document> tags with <document_content> and <source> (and any other metadata) subtags, nested inside a <documents> container with an index attribute per document. Structure gives the model clean boundaries — it can tell where one source ends and the next begins, and it can attribute quotes back to a named <source>.

The metadata subtags (<source>, index numbers, dates) cost almost nothing and pay off in attribution: when you later ask Claude to "quote from patient_records.txt", the tag names give it a handle to cite by.
Don't paste raw concatenated documents with no delimiters. Without tags the model has to guess boundaries, which is exactly where mid-prompt content gets lost (4.2).

DIAGRAM The canonical long-context prompt shape

flowchart TD A["Prompt assembly"] --> T["🟢 TOP: long documents
<documents> … </documents>
each in <document> + <source>"] T --> M["Instructions / few-shot examples"] M --> Q["🔵 BOTTOM: the query
(+ 'quote relevant passages first')"] Q --> R["Response quality
up to +30% vs query-on-top"] classDef ok fill:#d4edda,stroke:#28a745,color:#155724; classDef gate fill:#ffe8cc,stroke:#ff9f40,color:#9a4a0a; classDef bad fill:#f8d7da,stroke:#dc3545,color:#721c24; class T ok; class Q gate; class R ok;

CODE Assembling a long-context prompt

Documents rendered at the top of the user turn, wrapped and tagged; the query and a quote-first instruction go last. Note the cache_control breakpoint on the stable document block so repeat questions over the same corpus read from cache.

# Docs on TOP, query on the BOTTOM — the +30% ordering.
docs = """<documents>
  <document index="1">
    <source>annual_report_2023.pdf</source>
    <document_content>{annual_report}</document_content>
  </document>
  <document index="2">
    <source>competitor_analysis_q2.xlsx</source>
    <document_content>{competitor}</document_content>
  </document>
</documents>""".format(annual_report=rpt, competitor=comp)

resp = client.messages.create(
    model="claude-opus-4-8", max_tokens=4096,
    messages=[{"role": "user", "content": [
        {"type": "text", "text": docs,       # TOP: long data
         "cache_control": {"type": "ephemeral"}},  # reuse across questions
        {"type": "text", "text":            # BOTTOM: query + quote-first
         "First, pull the passages relevant to Q3 strategy into "
         "<quotes> tags. Then recommend focus areas in <plan> tags."},
    ]}],
)

TRADE-OFFS Structured placement vs. dump-and-ask

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

  • Up to +30% quality on complex multi-doc inputs
  • Named sources → attributable quotes
  • Stable prefix caches cleanly for repeat queries

⚠️ Query-on-top, raw concatenation

  • Model reads the question before it has the evidence
  • No boundaries → mid-prompt content gets lost (4.2)
  • Harder to ground or cite specific passages
Placement is free. It changes token order, not token count — so you get the lift without paying more input tokens.

EXAM TRAPS

Trap: "Put the question first so the model knows what to look for." → Backwards. Long data goes on top, the query at the end — query-at-the-end is the setting worth up to 30%.
Trap: Treating the 30% figure as a general prompting boost. → It is specifically the long-context, query-at-the-end effect (~20K+ tokens, complex/multi-document), not a universal number.
Trap: Skipping the quote-first step. → Asking Claude to quote relevant passages first is an explicit best practice; it helps the model cut through noise before it answers.
Trap: Concatenating documents with no tags. → Wrap each in <document> with <document_content> and <source> subtags — boundaries and metadata matter.

PRACTICE

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

Long documents go near the top of the prompt, above the query, instructions, and examples. The query goes at the end. This query-at-the-end ordering can improve response quality by up to 30% on complex, multi-document inputs.

Q2 · What token threshold triggers this "long context" advice?

Roughly 20K+ tokens — "large documents or data-rich inputs." Below that, ordering matters less; at 20K+ the placement rules and the quote-first technique start paying off meaningfully.

Q3 · Why ask Claude to quote relevant passages before answering?

It helps Claude cut through the noise of the rest of the document — the model surfaces the load-bearing evidence first, then reasons over that focused set instead of the whole corpus. Pair it with tagged <source> metadata so quotes are attributable.