AI Engineering
/
LLM Foundations
/

Choose long context or retrieval

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.

Medium
Conceptual

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 pricesValue
Context window128,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 cacheReuses 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
RetrievalReturns 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.

SliceQuestionsWhole lease correctRetrieval correctRetrieval returned every needed passage
Single fact, such as the rent due date80787778
Evidence in the middle third of the lease40333739
Synthesis across clauses, such as an addendum that changes a clause50423134
Unanswerable: the lease is silent, and a correct answer says so302622Not 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 search1.6 s

For Feature B, the team checked 50 past maintenance questions:

CheckResult
The team pack holds every policy the answer needs35 of 50
Retrieval over the asker's policies returned every needed passage44 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.

OptionPrompt partsFits the window?Cost per questionCost per dayFirst token against the target
A: whole lease, no prefix caching1,000 instructions + 40,000 lease + 200 question in; 300 out????
A: whole lease, cached prefixSame parts; the instructions and the lease form the cached prefix????
A: retrieval, top 5 passages1,000 instructions + 5 passages x 600 + 200 question in; 300 out; $0.0005 search????
B: every policy the staff member may see2,000 to 2,600 policies x 2,500 tokens????
B: maintenance team pack1,000 instructions + 100,000 pack + 200 question in; 300 out????
B: retrieval over each asker's policiesSame 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.

SliceQuestionsWhole lease correctRetrieval correctRetrieval returned every needed passageAccuracy, whole lease against retrievalWhat the gap showsLikely mechanismCheck that would confirm it
Single fact80787778????
Evidence in the middle third40333739????
Synthesis across clauses50423134????
Unanswerable302622Not applicable????
All slices200179167Not 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.

ChangeWhat movesFeature AFeature 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

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.

Practice the interview question

Answer it in your own words before you look at the reference answer.