Builds agentic RAG systems with autonomous planning, reflection, and tool use. Useful for implementing agent-driven retrieval, query decomposition, iterative refinement, or multi-source RAG orchestration.
Ships in rag-architect. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Let agents autonomously plan, retrieve, reflect, and refine for complex queries.
Traditional RAG: Query → Retrieve → Generate (one-shot)
Agentic RAG adds:
┌─────────────────────────────────────────────────────────────┐
│ User Query │
└─────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Query Planner │
│ "Break this into retrievable sub-queries" │
└─────────────────────────────────────────────────────────────┘
│
┌───────────────┼───────────────┐
▼ ▼ ▼
┌──────────┐ ┌──────────┐ ┌──────────┐
│ SubQ 1 │ │ SubQ 2 │ │ SubQ 3 │
└──────────┘ └──────────┘ └──────────┘
│ │ │
▼ ▼ ▼
┌─────────────────────────────────────────────────────────────┐
│ Tool Router │
│ "Which source for each sub-query?" │
└─────────────────────────────────────────────────────────────┘
│ │ │
▼ ▼ ▼
┌──────────┐ ┌──────────┐ ┌──────────┐
│ VectorDB │ │ Web API │ │ SQL DB │
└──────────┘ └──────────┘ └──────────┘
│ │ │
└───────────────┼───────────────┘
▼
┌─────────────────────────────────────────────────────────────┐
│ Reflection │
│ "Is this context sufficient to answer?" │
│ → No: Refine query, re-retrieve │
│ → Yes: Generate answer │
└─────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Response Generation │
└─────────────────────────────────────────────────────────────┘
Model IDs:
CHAT_MODELin these examples is a placeholder. Set it to a current chat model ID from your provider's models page (for exampleCHAT_MODEL = os.environ["CHAT_MODEL"]); model IDs are retired over time.
import json
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
DECOMPOSITION_PROMPT = """You are a query planner. Break down complex queries into simpler sub-queries.
Original query: {query}
Rules:
1. Each sub-query should be answerable independently
2. Sub-queries should cover all aspects of the original query
3. Return 2-5 sub-queries maximum
4. Return as JSON array
Example:
Query: "Compare Tesla and Ford's EV strategies and market performance"
Sub-queries: [
"What is Tesla's electric vehicle strategy?",
"What is Ford's electric vehicle strategy?",
"What is Tesla's market performance and stock price?",
"What is Ford's market performance and stock price?"
]
Sub-queries:"""
async def decompose_query(query: str, llm: ChatOpenAI) -> list[str]:
"""Break complex query into sub-queries."""
prompt = ChatPromptTemplate.from_template(DECOMPOSITION_PROMPT)
chain = prompt | llm
result = await chain.ainvoke({"query": query})
return json.loads(result.content)
In LangChain 1.x, create_agent replaces the older AgentExecutor / create_openai_tools_agent pair (those now live in the langchain-classic package). Each retrieval source becomes a tool; the tool docstring is what the model reads when it picks a source.
from langchain.agents import create_agent
from langchain.tools import tool
@tool
def company_docs(query: str) -> str:
"""Search internal company documentation. Use for policies, procedures, internal info."""
return format_docs(vector_store_company.similarity_search(query, k=5))
@tool
def product_catalog(query: str) -> str:
"""Search the product database. Use for product specs, pricing, availability."""
return sql_product_search(query) # parameterized, read-only queries only
@tool
def web_search(query: str) -> str:
"""Search the web. Use for current events and external information."""
return tavily_search(query)
@tool
def knowledge_graph(query: str) -> str:
"""Query the knowledge graph. Use for relationships between entities."""
return graph_query(query)
agent = create_agent(
model=ChatOpenAI(model=CHAT_MODEL), # or a "provider:model" string
tools=[company_docs, product_catalog, web_search, knowledge_graph],
system_prompt=(
"Answer using the tools. Cite the source of every fact. "
"If no tool returns relevant information, say so instead of guessing."
),
)
result = agent.invoke({"messages": [
{"role": "user", "content": "What's our return policy and how does it compare to Amazon's?"}
]})
answer = result["messages"][-1].content
# Expected routing: company_docs for the policy, web_search for the comparison
Give tools narrow, read-only capabilities. Do not expose a general eval() "calculator" or raw SQL execution: the model's tool input is untrusted text, and retrieved documents can carry prompt injection.
from langgraph.graph import StateGraph, END
from typing import TypedDict, Annotated
import operator
class AgentState(TypedDict):
query: str
sub_queries: list[str]
retrieved_docs: Annotated[list, operator.add]
reflection: str
answer: str
iteration: int
def plan_node(state: AgentState) -> AgentState:
"""Decompose query into sub-queries."""
sub_queries = decompose_query(state["query"])
return {"sub_queries": sub_queries}
def retrieve_node(state: AgentState) -> AgentState:
"""Retrieve docs for each sub-query."""
docs = []
for sq in state["sub_queries"]:
results = retriever.invoke(sq)
docs.extend(results)
return {"retrieved_docs": docs}
def reflect_node(state: AgentState) -> AgentState:
"""Evaluate if context is sufficient."""
prompt = f"""
Query: {state['query']}
Retrieved context: {state['retrieved_docs'][:5]}
Is this context sufficient to answer the query?
If not, what additional information is needed?
Return JSON: {{"sufficient": true/false, "missing": "..."}}
"""
reflection = llm.invoke(prompt)
return {"reflection": reflection.content}
def should_continue(state: AgentState) -> str:
"""Decide whether to refine or generate."""
reflection = json.loads(state["reflection"])
if reflection["sufficient"] or state["iteration"] >= 3:
return "generate"
return "refine"
def refine_node(state: AgentState) -> AgentState:
"""Refine query based on reflection."""
reflection = json.loads(state["reflection"])
refined = f"{state['query']} Specifically: {reflection['missing']}"
return {
"sub_queries": [refined],
"iteration": state["iteration"] + 1
}
def generate_node(state: AgentState) -> AgentState:
"""Generate final answer."""
context = "\n".join([d.page_content for d in state["retrieved_docs"]])
prompt = f"Context: {context}\n\nQuery: {state['query']}\n\nAnswer:"
answer = llm.invoke(prompt)
return {"answer": answer.content}
# Build graph
workflow = StateGraph(AgentState)
workflow.add_node("plan", plan_node)
workflow.add_node("retrieve", retrieve_node)
workflow.add_node("reflect", reflect_node)
workflow.add_node("refine", refine_node)
workflow.add_node("generate", generate_node)
workflow.set_entry_point("plan")
workflow.add_edge("plan", "retrieve")
workflow.add_edge("retrieve", "reflect")
workflow.add_conditional_edges("reflect", should_continue, {
"generate": "generate",
"refine": "refine"
})
workflow.add_edge("refine", "retrieve")
workflow.add_edge("generate", END)
app = workflow.compile()
class MultiSourceRAG:
"""Orchestrate retrieval across multiple sources."""
def __init__(self, sources: dict):
self.sources = sources # {"name": retriever}
self.router_llm = ChatOpenAI(model=CHAT_MODEL)
async def retrieve(self, query: str) -> dict:
# 1. Determine which sources to query
routing_prompt = f"""
Query: {query}
Available sources:
{list(self.sources.keys())}
Which sources should be queried? Return as JSON array.
"""
sources_to_query = json.loads(
(await self.router_llm.ainvoke(routing_prompt)).content
)
# 2. Query selected sources in parallel
tasks = []
for source_name in sources_to_query:
if source_name in self.sources:
tasks.append(self._query_source(source_name, query))
results = await asyncio.gather(*tasks)
# 3. Combine and deduplicate
combined = self._fuse_results(results)
return combined
async def _query_source(self, name: str, query: str):
retriever = self.sources[name]
docs = await retriever.ainvoke(query)
return {"source": name, "docs": docs}
def _fuse_results(self, results: list) -> list:
# Deduplicate and rank
seen = set()
fused = []
for r in results:
for doc in r["docs"]:
doc_hash = hash(doc.page_content[:100])
if doc_hash not in seen:
seen.add(doc_hash)
doc.metadata["source"] = r["source"]
fused.append(doc)
return fused
def self_reflective_rag(query: str, retriever, llm, max_iterations: int = 3):
"""RAG with self-reflection loop."""
for i in range(max_iterations):
# Retrieve
docs = retriever.invoke(query)
context = "\n".join([d.page_content for d in docs])
# Generate with reflection request
response = llm.invoke(f"""
Context: {context}
Query: {query}
Provide your answer, then reflect:
1. Does the context fully support your answer?
2. What information might be missing?
3. Confidence level (high/medium/low)?
Format:
ANSWER: ...
REFLECTION: ...
CONFIDENCE: ...
MISSING: ...
""")
# Parse reflection
if "CONFIDENCE: high" in response.content:
return extract_answer(response.content)
if "MISSING:" in response.content:
# Refine query with missing info
missing = extract_missing(response.content)
query = f"{query} Also include: {missing}"
return extract_answer(response.content)
Some questions (arithmetic, rewriting the user's own text, small talk) do not need retrieval. A cheap classification step skips the retriever for them. Use structured output instead of substring checks: "RETRIEVE" in text also matches "NO_RETRIEVE".
from typing import Literal
from pydantic import BaseModel, Field
class RetrievalDecision(BaseModel):
decision: Literal["retrieve", "no_retrieve"]
reason: str = Field(description="One sentence")
gate = ChatOpenAI(model=CHAT_MODEL, temperature=0).with_structured_output(RetrievalDecision)
def needs_retrieval(question: str) -> bool:
result = gate.invoke(
"Does answering this question require facts from our documents or current data? "
"Reasoning, math, or rewriting text the user supplied does not.\n\n"
f"Question: {question}"
)
return result.decision == "retrieve"
When in doubt, retrieve: a wrong no_retrieve produces an ungrounded answer, while a wrong retrieve only costs one retrieval call.
Check retrieval quality before generating. If the best result is weak, rephrase the query, retrieve again, deduplicate, and rerank.
def adaptive_retrieve(query: str, vectorstore, min_score: float, k: int = 5) -> list:
"""min_score is a threshold you calibrate on your own labeled queries."""
results = vectorstore.similarity_search_with_relevance_scores(query, k=k)
if not results or max(score for _, score in results) < min_score:
for alt in expand_query(query): # LLM-generated rephrasings
results.extend(vectorstore.similarity_search_with_relevance_scores(alt, k=k))
docs = deduplicate([doc for doc, _ in results])
return rerank(query, docs)[:k] # see hybrid-retrieval for rerankers
def expand_query(query: str) -> list[str]:
llm = ChatOpenAI(model=CHAT_MODEL)
response = llm.invoke(
f"Write 3 alternative phrasings of this search query, one per line:\n{query}"
)
return [line.strip() for line in response.content.splitlines() if line.strip()][:3]
Relevance scores are not comparable across embedding models or vector stores, so set min_score from your own data rather than reusing a number from elsewhere.
Whatever the pattern, the final step should number the retrieved passages and ask for citations, so a reader (or an evaluator) can check each claim:
def synthesize(question: str, docs: list) -> str:
numbered = "\n\n".join(
f"[{i}] ({d.metadata.get('source', 'unknown')}) {d.page_content}"
for i, d in enumerate(docs, 1)
)
prompt = (
"Answer the question using only the numbered sources. Cite them as [1], [2]. "
"If the sources do not contain the answer, say so.\n\n"
f"Question: {question}\n\nSources:\n{numbered}"
)
return ChatOpenAI(model=CHAT_MODEL).invoke(prompt).content
| Pattern | When to Use | Complexity |
|---|---|---|
| Retrieval gate | Mixed traffic where many questions need no documents | Low |
| Adaptive retrieval | Retrieval quality varies by query | Medium |
| Query decomposition | Multi-part or comparison questions | Medium |
| Tool-based routing | Several data sources | High |
| Reflection loop (LangGraph) | Research-style questions | High |
| Query Type | Standard RAG | Agentic RAG |
|---|---|---|
| Simple factual | ✅ | Overkill |
| Multi-part questions | ⚠️ | ✅ |
| Comparison queries | ❌ | ✅ |
| Research tasks | ❌ | ✅ |
| Multi-source needs | ❌ | ✅ |
Use agentic patterns selectively for complex queries, not for everything.
create_agent, @tool): https://docs.langchain.com/oss/python/langchain/agentsAgentExecutor moved to langchain-classic): https://docs.langchain.com/oss/python/migrate/langchain-v1What SKILL.md sets; muted values are Claude Code's defaults
Other plugins' skills for the same job
/rag-architect:agentic-ragfrom rag-architect
npx claudepluginhub latestaiagents/agent-skills --plugin rag-architect7 skills in this plugin
/chunking-strategiesOptimize document chunking for RAG performance and retrieval quality.
Use this skill when splitting documents, choosing chunk sizes, implementing
semantic chunking, or improving RAG retrieval accuracy.
Activate when: chunking, split documents, chunk size, text splitting,
document processing, RAG performance, semantic chunking, overlap.
/corrective-ragImplement Corrective RAG (CRAG) and Self-RAG for reliable, self-healing retrieval systems.
Use this skill when building reliable RAG, preventing hallucinations, implementing
retrieval evaluation, or adding self-correction to RAG pipelines.
Activate when: corrective RAG, CRAG, self-RAG, hallucination prevention,
retrieval evaluation, RAG reliability, self-healing RAG, document grading.
/graphrag-patternsImplement GraphRAG combining knowledge graphs with RAG for multi-hop reasoning.
Use this skill when building knowledge graph RAG, implementing multi-hop queries,
using Neo4j with RAG, or connecting entities across documents.
Activate when: GraphRAG, knowledge graph, multi-hop reasoning, Neo4j RAG,
entity extraction, relationship queries, graph database, connected data.
/hybrid-retrievalImplement hybrid search combining vector and keyword retrieval for RAG systems.
Use this skill when building RAG retrieval, combining semantic search with BM25,
implementing reciprocal rank fusion (RRF), or optimizing retrieval accuracy.
Activate when: vector search, keyword search, BM25, semantic search, hybrid RAG,
retrieval optimization, search relevance, reranking.
/production-rag-checklistDeploy RAG systems to production with proper architecture, monitoring, and operations.
Use this skill when deploying RAG to production, scaling retrieval systems,
implementing RAG monitoring, or building enterprise RAG infrastructure.
Activate when: production RAG, RAG deployment, enterprise RAG, RAG architecture,
RAG monitoring, RAG scaling, RAG infrastructure, RAG ops.
/rag-evaluationTest and benchmark RAG systems with proper metrics and evaluation frameworks.
Use this skill when evaluating RAG quality, measuring retrieval performance,
using RAGAS metrics, or building RAG test suites.
Activate when: RAG evaluation, RAG testing, RAGAS, retrieval metrics,
faithfulness, relevance, context precision, RAG benchmarking.
3 plugins in one repository ship this exact skill · first indexed Nov 5, 2026