Skip to main content

60 Intermediate Exercises

#ProblemExpected outcomeHintRelated chapters
061Add BM25 to a dense retriever and merge candidates.Hybrid candidate set with deterministic fusion.Keep source ranks before fusion.101–102
062Implement reciprocal-rank fusion for two ranked lists.Stable fused ranking with tunable constant.Sum reciprocal rank contributions.101
063Create a hybrid-search eval split by exact-ID vs semantic queries.Evidence of where each retriever wins.Segment metrics by query class.101–102, 109
064Add a second-stage reranker to top-50 candidates.Reranked top-k plus latency measurement.Retrieve broadly, rerank narrowly.103
065Determine whether reranking 20, 50, or 100 candidates is best.Quality/latency/cost curve.Benchmark rather than guess.103, 110
066Build a query rewriter that preserves quoted IDs and dates.Rewritten semantic text without losing constraints.Keep original alongside rewrite.104
067Implement multi-query retrieval with deduplication.Merged candidates and capped expansion.Track source query per hit.104
068Compare HyDE to original-query retrieval on 50 cases.Measured gain/regression by query class.HyDE can drift.104, 109
069Implement parent-child retrieval for policy sections.Small child indexed, larger parent returned.Separate retrieval and context granularity.105
070Route HR vs product-doc queries to separate indexes.Typed router with fallback.Hard ACL before model routing.106
071Add temporal filtering for effective policies.No expired/future policy retrieval.Use trusted timestamps.106
072Compress retrieved context to a token budget.Important evidence retained with traceability.Never lose source mapping.107
073Detect and remove overlapping duplicate chunks.Less context duplication without losing evidence.Compare source adjacency/text similarity.107
074Model conflicting_sources as a terminal RAG state.Answer does not invent resolution.Source authority can be deterministic.108
075Build recall@5, recall@10, and MRR calculator.Correct retrieval metrics from judgments.Evaluate retriever alone.109
076Build citation-validity grader.Every cited ID exists in supplied context.Deterministic grader.098, 110
077Design a groundedness rubric.Claims must be supported by cited evidence.Separate correctness from support.097, 110
078Run a RAG regression after changing chunk size.Before/after retrieval + generation metrics.Keep model/prompt constant.090–091, 110
079Install modern LangChain packages and call a chat model.Current provider integration works.Keep provider adapter isolated.111–113
080Convert raw prompt concatenation to structured messages.Roles/data boundaries remain explicit.Avoid one giant string.113
081Create a LangChain prompt template for support classification.Typed runtime variables and versioned template.Test rendered prompt.114
082Compose transform → model → parser as a runnable pipeline.Independently testable stages.Deterministic transforms can remain functions.115–116
083Decide whether a fixed extraction flow needs an agent.Justified “no” with simpler chain.Known sequence → deterministic.116, 130
084Add Zod structured output to a LangChain model call.Valid typed domain object.Runtime parse still matters.117
085Wrap an existing order service as a LangChain tool.Tool adapter does not duplicate domain logic.Domain service stays framework-free.118
086Add auth/tenant checks around that tool.Model cannot choose security context.Inject request context server-side.118–119
087Simulate an invalid tool argument and recover.Structured tool error/retry decision.Classify before retry.119, 129
088Build a minimal createAgent with one read tool.Current agent API runs.Pin current docs/packages.120
089Add model-routing middleware.Request selects model from deterministic policy/context.Middleware should remain observable.121
090Add retry/fallback middleware for transient model failure.Bounded fallback with trace evidence.Preserve capability contract.121, 129
091Write a custom LangChain retriever using hybrid search.Framework interface over product retrieval logic.Don’t reduce policy to vector top-k.122
092Swap pgvector adapter for Qdrant behind one interface.App behavior unchanged except provider tuning.Keep provider features in adapter.123
093Ingest Markdown with a document loader and source metadata.Traceable documents, not bare strings.Loader is only first stage.124
094Add recursive splitting and compare to heading-aware splitting.Eval-driven chunk choice.Same retrieval dataset.125
095Batch embeddings through a LangChain integration.Versioned embedding output and bounded retries.Store model/dimension metadata.126
096Normalize LangChain stream events to app events.UI contract independent from framework.Map only stable needed semantics.127
097Add trace IDs across model/retriever/tool calls.One reconstructable request trajectory.Avoid secrets in spans.128
098Add provider timeout and retry classification.No infinite retries.Framework does not remove network failures.129
099Rewrite a simple LangChain flow with plain SDK code.Compare complexity and dependencies.Use same evals.130
100Decide LangChain vs LangGraph for an approval workflow.LangGraph chosen for state/interrupt durability.Requirement drives layer.130, 149–155
101Define a modern LangGraph StateSchema with request/result.Zod-backed typed state.Prefer current StateSchema baseline.133–134
102Add a reducer for message/counter accumulation.Parallel updates merge predictably.Reducer semantics matter.135
103Implement retrieve and answer graph nodes.Small testable responsibilities.Inject services.136
104Add START/END edges and invoke the graph.Valid terminal result.Test terminal state.137–142
105Add conditional edge for needsRetrieval.Deterministic branch from state.Keep policy separate from model.139
106Return Command({update,goto}) from a node.State update plus dynamic route.Document dynamic edges.140
107Stream graph progress to an SSE adapter.Stable phase events.Translate framework events.143
108Add max model/tool iteration counters.Loop terminates safely.Budget is state/policy.144
109Fan out two independent retrieval nodes.Parallel results merge correctly.Define reducer/join semantics.145
110Add a checkpointer and thread ID.State persists between calls.Thread is not authorization.146–147
111Simulate process restart after a checkpoint.Run resumes from durable state.Use persistent production backend concept.146–148
112Add an approval interrupt().Caller receives JSON-serializable review payload.Persist before wait.149
113Resume the approval with Command({resume:true}).Same thread continues.Validate external input.150
114Place a side effect before interrupt() and demonstrate replay risk.Understand duplicate execution hazard.Resumed node restarts.151
115Refactor that side effect to be idempotent after approval.Safe replay.Prepare before interrupt; write after.151–152
116Route provider 429 vs auth denial differently.Retry transient; terminate policy denial.Error taxonomy.153
117Extract retrieval into a subgraph.Narrow typed subgraph contract.Avoid sharing all parent state.154
118Add cancellation/deadline to a long-running graph.Terminal cancelled/expired state.Persist deadline.155
119Version graph state and resume an old checkpoint in test.Compatible migration path demonstrated.Persisted state outlives deploys.148, 155
120Build a support graph combining retrieval, tool, validation, and approval.Complete typed path with safe termination.Use least agentic control needed.131–155