LLM Foundations · 2 to 3 hours
Choose long context or retrieval
Compare sending whole documents in a large context window with retrieving a few passages for two features of one product, using window fit, cost with prefix caching, latency, access, and a sliced pilot, and say what would change each choice.
Conceptual comparison
3 h
Used in interview rounds:
Technical Screen (Medium)Overview
Answer a common screen question, "why not put the documents in the prompt instead of building retrieval?", by comparing whole-document context with passage retrieval for a stated product, on the same dimensions and with figures. You practice on a fictional property manager's lease assistant and staff policy assistant.
Study time: 2 to 3 hours
Interview format: Technical screen comparison with a cost and evidence table
By the end you can
- Compare whole-document context and passage retrieval on the same dimensions for a stated product feature
- Check window fit with an output reserve, and separate a window's capacity from the model's effective use of it
- Compute cost per question and per day with and without prefix caching, stating the reuse assumption behind the discount
- Read a sliced pilot and tie each gap to its size, a mechanism, and a check that would confirm it
- Keep text a user may not see out of the prompt in both designs
- Name the constraint changes that would reverse a long-context or retrieval decision
Prerequisites
Helpful background: Comfort with per-million-token pricing, percentages, and reading a small results table helps.
Two options for one product
Two designs answer questions about documents:
- Whole-document context sends the documents the user may see, in full, with every question. There is no index.
- Passage retrieval sends only the top k passages an index returns. Here it is a component with measured results; ranking belongs to Embeddings & Retrieval, and chunking and citations to RAG & Data Pipelines.
Decide per feature, from its goal and constraints: who asks, how much text one user may see, how often it changes, who may see which parts, the latency target, and the volume. Then rate both options in one table on the same dimensions: fit, effective use, cost, latency, freshness, access, traceability, and failure mode.
Check the fit first
Use the simplified rule instructions + documents + question + output reserve <= window, and check the provider's own accounting. Count what one request would carry: the documents one user may see for that question, not the whole corpus, measured with the model's tokenizer, with headroom for growth. The largest documents decide, so check fit per document at request time, with a fallback for those that do not fit.
Fit is necessary, not sufficient: a request that fits can still be slow, costly, or poorly used.
Capacity is not effective use
Capacity is the number of tokens a request may hold; effective use is whether the model finds and combines the right ones. Models that find one planted fact almost perfectly can degrade on multi-fact, tracing, and aggregation tasks as input grows (RULER), and evidence in the middle of a long input is often used less well than evidence near its start or end (Lost in the Middle). A later amendment can override an earlier clause: whole-document context holds both and must apply the later one, while retrieval may return only one.
Test by slice on your own documents (single facts, evidence positions, synthesis, unanswerable questions), and re-ask with the evidence moved to the start, middle, and end.
Treat retrieval as a component
The key measurement is whether every passage the answer needs is among the k returned, per slice: single facts can score high while synthesis scores low. A miss is silent, so "not in these passages" is weaker evidence than "not in the document". Retrieval also adds an index to build, update, and delete from, plus a search per question.
Long windows do not make it obsolete: in one study, a short-window model with simple retrieval performed comparably to a longer-window model on long-context tasks with much less computation (Retrieval meets Long Context).
Cost per question and prefix caching
Cost per question is (uncached input x uncached rate + cached input x cached rate + output x output rate) / 1,000,000, plus any per-question fee such as a search.
Prefix caching reuses the provider's processing of an identical leading part of a prompt. Reads bill at a fraction of the input rate; writes cost the same or more, depending on the provider. The cache lasts minutes by default (some providers sell longer lifetimes), is refreshed on each use, and breaks on any change at or before the cached point; very short prefixes are not cached. Put stable content first and the question last.
Reuse sets the discount. A shared document is reused across users only if all of them may see it, and a per-user document only by that user's follow-ups within the lifetime, so n questions pay one write and n - 1 reads. Caching changes input cost and time to first token, not output cost or the answer.
Input length and the first token
The model processes the whole prompt before its first token (prefill; see Model Serving & Inference), so longer input usually means a later first token. A cached prefix skips most of that work; retrieval adds a search but sends a far shorter prompt.
Measure p95 time to first token per request kind (cache miss, cache hit, retrieval) against the target, and never average p95 values across kinds. Writing the cache before the first request moves that work earlier, and is paid for even if no question follows.
Freshness, access, and traceability
Freshness. Whole-document context reads the current file, and a changed document stops matching its old cached prefix. Retrieval must re-index changes and retire old revisions, and a deletion must reach every derived copy, such as passages and embeddings.
Access. What reaches the prompt is the access boundary. Retrieval uses a permission-aware filter (OWASP LLM08), applied before ranking. Whole-document context must assemble only text the user may see, annexes and notes included. An instruction not to reveal text is not a control, and the identity comes from the session, never the question.
Traceability. Retrieval returns passage IDs to cite; a whole-document answer quotes the clause, and the application checks the quote.
Failure modes, hybrids, and evidence
A retrieval miss leaves a needed passage out: a wrong answer or a false "not covered". Diluted attention leaves the evidence in the prompt but missed or misread, such as a middle position, a near-duplicate clause, or an overridden clause. Hybrids sit between: retrieve, then send whole sections or short documents; send small documents whole and retrieve from large ones; or route each question to one or the other, which one study found kept long-context quality at much lower cost (Li et al.).
Settle the choice on the same labeled questions, sliced, comparing accuracy, cost per question, and p95 time to first token. Read each gap against its slice's size: a one-question gap is noise; a large gap with a measured mechanism is evidence. To price a quality gap, divide the daily cost difference by wrong answers avoided per day and compare with what a wrong answer costs.
Scenario: two assistants for a property manager
Worked example
Suppose a property-management company adds two assistants to its apps, both on the same hosted model. The company, prices, and figures are fictional.
| Model and prices | Value |
|---|---|
| Context window | 128,000 tokens, input plus output |
| Input, uncached | $2.00 per million tokens |
| Input, cached prefix | $0.30 per million tokens |
| Output | $9.00 per million tokens; answers average 300 tokens, capped at 1,000 |
| Prefix cache | Reuses an identical prompt prefix for 5 minutes, refreshed on each use. In this scenario, a cache write costs the uncached input price. The application may also write the cache without a question; that write bills only the prefix. Prefixes under 2,048 tokens are not cached |
| Retrieval | Returns the top 5 passages of about 600 tokens each, for $0.0005 per question |
Every prompt starts with the same 1,000 tokens of instructions and ends with the question, about 200 tokens. Earlier turns are not resent.
Feature A: residents ask about their own lease.
- One lease per resident, about 40,000 tokens. It changes only at renewal or through a signed addendum, and the resident may read all of it.
- Residents open the lease chat about 1,200 times a day. 1,000 of those sessions include questions, 3 on average (3,000 a day), each within 5 minutes of the chat opening or of the previous question.
- Target: p95 time to first token of at most 3 seconds for every question.
- Option 1 sends the instructions, the whole lease, and then the question. Option 2 retrieves 5 passages from an index of all leases, filtered to the resident's lease.
Feature B: staff ask about internal policies.
- About 6,000 policy documents of about 2,500 tokens each; about 150 change each day, as new revisions or retirements.
- Each staff member may see the company-wide policies plus their own team's: 2,000 to 2,600 documents, depending on the team.
- 800 questions a day, 300 of them from the maintenance team.
- The maintenance lead proposes a team pack: send the team's 40 most-used policies (about 100,000 tokens) with every maintenance question and skip retrieval. Six of the 40 are visible only to maintenance supervisors.
- The alternative retrieves 5 passages from the policies the asker may see.
Scenario: the lease pilot and the policy sample
Worked example
The team ran both Feature A options on 200 past resident questions, each in one slice, sampled so that each slice's share matches production traffic. Every answer was graded against the lease.
| Slice | Questions | Whole lease correct | Retrieval correct | Retrieval returned every needed passage |
|---|---|---|---|---|
| Single fact, such as the rent due date | 80 | 78 | 77 | 78 |
| Evidence in the middle third of the lease | 40 | 33 | 37 | 39 |
| Synthesis across clauses, such as an addendum that changes a clause | 50 | 42 | 31 | 34 |
| Unanswerable: the lease is silent, and a correct answer says so | 30 | 26 | 22 | Not applicable |
| Request kind (pilot traffic) | p95 time to first token |
|---|---|
| Whole lease, cache miss (first question in a session) | 4.6 s |
| Whole lease, cache hit (follow-up within 5 minutes) | 1.3 s |
| Retrieval, including the search | 1.6 s |
For Feature B, the team checked 50 past maintenance questions:
| Check | Result |
|---|---|
| The team pack holds every policy the answer needs | 35 of 50 |
| Retrieval over the asker's policies returned every needed passage | 44 of 50 |
Work the fit and cost checks
Use the prices, prompt parts, and volumes in the scenario.
Predict the outcome
Work the fit and cost checks
Using the scenario's prices, prompt parts, and volumes, work out for each option whether it fits the window with the 1,000-token output reserve, its cost per question and per day, and how its first token compares with any measured target. Then reveal the answers.
| Option | Prompt parts | Fits the window? | Cost per question | Cost per day | First token against the target |
|---|---|---|---|---|---|
| A: whole lease, no prefix caching | 1,000 instructions + 40,000 lease + 200 question in; 300 out | ? | ? | ? | ? |
| A: whole lease, cached prefix | Same parts; the instructions and the lease form the cached prefix | ? | ? | ? | ? |
| A: retrieval, top 5 passages | 1,000 instructions + 5 passages x 600 + 200 question in; 300 out; $0.0005 search | ? | ? | ? | ? |
| B: every policy the staff member may see | 2,000 to 2,600 policies x 2,500 tokens | ? | ? | ? | ? |
| B: maintenance team pack | 1,000 instructions + 100,000 pack + 200 question in; 300 out | ? | ? | ? | ? |
| B: retrieval over each asker's policies | Same shape as Feature A's retrieval | ? | ? | ? | ? |
Read the lease pilot by slice
Compare each gap with the size of its slice before you call a winner.
Predict the outcome
Read the lease pilot by slice
For each slice of the lease pilot, compute both accuracies, say whether the gap is large enough to act on given the slice's size, name the likely mechanism, and name the check that would confirm it. Then reveal the answers.
| Slice | Questions | Whole lease correct | Retrieval correct | Retrieval returned every needed passage | Accuracy, whole lease against retrieval | What the gap shows | Likely mechanism | Check that would confirm it |
|---|---|---|---|---|---|---|---|---|
| Single fact | 80 | 78 | 77 | 78 | ? | ? | ? | ? |
| Evidence in the middle third | 40 | 33 | 37 | 39 | ? | ? | ? | ? |
| Synthesis across clauses | 50 | 42 | 31 | 34 | ? | ? | ? | ? |
| Unanswerable | 30 | 26 | 22 | Not applicable | ? | ? | ? | ? |
| All slices | 200 | 179 | 167 | Not applicable | ? | ? | ? | ? |
Change one constraint
Each row changes one fact; everything else stays as stated.
Predict the outcome
Change one constraint
Each row changes one fact in the scenario. Say what moves in the comparison (recompute any figure that changes) and whether the choice for each feature holds, then reveal the answers.
| Change | What moves | Feature A | Feature B |
|---|---|---|---|
| Leases become 300-page portfolio agreements of about 150,000 tokens | ? | ? | ? |
| Policies change every hour, and staff must see a change within the hour | ? | ? | ? |
| The provider halves the cached price to $0.15; other prices stay. | ? | ? | ? |
| The 40,000-token lease file turns out to include a 6,000-token internal annex of staff notes that residents may not see | ? | ? | ? |
| Sessions turn out to hold one question each: 3,000 sessions a day | ? | ? | ? |
| The first-token target for the first question in a session is relaxed to 5 seconds | ? | ? | ? |
Completion checklist
- I can check window fit with the output reserve and say why fitting is not enough.
- I can price a question with and without a reused prefix and state the reuse assumption.
- I can read a pilot by slice and tie each gap to a mechanism and a confirming test.
- I can explain how each option keeps restricted text out of the prompt.
- I can name the change that would reverse a long-context or retrieval decision.
Sources
- Lost in the Middle: How Language Models Use Long Contexts (Liu et al., 2023) Accuracy is often highest when relevant information is at the beginning or end of a long input and drops when it is in the middle, including for models built for long contexts.
- RULER: What's the Real Context Size of Your Long-Context Language Models? (Hsieh et al., 2024) Models that score almost perfectly on simple needle retrieval show large drops on multi-needle, multi-hop tracing, and aggregation tasks as input length grows, so a claimed context size overstates effective use.
- Retrieval meets Long Context Large Language Models (Xu et al., 2023) A 4K-window model with simple retrieval matched a 16K long-context model on long-context tasks with much less computation, and retrieval also improved models with extended windows.
- Claude API docs: Prompt caching One provider's documentation, as of November 2026: caching reuses an identical prompt prefix; cache reads cost a fraction of the base input price and writes can cost more; the default lifetime is 5 minutes, refreshed on use; a change invalidates the cache from that point on; caching improves time to first token for long documents but has no effect on output; a pre-warming request writes the cache before the first user request.
- Retrieval Augmented Generation or Long-Context LLMs? A Comprehensive Study and Hybrid Approach (Li et al., 2024) Across public datasets, long-context models outperformed RAG on average quality when resourced sufficiently, while RAG cost significantly less; routing each query to RAG or long context (Self-Route) cut computation cost with performance comparable to long context.
- OWASP Top 10 for LLM Applications 2025: LLM08 Vector and Embedding Weaknesses Recommends permission-aware vector stores and strict logical and access partitioning, and warns that shared stores can leak context between users or groups.
The property-management company, its assistants, documents, prices, token counts, pilot results, latency figures, and rubric are original synthetic teaching material about a fictional application; no real price, model, or benchmark result is claimed for the scenario. Study times and readiness thresholds are editorial estimates. Product, protocol, and model details are as of November 2026.
Related on CodeSnatch
Lessons, problems, and articles that build the skills this question uses.
AI Engineering
Explains prefill and prefix caching: why a long prompt delays the first token and what a cached prefix saves.
System Design
Hits, misses, and time-to-live, the terms for reasoning about which requests reuse a cached prompt prefix.
Practice problem
Practice scoring answers and correct abstentions per slice with named denominators, as a sliced pilot needs.
Practice problem
Select prompt context from the signed-in identity alone, so a crafted request cannot widen what the model sees.
Community article
Covers linear input pricing, prompt caching discounts, and the lost-in-the-middle effect in long prompts.
Practice the interview question
Answer it in your own words before you look at the reference answer.
