Skip to content
All articles

AI Agents · RAG

Follow one question through a RAG pipeline

“can dads carry it forward?” is phrased nothing like the policy that answers it. Query rewriting, hybrid retrieval and reranking still find both facts in one pass.

· 4 min read

One question moving through four RAG stages. An employee asks “can dads carry it forward?”. Step 1 rewrites it as “parental leave carry-forward policy”, resolving “dads” to fathers and parental leave and “it” to unused leave. Step 2, hybrid retrieval with keywords and vectors, returns four chunks: Carry-Forward, Parental, Annual and Sick. Step 3, reranking, keeps the two that answer it: Parental #105 and Carry-Forward #106. Step 4 writes a grounded answer with sources: fathers get 30 days of parental leave, and up to 10 unused days carry forward. One pass found both facts, so no agent loop was needed.

An employee types "can dads carry it forward?" into the company assistant. It is colloquial, it leans on a pronoun, and it is phrased nothing like the leave policy that answers it. The course follows this one constructed request through every stage of a retrieval pipeline. The rankings and the policy text are illustrative; the mechanism at each step is the real one.

Before the question: the policy, ingested once

Offline, Leave Policy.pdf was parsed into typed elements and split along its headings, so Annual Leave, Sick Leave, Parental Leave and a cross-cutting Carry-Forward Policy each became a chunk. Every chunk carries metadata (document, section, version, department), an embedding, and an entry in a parallel BM25 keyword index. Two of the records matter here, shown abridged:

{ "id": 105, "text": "Fathers receive 30 days of parental leave.", "section": "Parental Leave" }
{ "id": 106, "text": "Unused parental leave may carry forward up to 10 days into the next year.", "section": "Carry-Forward Policy" }

Four steps, one pass

  1. Rewrite the query. Query transformation resolves "dads" to fathers and parental leave, and "it" to unused leave, giving parental leave carry-forward policy. That works only because the previous turn of the conversation established what "it" means. Without that history, the right move is to ask which leave the employee means, not to invent a referent.
  2. Retrieve by keyword and by meaning. BM25 catches the exact words "carry" and "forward"; vector search catches "parental leave" by meaning. Reciprocal rank fusion merges the two lists into a top four: Carry-Forward, Parental, Annual and Sick.
  3. Rerank. A cross-encoder reads each candidate against the query and confirms that the two truly relevant chunks, #105 and #106, belong at the top.
  4. Generate, with sources. Both chunks go into the prompt with their sources, and the answer cites each fact: fathers receive 30 days of parental leave, and unused parental leave may carry forward up to 10 days into the next year.

Where an agent would come in

This query needed no agent. Hybrid retrieval plus reranking found both facts in a single pass, so a plain pipeline was enough. The loop earns its place when the system cannot know in advance how many facts it needs. In the course's earlier worked example, an agent searched for parental leave, read the result, noticed that the carry-forward rule was still missing, and searched again. That decision is the loop's value: knowing when one retrieval is not enough, instead of hard-coding "always fetch two chunks" and hoping both facts land in them.

Engineering Insight

Individual stages in this pipeline are optional when the workload does not need them. Keep the simpler pipeline as a baseline, and add an adaptive loop only when evaluation shows that choosing another source, or reformulating after an observation, improves the task.

Get one diagram a week

A short article built around one engineering diagram, from the same library as these courses.

One diagram-led article a week on AI and systems engineering. We email you once to confirm, and every newsletter has an unsubscribe link. Privacy policy