Skip to content
AgenticAIWiki
  1. Learn
  2. Part 11
  3. 58. Context engineering
  4. 58.2

Lesson 58.2 · 2 of 7 · 4 min read · Reviewed 24 Nov 2026

Just-in-time retrieval

By the end, you can

  • Retrieve information just in time instead of loading it all up front.
  • Use lightweight references like paths and ids.

The traditional approach front-loads everything: paste the docs, the database dump, the whole codebase. That’s costly, often doesn’t fit, and buries the relevant bit.

Fetch when needed

Give the agentAgent: A system where a model decides its own next steps, calling tools in a loop until the task is done. ways to look things up:

  • search_docs(query) returning short snippets with ids.
  • read_section(id) returning one section.
  • Grep and Glob for files (37.2).

The agent keeps references, such as paths, ids and URLs, which are tiny, and loads content only for the step that needs it.

Trade-offs

  • Slower: each lookup is a tool call.
  • Can miss things if search is poor, so invest in good search (Part 4).

A hybrid often works best

Load a little up front, such as a CLAUDE.md with key facts and a map of where things live, and fetch the rest just in time. Claude CodeClaude Code: Anthropic’s agentic coding tool that works in your terminal, IDE and CI, reading and editing code and running commands. works this way: project memory at the start, files read as needed.

Key points

  • Instead of pasting everything at the start, give the agent tools to fetch what it needs when it needs it.
  • Keep lightweight references, such as file paths, URLs and ids, and load the content only when used.
  • This is how coding agents handle big repos: search, then read only the relevant files.
Hands-on labRuns in the Playground

Answer from a manual with just-in-time lookups

Give an agent search and read tools over a large manual so only the relevant section enters the context, and compare the cost with pasting everything.

You will have: search_docs and read_section functions, an answer with its section id, and a token comparison.

Steps

  1. Read the MANUAL dictionary: 200 generated sections plus two real ones.
  2. Write search_docs(query) that returns the top 3 section ids with a 60-character snippet, ranked by word overlap.
  3. Write read_section(id) that returns the full text of one section.
  4. Answer the question by searching, reading the best section, and printing the answer line with its id.
  5. Print the tokens used (snippets plus one section) against the tokens for the whole manual.

Done when

  • The answer cites section s-reset.
  • Just-in-time lookups use less than 2% of the whole manual's tokens.
  • Only one full section is read.

Open in the Playground No API key needed.

Starter code (Python)
MANUAL = {"s-%03d" % i: "Section %d covers printer model %d settings, trays and paper sizes in detail. " % (i, i) * 6 for i in range(200)}
MANUAL["s-reset"] = "Factory reset: hold the power and menu buttons together for ten seconds until the light flashes amber."
MANUAL["s-wifi"] = "Wi-Fi setup: open the menu, choose Network, then pick your network and enter the password."

def tokens(text):
    return max(1, len(text) // 4)

def search_docs(query, k=3):
    # TODO: rank section ids by how many query words appear in the text; return [(id, snippet)]
    return []

def read_section(sid):
    # TODO: return the full text of one section
    return ""

question = "how do I factory reset the printer"
hits = search_docs(question)
print("hits:", hits)
Show a solution
MANUAL = {"s-%03d" % i: "Section %d covers printer model %d settings, trays and paper sizes in detail. " % (i, i) * 6 for i in range(200)}
MANUAL["s-reset"] = "Factory reset: hold the power and menu buttons together for ten seconds until the light flashes amber."
MANUAL["s-wifi"] = "Wi-Fi setup: open the menu, choose Network, then pick your network and enter the password."

def tokens(text):
    return max(1, len(text) // 4)

def search_docs(query, k=3):
    words = [w for w in query.lower().split() if len(w) > 3]
    scored = sorted(MANUAL, key=lambda s: (-sum(w in MANUAL[s].lower() for w in words), s))
    return [(s, MANUAL[s][:60]) for s in scored[:k]]

def read_section(sid):
    return MANUAL[sid]

question = "how do I factory reset the printer"
hits = search_docs(question)
best = hits[0][0]
text = read_section(best)
print("hits:", [h[0] for h in hits])
print("answer:", text, "[" + best + "]")
used = sum(tokens(s) for _, s in hits) + tokens(text)
whole = sum(tokens(t) for t in MANUAL.values())
print("tokens used: %d of %d (%.2f%%)" % (used, whole, 100 * used / whole))
Quick checkAn agent must answer questions about a 5,000-page manual. What's the best approach?
How sure are you?
Pick an answer and how sure you are