Langgraph Promptchaining
Langgraph - Prompt Chaining
This notebook demonstrates how to use prompt chaining with LangGraph to build a multi-step email assistant. The assistant guides the writing process through three distinct stages:
-
Generating an outline based on subject and bullet points
-
Writing the initial draft using the outline and desired tone
-
Refining tone if needed to match the specified style
This approach enables fine-grained control over the content generation process by decomposing the task into logical steps. Each stage in the graph is handled by a separate node, enabling targeted prompting, intermediate outputs, and conditional logic.
In addition, the entire workflow is instrumented with Phoenix, which provides OpenTelemetry-powered tracing and debugging. You can inspect each stepβs inputs, outputs, and transitions directly in the Phoenix UI to identify bottlenecks or missteps in generation.
This is a template for prompt chaining with LangGraph. It is an email writer, with 3 steps: writing an outline, writing the email, and refining tone.
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.
LLM of choice
Defining Graph State
The EmailState defines the shared memory for our email-writing agent. Each field represents the evolving state of the email β starting from the userβs initial inputs (subject, notes, tone) and gradually building up through the stages of outline generation, drafting, and final tone refinement. This state dictionary is passed between nodes to ensure context is maintained and updated incrementally throughout the workflow.
Step-by-Step Prompt Chain: Outline β Draft β Tone Check
This workflow chains multiple LLM calls to transform raw notes into a polished email:
generate_outline: Converts user bullet points into a structured outline.
write_email: Expands the outline into a complete email draft using the desired tone.
tone_gate: Checks if the draft meets the requested tone using a lightweight LLM classification.
reform_tone: If the tone doesn't match, this node rewrites the draft while preserving the content.
Each node is modular, enabling targeted debugging and reuse across different tasks or formats. This multi-step refinement mirrors human drafting processes and produces higher-quality outputs.
Compiling the Email Prompt Chain with LangGraph
Here we assemble the full LangGraph that represents our email generation pipeline. The graph begins at the outline_generator, moves to the email_writer, and conditionally routes to tone_reformer only if the tone check fails. This structure demonstrates the prompt chaining pattern with a dynamic control flowβadapting based on the modelβs output. Once compiled, this graph can be invoked on user input and traced using Phoenix for debugging or optimization.
Example Usage
Make sure to view your traces in Phoenix!
Let's add some Evaluations (Evals)
In this section we will evaluate each agent's success at its defined task.
- Outline Generation: Evaluating clarity/structure and relevance.
- Email Writing: Evaluating grammar/spelling and content coherence.
- Tone Checking: Evaluating whether tone detection correctly passed or failed.
- Tone Refinement: Evaluating if the rewritten email matches the desired tone.