# Just-in-Time Retrieval vs Preloading Context

> Preloading context versus just-in-time retrieval for AI agents: what each does, the trade-off, and how to choose, explained simply for your AI interview.

**In short:** Preloading puts the material an AI needs into its prompt before it answers. Just-in-time retrieval gives it pointers and lets it fetch only what it needs while it works. Anthropic says that in some settings the most effective agents may mix the two.

AI and LLM interview question Q106, Prompt and context engineering. Advanced, occasional (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/just-in-time-retrieval-vs-preloading-context

## Say it in 30 seconds

Preloading means putting the material an AI might need into its prompt before it answers. Just-in-time retrieval means giving it a short list of where things are, and letting it fetch one item when it needs it. Anthropic says that in certain settings the most effective agents might mix both, with some data up front for speed and more fetched on demand. In a setup like this, I would preload the small rules that never change and fetch the rest with a tool, because every token in the window costs attention and money.

## Say it in 2 minutes

Before the model answers, your code finds the likely material and pastes it into the prompt. Many AI apps built on search still work this way. The search runs first, and the model gets the top matches. It is simple, and the model has everything in front of it from the start.

Just-in-time retrieval flips that. The prompt carries only pointers, like file names or stored queries. The model decides what it needs, calls a tool, and reads just that part. Anthropic's engineers describe agents that load data at runtime with tools, and they say Claude Code works this way for large data work. The trade-off is speed: fetching on demand is slower than reading pre-computed data.

Why does it matter? The window is finite. Anthropic notes that as the number of tokens grows, recall gets worse, which it calls context rot. So I would keep the stable rules preloaded, where caching makes repeated reads cheap, and fetch the long, changing material on demand. For a claims desk with 200 clauses, the model reads one clause for a claim, not the whole rulebook every time.

A good answer names the trade-off and the failure of each side: a bad index makes the agent wander, and a bad preload buries the useful part. Anthropic's own advice is to do the simplest thing that works first.

## What is the difference, in plain words?

A model can only work with the text in its prompt and the tool results it has received. That space is its context window. Preloading fills the space with the likely material before the first reply. Just-in-time retrieval fills it with pointers, and the model fetches the exact item when it needs it.

Think of a new claims handler on day one. One way is to put the whole policy binder on the desk. The other is to hand over the index and a runner who brings the one page a customer asks about. The binder is quick to start with but heavy to carry around. The runner keeps the desk light, but every page costs a wait.

The same choice faces an AI assistant that answers claims questions from 200 policy clauses. Each question needs one or two of them.

- **Context window:** All the text a model can see in one call, counted in tokens. A token is a small piece of text, often a word or part of one.
- **Preloading:** Putting material into the prompt before the model starts to answer.
- **Just-in-time retrieval:** The model fetches items with tools while it works, using pointers it holds.
- **Pointer:** A file name, ID or stored query that says where content lives.

## How does each approach work?

Many AI apps built on search still preload. A search compares the meaning of the question with the stored passages, the best matches are pasted in, and only then does the model start. Anthropic's engineers describe this as retrieval that runs before inference, which is the step before the model starts to answer.

Just-in-time retrieval skips the pre-search. The agent keeps lightweight identifiers, such as file paths or stored queries, and uses tools to load data into context at runtime. Nothing is read until the model asks for it.

*Diagram, Two ways to hand over the same 200 clauses: The same 200 clauses handled two ways. Preloading sends the whole rulebook on every call. Just in time sends only a list of file names, and the model can then ask for one clause.*
1. The whole rulebook lands in the prompt, on every call.
2. The model sees only the names and asks for the one it needs.
3. It answers from the clause it has read.

Anthropic says teams are increasingly adding just-in-time strategies to their retrieval systems. Its own example is Claude Code, which runs complex data analysis over large databases. It writes targeted queries, stores the results, and uses commands such as head and tail, so the full data never enters the context.

*Diagram, One just-in-time lookup: A small loop. The model calls a tool for one file, and the clause comes back to it. Each lookup is a round trip, so a few lookups add up.*
1. The question goes to the model.
2. The model calls the tool for the one file it needs.
3. The clause comes back, and the model answers from it.

## Why does the choice matter?

Every token in the window competes for the model's attention. Anthropic treats context as a limited resource and warns that bigger windows do not remove the problem. Preloading uses that space on every call, whether or not the text is needed. Anthropic calls the drop in recall as the window fills context rot.

> "Context, therefore, must be treated as a finite resource with diminishing marginal returns." (Anthropic)
>
> "But it's likely that for the foreseeable future, context windows of all sizes will be subject to context pollution and information relevance concerns" (Anthropic)
>
> "as the number of tokens in the context window increases, the model's ability to accurately recall information from that context decreases" (Anthropic)

Speed is the price of fetching on demand. Anthropic says runtime exploration is slower than retrieving pre-computed data. Agents also need clear tools and guidance, or they waste the window on dead ends.

- **Preload: Fast to start, heavy per call.** Everything is in the window from the first token. Each call reads it in full unless prompt caching makes repeat reads cheap, and stale copies stay until someone refreshes them.
- **Just in time: Light and fresh, but slower.** Only the pieces needed are read, and the source stays current. It needs a search or index, and tools the model can use well.

### How would you set up the claims desk?

Anthropic says the hybrid strategy, with some data up front and more fetched at the model's discretion, may suit contexts with less dynamic content, such as legal or finance work. Clause text changes each quarter, so the stable core is preloaded and the rest is fetched by path.

1. **Preload the stable core:** The few rules that never change, such as the claim form fields, go in the prompt. Caching keeps the start of a prompt so the next call can reuse it at a lower price. Anthropic's caching docs say a cache read costs 0.1 times the base input price. A core shorter than the model's minimum cacheable length is not cached, so check the cache usage fields.
2. **Index every clause:** Each clause gets a stable file path and a short title. The model can find the right one without reading all 200.
3. **Give it a read tool and a search tool:** The model calls them for the clause it needs, and gets back only that text.
4. **Cap what one answer can pull:** Code limits how many clauses one answer may read, and logs each read, so a runaway loop shows up early.

Here is the lookup in plain Python. The numbers are for a made-up desk. Almost all of the 4,856 characters are the 200 file names, and a search tool would remove even that list. Characters stand in for size here, not tokens. The output is what the script printed.

```python
# A made-up claims desk with 200 policy clauses. One claim needs one clause.
POLICIES = {
    f"policies/clause-{i:03d}.txt": f"Clause {i}: claims of type {i % 12} need evidence within {7 + i % 30} days."
    for i in range(200)
}

def preload():
    # Every clause goes into the prompt before the first reply.
    return "\n".join(POLICIES.values())

def list_paths():
    # Only the file names go into the prompt; the text stays on disk.
    return "\n".join(POLICIES.keys())

def read_file(path):
    # A tool the model calls when it needs one clause.
    return POLICIES[path]

pre_size = len(preload())
jit_size = len(list_paths()) + len(read_file("policies/clause-042.txt"))
print("Preloaded characters:", pre_size)
print("Just-in-time characters:", jit_size)
print("Fetched:", read_file("policies/clause-042.txt"))
```

Output:

```text
Preloaded characters: 11700
Just-in-time characters: 4856
Fetched: Clause 42: claims of type 6 need evidence within 19 days.
```

Test it on real claims. Check that the answer cites the right clause, and that the model did not pull the whole folder for a simple question.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] A plain definition: preloading loads material first, just-in-time fetches it while the model works.
- [ ] The trade-off: preloading is fast but heavy, just-in-time is light and fresh but slower.
- [ ] Why it matters: the window is finite, and recall gets worse as it fills.
- [ ] A hybrid answer: stable rules preloaded, changing material fetched with tools.
- [ ] Design of the tools and file names, or the agent wanders and burns the window.
- [ ] Measurement on real questions: right sources cited, and token use checked.

