learn-prompt-engineering-with-phoebe / Builder session 5 of 10
Learn Prompt Engineering with Phoebe · Builder track · Session 5 of 10

Long context, grounding, and caching

The day the 50-page policy manual lands in your prompt, three things start to matter that never did before: where things go, whether the answer actually comes from the documents, and what all those tokens cost per call. The vendors disagree on the first one - measurably. Let's be honest about it.

🟡 Builder track Practitioners Any model or API 45 min live · build-along inside
0-3 · Welcome 3-20 · Placement, grounding, caching 20-42 · Build-along 42-45 · Q&A
Part 0

The manual arrives

Session 4 gave TriageBot a router: small model for the easy 85%, reasoning model for the hard 15%. Then Meridian's legal team shipped a 50-page refund and returns policy that every triage decision must now respect. Suddenly TriageBot is a long-context system, and everything you learned about short prompts needs a supplement: document placement, grounding the answer in the actual policy, and caching so the manual does not bankrupt the ticket queue.

Live Self-study ★ Build-along - bring a laptop Any model · any API
★ What you walk out with today The three vendors' conflicting placement rules and how to handle the disagreement like an adult (test on your task), the document-tag and quote-grounding patterns that keep long-doc answers honest, the context-reliance dial (docs-only vs blend), the static-first layout that makes prompts cache-friendly on every vendor, and TriageBot v3: policy-aware, quote-grounded, cache-shaped, and unwilling to improvise where the policy is silent.
Part 1 · the placement wars

Where things go: three vendors, three answers 6 min live

Ask the three big vendors where instructions and documents belong in a long prompt and you get three different official answers. Claude's docs: put long documents at the top and the query AFTER the docs - worth up to +30% response quality on long-document tasks. The GPT-4.1 guide: put instructions at BOTH the beginning and the end of long context. Gemini 3: data first, question LAST. These are not rumors; they are each vendor's published guidance.

Claude · query after docs Long documents The 50-page manual, tagged, at the top. Query + instructions After the docs. Up to +30% on long-document tasks. GPT-4.1 · both ends Instructions Once at the top... Long documents The same manual, in the middle. Instructions again ...repeated at the bottom. Gemini 3 · question last Data first All documents and data before anything else. Question LAST The ask comes at the very end, after all the data. Dark = documents, amber = instructions/query. Three official layouts. Placement is measurable: A/B it on YOUR task.
🔍 Click to zoom - the placement wars: three vendor-official layouts for the same long prompt
LiveHow to hold three contradicting truths4 min▶

The disagreement is not sloppiness - it reflects how each model family was trained to attend over long sequences. The honest takeaways:

  • Placement matters. Claude's +30% figure alone should end any "it doesn't matter where things go" argument. Position is one of the cheapest quality levers you have on long prompts.
  • Know your vendor's guidance and start there. Claude: docs top, query after. GPT-4.1: instructions at both ends. Gemini 3: data first, question last. Defaults first, opinions later.
  • Then test on YOUR task. Vendor guidance is a prior, not a law. A 20-minute A/B with your real documents beats any blog post, including this one. Sessions 8-9 make that A/B a repeatable eval.
Real world

The cross-vendor migration trap. Meridian's team drafted TriageBot v3 for Claude (manual at top, query after), then trialed the same string on a GPT model and quality dipped. Nothing was "broken" - the layout was tuned for one vendor's attention pattern. Multi-vendor systems need per-vendor layout variants, not one blessed string.

Part 2 · grounding

Making the answer come from the documents 8 min live

A long document in the prompt does not guarantee the answer comes from it. Models happily blend the document with their training data and their imagination. Grounding is the set of patterns that force the answer back onto the page - and it is mostly three moves.

LiveMove 1: document tags with metadata3 min▶

Wrap each document in tags that carry its identity, so instructions can refer to documents by name and the model can cite its source:

The document-tag pattern (Anthropic idiom, works everywhere)<documents> <document index="1"> <source>meridian-refund-returns-policy-v4.pdf (2026-06, 50 pages)</source> <document_contents> {{POLICY_MANUAL}} </document_contents> </document> </documents>

The metadata is not decoration. "Quote the source you used" only works if the source has a name, and multi-document prompts collapse into soup without per-document boundaries. This is the same data/instruction separation from session 2, scaled up.

LiveMove 2: extract quotes first, then answer3 min▶

The single most effective anti-hallucination move on long documents (tutorial Ch8, and the consolidated best-practices docs): make the model extract the relevant quotes before it answers. Two steps in one prompt: "First, pull the exact lines from the policy that bear on this ticket into quote tags. Then answer using only those lines."

  • The quotes act as a checkable intermediate: if the quote step comes back empty, the honest answer is "the document does not cover this" - and the model is far more likely to say so.
  • You get an audit trail for free. When TriageBot approves a refund, the quoted policy line is right there in the output.
  • It composes with everything else: schemas, routing, reasoning models. Quote-grounding is a layer, not a fork.
