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.

Related rules

Astro content site rules

Cursor rules for Astro 5 sites with content collections, islands, and zero-JS-by-default pages.

CursorAstro

Chrome extension (Manifest V3) rules

Cursor rules for building Manifest V3 browser extensions with TypeScript and minimal permissions.

CursorChrome extensions

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

Cloudflare Agents SDK rules

Cursor rules for stateful AI agents on Cloudflare (agents package): Durable Object state, SQL, scheduling, and WebSockets.

CursorCloudflare Workers / Hono

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.