Welcome to the seventh part of our ongoing series“Building Agents with LangGraph” course! So far, we’ve explored agents with a single Large Language Model (LLM) and a simple state. Now, we’re going to create a much more sophisticated agent composed of multiple, distinct LLM calls and a more complex state.
In this tutorial, we will build a multi-agent system that collaborates to write an essay. This system will follow a cyclical, reflective process: it will plan, research, write, and then critique its own work to produce a refined final draft.
This demonstrates how LangGraph can be used to orchestrate complex, stateful workflows involving multiple specialized “agents” or roles.
Table of Contents:
Project Setup and Dependencies
Defining the Agent’s State
Creating the Agent Roles (Nodes)
The Planner Agent
The Writer Agent
The Reflector Agent
The Researcher & Critique Agent
4. Implementing the Node Functions
5. Defining the Graph’s Logic and Edges
6. Visualizing and Running the Agent
This article is the Seventh Article in the ongoing series of Building LLM Agents with LangGraph:
Putting it All Together! Building Essay Writer Agent (You are here!)
This series is designed to take readers from foundational knowledge to advanced practices in building LLM agents with LangGraph.
Each article delves into essential components, such as constructing simple ReAct agents from scratch, leveraging LangGraph’s building units, utilizing agentic search tools, implementing persistence and streaming capabilities, integrating human-in-the-loop interactions, and culminating in the creation of a fully functional essay-writing agent.
By the end of this series, you will have a comprehensive understanding of LangGraph, practical skills to design and deploy LLM agents, and the confidence to build customized AI-driven workflows tailored to diverse applications.
Back to School Discounts: 90% Discount on Everything
Happy back-to-school season! On this occasion, I am offering a massive 90% discount on the books, courses, and To Data & Beyond during this week, so we can learn and grow together!
1. Project Setup and Dependencies
First, let’s set up our environment. We’ll load our API keys from a .env file and import the necessary libraries. This includes components from langgraph for building the graph, langchain_core for message types, and langchain_openai for the model. We’ll also set up an in-memory checkpointer using SqliteSaver to persist the state of our graph.
from dotenv import load_dotenv
_ = load_dotenv()
from langgraph.graph import StateGraph, END
from typing import TypedDict, Annotated, List
import operator
from langgraph.checkpoint.sqlite import SqliteSaver
from langchain_core.messages import AnyMessage, SystemMessage, HumanMessage, AIMessage, ChatMessage
memory = SqliteSaver.from_conn_string(":memory:")
2. Defining the Agent’s State
The state is the backbone of our agent, defining the structure of data that gets passed between nodes. For our essay writer, the state needs to track several pieces of information: the initial task, the plan, the research content, the draft, critiques, and the number of revisions.
We define this using a TypedDict:
class AgentState(TypedDict):
task: str
plan: str
draft: str
critique: str
content: List[str]
revision_number: int
max_revisions: int
Next, we initialize our model. We will use GPT-4 for this example.
from langchain_openai import ChatOpenAI
model = ChatOpenAI(model="gpt-4", temperature=0)
3. Creating the Agent Roles (Nodes)
Our multi-agent system will consist of several specialized roles, each represented by a node in our graph. Each role has a specific prompt that guides the LLM to perform its task.
The Planner Agent
The first step is to create a high-level plan. The planner node takes the user’s task and generates an essay outline.
PLAN_PROMPT = """You are an expert writer tasked with writing a high level outline of an essay. \
Write such an outline for the user provided topic. Give an outline of the essay along with any relevant notes \
or instructions for the sections."""
The Writer Agent
The writer node is responsible for generating the essay draft based on the plan and any research content. It’s also designed to revise its draft based on critiques.
Keep reading with a 7-day free trial
Subscribe to To Data & Beyond to keep reading this post and get 7 days of free access to the full post archives.