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.