The Problem: One Try Is Not Enough
We want a LinkedIn post that follows five editorial rules.
The issue
We want a LinkedIn post with a strong hook, short paragraphs, 150 to 200 words, an ending question, and no hashtags.
The first try
Ask the model once, or use a sequential graph: write, then review.
Why it fails
The first draft often breaks a rule. A review that only comes at the end cannot send the draft back, so a bad post goes out.
The fix
A loop: the reviewer checks the draft. If it fails, its feedback goes back to the writer, who rewrites.
The Idea: Reflection
DefinitionReflection means one step writes, another step reviews, and the review is used to rewrite. The loop repeats until the work is good enough.
| State key | Who writes it | What it holds |
|---|---|---|
topic | you, at the start | what the post is about |
draft | writer | the latest version of the post |
attempt | writer | how many drafts so far |
reviewer_feedback | reviewer | what to fix |
is_approved | reviewer | true or false |
The five keys of your
State in 01_iterative.py.The Writer and the Reviewer
Each node has one job, and its own temperature.
def writer_node(state:State)->dict: """Write the post on give topic and also improve if any feedback is there?""" attempt = state.get("attempt",0) +1 chain = writter_prompt | writter_llm | parser response = chain.invoke({ "topic":state["topic"], "draft":state["draft"] , "feedback":state["reviewer_feedback"]}) return {"draft":response,"attempt":attempt} class ReviewerFeedBack(BaseModel): is_approved : bool = Field(description="Weather Draft is approved or not") feedback:str=Field(description="Feedback of draft to improve") structured_reviewer = reviewer_llm.with_structured_output(ReviewerFeedBack) def reviewer_node(state:State)->dict: draft = state["draft"] topic = state["topic"] chain = reviewer_prompt | structured_reviewer response = chain.invoke({"draft":draft,"topic":topic}) return {"reviewer_feedback":response.feedback,"is_approved":response.is_approved }
Writer
Adds 1 to
attempt. On the first try it writes fresh. After a rejection it gets the old draft and the feedback, and rewrites. Its model uses temperature=0.7, which is more creative.Reviewer
Reads the draft against five criteria and returns two things:
is_approved and feedback. Its model uses temperature=0.3, which is stricter.Structured output
with_structured_output(ReviewerFeedBack) makes the reviewer answer in a fixed shape, so the loop can test is_approved as a real true or false. You learned this in the Structured Output lesson.The Stop Rule
A loop must always know when to stop.
def quality_gate(state:State): if state["is_approved"]: return END elif state["attempt"] >=3: return END else: return "writer"
| Situation | quality_gate returns | So the graph… |
|---|---|---|
| the reviewer approved | END | stops, with a good draft |
not approved, but attempt is 3 or more | END | stops anyway, with the best draft so far |
| not approved, fewer than 3 attempts | "writer" | loops: the writer rewrites |
Why a limit
A reviewer that never approves would loop forever. Every round costs model calls and money.
What we saw
We tested a loop with no stop rule on the installed LangGraph. It stopped only after 10,007 steps, with
GraphRecursionError. With a real model that is thousands of paid calls.The rule
Every loop needs a stop rule. Yours has two: approved, or 3 attempts.
We also tested your
quality_gate logic with fake nodes: with a reviewer that never approves, the graph stops with attempt equal to 3.