Langgraph Evaluator
LangGraph: Evaluator–Optimizer Loop
In this tutorial, we’ll build a code generation feedback loop using LangGraph — where a generator LLM writes code and an evaluator LLM provides structured reviews. This iterative pattern is useful for refining outputs over multiple steps until they meet a defined success criterion.
The workflow consists of:
A generator LLM that produces or revises code based on feedback.
An evaluator LLM that assigns a grade (pass or fail) and gives feedback if needed.
A LangGraph state machine that loops the generator until the evaluator approves the result.
To make this fully observable and production-grade, we’ve instrumented the graph with Phoenix tracing. This enables you to inspect each generation and evaluation step, see what the model produced, and understand why it did (or didn’t) pass.
Configure Phoenix Tracing
Make sure you go to https://app.phoenix.arize.com/ and generate an API key. This will allow you to trace your Langgraph application with Phoenix.
Evaluator‑Optimizer • Code‑Writing Loop
Input : problem_spec (natural‑language description of the function/program)
Output : refined, accepted code (Python string)
LLMs
• generator_llm : writes / rewrites the code
• evaluator_llm : grades the code via structured output
To enable structured, reliable feedback from the evaluator LLM, we define a Pydantic schema called Review. This schema ensures that all evaluations include both a binary grade (pass or fail) and feedback if the code needs improvement.
By binding this schema to the evaluator LLM, we guarantee consistent output formatting and make it easier to route logic in the graph. This step is essential for closing the feedback loop and driving iterative optimization.
Langgraph Shared State
Defines the shared state for the evaluator-optimizer loop, tracking the problem description, generated code, feedback, and evaluation grade.
Node Functions: Generator & Evaluator
These nodes power the evaluator–optimizer loop. The code_generator node uses the generator LLM to produce or revise Python code based on the task and prior feedback. The code_evaluator node uses a structured evaluator LLM to simulate a code review — mentally testing the code against the spec and returning a binary grade along with constructive feedback if needed. This feedback is then looped back to the generator until the code passes.
Routing Logic
To support iterative refinement, we define a simple routing function called route. After the code is evaluated, this function checks the grade returned by the evaluator: if the code passes, the process ends; if it fails, control loops back to the generator for another revision. This logic ensures the LLM can continuously improve its output based on feedback until it meets the quality bar.
Building the LangGraph
We now define our LangGraph-based workflow. Each component — the generator and evaluator — is added as a node. Directed edges specify the flow: the graph starts with generation, then moves to evaluation. Conditional edges use the routing logic to either terminate the loop (if passed) or revisit the generator (if failed). This structure enables self-correcting behavior with every iteration traceable via Phoenix.