aiml

Building Agentic RAG Applications

Build a documentation assistant that answers from permitted, versioned sources. Learn retrieval, bounded agent decisions, service integration and evaluation through runnable examples.

How does the agent decide what to retrieve next?

A fixed RAG pipeline retrieves once before answering. A retrieval agent can inspect tool results and choose another search, read a passage, propose an answer, or stop. The model proposes actions; application code controls which tools may run and enforces the limits.

Agentic RAG: inspect passages, then choose the next action

Question +versionPinned documentsnapshotGemini agentSearch, read,answer or stopAdmit the actionCounts and sourcebytesSearch / readtoolsExact documentsnapshotToolobservationsReturned sourcetextCheck the answerSource IDs + exactquotesCited answerStill needsoutcome checksQuestion +versionPinned documentsnapshotGemini agentSearch, read,answer or stopAdmit the actionCounts and sourcebytesSearch / readtoolsExact documentsnapshotToolobservationsReturned sourcetextCheck the answerSource IDs + exactquotesCited answerStill needsoutcome checks

When the run stops

These are alternative exits from the components above, not additional steps after the answer.

  • 8
    Gemini agentStop without an answer when chosen.
    Stop or refuse
  • 9
    Admit the actionRefuse invalid actions or exhausted budgets.
    Stop or refuse
  • 10
    Search / read toolsStop on tool failure.
    Stop or refuse
  • 11
    Check the answerReject invalid source IDs or quotes.
    Stop or refuse
The model chooses search, read, answer or stop. Tool observations return to the model. Application code enforces action limits and checks source quotations. Invalid actions, exhausted budgets, tool failures or invalid citations end without an accepted answer.
  1. Assess the question and available observations.
  2. Propose a search or read action.
  3. Execute only an admitted tool call.
  4. Return the real tool result.
  5. Assess the retrieved passages before choosing again.
  6. Propose an answer when ready.
  7. Return an answer with valid source references.
  8. Stop without an answer when chosen.
  9. Refuse invalid actions or exhausted budgets.
  10. Stop on tool failure.
  11. Reject invalid source IDs or quotes.

The course separates fixed pipelines, scripted query policies and model-selected actions. Hybrid retrieval combines keyword and vector rankings; it is a retrieval method, not an agent by itself.

What you'll learn

  • Trace RAG requests, replay saved replies and consume a real Gemini provider stream.
  • Ingest and version documentation, then compare lexical, vector and hybrid retrieval.
  • Choose source windows and fit them into a token-bounded request.
  • Compare fixed, scripted and Gemini-selected retrieval policies, then expose search and read through HTTP and MCP.
  • Apply collection permissions, caching, tracing and concurrency limits, then use task evaluations to control a local release.

Lessons

Module 1 · 2 lessons · about 31 minRAG fundamentals

Follow a question through retrieval, context assembly and an answer before adding agent decisions.

  1. How RAG worksRead

    Learn how an application searches documents, supplies relevant passages to a language model and links its answer to sources.

    6 min readLabQuiz

  2. Trace a RAG request from search to answerRead

    Follow a question through document search, context assembly and the model request, then check the answer’s source links.

    25 min readLabQuiz

Module 2 · 2 lessons · about 53 minDefine the task and baseline

Define representative questions and expected behavior, then record a fixed-pipeline baseline.

  1. Build a RAG eval datasetRead

    Define which questions your documents can answer and which passages support them. Keep documentation versions and evaluation splits separate.

    25 min readLabQuiz

  2. Record a RAG baselineRead

    Save a fixed RAG run before changing it. Compare retrieved passages and answer checks while keeping failures and missing responses visible.

    28 min readLabQuiz

Module 3 · 3 lessons · about 1 hourDocument ingestion

Read documentation into passages, retain source locations and report files that cannot be ingested.

  1. Load developer documentationRead

    Turn pinned local documentation files into text records. Reject changed inputs and keep each document’s identity, source link and version.

    20 min readLabQuiz

  2. Track passage sourcesRead

    Keep a passage tied to its exact source lines. Detect changed source files or altered passage text before displaying it.

    20 min readLabQuiz

  3. Handle ingestion failuresRead

    Continue loading valid documents when another source fails. Keep accepted text and failure reasons together, and mark incomplete batches.

    20 min readLabQuiz

Module 4 · 3 lessons · about 1 hourMaintain the corpus

Version indexed content, resume interrupted ingestion and apply document updates and deletions.

  1. Version documents and chunksRead

    Give document text, metadata and chunks their own revision identities. Compare a source-text edit with a metadata-only title change.

    25 min readLabQuiz

  2. Resume document ingestionRead

    Restart interrupted ingestion without skipping unfinished sources or duplicating completed chunks. Check stored content before trusting a checkpoint.

    25 min readLabQuiz

  3. Update and delete indexed contentRead

    Replace or remove document passages and their text-index entries together. Verify search results and roll back interrupted changes.

    25 min readLabQuiz

Module 5 · 3 lessons · about 1 hourRetrieval fundamentals

Compare keyword and vector matching, then enforce the same filters across both retrievers.

  1. How BM25 ranks documentsRead

    See how matching words and document lengths affect SQLite BM25 ranking. Inspect indexed terms and scores before interpreting a result.

    25 min readLabQuiz

  2. Add vector retrievalRead

    Search with saved text embeddings in a LangChain vector store. Inspect ranked passages and reject questions outside the recording.

    25 min readLabQuiz

  3. Apply filters across retrieversRead

    Apply the same documentation-version rule before choosing lexical and vector results. See why filtering after a result limit can lose useful passages.

    25 min readLabQuiz

