hub.gazar.dev · cheat sheet← all cheat sheets

Week 2 · Context engineering & retrieval

RAG & context engineering cheat sheet.

The real lever isn't prompt wording, it's what reaches the window. The pipeline, hybrid search, chunking, re-ranking, agentic retrieval, and multi-turn memory, on one page.

The window is a budget Lost in the middle The RAG pipeline Hybrid search Chunking Re-ranking Agentic retrieval Memory Params
01

The window is a budget you allocate

Everything competes for finite space: instructions, retrieved context, history, and room for the answer. The goal is not maximum context, it is minimum sufficient context.

distraction

Irrelevant passages

The model cannot tell your junk from your gold; noise pulls the answer off course.

rot

Context rot

Quality drifts as the window grows; long contexts drift and contradict themselves.

cost

Cost & latency

You pay per token in and out, and a fat prompt is slower on every single call.

The tell: if you cannot say what you deliberately kept out of the window and why, you did not engineer the context, you dumped it.
02

Models forget the middle

Recall follows a U-curve: the model reliably uses what is at the start and end and loses the middle. The Lost in the Middle study measured accuracy sagging from the high tens into the fifties when the answer sat mid-context. A fact is not "in context" just because it is in the tokens.

Practical consequence: put the highest-scoring chunk nearest the question, and keep the window lean so nothing important gets buried.
03

Retrieval is a pipeline, not a vector lookup

"Embed the query, grab top-k, stuff it in" is the naive version, and it is why so many RAG demos feel dumb. The real pipeline has stages.

flowchart LR
  Q["Query"] --> RW["Rewrite / expand
optional"] RW --> H(("Hybrid
retrieve")) H --> RR["Re-rank
keep best few"] RR --> AS["Assemble window
dedup, order, trim"] AS --> M{"Model"} M --> A["Grounded answer"] classDef s fill:#DCEBFE,stroke:#1F2937,color:#0E1726; classDef m fill:#EEE6FF,stroke:#1F2937,color:#0E1726; class RW,RR,AS s; class M m;
04

Hybrid search: meaning AND keywords

Dense vector search finds things that mean the same. Sparse keyword/BM25 finds the exact term, product codes, names, error strings, that embeddings blur. Run both, fuse, dedup.

flowchart TB
  Q["Query"] --> D["Dense / vector
semantic"] Q --> S["Sparse / BM25
exact keyword"] D --> F(("Fuse
+ dedup")) S --> F F --> R["Ranked candidates"] classDef a fill:#DCEBFE,stroke:#1F2937,color:#0E1726; classDef b fill:#D6F5E3,stroke:#1F2937,color:#0E1726; class D a; class S b;
Pure vector search silently fails on "error code E-4021" or "the Q3 Whitfield contract". Keywords save you there.
05

How you chunk decides what you can find

You retrieve chunks, so chunking is a design decision, not preprocessing.

too big

A whole page

Comes back for one sentence of signal; the window fills with filler.

just right

Semantic units

A section, a Q&A, a function, with a little overlap and metadata (source, date, permissions).

too small

A severed fact

Split from the thing it describes; retrieved alone it is meaningless.

06

Retrieve broadly, then re-rank ruthlessly

First-pass retrieval trades precision for recall, grab ~40. Then a re-ranker scores each against the query and you keep the top few. Cheap step, huge quality gain, and the single most commonly skipped one.

flowchart LR
  Q["Query"] --> RET["Retrieve top 40
high recall"] RET --> RANK["Re-rank by relevance
cross-encoder / LLM"] RANK --> TOP["Keep top 5
high precision"] TOP --> W["Into the window"] classDef s fill:#DCEBFE,stroke:#1F2937,color:#0E1726; classDef ok fill:#D6F5E3,stroke:#1F2937,color:#0E1726; class RANK s; class TOP,W ok;
07

Agentic retrieval: fetch on demand

Classic RAG retrieves once, up front, blind. Agentic retrieval gives the model retrieval as a tool: it decides whether to look something up, searches, reads, and searches again for multi-hop questions. More power, more cost, so cap the loop.

flowchart LR
  Q["Question"] --> M{"Model
need to look it up?"} M -- yes --> SR["Search + read"] SR --> M M -- "enough" --> A["Grounded answer"] classDef m fill:#EEE6FF,stroke:#1F2937,color:#0E1726; classDef ok fill:#D6F5E3,stroke:#1F2937,color:#0E1726; class M m; class A ok;
08

Multi-turn memory rots, curate it

In a long chat the history grows every turn, and with it cost, latency, and the U-curve middle. You cannot just append forever.

summarize

Compress old turns

Into a running brief; keep the last few verbatim.

prune

Drop dead ends

Turns that no longer matter; do not carry them forever.

externalize

Store durable facts

Name, goal, decisions, outside the window; re-inject on need.

09

Quick reference: sane starting params

KnobStart atTune when
Chunk size1 semantic unit (~200-500 tokens)answers miss context, go bigger; noisy, go smaller
Overlap10-15%facts split across chunk edges
First-pass k~40the answer is not in the candidates, raise it
Re-rank keeptop 3-5window is bloated, keep fewer
Score floordrop below a min relevanceungrounded answers, raise the floor
Context budgetleave headroom, never fill to the brimlatency/cost too high, cut context first
Naive vs proper: same corpus, same model. Naive top-k returns ten chunks that all sound relevant; hybrid + re-rank returns the two that actually answer the question. The only difference is what reached the window, and that difference is the whole answer.