A hands-on tutorial for building a multi-agent code review system using LangGraph.
This workshop teaches you to build a LangGraph application that:
- Generates code based on requirements
- Reviews the generated code
- Refactors the code based on review feedback
- Python 3.12+
- OpenAI API key
- Basic understanding of Python and AI concepts
Checkpoint: Install environment manager
- Mac/Linux:
- Install Conda (or can use venv if preferred)
- Windows:
- Skip this step. You can just use global
pippackage manager.
- Skip this step. You can just use global
What you should have: An environment manager
- Create a new directory called
langgraph-task-1with a file calledmain.pyinside it - Conda users (Mac/Linux), use
conda create -n langgraph-task-1, thenconda activate langgraph-task-1inside the directory- Venv users, setup an environment for this project as you normally would
What you should have: An environment to work in
Inside your project directory, run:
pip install langchain-openai langgraphCheckpoint: Basic imports
Add the required imports to main.py:
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langgraph.graph import StateGraph, END, STARTWhat you should have: A file with the basic LangGraph and LangChain imports.
Checkpoint: LLM configuration
Initialize the OpenAI model:
llm = ChatOpenAI(model="gpt-4", api_key="YOUR API KEY HERE")Note: Replace "YOUR API KEY HERE" with your actual OpenAI API key.
What you should have: An LLM instance ready to use in your agents.
Checkpoint: Prompt templates
Define prompts for each agent role:
coder_prompt = ChatPromptTemplate.from_messages([
("system", "You are a Coder. Write Python code based on the given requirements."),
("human", "{input}")
])
reviewer_prompt = ChatPromptTemplate.from_messages([
("system", "You are a Reviewer. Review the given code and suggest improvements."),
("human", "{code}")
])
refactorer_prompt = ChatPromptTemplate.from_messages([
("system", "You are a Refactorer. Implement the suggested improvements in the code."),
("human", "Code:\n{code}\n\nReview:\n{review}")
])What you should have: Three distinct prompt templates for coder, reviewer, and refactorer roles.
Checkpoint: Agent implementations
Create the three agent functions:
def coder_agent(state):
response = llm.invoke(coder_prompt.format_messages(input=state["input"]))
state["code"] = response.content
return state
def reviewer_agent(state):
response = llm.invoke(reviewer_prompt.format_messages(code=state["code"]))
state["review"] = response.content
return state
def refactorer_agent(state):
response = llm.invoke(refactorer_prompt.format_messages(
code=state["code"], review=state["review"]))
state["refactored_code"] = response.content
return stateWhat you should have: Three functions that each take the state, call the LLM with appropriate prompts, and update the state.
Checkpoint: Graph construction
Create the LangGraph workflow:
builder = StateGraph(dict)
builder.add_node("coder", coder_agent)
builder.add_node("reviewer", reviewer_agent)
builder.add_node("refactorer", refactorer_agent)
builder.add_edge(START, "coder")
builder.add_edge("coder", "reviewer")
builder.add_edge("reviewer", "refactorer")
builder.add_edge("refactorer", END)
graph = builder.compile()What you should have: A compiled graph that defines the workflow: START → coder → reviewer → refactorer → END.
Checkpoint: Working application
Add the example usage:
task = "Write a function that checks if a string is a palindrome"
initial_state = {"input": task}
final_state = graph.invoke(initial_state)
print("======================================")
print("INITIAL CODE")
print("======================================")
print("\nInitial Code:\n", final_state["code"])
print("\n======================================")
print("CODE REVIEW")
print("======================================")
print("\nReview Feedback:\n", final_state["review"])
print("\n======================================")
print("REFACTORED CODE")
print("======================================")
print("\nRefactored Code:\n", final_state["refactored_code"])What you should have: A complete working application that generates, reviews, and refactors code.
python main.pySolution: Ensure all packages are installed: pip install langchain-openai langgraph
Solution: Set your OpenAI API key in the code or as an environment variable
Solution: Check that all edges are properly connected and the graph is compiled
Solution: Verify each agent function returns the updated state
- Experiment with different prompts
- Add error handling
- Implement conditional logic in the graph
- Add human-in-the-loop interactions
- Extend with additional agents (e.g., tester, documentation writer)
If you're stuck at any step, reference the step number and checkpoint description for targeted help.
