Home › LLM apps & agents › LangGraph agent rules

LangGraph agent rules

Cursor rules for building agents with LangGraph in Python: typed state, nodes, tools, checkpointers, and tests.

CursorLLM apps & agents

How to use

Save this as .cursor/rules/langgraph.mdc in your repository root, commit it, and your agent will pick it up automatically. Edit the commands to match your project.

.cursor/rules/langgraph.mdcDownload  
---
description: LangGraph agent conventions
globs: ["agent/**/*.py","**/*graph*.py"]
alwaysApply: false
---

# LangGraph agents (Python)

- State is a `TypedDict` (or Pydantic model) in `agent/state.py`. Use reducers for accumulating fields, e.g. `messages: Annotated[list, add_messages]`.
- Nodes are plain functions `(state) -> dict` that return only the keys they update. Keep each node single-purpose and unit-testable without a model.
- Build the graph in `agent/graph.py` with `StateGraph(State)`, `add_node`, `add_edge(START, ...)`, and `add_conditional_edges` with a routing function that returns node names.
- Tools are functions with type hints and docstrings (`@tool`); validate arguments and return short strings or JSON. Run them through `ToolNode`.
- Persist with a checkpointer (`InMemorySaver` in tests, Postgres/SQLite saver in prod) and pass `config={"configurable": {"thread_id": ...}}`.
- Human-in-the-loop with `interrupt()` and resume via `Command(resume=...)` before any irreversible tool.
- Cap loops with `recursion_limit` and explicit stop conditions.
- Stream to users with `graph.stream(..., stream_mode="updates")` or `"messages"`.
- Model and temperature come from config; trace with LangSmith only if keys are set.

## Don't
- Don't put business logic in prompt strings when a node can do it in code.
- Don't let tools run shell commands or send messages without an approval step.

## Done when
`pytest` passes with nodes tested in isolation and one graph test using a fake chat model.

More: all LLM apps & agents rules · all Cursor rules

Related rules

Claude Agent SDK CLAUDE.md

CLAUDE.md for building agents with the Claude Agent SDK (Python/TypeScript): options, tools, permissions, and hooks.

Claude Code (CLAUDE.md)LLM apps & agents

CrewAI multi-agent AGENTS.md

AGENTS.md for CrewAI 1.x projects: YAML agents and tasks, crews and flows, tools, and cost-aware runs.

AGENTS.mdLLM apps & agents

LLM app (Python) AGENTS.md

AGENTS.md for Python apps that call LLM APIs (OpenAI, Anthropic, Gemini): prompts, structured output, evals, and cost.

AGENTS.mdLLM apps & agents

OpenAI Agents SDK rules

Cursor rules for the OpenAI Agents SDK (Python 0.2x): agents, function tools, handoffs, guardrails, sessions, and tracing.

CursorLLM apps & agents

Vercel AI SDK rules

Cursor rules for the Vercel AI SDK 5 in TypeScript: streamText, structured output, tools, and useChat UIs.

CursorLLM apps & agents

Jupyter notebook hygiene rules

Cursor rules for Jupyter notebooks in data projects: reproducible cells, small outputs, and moving code into modules.

CursorPython Data & ML

Get new rules weekly

One email a week: fresh rule files, agent tips, and what's changing in Cursor, Claude Code and Copilot. Free, unsubscribe anytime.

Opens the Rulestack Newsletter on beehiiv in a new tab. Or subscribe directly on beehiiv.