# Agentic RAG: plan, search in hops, stop when the evidence is enough

Some answers live in two documents that do not mention each other. An agent can search, read, and search again with what it learned, which a single retrieval cannot do.

<div data-sim="ragx" data-v="agentic"><p>Interactive simulation. It needs JavaScript; the text below explains what it shows.</p></div>

## What you are looking at

Step 1 is the plan, steps 2 and 3 are tool calls with their observations, step 4 is the stop decision. The second hop's query depends on the first hop's result. All RAG simulations share one knowledge base of 18 chunks from 11 documents of a fictional outdoor-gear shop, and three questions: one phrased differently from the documents, one that needs two documents, and one about the whole corpus.

## Things to try

- Run the fabric question: the first hop finds the fabric name, the second finds who makes it.
- Run the torn-tent question: two policies, two hops.
- Think about budgets: every hop costs a model call.

## How it works in real systems

LangGraph, the OpenAI Agents SDK and LlamaIndex agents implement agentic RAG with retrieval as a tool. Production agents cap hops and tokens and log every step for evaluation.

## Code lab

The same idea in real code, step by step. Copy each block into a notebook or a file and run it in order.

<div class="cl-start"></div>

### Step 1 · Index the knowledge base

Install the libraries and index a small knowledge base with a sentence-embedding model. Normalised embeddings make the dot product equal to cosine similarity.

```bash
pip install sentence-transformers rank-bm25 numpy openai
```

```python
import numpy as np
from sentence_transformers import SentenceTransformer
chunks = [
    "Most items can be returned within 30 days of delivery for a full refund.",
    "Damaged or defective items qualify for a full refund or a free replacement; send a photo of the damage to support first.",
    "Refunds are issued within five business days after the returned item reaches our warehouse.",
    "Parcels damaged in transit must be reported to the carrier within 48 hours of delivery.",
    "The Ridge 2 tent weighs 1.9 kg and uses Aurora ripstop fabric for its fly sheet.",
    "Aurora ripstop fabric is woven by Lumen Textiles at its mill in Porto, Portugal.",
    "Customer complaints in Q3 were mostly about late deliveries during the September peak.",
]
emb = SentenceTransformer("all-MiniLM-L6-v2")                 # 384 dimensions, small and fast
E = emb.encode(chunks, normalize_embeddings=True)
def dense_search(q, k=3):
    s = E @ emb.encode([q], normalize_embeddings=True)[0]
    top = np.argsort(-s)[:k]
    return [(int(i), float(s[i])) for i in top]
```

### Step 2 · Give the agent a search tool

```python
# pip install langgraph langchain-openai
from langchain_core.tools import tool
from langchain_openai import ChatOpenAI
from langgraph.prebuilt import create_react_agent

@tool
def search_kb(query: str) -> str:
    """Search the shop's knowledge base. Returns the three best passages."""
    return "\n".join(f"- {chunks[i]}" for i, _ in dense_search(query, 3))

agent = create_react_agent(ChatOpenAI(model="gpt-4o-mini", temperature=0), [search_kb],
    prompt="Answer questions about the shop. Search as many times as you need; when one passage mentions a name you need to look up, search for that name. Cite what you found. Stop when you can answer.")
```

### Step 3 · Watch the hops

```python
q = "Who makes the fabric used in the Ridge 2 tent, and where?"
for step in agent.stream({"messages": [("user", q)]}, {"recursion_limit": 12}, stream_mode="values"):
    m = step["messages"][-1]
    print(type(m).__name__, getattr(m, "tool_calls", None) or m.content[:200])
```

The first search finds "Aurora ripstop fabric"; the agent then searches for that name and finds the supplier. `recursion_limit` caps the number of steps so a confused agent cannot loop forever.

<div class="cl-end"></div>

## More simulations

[← Self-RAG](/ai-simulations/rag-self) · [All AI Simulations](/ai-simulations) · [GraphRAG →](/ai-simulations/graphrag)
