Agent Decision Flow + RAG Loop

The complete execution path showing how agents process queries, make decisions, call tools, manage state, and iterate through Agentic RAG reflection cycles

🎯
User Query Received
Agent receives input from user (question, task, or command)
↓
πŸ”„
Initialize State
Create state object with query, system instructions, empty message history, and tool definitions
πŸ“‹ State Contents: Query text, system prompt, messages[], tool_calls[], intermediate_results[], current_step
↓
🧠
Check Memory (Optional)
Retrieve relevant context from short-term (session) or long-term (cross-session) memory if applicable
↓
πŸ“
Prepare Messages
Construct message array: system prompt + memory context + conversation history + current query
↓
πŸ€–
Call LLM
Send messages + available tools to language model for reasoning and response generation
↓
πŸ”
Parse LLM Response
Extract text response, function calls (if any), and any structured outputs
↓
❓
Tools Needed?
Did LLM request function calls?
βœ… YES - TOOL EXECUTION PATH
πŸ› οΈ
Extract Tool Calls
Parse function name + arguments from LLM response
↓
βœ…
Validate Arguments
Check required fields, types, formats using schemas
↓
⚑
Execute Tool
Call API, query database, run search, or perform computation
↓
πŸ“Š
Process Results
Format tool output for next LLM call
↓
πŸ”„
Update State
Add tool results to message history, increment step counter
↓
πŸ”
More Tools?
Need additional tools or final response?
If more tools needed, loop back to "Call LLM"
Otherwise, proceed to final response
β›” NO - DIRECT RESPONSE PATH
πŸ“„
Extract Text Response
LLM provided complete answer without needing tools
↓
✨
Validate Output
Check for structured output compliance (if Pydantic schema provided)
↓
πŸ’Ύ
Update Memory
Store conversation in short-term memory for session continuity
↓
βœ…
Return Final Response
Send answer back to user
↓
πŸŽ‰
Task Complete
Agent returns final synthesized response to user
πŸ”„ Agentic RAG Reflection & Retry Loop
When does this activate? When using retrieval tools (vector search, web search, database queries), Agentic RAG adds an evaluation→reflection→retry cycle to improve result quality.
1
πŸ”
Retrieve
Execute search query, get initial results from vector DB or web
2
πŸ€–
Generate
LLM creates answer based on retrieved context
3
βš–οΈ
Evaluate
Judge quality: Is answer complete? Are sources relevant?
4
πŸ”
Decide
Good enough? Return. Needs work? Reflect & retry
⟲ If quality insufficient, loop back with improved query ⟲
Key Difference from Traditional RAG: Traditional RAG is "retrieve once, generate once." Agentic RAG actively evaluates output quality, reflects on what's missing, reformulates queries, and retries until satisfactoryβ€”like a researcher iterating on a literature review.

⚠️ Error Handling & Resilience

πŸ”Œ
API Failure
Retry with backoff, use fallback tool, or inform user gracefully
❌
Validation Error
Request corrected output from LLM with error details
⏱️
Timeout
Cancel long-running tool, return partial results or error message
πŸ”’
Auth Failure
Prompt user to reconnect or use alternative data source

πŸ“– Visual Legend

Start/Entry Point
User query begins execution
Process Step
Standard operation or transformation
Decision Point
Conditional logic determines path
Action/Tool Call
External system interaction
End/Completion
Final response returned to user
Error State
Exception handling or recovery