Press enter or click to view image in full size
If you’ve been following the AI space, you’ve definitely heard of RAG (Retrieval-Augmented Generation). The idea is simple: since an LLM has a knowledge cutoff and a limited context window, you should fetch relevant documents from a database and “stuff” them into the prompt before the AI answers.
Traditional RAG (often called “2-Step RAG”) follows a rigid pipeline:
- Search the database for keywords in the user’s query.
- Pass everything found to the LLM to generate an answer.
But there’s a problem. What if the user asks: “How do I add two numbers in Python?” A standard RAG pipeline will still waste time searching your internal company runbooks for “Python math,” potentially retrieving irrelevant data and confusing the model.
Today, we are building Agentic RAG — giving the AI the power to decide if, when, and how to retrieve information.
1. The Mock Knowledge Base
In a production environment, you would use a Vector Database like Pinecone or ChromaDB. For this example, we’ll use a simple dictionary to simulate our company’s internal “Runbooks.”
MOCK_VECTOR_DB = {
"payment service": "RUNBOOK: To restart the payment service, use `kubectl rollout restart deployment/payment-v2`.",
"kafka lag": "RUNBOOK: Kafka lag usually means consumers are under-provisioned. Scale them up.",
}2. Defining the Retrieval Tool
We wrap our database search in a LangChain @tool. The critical part is the docstring. It tells the AI exactly what is inside this database so it can decide if it needs to look there.
@tool
def search_internal_runbooks(query: str):
"""
Searches internal DevOps Knowledge Base for runbooks.
Use this only when you need specific company-specific procedures.
"""
# Logic to search MOCK_VECTOR_DB...
return results3. The Power of “Toggling” RAG
When we create our agent and give it the
When we create our agent and give it the search_internal_runbooks tool, something magical happens. The agent becomes a “Thinking Gatekeeper.”
agent = create_agent(
model=model,
tools=[search_internal_runbooks],
)Scenario A: The Internal Query
- User: “How do I restart the payment service?”
- Agent’s Thought: “I don’t know the specific command for this user’s company ‘payment service.’ Better check the internal runbooks.”
- Action: Calls the tool, gets the
kubectlcommand, and provides a precise answer.
Scenario B: The General Query
- User: “Write a Python function to add two numbers.”
- Agent’s Thought: “I already know Python perfectly well. Searching the DevOps runbooks for this would be a waste of time.”
- Action: Skips the tool entirely and writes the code directly.
Why Agentic RAG is the Future
- Cost & Performance: You only pay for search and retrieval when it’s actually needed.
- Higher Accuracy: By not “stuffing” irrelevant documents into the prompt for general questions, you reduce the chance of the model getting distracted or hallucinating based on bad search results.
- Complex Reasoning: An agent can search once, realize the information is missing a specific detail, and then search again with a different query. A traditional pipeline can’t do that.
Conclusion: Selective Intelligence
Agentic RAG represents a shift from “Passive Retrieval” to “Active Reasoning.” Your AI is no longer just a box that receives data; it’s an assistant that knows the limits of its own knowledge. It knows when to rely on its training and when to reach for a book.
This marks the end of our deep dive into LangChain 1.2 agents. We’ve covered everything from Unified Interfaces to Dynamic Security and now Agentic RAG.
💬 What do you think?
Drop your thoughts, questions, or suggestions in the comments below!
Check out my YouTube channel for more exciting content! [YouTube Channel Link — Harsha Selvi]
Disclaimer: This text has been rephrased using AI tools, and some parts are derived from various sources to provide a comprehensive overview.
#AI #LangChain #RAG #MachineLearning #Python #AIEngineering #Search #NLP #DevelopmentTips














