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

Reflection Agent - Creating Chains

Compose generator and reflector chains with explicit contracts so reflection remains testable and stable.

Core Theory

Before building the full graph, you define two reusable chains: one for generation and one for reflection. Clean chain boundaries make reflection logic debuggable.

Generation chain contract:

  • Input: user objective + context + constraints.
  • Output: structured draft object (answer, rationale, citations).

Reflection chain contract:

  • Input: draft + rubric + optional source evidence.
  • Output: structured critique (score, issues, actionable revision hints).

Why separate chains first: you can unit-test each chain independently before graph orchestration. This reduces debugging surface once loops are introduced.

Key implementation detail: keep outputs strongly typed. Graph routing should read numeric score/flags, not parse narrative critique text.

Failure modes: incompatible schemas between chains, reflector feedback too vague to drive revision, or generator ignoring critique instructions.

Mitigation: enforce schema validation and add revision-specific prompt slots (for example list of failing rubric checks).

Deepening Notes

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

  • in this section we'll be going ahead and building a very basic reflection agent using L graph and this is going to be the first section where we're actually going to be touching on
  • e we're actually going to be touching on L graph so I'm pretty excited we also have a condition right here okay should continue or not so basically in this should continue node we
  • this file I've imported two things chat prom template messages placeholder if you're familiar with Lang chain then you should probably know what these things mean if you do not kno
  • weet and if at all the other system the other agent provides a critique it needs to respond with a revised version of your previous attempts and that is exactly what we're specifyi
  • this particular area so that is what this placeholder is for and we have the name for that as well which is called messages all right so let's also go ahead and create the reflect

Interview-Ready Deepening

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

  • Compose generator and reflector chains with explicit contracts so reflection remains testable and stable.
  • Failure modes: incompatible schemas between chains, reflector feedback too vague to drive revision, or generator ignoring critique instructions.
  • Reflection chain contract: Input: draft + rubric + optional source evidence.
  • Graph routing should read numeric score/flags, not parse narrative critique text.
  • Mitigation: enforce schema validation and add revision-specific prompt slots (for example list of failing rubric checks).
  • Immutable images improve reproducibility, but frequent rebuilds increase CI cost without layer optimization.
  • Container isolation improves dependency safety, but operational complexity grows around networking and storage.
  • Pinning versions stabilizes releases, but can delay security upgrades if dependency refresh cycles are weak.

Tradeoffs You Should Be Able to Explain

  • Immutable images improve reproducibility, but frequent rebuilds increase CI cost without layer optimization.
  • Container isolation improves dependency safety, but operational complexity grows around networking and storage.
  • Pinning versions stabilizes releases, but can delay security upgrades if dependency refresh cycles are weak.

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

Chain-contract example: 1) draft_chain returns structured answer + citations. 2) review_chain returns numeric score + issue list + revision instructions. 3) Router reads score directly and decides revise/finalize. Because contracts are typed, each chain can be unit-tested independently before graph assembly.

🧠 Beginner-Friendly Examples

Guided Starter Example

Chain-contract example: 1) draft_chain returns structured answer + citations. 2) review_chain returns numeric score + issue list + revision instructions. 3) Router reads score directly and decides revise/finalize. Because contracts are typed, each chain can be unit-tested independently before graph assembly.

Source-grounded Practical Scenario

Compose generator and reflector chains with explicit contracts so reflection remains testable and stable.

Source-grounded Practical Scenario

Failure modes: incompatible schemas between chains, reflector feedback too vague to drive revision, or generator ignoring critique instructions.

🧭 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 Reflection Agent - Creating Chains.
  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

Reflection and generation chains used inside the loop.

content/github_code/langgraph/2_basic_reflection_system/chains.py

Prompt + model chains for generation and critique.

Open highlighted code β†’
  1. Compare role of generation chain vs reflection chain.

🎯 Interview Prep

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

  • Q1[beginner] Why build generator and reflector as separate chains before graph assembly?
    Separate draft and review chains so each can be tested independently before introducing loop complexity. This isolates contract bugs early and reduces integration debugging.
  • Q2[beginner] What output schema is required for reliable reflection routing?
    Routing-safe reviewer output should include typed fields like score, issue list, and revision hints. Avoid route logic that parses narrative text.
  • Q3[intermediate] How do you ensure reflector feedback is actionable for revision?
    Make feedback actionable by requiring issue categories and concrete revision instructions aligned to the generator prompt slots.
  • Q4[expert] What unit tests should exist before wiring these chains into a loop?
    Pre-graph tests should validate schema compatibility, expected scoring behavior on known bad drafts, and deterministic issue extraction.
  • Q5[expert] How would you explain this in a production interview with tradeoffs?
    High-quality design keeps contracts explicit: typed outputs, schema validation, and revision hooks that the generator can directly consume.
πŸ† 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...