← Master Index
Vol. 15 Module 15.1 Lecture

Reflection

Agent Fundamentals

How This Lesson Fits the Module & Volume

Reasoning chooses the next move; memory stores what happened. Reflection is a deliberate extra pass: critique a draft, a plan, or a tool trace, then revise. Volume 13 introduced reflection as a prompting technique. Here it becomes an agent pattern—Reflexion-style verbal feedback, evaluator–optimizer loops, and optional human critique.

LangGraph makes reflection a node with a cycle back to generate. Without halt rules, those cycles burn tokens. Pair reflection with the agent loop budgets from later lectures.

Learning Objectives

By the end of this lesson, students should be able to:

  • Define reflection as critique-then-revise over an artifact or trace.
  • Contrast self-reflection, tool-based evaluation, and human critique.
  • Implement a two-call evaluator–optimizer loop in Python.
  • Set max revision counts and stop when scores plateau.
  • Explain when reflection helps vs when it rationalizes errors.
  • Connect reflection traces to episodic memory (Module 15.2).
Definition

Reflection is an agent mechanism that inspects an intermediate result (answer, plan, code, or tool trace), produces a critique against explicit criteria, and uses that critique to revise or halt. It is not “think longer”—it is a separate evaluation step with a stop condition.

Three Sources of Critique

SourceHow it judgesBest forRisk
Self-reflectionSame (or sibling) model critiques its draftStyle, missing steps, obvious holesShared blind spots
Tool / test evalUnit tests, schema, search, calculatorCode, JSON, factual lookupEval harness gaps
Human critiqueReviewer edits or rejectsHigh-stakes, taste, policyLatency, cost

Evaluator–Optimizer Loop

Separate roles even inside a single-agent process: one prompt writes, another scores against a rubric. Store critiques in memory so the next attempt does not repeat the same mistake (the Reflexion idea).

import json from openai import OpenAI client = OpenAI() RUBRIC = """Score 1-5 on: (1) uses only provided facts, (2) actionable next step, (3) no invented policy. Return JSON {"score": int, "critique": str, "pass": bool}. pass=true iff score >= 4.""" def generate(goal: str, facts: str, last_critique: str | None) -> str: extra = f"\nAddress this critique:\n{last_critique}" if last_critique else "" r = client.chat.completions.create( model="gpt-4o-mini", messages=[{"role": "user", "content": f"Goal: {goal}\nFacts:\n{facts}{extra}\nWrite a short support reply."}], ) return r.choices[0].message.content def critique(draft: str, facts: str) -> dict: r = client.chat.completions.create( model="gpt-4o-mini", messages=[{ "role": "user", "content": f"{RUBRIC}\nFacts:\n{facts}\nDraft:\n{draft}", }], response_format={"type": "json_object"}, ) return json.loads(r.choices[0].message.content) facts = "Order ORD-1042 shipped Friday. Refunds: 5–7 days after approval. No SLA invented." goal = "Tell the customer when the refund arrives." critique_text = None for attempt in range(3): draft = generate(goal, facts, critique_text) verdict = critique(draft, facts) print(f"attempt {attempt} score={verdict['score']} pass={verdict['pass']}") if verdict["pass"]: print(draft) break critique_text = verdict["critique"] else: print("HALT: escalate to human")

When Reflection Pays

Usually worth it

  • Code that can run tests
  • Schema-valid JSON
  • Long plans with missed steps

Often skip

  • One-shot classification
  • Already-grounded FAQ
  • Hard latency SLAs

Must bound

  • Max 1–3 revisions
  • No score improvement → stop
  • Escalate, do not loop forever

Strengths

  • Catches missed constraints
  • Improves with external tests
  • Natural LangGraph cycle
  • Feeds episodic memory

Tradeoffs

  • 2×+ model calls
  • Self-critique can agree with errors
  • Vague rubrics → noise
  • Can delay users
Common Misconception

“Always add a reflection step; it can only help.” Reflection without a rubric, tests, or halt condition often just polishes a wrong answer and doubles cost. Prefer tool-based evaluation when possible; use self-critique as a supplement, not a substitute for ground truth.

Knowledge Check

  1. Short Answer: What are the two moves in reflection? Answer: Critique an artifact/trace, then revise or halt.
  2. True/False: Reflection is identical to making CoT longer in one completion. Answer: False—it is a separate evaluation step.
  3. Multiple Choice: The sketch stops early when: (a) GPU heats, (b) pass is true, (c) CSS loads. Answer: (b).
  4. Short Answer: Why separate generate vs critique prompts? Answer: Different roles/rubrics; reduces the writer grading its own homework as harshly.
  5. True/False: Unit tests are a form of tool-based reflection. Answer: True.
  6. Multiple Choice: Shared blind spots are a risk of: (a) self-reflection, (b) FAISS nprobe, (c) pooling. Answer: (a).
  7. Short Answer: How does Module 15.2 relate? Answer: Store critiques/episodes so later runs do not repeat failures.
  8. Short Answer: Name a Vol. 14 way to implement reflection cycles. Answer: LangGraph conditional edges back to a generate node.
  9. Multiple Choice: If scores stop improving you should: (a) loop forever, (b) halt/escalate, (c) drop the schema. Answer: (b).
  10. True/False: Vague rubrics make reflection more reliable. Answer: False.

Key Takeaways

  • Reflection is critique-then-revise with an explicit stop—not unlimited rumination.
  • Prefer testable evaluators; use self-critique and HITL where tests cannot reach.
  • Cap revisions; persist critiques into memory for Reflexion-style improvement.
  • LangGraph cycles are the orchestration form of this pattern.
  • Next: tool calling—the actions reflection often depends on.
Trainer’s Guide

Whiteboard: Write a 4-point rubric for “safe refund email.” Mark which points a model can self-score vs which need a tool.

Lab: Add a deterministic checker: fail the draft if it mentions an SLA number not present in facts.

Recap: Reflection critiques, revises, and stops. Continue with Tool Calling.