Skip to content
LangGraph

Code Review Agent with LangGraph

Build an AI code review agent that reads files, searches for patterns, analyzes code quality, and suggests improvements — with LangGraph.

code reviewstatic analysisqualitydeveloper tools

Working Code

LangGraph
from langchain_openai import ChatOpenAI
from langchain_core.tools import tool
from langgraph.prebuilt import create_react_agent
@tool
def read_file(path: str) -> str:
"""Read a file from the project."""
return Path(path).read_text()
@tool
def find_files(pattern: str) -> str:
"""Find files matching a glob pattern."""
files = list(Path(".").rglob(pattern))
return "\n".join(str(f) for f in files[:20])
@tool
def search_code(pattern: str, path: str = ".") -> str:
"""Search for a regex pattern in source files."""
import subprocess
result = subprocess.run(
["grep", "-rn", pattern, path, "--include=*.py"],
capture_output=True, text=True,
)
return result.stdout[:3000] or "No matches found."
agent = create_react_agent(
ChatOpenAI(model="gpt-4o"),
tools=[read_file, find_files, search_code],
prompt="You are a code reviewer. Find relevant files, read them, search for patterns, and provide a structured review covering security, performance, and best practices.",
)
result = await agent.ainvoke({
"messages": [("user", "Review the Python files in src/ for common security issues")]
})
print(result["messages"][-1].content)

Step by Step

1

Install dependencies

Install LangGraph and the required tools for this use case.

2

Define your tools

Create the domain-specific tool functions your agent will use to interact with external services.

3

Create the agent and run

Initialize the LangGraph agent with your tools, set the system prompt, and execute a query.

Ready to build with LangGraph?

Generate a production-ready project with LangGraph pre-configured — FastAPI + Next.js, auth, streaming, and more.

Get Started

Ready to build your first production AI agent?

Open-source tools, battle-tested patterns, zero boilerplate. Configure your stack and ship in minutes — not months.