Reflection
Critique and improve.
People improve their writing by reading it back, and fixing problems. Agents can too. A writer drafts. A critic checks the draft against rules. If it fails, the writer revises, using the feedback.

- Draft
- Critique
- Revise
Draft, check, revise
The model, the rules, and the review format live in a small helper file. The write node drafts a shop announcement, using any feedback so far. The critic returns structured output. Does it pass, and if not, what to fix? We loop back to write until it passes, or we've tried three times. Run it. We see each draft, and the critic's verdict.
from langgraph.graph import StateGraph, START, END
from reflect_setup import model, Review, State, RULES
def write(s):
prompt = f"Write a story-time announcement. Rules: {RULES}. Fix: {s.get('feedback', '')}"
return {"draft": model.invoke(prompt).text.strip(), "tries": s.get("tries", 0) + 1}
def critic(s):
r = model.with_structured_output(Review).invoke(f"Rules: {RULES}\nDraft: {s['draft']}")
print(f"try {s['tries']}: {s['draft']} [passes={r.passes}]")
return {"passes": r.passes, "feedback": r.feedback}
b = StateGraph(State); b.add_sequence([write, critic]); b.add_edge(START, "write")
b.add_conditional_edges("critic", lambda s: END if s["passes"] or s["tries"] >= 3 else "write")
b.compile().invoke({})try 1: Join us for a free story-time this Saturday at 10 AM! [passes=True]
Also uses reflect_setup.py
from typing import TypedDict
from pydantic import BaseModel
from langchain.chat_models import init_chat_model
model = init_chat_model("google_genai:gemini-flash-lite-latest", temperature=0)
RULES = "one sentence, says Saturday at 10am, says it is free, under 25 words"
class Review(BaseModel):
passes: bool
feedback: str
class State(TypedDict):
draft: str
feedback: str
passes: bool
tries: intReflection tips
Give the critic clear, checkable rules. Pass its feedback to the writer. Always cap the number of tries. And the critic can be a different, stronger model, or even plain code for simple rules, like word counts.
| key | value |
|---|---|
| Clear rules | checkable |
| Feedback | to the writer |
| Cap | the tries |
| Critic | model or code |
Summary
Reflection loops a writer and a critic until the work meets the rules, or the tries run out. Next, a quiz on these patterns.
Key takeaways- Writer drafts
- Critic checks
- Loop with a cap