Self-studyMove 3: tune context reliance2 min read▶

The GPT-4.1 guide makes explicit a dial most people never set: how much the model should rely on the provided context vs its own internal knowledge.

ModeInstruction shapeWhen
Docs only"Answer ONLY from the provided documents. If they do not contain the answer, say so."Policy, legal, compliance, anything auditable - TriageBot v3 lives here
Blend"Use the documents first; supplement with general knowledge and flag which is which."Research assistants, drafting, exploratory work

Leaving the dial unset means the model picks per answer, silently. Set it in writing, every long-context prompt.

Self-studyPaste, RAG, or "grounding"? Plus two pointers3 min read▶
ApproachWhat it isWhen it fits
Just paste itWhole document in the prompt, patterns from this sessionCorpus fits in context and is stable - a 50-page manual qualifies easily
RAGRetrieve relevant chunks per query, inject only thoseCorpus too big for context, or per-query freshness needed
Vendor "grounding"Google's term for retrieval-backed answers (e.g. grounding with Search); RAG by another nameYou want the vendor's managed retrieval instead of building your own

Two pointers to file away. First, when you do build RAG, the eval vocabulary is the RAG Triad: context relevance (did we retrieve the right chunks), groundedness (does the answer stick to them), answer relevance (does it answer the question). Sessions 8-9 put numbers on all three. Second, prompt compression - trimming long context algorithmically to save tokens - exists, and the honest read is that it is marginal: a dedicated course spends 17 minutes on it. Know the term, reach for caching and retrieval first.

Part 3 · caching

Structure for speed and cost 6 min live

TriageBot v3 will send the same 50-page manual with every single ticket. Unoptimized, that is the same tokens billed thousands of times a day. Prompt caching fixes it - but only if the prompt is shaped for it, and the shape rule is the one thing every vendor agrees on: static content first, variable content last.

Static · identical every call · cached after the first request Variable · last System instructions Role, categories, rules, schema. Policy manual 50 pages in document tags. The big, boring, unchanging payload. Examples Few-shot exemplars, fixed across calls. The ticket Changes every call. Everything before it stays byte-identical. Cached prefix Anthropic: cache breakpoints · OpenAI: automatic static-prefix caching. Same shape wins on both. One early variable token breaks the prefix. Keep everything that changes at the very end.
🔍 Click to zoom - the cache-shaped prompt: static prefix first, the variable ticket dead last
LiveThe shape rule, and why v1 already had it4 min▶

Caching works on prefixes: the provider reuses the processed form of the leading tokens if they are byte-identical to a previous call. Anthropic prompt caching lets you mark cache breakpoints explicitly; OpenAI caches static prefixes automatically and its prompt-engineering guide tells you to put static content first for exactly this reason. Either way the design rule is identical: static content first, variable content last - a timestamp or ticket ID at the top of the prompt silently kills the whole cache.

Here is the satisfying part: TriageBot v1 was already cache-shaped. Back in session 2 we put the fixed instructions first and the ticket last purely for clarity - data/instruction separation. That same layout is what caching rewards. Good structure keeps paying dividends you did not plan for.

When NOT to bother Caching earns nothing on short prompts (the prefix is too small to matter), one-off calls (nothing reuses the cache before it expires), or prompts you are actively rewriting every hour (each edit is a cache miss). Optimize the stable, high-volume paths - for Meridian that is exactly one prompt: TriageBot v3 with its 50-page prefix, hit thousands of times a day.
The build-along

TriageBot v3: the policy manual arrives ★ 22 min · laptops open

Meridian ships the 50-page refund and returns policy, and every triage decision must now respect it. We rebuild TriageBot with everything from today: document tags, vendor-aware placement, quote-grounding, a context-reliance rule with a POLICY_GAP escape hatch, and a cache-shaped layout.

Wrap the policy in document tags. Source name, version, date, page count in the metadata - v4 of the policy will ship eventually, and you will want to know which version each decision quoted.

Place it per your vendor's guidance. On Claude: manual at the top, instructions and ticket after (the skeleton below). On GPT-4.1: repeat the key instructions above AND below the manual. On Gemini: data first, question last. Note in a comment where you would A/B - placement is the first eval you will run in session 8.

Add quote-grounding. "Before answering, extract the exact policy lines that apply into policy_quotes tags, then decide." The quotes become your audit trail and your hallucination brake in one move.

