Skip to main content
rule

langgraph — Cursor rule: build agent graphs that stay readable

A Cursor rule with LangGraph practices: how to split nodes, state and subgraphs in a Python project.

Install into Cursor · Manually: put the files in place All methods and platforms ↓
.cursor/rules/langgraph.mdc
Open on GitHub

What langgraph does

The rule attaches to **/*.py files and sets a style for LangGraph work. The core idea: one job per node, and repeated pieces move into subgraphs. Graphs should stay acyclic where possible, with cycles reserved for feedback loops that actually need them.

Inside are code samples: StateGraph, a TypedDict for state, node functions that return updated state, and a subgraph wired with add_node, add_edge, set_entry_point and set_finish_point. This is not a library or a plugin — it is a rules file Cursor reads as context. So installing it means dropping langgraph.mdc into .cursor/rules/.

The rule is tied to Python, so it does nothing in a TypeScript project. And keep in mind it is advice, not a type checker: Cursor nudges the model toward these patterns, and the model can still drift.

What it helps with

  • split agent logic into nodes with one job each
  • move repeated steps into a reusable subgraph
  • describe agent state with TypedDict
  • wire a graph with an entry point and a finish point
  • decide when a cycle in the graph earns its place

Example prompts for the agent

  • build a LangGraph graph for request handling: a fetch node, an analyze node, an edge between them
  • move context preparation into a subgraph and plug it into the main graph
  • define the agent state with TypedDict and add a node that appends messages

How to install langgraph

Manually: put the files in place

.cursor/rules/langgraph.mdc

Frontmatter: description, globs, alwaysApply

Step by step, with commands for each platform: Cursor rules: .mdc files, four modes and what replaced .cursorrules

Questions about langgraph

How do I install langgraph in Cursor?

Create .cursor/rules/langgraph.mdc in your project root and paste the rule contents into it. Cursor picks it up automatically for files matching **/*.py.

What does this rule do?

It sets practices for building LangGraph agent graphs: one responsibility per node, subgraphs for repeated patterns, a preference for acyclic graphs. Python examples with StateGraph are included.

Which files does the rule apply to?

The glob **/*.py, so Python files in the project. It does not apply in projects written in other languages.

Source text

langgraph.mdc · CC0-1.0
---
description: This guide provides definitive best practices for building robust, maintainable, and performant AI agent workflows using LangGraph.
globs: **/*.py
---
# langgraph Best Practices

## Code Organization and Structure

### 1. Explicit Graph Topology & Modularity
Define clear, single responsibilities for each node. Encapsulate common patterns into reusable subgraphs. Prefer Directed Acyclic Graphs (DAGs); use cycles only when essential for feedback loops.

✅ GOOD: Focused nodes, composable subgraphs
```python
from langgraph.graph import StateGraph, END
from typing import TypedDict, List

class AgentState(TypedDict):
    messages: List[str]
    tool_output: str | None

def fetch_data_node(state: AgentState) -> AgentState:
    return {"messages": state["messages"] + ["Data fetched."]}

def analyze_data_node(state: AgentState) -> AgentState:
    return {"messages": state["messages"] + ["Data analyzed."]}

def build_subgraph():
    builder = StateGraph(AgentState)
    builder.add_node("fetch", fetch_data_node)
    builder.add_node("analyze", analyze_data_node)
    builder.add_edge("fetch", "analyze")
    builder.set_entry_point("fetch")
    builder.set_finish_point("analyze")
    return builder.compile()
```

### 2. Functional API Preference
Prior

© sanjeed5 · CC0-1.0 · sanjeed5/awesome-cursor-rules-mdc

Similar IDE rules

All IDE rules for Agent setup: memory, context, orchestration →

From the same set69

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor