-
Alex writes a draft.
-
The editor critiques it: gaps, errors, tone.
-
Alex revises the draft using that critique.
-
The editor either accepts the revision or asks for another iteration.
Humans improve by reflecting:
-
“Did I answer correctly?”
-
“How can I improve this?”
-
“Where did I go wrong?”
AI can do the same.
That’s the idea behind reflection agents — systems where an AI:
-
Generates a draft
-
Critiques its own answer
-
Improves based on feedback
-
Repeats until quality is acceptable
Reflection is the foundation behind advanced agent systems like:
-
Self-Refine
-
Reflexion (Xu et al.)
-
ReAct + Reflection
-
Evaluator-based refinement
-
Graph structured multi-step reasoning (LangGraph)
Reflection improves AI performance dramatically — often by 20–70% on complex reasoning tasks.
What does “reflection” mean in AI?
A reflection agent is simply:
- An AI system that improves its answer through structured self-critique.
- It does not give the final output immediately. Instead, it goes through a loop:
LLMs are powerful pattern predictors but not perfect reasoners. Common problems:
-
Hallucinations (made-up facts)
-
Incorrect calculations
- Hallucinations
-
Incorrect calculations
-
Weak reasoning
-
Poor structure
-
Missing context
-
Biases
Missing context or steps
Tendency to be overly verbose or under-informative
- Writing assistants
- Code review agents
- Research agents
- Multi-tool LLM agents
- Long-form reasoning (math, logic, planning)
Reflection mitigates these by explicitly checking and improving outputs.
How LLM reasoning evolved (quick timeline)
-
Zero-shot: Give a task and rely on model knowledge. Fast but brittle.
-
Few-shot: Provide examples to shape output; improves style and format.
-
Chain-of-Thought (CoT): Ask the model to show intermediate steps to improve reasoning.
-
Self-Critique / Self-Refine: Model critiques its own answers, then rewrites.
-
Reflexion (research concept): Agents track prior mistakes and learn from them.
-
Graph Reflection: Deterministic, node-based loops (LangGraph) for controlled, repeatable reflection.
-
CoT — “think step-by-step” to reveal reasoning. Useful for math and logic.
-
Self-Critique — “what's wrong with this answer?” prompts the model to find faults.
-
ReAct — blends reasoning with actions (tool calls); reflection can be a special action.
-
Reflexion — models that keep a memory of past errors and improve over sessions.
-
Graph Reflection — explicit nodes and edges implement critique/improve loops with deterministic control.
LangChain Reflection Agent (Linear, prompt-driven)
LangChain is excellent for connecting prompts, tools, and memory into chains. For reflection we’ll implement a straightforward draft → critique → improve loop.
When to use LangChain reflection
-
You want a simple, understandable pipeline.
-
Your task fits into a few reflection passes.
-
You prefer prompt-driven, model-controlled iteration.
High-level architecture
Key tradeoffs
-
Pros: Easy to implement; minimal infra; fast to prototype.
-
Cons: Hard to debug loop logic; state lives in prompts; less deterministic control.
LangChain Reflection — Example
Project layout (langchain_version):
How to run:
-
You can iterate the loop multiple times by feeding the
improvedanswer back into the critic for another pass. -
Keep prompts short and explicit. Too vague critic prompts produce weak critiques.
-
For critical systems, add deterministic checks (e.g., unit tests on model output).
LangGraph Reflection Agent
LangGraph is designed for graph-based workflows. Reflection maps naturally to a graph: nodes for draft/critic/improve/evaluate and edges for flow control (including loops).
Why LangGraph for reflection?
-
Deterministic flow control — edges with conditions decide next node.
-
Stateful — shared
stateobject persists across nodes. -
Debuggable — visualize graph and trace execution.
-
Robust — supports retries, failure handling, and parallel nodes.
When to use LangGraph
-
Multi-pass reflection with exit conditions.
-
Multi-tool agents (call calculators, search, databases).
-
Production systems needing observability and recovery.
High-level LangGraph reflection flow
Full LangGraph project (with Streamlit UI)
Project layout (langgraph_version):
requirements.txt:
.env with keys for OpenAI and Anthropic.state.py — typed state:nodes/draft.py:
Streamlit UI (LangGraph only)
- We include a simple Streamlit interface so readers can interactively run reflection loops.
Run UI:
Running locally & deployment notes
Install (conda/venv)::
Environment (.env)::
Costs & rate limits
Reflection loops multiply LLM calls: 1 draft + 1 critique + 1 improve = 3 model calls per user query.
Use low-temperature deterministic models for critique & evaluation.
Consider cheaper embedding or smaller models for critic steps if quality acceptable.
Deploying UI
Streamlit Cloud or Render are easy for deploying the LangGraph streamlit app.
For production, wrap the graph in a serverless function or container with monitoring and API auth.
Design decisions & practical tips
-
Termination Heuristic: Evaluate node checks iteration count and critique emptiness. Production systems use semantic quality checks (BLEU/ROUGE-like, factuality checks, or human-review signals).
-
Tool Integration: Add nodes that call calculators, retrieval systems, or external validators — they write into
stateand shape flow. -
Safety: Rate-limit loops and add validation nodes to prevent runaway API costs.
-
Observability: Log node inputs/outputs. LangGraph makes it easy to trace execution.
🧩 LangChain vs LangGraph (Summary Table)
-
Automated factuality checks: run a fact-checker node (external search) and reject outputs failing checks.
-
Adaptive iteration: dynamic stop conditions based on scoring model outputs.
-
Human-in-the-loop: send flagged outputs to reviewers and use their feedback as new training examples.
-
Persistent learning: store critiques and successes to fine-tune future prompts or retrain small models.
🧩when to use what
-
Start with LangChain if:
-
You're prototyping fast.
-
You need to test prompt strategies (CoT, self-critique).
-
You prefer simple code and quick iteration.
-
-
Move to LangGraph when:
-
You need deterministic loops, observability, retries, and multi-tool orchestration.
-
You're building production agents that must be auditable.
-
You want to visualize and debug the reasoning steps.
🔮 Future of Reflection Agents
- Reflection agents are the future of AI reliability.
- As models become more capable, the challenge becomes controlling, evaluating, and refining their outputs.
- LangGraph’s deterministic loops are the next evolution.
🚀 ALL AI / LangChain Post
🚀 ALL AI / LangChain Post