Add the context-reliance rule. Docs-only mode, with an escape hatch: "If the policy does not cover this case, output POLICY_GAP - do not improvise." Every POLICY_GAP is routed to a human and logged; Meridian's policy team gets a weekly list of holes in their own manual. Everyone wins.

Keep it cache-shaped. Instructions and manual and examples first, byte-identical on every call; the ticket dead last. Mark the cache breakpoint if your vendor uses explicit ones.

Test both sides of the boundary. Run one ticket the policy clearly covers (expect a decision with quoted lines) and one it does not (expect POLICY_GAP, not a confident improvisation). The second test is the one that matters - it is the difference between a grounded system and a fluent one.

TriageBot v3 skeleton - grounded, gap-aware, cache-shaped (Claude layout)You are TriageBot, the support-ticket triage system for Meridian Retail. <documents> <document index="1"> <source>meridian-refund-returns-policy-v4.pdf (2026-06, 50 pages)</source> <document_contents> {{POLICY_MANUAL}} </document_contents> </document> </documents> Categories: BILLING, SHIPPING, RETURNS, PRODUCT, ACCOUNT, OTHER. Instructions: 1. Before answering, extract the exact policy lines that apply to this ticket into <policy_quotes> tags. If no lines apply, leave the tags empty. 2. Then triage: category, priority, and whether any requested refund is allowed under the quoted lines. 3. Answer ONLY from the policy above. If the policy does not cover the case, set "refund_allowed" to "POLICY_GAP" - do not improvise. 4. Output schema-valid JSON: {"category": "...", "priority": "P1|P2|P3", "refund_allowed": true|false|"POLICY_GAP", "policy_basis": "quoted line, or POLICY_GAP"} Ticket: === {{TICKET}} ===
Everything above the === is static Same instructions, same manual, same order, every call - that whole block is your cached prefix. Only the ticket changes. You built a grounded system and a cheap one in the same move.
Homework

Three things before session 6 ◐ 30 min total

Source material

What this session covers

Builder-level coverage of the sources below, including the honest cross-vendor disagreement on placement. Official vendor docs and course videos stay with the vendors; links live in the course map.

✓The placement wars: Claude query-after-docs (up to +30%) vs GPT-4.1 both-ends vs Gemini 3 data-first-question-lastPart 1 · three-lane comparison + test-on-your-task rule
✓Document tags with source metadata + quote-grounding (tutorial Ch8 + consolidated best-practices docs)Part 2 · moves 1 and 2, plus the v3 build
✓Context reliance tuning - docs-only vs blend (GPT-4.1 guide)Part 2 · move 3 + the POLICY_GAP pattern
✓RAG vs Google "grounding" vs pasting - when each fitsPart 2 · decision table
✓Prompt caching: Anthropic cache breakpoints + OpenAI static-prefix caching, static-first layoutPart 3 · shape rule + when not to bother
◐RAG Triad - context relevance / groundedness / answer relevancePart 2 · vocabulary only; measured in sessions 8-9
◐Prompt compressionPart 2 · one honest paragraph; deliberately not a pillar
Check yourself

Three questions before you go 🎯 ◐ 90 seconds

1 · You are building a long-document prompt for Claude. Per the vendor's own guidance, the layout is...

Claude: docs top, query after (+30%). Both-ends is GPT-4.1's guidance; data-first-question-last is Gemini's. Know your vendor's default, then A/B on your own task - the three officially disagree.

2 · The point of "extract the exact policy lines first, then answer" is...

Quote-grounding is the anti-hallucination move for long docs: a checkable intermediate step, an audit trail, and a natural path to "the document does not cover this" instead of a confident improvisation.

3 · To make TriageBot v3 cache-friendly on any vendor, you put...

Caching works on identical prefixes - Anthropic via cache breakpoints, OpenAI automatically. One early variable token (like that timestamp) breaks the prefix and silently kills the entire cache.

Builder session 5 cheat sheet · pin this

Placement warsClaude: docs top, query after (+30%). GPT-4.1: instructions both ends. Gemini 3: data first, question LAST.
The adult answerPlacement matters and vendors disagree. Start from your vendor's guidance, then A/B on your own task.
Document tagsWrap each doc with source metadata (name, version, date). Citations and multi-doc prompts depend on it.
Quote-groundingExtract exact lines first, answer from the quotes. Anti-hallucination + audit trail in one move.
Context relianceSet the dial in writing: docs-only (with a POLICY_GAP escape hatch) or blend-and-flag. Never leave it implicit.
Paste vs RAGFits in context and stable? Paste it with today's patterns. Too big or needs freshness? RAG - "grounding" is Google's word for it.
Cache shapeStatic first, variable last, everywhere. One early changing token kills the prefix. TriageBot v1's layout was already right.
Skip caching whenShort prompts, one-off calls, prompts under active edit. Optimize the stable, high-volume path.