
Recursive Retrieval: The Thinking Agent
Iterate to clarity. Learn how to build 'Recursive' retrieval loops where the AI agent queries the graph, evaluates the results, and performs follow-up queries until the information gap is closed.
Recursive Retrieval: The Thinking Agent
Most RAG systems are "One-Shot": User asks, system retrieves, AI answers. But what if the first query doesn't find the answer? A standard system says "I don't know." A Recursive Agent says: "I found Sudeep, but I don't see his project. Let me search for the 'Teams' he belongs to instead." This is the difference between a Script and an Agent.
In this lesson, we will look at Recursive Retrieval. We will learn how to build a logic loop where the LLM evaluates its "Information State" and decides whether to go deeper into the graph. We will understand the "Gap Analysis" pattern and how to handle the risk of "Runaway Recursive Loops" that consume thousands of tokens.
1. The Multi-Step Agent Workflow
- Initial Query: "Who is Sudeep?" -> Result: (Sudeep:Person).
- Gap Assessment: "I have the person, but I don't know his projects."
- Recursive Step: "Query:
MATCH (p:Person {name: 'Sudeep'})-[:WORKS_ON]->(proj) RETURN proj." - Answer Synthesis: "Sudeep works on Project Alpha."
2. Managing the Information Gap
An agent needs a "Goal."
- Goal: "Find the email of the person who approved the budget."
- Current State: "I found the budget, but it doesn't list an approver."
- Action: "Find the 'Manager' of the department that owns the budget."
By explicitly defining the Information Gap, the AI can determine exactly what its next "Hop" should be.
3. The "Loop Stop" Rule
Recursive retrieval can be expensive. You must implement Termination Criteria:
- Depth Limit: Max 3 follow-up queries.
- Redundancy check: If the second query returns the same IDs as the first, stop.
- Ambiguity Score: If the search finds 50 different "Sudeeps," the agent should stop and ask for clarification instead of recursing on all 50.
graph TD
User -->|Question| A[Agent]
A -->|Query 1| DB[(Graph)]
DB -->|Result| A
A -->|Is answer here?| Decision{Yes/No}
Decision -->|Yes| Finish[Final Answer]
Decision -->|No| R[Refine Query]
R -->|Query 2| DB
style Decision fill:#f4b400,color:#fff
style Finish fill:#34A853,color:#fff
4. Implementation: A Recursive Retrieval Prompt
YOU ARE AN INVESTIGATIVE AGENT.
PREVIOUS FINDINGS:
{history}
MISSING INFORMATION:
We have the Project, but we do not have the 'Deadline'.
TASK:
Write a Cypher query to find the 'Deadline' property for this project.
QUERY:
5. Summary and Exercises
Recursive retrieval turns the AI from a "Searcher" into a "Problem Solver."
- Gap Analysis drives the "Next Step" logic.
- Multi-step loops allow for error recovery and depth.
- Termination rules are critical for cost control.
- Clarification is a valid recursive step (Ask the human, not the graph).
Exercises
- Loop Logic: What happens if the agent asks the same question in Step 1 and Step 2? How do you record "Reasoning History" to prevent this?
- The "Clarification" Threshold: At what point should the AI stop searching the graph and ask the user for help? (e.g., after 2 empty searches or after finding 100 possible matches?).
- Visualization: Draw a 3-step loop for finding "The ingredients in a Big Mac." Step 1: Find Big Mac. Step 2: Find Components. Step 3: Find Ingredients for each component.
In the next lesson, we will look at focus: Constraint Injection in Multi-Step Inference.