Module 6 · 3 lessons · about 1 hourHybrid search and reranking

Combine retrieval results, rerank candidate passages and compare configurations on the same questions.

  1. Build hybrid retrievalRead

    Combine word-based and vector-search rankings without adding incompatible scores. Trace each passage’s contribution to the combined order.

    25 min readLabQuiz

  2. Rerank retrieved passagesRead

    Reorder retrieved passages with term coverage, Gemini and a cross-encoder. Compare their recorded rankings and the limits of each scorer.

    25 min readLabQuiz

  3. Compare retrieval configurationsRead

    Compare word-based, vector, hybrid and reranked search on the same questions and source targets. Inspect individual misses before averages.

    25 min readLabQuiz

Module 7 · 3 lessons · about 1 hourContext engineering

Choose passage boundaries, recover nearby text and fit selected context into a token budget.

  1. Choose chunk boundariesRead

    Compare character, paragraph and heading-based splits on one document. Check whether each chunk keeps an instruction with its qualifier.

    25 min readLabQuiz

  2. Retrieve surrounding contextRead

    Add nearby source text around selected passages, merge overlaps, and keep each window inside the same section and document revision.

    25 min readLabQuiz

  3. Assemble context within a token budgetRead

    Fit source windows, instructions and the question into a complete token-counted request. Reserve space for output and reject required-content overflow.

    25 min readLabQuiz

Module 8 · 2 lessons · about 50 minAnswer streaming and source conflicts

Stream answer text and handle passages that describe conflicting versions.

  1. Stream RAG answersRead

    Reconstruct complete replies from streamed data and check source references before accepting them. Compare saved replay with a recorded Gemini stream.

    25 min readLabQuiz

  2. Handle conflicting documentationRead

    Keep the requested documentation version, compare explicit source rules, and withhold an answer request when eligible claims conflict.

    25 min readLabQuiz

Module 9 · 5 lessons · about 2 hoursBounded retrieval policies

Expose search and read actions, bound repeated retrieval and compare scripted and model-selected decisions.

  1. Add search and read toolsRead

    Separate candidate search from reading source text. Bind each read to a result issued for the same request and document snapshot.

    25 min readLabQuiz

  2. Refine queries with a scripted policyRead

    Use a scripted policy to read a passage, refine its search query and decide when to stop. Keep each attempt and its stopping reason.

    25 min readLabQuiz

  3. Bound scripted retrieval runsRead

    Check search, read and source-byte allowances before running a scripted action. Distinguish denied calls from admitted calls that fail.

    25 min readLabQuiz

  4. Compare fixed, scripted and Gemini retrieval policiesRead

    Compare fixed, scripted and Gemini-selected retrieval on the same tasks. Check source reads separately from whether the controller finished.

    25 min readLabQuiz

  5. Build a model-driven RAG agentRead

    Let Gemini choose search, read, answer or stop actions. Enforce tool limits, check citations and compare the recorded results with fixed RAG.

    35 min readLab

Module 10 · 3 lessons · about 1 hourApplication integration

Serve retrieval over HTTP, inspect selected passages and expose the tools through MCP.

  1. Serve the RAG application with FastAPIRead

    Expose the retrieval core through FastAPI. Validate requests, return source traces and distinguish HTTP errors from limited retrieval runs.

    25 min readLabQuiz

  2. Build a retrieval inspectorRead

    Inspect retrieval decisions and source passages in a browser. Check response bytes and request identity before displaying the latest result.

    25 min readLabQuiz

  3. Connect retrieval tools through MCPRead

    Expose documentation search and reads through an MCP client and server. Discover tool schemas and keep issued passage handles in the same session.

    25 min readLabQuiz

Module 11 · 4 lessons · about 2 hoursRuntime controls

Authorize searches, cache results, trace requests and limit concurrent work.

  1. Authorize collection searchesRead

    Limit search and passage reads to collections the caller may access. Recheck identity and current permissions before returning source text.

    20 min readLabQuiz

  2. Cache retrieval resultsRead

    Reuse completed retrieval results only for matching callers, permissions and source snapshots. Expire old entries and skip failed or incomplete runs.

    20 min readLabQuiz

  3. Trace RAG requestsRead

    Follow each local retrieval execution through its operations. Separate concurrent attempts and keep questions, source text and credentials out of trace logs.

    25 min readLabQuiz

  4. Limit concurrent RAG requestsRead

    Limit active retrieval work and waiting requests. Inspect queue overflow, waiting deadlines and slot cleanup when a client cancels or times out.

    25 min readLabQuiz

Module 12 · 4 lessons · about 2 hoursEvaluation and local release checks

Evaluate corpus changes and retrieval tasks, then run local release checks and assemble the assistant.

  1. Run RAG evals on an updated corpusRead

    Compare retrieval before and after a document update. Reject stale target labels and keep deleted sources visible in the comparison.

    20 min readLabQuiz

  2. Evaluate scripted retrieval tasksRead

    Check whether fixed and scripted policies read their required passages. Separate execution checks, task outcomes and local release decisions.

    25 min readLabQuiz

  3. Release the RAG applicationRead

    Check a staged local retrieval service against its files, runtime behavior and task report. Preserve failed promotion and rollback results.

    25 min readLabQuiz

  4. Build the developer knowledge assistantRead

    Build a local documentation assistant with a browser UI and CLI. Connect version-aware search, collection permissions, source-linked answers and evaluation.

    35 min readLabQuiz