**Common traps**

- Trap: "Put the whole rulebook in the prompt, it is the safest." Better: It buries the one clause that matters, and you pay for the rest on every call. Preload only the stable core.
- Trap: "A bigger window makes search unnecessary." Better: Bigger windows still have pollution and relevance problems, and recall drops as the window fills. Every extra token is more to pay for.
- Trap: "The model will find everything on its own." Better: Without an index and clear tools, it chases dead ends. Design the file names and tools first.

## Can you spot the problem?

**Spot the problem:** A claims assistant loads all 200 clauses before every answer, even for a question about one clause. What is the main problem?

A. The model should be fine-tuned on all 200 clauses instead.
B. Every call pays for the whole rulebook, when one or two clauses are needed.
C. The model should read the clauses in order, from clause 1 to 200.
D. Each clause should be pasted into its own separate prompt.

**Answer:** B. Preloading sends the whole rulebook each time, so most of the text is unused on every claim. Keep the stable rules in the prompt and fetch the one clause by its file path.

**Quick check:** Clauses change every quarter, and a claim needs one or two of them. Which setup suits this best?

A. Preload all clauses into every prompt
B. Keep a clause index and let the model fetch the matching clause when it needs it
C. Fine-tune the model once on last quarter's clauses
D. Add more clauses to every prompt so no clause is missed

**Answer:** B. Fresh clauses are read from their files at question time. Preloading copies the whole rulebook into every call, and fine-tuning freezes last quarter's text into the model.

## Can you answer these from memory?

**Flash cards**

- **What is preloading?** Putting the material into the prompt before the model answers.
- **What is just-in-time retrieval?** The model fetches items with tools while it works, using pointers.
- **Main trade-off?** Preloading is fast but heavy. Just-in-time is lighter and fresher, but slower.
- **What is context rot?** Recall getting worse as the context window fills with more tokens.
- **A good hybrid?** Preload the stable core, and fetch the changing material on demand.
- **Does a bigger window fix it?** No. Anthropic says pollution and relevance concerns apply to context windows of all sizes.

## Likely follow-up questions

- Q111 What is RAG, and why use it? https://designervenkat.online/interview/ai/what-is-rag
- Q138 RAG vs long context: which to use? https://designervenkat.online/interview/ai/rag-vs-long-context
- Q137 What is agentic RAG? https://designervenkat.online/interview/ai/what-is-agentic-rag
- Q096 What is compaction (summarising history)? https://designervenkat.online/interview/ai/what-is-compaction-summarising-history
- Q097 What is structured note-taking (external memory)? https://designervenkat.online/interview/ai/what-is-structured-note-taking-external-memory
- Q105 How do you budget tokens for chat history? https://designervenkat.online/interview/ai/how-do-you-budget-tokens-for-chat-history

## Learn it in depth

- [Context Engineering](https://designervenkat.online/learn/ai-engineering/context-engineering)
- [From question to context](https://designervenkat.online/learn/ai-engineering/retrieval-process)
