Agentic RAG, multi-step reasoning, tools, planning (2026)
Klasyczny RAG ma jedną prostą strukturę: query → retrieve → generate. Działa dla wielu aplikacji, ale nie dla wszystkich. Niektóre queries wymagają multi-step reasoning („najpierw znajdź obecne stawki podatkowe, potem oblicz hipotetyczne nowe stawki, porównaj z konkurencją”), tool use (calculations, API calls), planning (decompose complex query into sub-queries, execute, combine).
Spis treści
Klasyczny RAG ma jedną prostą strukturę: query → retrieve → generate. Działa dla wielu aplikacji, ale nie dla wszystkich. Niektóre queries wymagają multi-step reasoning („najpierw znajdź obecne stawki podatkowe, potem oblicz hipotetyczne nowe stawki, porównaj z konkurencją”), tool use (calculations, API calls), planning (decompose complex query into sub-queries, execute, combine).
Agentic RAG to architektura gdzie LLM dostaje agency, zdolność do podejmowania decyzji o tym, jakie kroki podjąć, jakich tools użyć, kiedy szukać więcej informacji, kiedy odpowiedzieć. To znacznie potężniejszy paradygmat, ale też znacznie bardziej complex i kosztowny.
Ten tekst opisuje agentic RAG architecture, frameworks, production considerations dla 2026.
Czym jest agentic RAG
Standard RAG: retrieval i generation w fixed sequence.
Agentic RAG: LLM jest agentem decydującym o flow:
- Czy potrzebuje retrieval? (czasem nie, proste pytania faktyczne).
- Jakiego query użyć dla retrieval? (może transform original query).
- Jakich tools użyć (calculator, web search, database queries)?
- Czy retrieval results są wystarczające?
- Czy potrzebuje multi-step reasoning?
- Kiedy ostatecznie odpowiedzieć?
LLM iteruje (loop), aż uzna, że ma sufficient information do answer.
Differences vs standard RAG
| Aspect | Standard RAG | Agentic RAG |
|---|---|---|
| Flow | Fixed (1 pass) | Dynamic (multi-pass) |
| Decision making | None | LLM decides next steps |
| Tools | Just retrieval | Multiple tools available |
| Latency | Predictable | Variable (depends na complexity) |
| Cost | Predictable | Variable, often higher |
| Quality | Limited do simple queries | Handles complex queries |
| Implementation complexity | Lower | Higher |
Patterns agentic RAG
Pattern 1: query decomposition.
Complex query: “Jak nasze ceny porównują się do konkurencji w segmencie premium na rynku CEE?”
Decomposition: 1. “Jakie są nasze obecne ceny w segmencie premium?” → RAG na firmowe price lists. 2. “Kim są nasi konkurenci w segmencie premium na rynku CEE?” → RAG na competitive intelligence. 3. “Jakie są ceny tych konkurentów?” → web search lub external API. 4. Aggregation i comparison: combine results.
Pattern 2: iterative retrieval.
User: “Co powinienem zrobić w sprawie X?”
Step 1: retrieve relevant policies. LLM evaluates: insufficient context. Step 2: retrieve case studies. LLM evaluates: missing technical details. Step 3: retrieve technical documentation. LLM: now sufficient. Step 4: synthesize answer.
Pattern 3: tool use.
User: “Ile ZUS-u zapłaci pracownik z wynagrodzeniem 8500 zł brutto?”
LLM: 1. Recognizes need dla calculation. 2. Calls calculator tool z formula. 3. Returns result with explanation.
Pattern 4: ReAct (Reasoning + Acting).
LLM thinks step-by-step, takes actions (retrieval, tool calls), observes results, continues thinking.
Question: User's question
Thought: I need to find X to answer this.
Action: search("X")
Observation: Found relevant info Y.
Thought: Now I need Z to complete the answer.
Action: search("Z")
Observation: Found Z info.
Thought: I have enough information now.
Final answer: ...
ReAct paradigm popular dla agentic systems.
Frameworki dla agentic RAG
LangGraph: framework LangChain do budowy agentów jako grafu stanów.
from langgraph.graph import Graph, END
from langchain_openai import ChatOpenAI
from langchain.tools import Tool
llm = ChatOpenAI(model="gpt-4o")
def should_retrieve(state):
# LLM decides if retrieval needed
decision = llm.invoke(f"Need retrieval for: {state['query']}? yes/no")
return "retrieve" if "yes" in decision.content.lower() else "answer"
def retrieve_node(state):
# Perform RAG retrieval
results = vector_db.search(state['query'])
state['context'] = results
return state
def answer_node(state):
# Generate final answer
response = llm.invoke(f"Question: {state['query']}\nContext: {state.get('context', '')}\nAnswer:")
state['answer'] = response
return state
# Build graph
graph = Graph()
graph.add_node("decide", should_retrieve)
graph.add_node("retrieve", retrieve_node)
graph.add_node("answer", answer_node)
graph.add_edge("retrieve", "answer")
graph.add_conditional_edges("decide", lambda x: x, {"retrieve": "retrieve", "answer": "answer"})
graph.add_edge("answer", END)
graph.set_entry_point("decide")
app = graph.compile()
result = app.invoke({"query": "..."})
LangChain Agents: lighter-weight wrapper. Less powerful niż LangGraph.
LlamaIndex Agents: alternative, particularly strong dla data agents.
AutoGen (Microsoft): dla multi-agent systems.
CrewAI: simplifies multi-agent workflows.
OpenAI Agents SDK: vendor-specific, simpler ale lock-in.
Tools dla agentic systems
Co tools może mieć agent:
Internal tools:
- RAG retrieval z różnych collections;
- SQL queries do databases;
- Calculators;
- Internal API calls (CRM, ERP);
- Custom Python functions.
External tools:
- Web search (Tavily, Serper, Perplexity API);
- News APIs;
- Weather, financial data;
- Translation services;
- Image generation (DALL-E, Stable Diffusion).
Function calling:
Aktualne modele (GPT, Claude, Gemini) obsługują strukturalne wywoływanie funkcji. Definicja:
tools = [
{
"type": "function",
"function": {
"name": "search_knowledge_base",
"description": "Search firmowe knowledge base for documents",
"parameters": {
"type": "object",
"properties": {
"query": {"type": "string"},
"category": {"type": "string", "enum": ["HR", "Finance", "Tech"]}
},
"required": ["query"]
}
}
},
{
"type": "function",
"function": {
"name": "calculate",
"description": "Perform mathematical calculation",
"parameters": {
"type": "object",
"properties": {
"expression": {"type": "string"}
}
}
}
}
]
response = llm.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": user_query}],
tools=tools
)
# LLM returns tool call
if response.choices[0].message.tool_calls:
tool_call = response.choices[0].message.tool_calls[0]
# Execute tool
result = execute_tool(tool_call.function.name, tool_call.function.arguments)
# Pass result back to LLM
# ...
Wyzwania na produkcji
Challenge 1: latency unpredictable.
Agentic systems mogą wymagać 1-10 LLM calls per user query. Każdy = 1-3 sekundy. Total: 1-30 sekund.
User experience design considerations:
- Streaming intermediate results;
- Progress indicators;
- Async processing dla longer-running tasks.
Challenge 2: cost variability.
Standardowy RAG: koszt zapytania przewidywalny, zależny od modelu i liczby fragmentów w kontekście. Agentowy RAG: koszt zmienny i zwykle kilkukrotnie wyższy, bo jedno pytanie to kilka wywołań modelu.
Cost monitoring:
- Per-user budgets;
- Query complexity analysis;
- Caching dla common patterns.
Challenge 3: failure modes.
Agent może:
- Loop infinitely (call tools repeatedly bez progress);
- Misinterpret tool results;
- Fail tool execution gracefully;
- Make irreversible decisions (jeśli writes do databases).
Mitigations:
- Max iterations limits;
- Tool execution sandboxing;
- Human-in-the-loop dla high-stakes decisions;
- Comprehensive logging.
Challenge 4: evaluation harder.
Standard RAG: clear metrics (faithfulness, recall). Agentic RAG: trajectory matters (czy agent wybrał right path?), final answer matters, efficiency matters.
Evaluation frameworks:
- LangSmith (LangChain) dla trajectory tracing;
- Custom evaluation pipelines;
- Agent benchmarks (AgentBench, ToolBench).
Challenge 5: debugging complex.
Why did agent do X? Multi-step traces require tooling.
Best practice:
- Structured logging każdego step;
- LangSmith / similar dla visual tracing;
- Replay system (re-run agent w determinist mode).
Zastosowania agentowego RAG
Use case 1: research assistant.
User: “Przygotuj briefing na temat regulacji NIS2 dla naszej branży”.
Agent: 1. Search firmowe documents (czy mamy notes na NIS2?); 2. Web search dla recent news (jakie aktualizacje?); 3. RAG na regulatory database (treść dyrektywy); 4. Compose briefing.
Use case 2: complex customer support.
User: “Moje zamówienie 12345 nie dotarło, chcę zwrotu, a do tego dwa razy nie zadziałał mi kod źródłowy”.
Agent: 1. Tool: lookup order 12345 status; 2. Tool: check return policy applicability; 3. Tool: lookup customer’s recent support tickets; 4. RAG: find solutions dla code issues; 5. Tool: initiate return; 6. Compose comprehensive response.
Use case 3: data analysis assistant.
User: “Jak wyglądały nasze sprzedaże ostatniego kwartału w porównaniu do analogicznego okresu zeszłego roku?”
Agent: 1. Tool: query sales database dla Q2 2026; 2. Tool: query sales database dla Q1 2025; 3. Tool: calculate percentage change; 4. RAG: find context (czy były niezwykłe zdarzenia?); 5. Compose analytical response z visualizations.
Use case 4: code debugging assistant.
User: “My function isn’t working as expected, here’s the code: …”.
Agent: 1. Tool: parse code, identify language and structure; 2. RAG: find similar known issues w internal codebase; 3. Tool: run code w sandbox z test cases; 4. Analyze errors; 5. Compose explanation z fix suggestions.
Multi-agent systems
Bardziej zaawansowany pattern: multiple agents specialized per task, orchestrated.
Example dla legal contract analysis:
- Agent 1: contract parser (identify clauses, parties, terms);
- Agent 2: risk analyzer (identify risky clauses);
- Agent 3: comparison agent (compare z standard templates);
- Agent 4: summary writer (compose final report).
Orchestrator agent decyduje które sub-agents call kiedy.
Frameworks: AutoGen, CrewAI, LangGraph z multi-agent patterns.
Plusy:
- Each agent specialized (better quality);
- Parallel execution possible;
- Easier debugging (clear responsibilities).
Minusy:
- Higher complexity;
- More LLM calls (higher cost);
- Coordination overhead.
Trendy 2026
Trend 1: agentic adoption accelerates.
Trend 2: better function calling. Każdy major LLM provider improved function calling. Reliability i quality drastically better.
Trend 3: agent benchmarks emerge. AgentBench, GAIA, others. Better understanding agent capabilities.
Trend 4: agent observability tooling. LangSmith, Arize, Phoenix, tooling specifically dla agent monitoring.
Trend 5: hybrid agent + RAG architectures dominate. Pure RAG dla simple queries. Agent dla complex.
Kiedy agentic warto, kiedy nie
Warto agentic:
- Złożone queries wymagające multi-step reasoning;
- Tool use crucial (calculations, external data);
- Variable workflows (różne queries → różne flows);
- Quality > speed/cost.
Nie warto:
- Simple FAQ aplikacja;
- Real-time requirements (latency critical);
- Cost-sensitive applications;
- Stable, predictable workflows.
Default: zacznij od standard RAG. Add agentic capabilities tylko gdy clearly needed.
Najczęstsze błędy
Błąd 1: agentic dla simple use cases. Overengineering. Standard RAG by wystarczył.
Błąd 2: brak max iteration limits. Agent może loop infinitely. Cost explosion.
Błąd 3: niewystarczające tool descriptions. LLM mistakes which tool to use. Dokładne descriptions kluczowe.
Błąd 4: brak human-in-the-loop dla high-stakes. Agent decyduje o money transfers, contracts, etc. bez human review.
Błąd 5: ignorowanie costs. Agent produkcyjny potrafi kosztować za jedno pytanie wielokrotnie więcej niż zwykły RAG. Bez monitoring, bill shock.
Błąd 6: brak error handling. Tool failures should be handled gracefully. Without, agent crashes.
Bibliografia
- Yao, S., et al. (2023). ReAct: Synergizing Reasoning and Acting in Language Models. ICLR 2023. https://arxiv.org/abs/2210.03629
- Schick, T., et al. (2023). Toolformer: Language Models Can Teach Themselves to Use Tools. NeurIPS 2023. https://arxiv.org/abs/2302.04761
- LangChain. (2024). LangGraph Documentation. https://langchain-ai.github.io/langgraph/
- Microsoft. (2024). AutoGen Documentation. https://microsoft.github.io/autogen/
- OpenAI. (2024). Function Calling Documentation. https://platform.openai.com/docs/guides/function-calling
- Liu, X., et al. (2024). AgentBench: Evaluating LLMs as Agents. ICLR 2024. https://arxiv.org/abs/2308.03688
- Mialon, G., et al. (2024). GAIA: A benchmark for General AI Assistants. https://arxiv.org/abs/2311.12983
Agentic RAG to przyszłość enterprise LLM aplikacji. W newsletterze RAGPolska.pl informujemy o nowych artykułach, narzędziach i publikacjach. Zapisz się, bezpłatnie.