Agentic RAG: When the Model Decides What to Retrieve

Posted on Fri 03 December 2027 in GenAI

The Question Classic RAG Can't Answer

Our RAG API handles simple questions well. But try this one:

"Can I return the headphones from order 1234? I bought them about 40 days ago."

Classic RAG turns that into one search, retrieves a few policy chunks, and answers. It never looks up the order, so it can't know the delivery date. A human would work differently: check the policy, check the order, then combine both. That "decide what to look up, then check if it's enough" behavior is agentic RAG.

What Makes RAG "Agentic"

Classic RAG is a fixed pipeline: retrieve once, then answer. In agentic RAG, retrieval becomes a tool the model can call, like in the function calling post. The model decides:

  • Whether to retrieve at all
  • What to search for
  • Which source to use (documents, database, API)
  • Whether the results are enough, or it should rephrase and try again
  • When to stop

In short: classic RAG goes question → retrieve → answer, while agentic RAG goes question → think → retrieve → check → (not enough?) retrieve again → answer. The pipeline becomes a loop.

A Worked Example

Here's how a good agent handles the headphones question:

  1. Search the documents for the return policy. Finding: returns are accepted within 30 days of delivery.
  2. Look up order 1234. Finding: it was delivered 25 days ago.
  3. Answer: "Yes. It was delivered 25 days ago, and the 30-day window starts at delivery," with a citation to the policy.

The agent noticed that the window runs from delivery, not purchase, and went to a second source to check. A single-pass pipeline would likely have missed it.

How to Build It (In Plain Words)

You already have the pieces from earlier posts: the retrieval step from the RAG API post, and the tool loop from the framework-free post. Putting them together takes four steps:

  1. Turn retrieval into a tool. Wrap your search function so the model can call it with a query. When it finds nothing, return a helpful note like "nothing found, try rephrasing," so the model knows what to do next.
  2. Describe each tool clearly. The model picks tools based only on their descriptions. Say when to use each one, and tell it to make one focused search per call and to search several times for multi-part questions.
  3. Write a short retrieval policy in the system prompt. Break multi-part questions into separate searches, rephrase and retry when results look weak (with a limit), stop once there's enough information, cite sources, say "I don't know" when nothing is found, and treat tool results as data, not instructions.
  4. Run the loop with safeguards. Let the model call tools and feed the results back until it answers, but set a hard step limit and block exact repeat calls, so it can't loop forever.

For production, also add an allowlist of tools, validation of the model's arguments, error handling, and logging of every step so you can debug later.

The Trade-offs

Agentic RAG is not a free upgrade:

  • Slower and costlier. Every loop is another model call. One study found most of the extra latency comes from the model's reasoning steps, not from retrieval itself.
  • Less predictable. Some questions finish in one step, others take six, so watch p95 latency and cost, not just the average.
  • Harder to debug. You need traces of what the agent searched and why.
  • New failure modes: endless loops, repeated calls, context bloat, and trusting a weak chunk.
  • Bigger prompt injection risk, since retrieved text can now steer an agent that has tools.
  • It can't fix bad data. If parsing and chunking are poor, the agent just searches a broken index more cleverly.

When to Use It

Use it for multi-part or ambiguous questions, answers spread across several sources, or research-style tasks where first-pass retrieval often misses.

Skip it for simple FAQ lookups, strict latency or cost limits, and early projects where you don't yet know where retrieval fails.

A good middle path is to route by difficulty: try classic RAG first, and escalate to the agent only when the best match looks weak. Tune that "weak match" threshold on your own data rather than trusting a default.

Guardrails Before Production

  • Budgets: cap steps, tool calls, tokens, and time
  • Trim tool results so the prompt doesn't balloon
  • Read-only tools, with permissions enforced in your backend, not in the prompt
  • Treat retrieved text as untrusted
  • Log full traces, and test with unanswerable questions, where a good agent says "I don't know" instead of looping
  • Compare against classic RAG on the same questions, to confirm the extra cost buys better answers

Quick Checklist

  • Classic RAG is a pipeline, agentic RAG is a decision loop
  • Make retrieval a tool with clear descriptions
  • Cap steps, and block repeated calls
  • Keep tools read-only and permission-checked
  • Route easy questions to classic RAG
  • Log traces, and compare against the simple baseline
  • Fix data quality first

Closing Thought

Agentic RAG lets the model check its own work: search, notice a gap, and search again. That can turn a confidently wrong answer into a verified one. But it costs time and adds complexity, so use it where questions are genuinely complex, keep it on a short leash, and always measure it against the simpler pipeline. Sometimes the smartest decision is knowing a single search was enough.