Skip to content
Concept-Lab
← LangGraphπŸ•ΈοΈ 15 / 42
LangGraph

Reflexion Agent - Building Graph

Wire responder, tool execution, and reviser into a bounded iterative graph with clear termination rules.

Core Theory

This step assembles Reflexion into a production-safe graph loop. You connect responder, tool execution, and reviser with explicit routing logic and bounded iteration policy.

Reference flow:

  1. START -> responder (draft + critique + search intents)
  2. responder -> tool execution (gather evidence)
  3. tool execution -> reviser (produce grounded revision)
  4. reviser -> conditional route:
    • quality high enough -> END
    • quality improving and attempts remaining -> responder/reviser loop
    • attempt cap or plateau reached -> fallback END

State fields to track: attempt_count, quality_score_history, unresolved_issues, tool_cost, termination_reason.

Why this matters for beginners: it shows how to convert "self-improving" behavior into deterministic software with explicit stop rules.

Production must-haves: hard loop ceiling, plateau detection, tool budget limits, and trace logging for each pass.

Deepening Notes

Source-backed reinforcement: these points are extracted from the LangGraph source note to sharpen architecture and flow intuition.

  • executor node we have the reviser node if you remember we just need to say graph. add node and the first argument is going to be the name of the node and then the second is going t
  • what we're doing is in the event glop again pretty much every node is going to get the list of all the history messages right human AI tool message human AI whatever the entire th
  • m basically calculating how many tool messages are there in this particular list list okay so that is exactly what I'm doing I'm looping through it and I'm calculating the sum of i
  • he the speed in that case in that case we can just go for the reflexion graph all right if the max if the number of tool messages are more than two it means that we have to end the
  • han two it means that we have to end the graph or else we can go for the execute tools again perfect so that is exactly what I've done here so if it is greater than this thing we a

Interview-Ready Deepening

Source-backed reinforcement: these points add detail beyond short-duration UI hints and emphasize production tradeoffs.

  • Wire responder, tool execution, and reviser into a bounded iterative graph with clear termination rules.
  • State fields to track: attempt_count, quality_score_history, unresolved_issues, tool_cost, termination_reason.
  • This step assembles Reflexion into a production-safe graph loop.
  • Production must-haves: hard loop ceiling, plateau detection, tool budget limits, and trace logging for each pass.
  • More agent autonomy increases adaptability but also increases non-determinism and debugging effort.
  • Tool-heavy loops improve grounding, but latency and failure surfaces rise with each external dependency.
  • Fine-grained state graphs improve control, but poor state contracts can create brittle routing behavior.
  • Why this matters for beginners: it shows how to convert "self-improving" behavior into deterministic software with explicit stop rules.

Tradeoffs You Should Be Able to Explain

  • More agent autonomy increases adaptability but also increases non-determinism and debugging effort.
  • Tool-heavy loops improve grounding, but latency and failure surfaces rise with each external dependency.
  • Fine-grained state graphs improve control, but poor state contracts can create brittle routing behavior.

First-time learner note: Think in state transitions, not giant prompts. Keep node responsibilities small and route logic deterministic so each step is easy to reason about.

Production note: Bound autonomy with loop limits, tool policies, and checkpoints. Capture route decisions and state snapshots for replay and incident analysis.

🧾 Comprehensive Coverage

Exhaustive coverage points to ensure complete topic understanding without missing core concepts.

Loading interactive module...

πŸ’‘ Concrete Example

Reflexion graph run: 1) User asks for market summary. 2) Responder drafts answer and requests latest metrics. 3) Tool node retrieves earnings snippets. 4) Reviser improves draft and raises score from 0.55 -> 0.78. 5) Second loop adds missing citation, score becomes 0.86. 6) Router finalizes because threshold (0.85) is met. If score had stalled at 0.80 after cap, graph would exit via fallback with "manual review recommended."

🧠 Beginner-Friendly Examples

Guided Starter Example

Reflexion graph run: 1) User asks for market summary. 2) Responder drafts answer and requests latest metrics. 3) Tool node retrieves earnings snippets. 4) Reviser improves draft and raises score from 0.55 -> 0.78. 5) Second loop adds missing citation, score becomes 0.86. 6) Router finalizes because threshold (0.85) is met. If score had stalled at 0.80 after cap, graph would exit via fallback with "manual review recommended."

Source-grounded Practical Scenario

Wire responder, tool execution, and reviser into a bounded iterative graph with clear termination rules.

Source-grounded Practical Scenario

State fields to track: attempt_count, quality_score_history, unresolved_issues, tool_cost, termination_reason.

🧭 Architecture Flow

Loading interactive module...

🎬 Interactive Visualization

Loading interactive module...

πŸ›  Interactive Tool

Loading interactive module...

πŸ§ͺ Interactive Sessions

  1. Concept Drill: Manipulate key parameters and observe behavior shifts for Reflexion Agent - Building Graph.
  2. Failure Mode Lab: Trigger an edge case and explain remediation decisions.
  3. Architecture Reorder Exercise: Reorder 5 flow steps into the correct production sequence.

πŸ’» Code Walkthrough

Full Reflexion graph wiring with bounded iteration.

content/github_code/langgraph/4_reflexion_agent_system/reflexion_graph.py

Draft -> execute_tools -> revise loop with max iteration guard.

Open highlighted code β†’
  1. Confirm event_loop termination condition is explicit and bounded.

🎯 Interview Prep

Questions an interviewer is likely to ask about this topic. Think through your answer before reading the senior angle.

  • Q1[beginner] What are mandatory nodes in a Reflexion graph?
    Strong answer structure: define the concept in one sentence, ground it in a concrete scenario (Wire responder, tool execution, and reviser into a bounded iterative graph with clear termination rules.), then explain one tradeoff (More agent autonomy increases adaptability but also increases non-determinism and debugging effort.) and how you'd monitor it in production.
  • Q2[intermediate] How do you define stop conditions?
    Strong answer structure: define the concept in one sentence, ground it in a concrete scenario (Wire responder, tool execution, and reviser into a bounded iterative graph with clear termination rules.), then explain one tradeoff (More agent autonomy increases adaptability but also increases non-determinism and debugging effort.) and how you'd monitor it in production.
  • Q3[expert] How do you cap compute cost in iterative graphs?
    Strong answer structure: define the concept in one sentence, ground it in a concrete scenario (Wire responder, tool execution, and reviser into a bounded iterative graph with clear termination rules.), then explain one tradeoff (More agent autonomy increases adaptability but also increases non-determinism and debugging effort.) and how you'd monitor it in production.
  • Q4[expert] How would you explain this in a production interview with tradeoffs?
    A safe Reflexion graph is bounded and observable.
πŸ† Senior answer angle β€” click to reveal
Use the tier progression: beginner correctness -> intermediate tradeoffs -> expert production constraints and incident readiness.

πŸ“š Revision Flash Cards

Test yourself before moving on. Flip each card to check your understanding β€” great for quick revision before an interview.

Loading interactive module...