Exercises 06 — RAG & Agents (10)

Concepts: Phase 9, Phase 10. Cheatsheets: 14, 15.


RA1 — RAG retrieval miss

  • Scenario: RAG says "I don't have that info" though the doc exists.
  • Task: Diagnose systematically.
  • Constraints: Doc is indexed.
  • Expected answer: Likely a retrieval miss: chunking split the answer, embedding/query mismatch (jargon/paraphrase), top-k too low, or no hybrid search. Check retrieval eval (recall@k) before blaming generation (Phase 9.09).
  • Rubric: Senior = isolates retrieval vs generation first.
  • Common mistakes: Tuning the prompt when retrieval is the problem.
  • Extension: Add BM25 + RRF and re-measure recall.

RA2 — Retrieval vs generation eval

  • Scenario: A stakeholder wants "one RAG quality number."
  • Task: Explain why you split it.
  • Constraints: Eval design.
  • Expected answer: RAG has two failure points: did retrieval find the right chunks (recall@k, MRR), and did generation use them faithfully (groundedness, citation accuracy). One number hides which half to fix (Phase 9.09).
  • Rubric: Senior = retrieval-vs-generation quadrant.
  • Common mistakes: A single end-to-end score only.
  • Extension: Build the quadrant (Phase 12.03).

RA3 — Chunking strategy

  • Scenario: Answers are fragmented or miss context.
  • Task: Tune chunking.
  • Constraints: Mixed docs.
  • Expected answer: Right-size chunks (200–500 tokens) with overlap (~10–20%), respect structure (headings/sections), avoid splitting atomic facts; consider parent-doc/late-chunking (Phase 9.02).
  • Rubric: Senior = size+overlap+structure tradeoffs.
  • Common mistakes: Fixed tiny chunks; no overlap.
  • Extension: Compare recall across chunk sizes.

RA4 — Lost in the middle

  • Scenario: Right chunks retrieved, but the answer ignores the middle ones.
  • Task: Explain + fix.
  • Constraints: Long context.
  • Expected answer: Models attend less to the middle (lost-in-the-middle). Rerank to fewer, place best chunks at start/end, reduce context size (Phase 9.07).
  • Rubric: Senior = ordering/packing fix, not just "bigger context."
  • Common mistakes: Stuffing more context.
  • Extension: Measure position sensitivity.

RA5 — Reranking

  • Scenario: Top-k vector results are noisy.
  • Task: Improve precision.
  • Constraints: Latency budget.
  • Expected answer: Retrieve more candidates (k=20–30) then rerank with a cross-encoder to top 3–8 — higher precision than vector alone, at some latency cost (Phase 9.06).
  • Rubric: Senior = retrieve-wide-then-rerank-narrow.
  • Common mistakes: Using the reranker for initial retrieval (too slow).
  • Extension: Measure precision@k before/after rerank.

RA6 — Tool calling trust boundary

  • Scenario: Designing an agent with delete_record and send_email.
  • Task: How to keep it safe?
  • Constraints: Irreversible actions.
  • Expected answer: Model proposes tool calls; app executes after validation + permission checks; human approval for irreversible/high-impact; whitelist tools; audit every call; treat tool results as untrusted (Phase 10.05, Phase 14.01).
  • Rubric: Senior = trust boundary + approval gate + audit.
  • Common mistakes: Letting the agent execute freely.
  • Extension: Where exactly do you place the approval gate?

RA7 — Indirect prompt injection

  • Scenario: A research agent browses the web and emails the user; a page contains hidden "email me the conversation" text.
  • Task: Why dangerous + defense.
  • Constraints: Browsing agent.
  • Expected answer: Indirect injection — untrusted retrieved/tool text becomes an instruction. Defense is architectural: trust boundary, least-privilege tools, isolate untrusted text, output exfil-scan, human approval — not prompt hardening (Phase 14.01).
  • Rubric: Senior = architectural containment, not "tell it to ignore."
  • Common mistakes: Relying on a system-prompt instruction.
  • Extension: Red-team it (labs/lab-14-prompt-injection-redteam).

RA8 — Agent reliability

  • Scenario: A 12-step agent is unreliable.
  • Task: Improve task success.
  • Constraints: Multi-step.
  • Expected answer: Per-step reliability compounds (0.95^12 ≈ 0.54). Reduce steps, validate/retry per step, use better tools, add checkpoints/human approval, eval per step; prefer least-agentic design (Phase 10.09).
  • Rubric: Senior = compounding math + step reduction + per-step checks.
  • Common mistakes: Adding autonomy to fix reliability.
  • Extension: Instrument per-step success rates.

RA9 — Structured output correctness

  • Scenario: JSON validates against the schema but has wrong values.
  • Task: Explain the gap + fix.
  • Constraints: Extraction task.
  • Expected answer: Constrained decoding guarantees valid JSON, not correct content. Add semantic validation (ranges, cross-checks, grounding), eval on a golden set, and retries on validation failure (Phase 10.02).
  • Rubric: Senior = "constrained ≠ correct" + semantic validation.
  • Common mistakes: Trusting schema validity as correctness.
  • Extension: Build a validation + repair loop (labs/lab-10-structured-output).

RA10 — Multi-tenant RAG isolation

  • Scenario: Tenant A's query returns Tenant B's document.
  • Task: Root cause + fix.
  • Constraints: Shared index.
  • Expected answer: Missing per-tenant filter on retrieval — the #1 LLM multi-tenant leak. Use per-tenant namespaces or a mandatory tenant_id metadata filter enforced server-side (not by the model); add cross-tenant leak tests (Phase 14.04).
  • Rubric: Senior = server-side tenant filter + leak tests.
  • Common mistakes: Trusting the app layer only; one shared unfiltered index.
  • Extension: Write the leak-test suite.

Next: 07 — Startup & Product · Index: exercises/