Skip to content
AgenticAIWiki
  1. Learn
  2. Part 11
  3. 59. Budgeting the context window
  4. 59.3

Lesson 59.3 · 3 of 3 · 4 min read · Reviewed 24 Nov 2026

Lost in the middle: where to put key facts

By the end, you can

  • Place key information where the model uses it best.
  • Structure long prompts: documents first, question last.

Research on long contexts found a “lost in the middleLost in the middle: The tendency of models to use information at the start and end of a long context better than information in the middle.” effect: models use facts at the beginning and end of a long input more reliably than facts in the middle. Newer models have improved a lot, but placement still matters.

Put the question last

Anthropic’s long-context tips recommend placing long documents near the top and the query and instructions at the end. That ordering noticeably improves answers on long inputs.

<documents>
  <document><source>contract.pdf</source><content>…</content></document>
  <document><source>amendment.pdf</source><content>…</content></document>
</documents>

Using the documents above, list every termination clause with its notice period.

Ask for quotes first

For long-document tasks, ask the model to pull out relevant quotes in <quotes> tags, then answer from the quotes. That focuses attentionAttention: The mechanism that lets each token weigh every other token in the context when building its representation. and makes the answer easy to check.

Pin critical facts

In long agent runs, key constraints set early, such as “never contact customer X”, can fade. Keep them in the system promptSystem prompt: Instructions passed in the top-level system field that set the model’s role, rules and context for the whole conversation. or a pinned notes section near the latest turn.

Key points

  • Models tend to use information at the start and end of a long context better than information buried in the middle.
  • For long inputs, put documents near the top and your question and instructions at the end.
  • Repeat or pin critical facts near the end, and ask the model to quote relevant passages first.
Hands-on labRuns in the Playground

Lay out a long-context prompt

Build a long-context prompt with documents first in tagged blocks, a quotes-first instruction, pinned constraints and the question at the end, then check the layout in code.

You will have: A build_prompt function and a layout checker that reports where the question and constraints sit.

Steps

  1. Read the three DOCS and the QUESTION and CONSTRAINTS in the starter.
  2. Wrap each document in <document> tags with its <source> and <content>, inside one <documents> block.
  3. After the documents, add the pinned constraints, an instruction to quote relevant passages in <quotes> tags first, then the question last.
  4. Write check_layout(prompt) that reports whether the question is in the last 10% and the documents start in the first 10%.
  5. Run the checker on your prompt and on a bad layout with the question first.

Done when

  • Your prompt passes both layout checks.
  • The bad layout fails the question-at-end check.
  • Each document's source appears inside its tags.

Open in the Playground No API key needed.

Starter code (Python)
DOCS = [("contract.pdf", "Clause " * 900), ("amendment.pdf", "Amended clause " * 600), ("letter.pdf", "Notice " * 400)]
QUESTION = "List every termination clause with its notice period."
CONSTRAINTS = "Ignore clauses marked superseded."

def build_prompt(docs, question, constraints):
    # TODO: documents first in tags, then constraints, a quotes-first instruction, and the question last
    return question + "\n" + "\n".join(c for _, c in docs)

def check_layout(prompt):
    # TODO: return {"question at end": ..., "documents first": ...}
    return {}

good = build_prompt(DOCS, QUESTION, CONSTRAINTS)
print("layout:", check_layout(good))
Show a solution
DOCS = [("contract.pdf", "Clause " * 900), ("amendment.pdf", "Amended clause " * 600), ("letter.pdf", "Notice " * 400)]
QUESTION = "List every termination clause with its notice period."
CONSTRAINTS = "Ignore clauses marked superseded."

def build_prompt(docs, question, constraints):
    blocks = ["<document><source>%s</source><content>%s</content></document>" % (s, c) for s, c in docs]
    return "\n".join(["<documents>"] + blocks + ["</documents>",
        "Constraint: " + constraints,
        "First quote the relevant passages in <quotes> tags, then answer from the quotes.",
        question])

def check_layout(prompt):
    n = len(prompt)
    return {"question at end": prompt.rfind(QUESTION) >= 0.9 * n,
            "documents first": 0 <= prompt.find("<documents>") <= 0.1 * n}

good = build_prompt(DOCS, QUESTION, CONSTRAINTS)
bad = QUESTION + "\n" + "\n".join(c for _, c in DOCS)
print("good layout:", check_layout(good))
print("bad layout:", check_layout(bad))
print("sources tagged:", all("<source>%s</source>" % s in good for s, _ in DOCS))
Quick checkYou send 80 pages of documents and a question. Where should the question go?
How sure are you?
Pick an answer and how sure you are

Finished the chapter? Take the chapter check.