Skip to main content
Agentic RAG introduces iterative decision-making into the retrieval pipeline. Instead of a single retrieve-then-generate pass, the system evaluates its candidate answer and decides whether to retrieve more evidence, reflect on the answer, or finalize — repeating until quality is acceptable or the iteration limit is reached.

How it works

The pipeline uses AgenticRouter (from components/agentic_router.py) as the central decision-maker. The router is a ChatGroq LLM that returns structured JSON decisions at each step.

Decision loop

Router decisions

The AgenticRouter.route() method receives:
  • query: the original user question
  • has_documents: whether documents have already been retrieved
  • current_answer: the current draft answer (if any)
  • iteration: current iteration number (1-indexed)
  • max_iterations: the hard loop limit
It returns {"action": "search|reflect|generate", "reasoning": "..."}. When iteration >= max_iterations, the router automatically returns "generate" regardless of quality — this is the safety mechanism that prevents infinite loops.

State machine

Called when no documents have been retrieved yet, or when reflection identified information gaps requiring additional retrieval.

Pipeline implementation

The agentic RAG pipeline orchestrates the router, retriever, and generator:
src/vectordb/langchain/agentic_rag/base.py

Configuration

When to use it

  • Complex multi-hop questions where the answer requires combining information from multiple retrieval passes
  • High-stakes applications where answer completeness and grounding must be verified before delivery
  • Workflows where single-pass retrieval and generation consistently underperforms on hard questions

When not to use it

  • Strict low-latency endpoints where each LLM call (routing + reflection) adds hundreds of milliseconds
  • Simple factual lookups where one-shot retrieval consistently finds the answer

Tradeoffs

Settings to tune first

integer
default:"3"
Start with 2; increase only if quality does not converge at lower values.Without a hard loop cap, the router can cycle indefinitely. Always set a finite limit.
float
default:"0.0"
Use 0.0 for routing (deterministic decisions); 0.7 for answer generation (creative).
string
default:"llama-3.3-70b-versatile"
The routing template directly determines how well the LLM navigates the state machine.Recommended: llama-3.3-70b-versatile or gpt-4

Router prompt template

The AgenticRouter.ROUTING_TEMPLATE is a structured prompt that includes the current query, has_documents flag, current_answer, and iteration count. The LLM must return a JSON object with "action" and "reasoning" keys.
Invalid JSON or unrecognized action values raise ValueError for fast debugging.

Example execution trace

Common pitfalls

No hard loop cap: Without max_iterations, the loop can cycle indefinitely. Always set a finite limit.
Ambiguous routing prompts: If the prompt does not clearly define when to choose "search" vs "reflect" vs "generate", the router oscillates without making progress.
Missing observability: Log every routing decision, action, and reasoning string. The AgenticRouter logs at INFO level for decisions and DEBUG level for full prompts.

Backends supported

Chroma, Milvus, Pinecone, Qdrant, Weaviate.

Next steps

Query enhancement

Use query enhancement for lighter-weight recall improvements without the full agentic loop

Reranking

Use reranking for faster single-pass pipelines sufficient for simpler queries

Components

Explore the AgenticRouter and QueryEnhancer components in detail

Semantic search

Start with baseline semantic search before adding agentic patterns