Sub topic 3
Knowing When to Stop
Assume you know Python, prompts, embeddings, vector search, and classic retrieve-then-generate RAG. This chapter focuses on controlling an agentic RAG system after retrieval becomes a model-selected tool.
Knowing when to stop
An agent should stop when it has enough relevant evidence to answer, or when further searching is unlikely to help. Set explicit limits:
- A maximum number of tool-call rounds.
- A maximum number of retrieved passages.
- A time or token budget.
- A failure result when evidence remains insufficient.
A final answer should state what the sources support and what they do not support. Never let the model search forever.
Keeping the context under control
Each tool result adds tokens to the model’s context. Long, repeated passages increase cost and may distract the model. Keep only the most relevant results, truncate passage text, and remove duplicates. After several searches, summarize findings into a compact evidence record instead of retaining every raw result.
def keep_context(chunks, limit=6, chars=1200):
seen = set()
kept = []
for chunk in chunks:
if chunk["id"] not in seen:
kept.append({"id": chunk["id"], "text": chunk["text"][:chars]})
seen.add(chunk["id"])
return kept[:limit]Grounding answers and citing sources
Grounding means basing an answer on retrieved evidence. Give every passage a stable identifier, such as a document and section ID. Instruct the model to cite those identifiers and to say when the evidence is missing. Citation presence alone is not proof of grounding; each citation must support the nearby claim.
Untrusted documents and prompt injection
Retrieved text is data, not instructions. A document might contain text such as “ignore the system prompt.” The agent must not obey it. Separate tool output from instructions, label it as untrusted, and allow the model to extract facts only. Validate tool arguments and restrict tools to authorized sources.
Evaluating retrieval, grounding and agent behavior
Evaluate these properties separately:
- Retrieval quality: are relevant passages returned?
- Grounding: do claims follow from those passages?
- Agent behavior: does it search, retry, route, and stop appropriately?
A good final answer can still hide poor retrieval if the model already knew the answer.
Cost, latency and when not to use an agent
Agentic RAG adds model calls and search latency. Use classic RAG when one stable query usually finds the needed evidence. Use an agent when questions need decomposition, source routing, retries, or selective retrieval. Keep budgets small and measure quality against cost.
Self-RAG and Corrective RAG in context
Self-RAG has the model decide when to retrieve and critique its evidence. Corrective RAG checks retrieved results and triggers another retrieval strategy when they are weak. Both are patterns implemented with the same loop: retrieve, inspect, revise, and stop.
Exercise
Why should an agent have both an evidence condition and a step budget?
Show answer
Evidence tells it when an answer is justified; the step budget prevents endless searching when evidence cannot be found.