esc
  • WorkflowsPages
  • Case studiesPages
  • Machine Learning FoundationsLearn
  • AI EngineeringLearn
  • AI Guardrails & Red TeamingLearn
  • UI/UXLearn
  • Design SystemsLearn
  • Production System DesignLearn
  • AI papersPages
  • Interview KitPages
  • Book a callContact
Let’s talk

I’ll build the agent for it.

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.

11 min readAdvancedOccasional · estimateFacts checked 27 November 2026

Say it out loud

Read it once, then say it without looking. Start with the short one; use the long one when they ask you to go deeper.

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.

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.

Key words

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.

Two ways to hand over the same 200 clauses

Preload up front

All 200 clauses

pasted in before the first reply

Just in time

List of file names

small; the text stays on disk

Model

answers from what is in its window

Answer

cites the clause it read

  1. 1The whole rulebook lands in the prompt, on every call.
  2. 2The model sees only the names and asks for the one it needs.
  3. 3It answers from the clause it has read.
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.

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.

One just-in-time lookup

Claim question

Does clause 42 still apply?

Model

decides which file it needs

read_file(clause-042)

a tool returns just that file

  1. 1The question goes to the model.
  2. 2The model calls the tool for the one file it needs.
  3. 3The clause comes back, and the model answers from it.
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.

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. 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. 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. 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. 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

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."

    BetterIt 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."

    BetterBigger 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."

    BetterWithout 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?

Quick check

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

Can you answer these from memory?

Flash cards · tap a card to flip it

1 / 6

What will they ask next?

  1. Q111What is RAG, and why use it?13 min
  2. Q138RAG vs long context: which to use?12 min
  3. Q137What is agentic RAG?12 min
  4. Q096What is compaction (summarising history)?11 min
  5. Q097What is structured note-taking (external memory)?12 min
  6. Q105How do you budget tokens for chat history?10 min

Learn it in depth

Last updated on 27 November 2026

Building an agent that has to find the right clause?

I design and build AI products and agents, including the UI and the system behind them. Book a short call and tell me what your documents look like and what the agent has to do.