Skip to content

Commit 6425c8f

Browse files
committed
Add Cursor rule for LangGraph in TypeScript
1 parent b044f95 commit 6425c8f

2 files changed

Lines changed: 101 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ By adding selected `.mdc` files to `.cursor/rules/`, you can use these rules dir
134134
- [Java (Spring Boot, JPA)](https://github.com/PatrickJS/awesome-cursorrules/blob/main/rules/java-springboot-jpa-cursorrules-prompt-file.mdc) - Java development with Spring Boot and JPA integration.
135135
- [Knative (Istio, Typesense, GPU)](https://github.com/PatrickJS/awesome-cursorrules/blob/main/rules/knative-istio-typesense-gpu-cursorrules-prompt-fil.mdc) - Knative development with Istio, Typesense, and GPU integration.
136136
- [Kotlin Ktor Development](https://github.com/PatrickJS/awesome-cursorrules/blob/main/rules/kotlin-ktor-development-cursorrules-prompt-file.mdc) - Kotlin development with Ktor integration.
137+
- [LangGraph (TypeScript)](https://github.com/PatrickJS/awesome-cursorrules/blob/main/rules/langgraph-typescript.mdc) - Best practices for building stateful multi-agent workflows with LangGraph in TypeScript.
137138
- [Laravel (PHP 8.3)](https://github.com/PatrickJS/awesome-cursorrules/blob/main/rules/laravel-php-83-cursorrules-prompt-file.mdc) - Laravel development with PHP 8.3 integration.
138139
- [Laravel (TALL Stack)](https://github.com/PatrickJS/awesome-cursorrules/blob/main/rules/laravel-tall-stack-best-practices-cursorrules-prom.mdc) - Laravel development with TALL Stack best practices.
139140
- [Manifest](https://github.com/PatrickJS/awesome-cursorrules/blob/main/rules/manifest-yaml-cursorrules-prompt-file.mdc) - YAML-based configuration and metadata files.

rules/langgraph-typescript.mdc

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
---
2+
description: Best practices for building stateful multi-agent workflows with LangGraph in TypeScript. Apply this rule when creating or modifying AI agent architectures, graph states, nodes, or conditional edges.
3+
globs: ["**/*.ts", "**/*.tsx"]
4+
alwaysApply: false
5+
---
6+
# LangGraph TypeScript Guidelines
7+
8+
Best practices and patterns for designing robust, type-safe multi-agent systems using LangGraph in TypeScript.
9+
10+
## Core Principles
11+
12+
- **State Immutability:** Treat the graph state as immutable. Nodes must return updates rather than mutating state directly.
13+
- **Strict Typing:** Always explicitly type your graph state, node inputs/outputs, and edge conditions.
14+
- **Single Responsibility Nodes:** Each node should do one logical job (e.g., retrieval, formatting, routing).
15+
- **Explicit Stop Conditions:** Ensure conditional edges always have a guaranteed termination path to prevent infinite execution loops.
16+
17+
## State Definition
18+
Use the modern `Annotation.define` pattern to establish a type-safe graph state.
19+
20+
```typescript
21+
import { Annotation } from "@langchain/langgraph";
22+
import { BaseMessage } from "@langchain/core/messages";
23+
24+
// 1. Define the TypeScript interface representing your state
25+
export interface AgentState {
26+
messages: BaseMessage[];
27+
nextStep: string;
28+
isCompleted: boolean;
29+
}
30+
31+
// 2. Define the LangGraph State Annotation
32+
export const AgentStateAnnotation = Annotation.Root<AgentState>({
33+
messages: Annotation<BaseMessage[]>({
34+
reducer: (x, y) => x.concat(y),
35+
default: () => [],
36+
}),
37+
nextStep: Annotation<string>({
38+
reducer: (x, y) => y ?? x,
39+
default: () => "start",
40+
}),
41+
isCompleted: Annotation<boolean>({
42+
reducer: (x, y) => y ?? x,
43+
default: () => false,
44+
}),
45+
});
46+
```
47+
48+
## Node Implementation
49+
Nodes are async functions that accept the current state and return a partial update to the state.
50+
51+
```typescript
52+
import { AgentState } from "./state.js";
53+
54+
// Always type node inputs/outputs explicitly
55+
export async function callModelNode(state: typeof AgentStateAnnotation.State): Promise<Partial<AgentState>> {
56+
const { messages } = state;
57+
58+
// Implement agent tool call or LLM completion logic here...
59+
60+
return {
61+
messages: [/* new response message */],
62+
nextStep: "verify",
63+
};
64+
}
65+
```
66+
67+
## Edge and Router Logic
68+
Conditional edges determine the control flow of your graph dynamically.
69+
70+
```typescript
71+
import { AgentState } from "./state.js";
72+
73+
// Router determines which node to navigate to next
74+
export function shouldContinue(state: typeof AgentStateAnnotation.State): "continue" | "end" {
75+
if (state.isCompleted || state.messages.length > 10) {
76+
return "end";
77+
}
78+
return "continue";
79+
}
80+
```
81+
82+
## Graph Assembly
83+
Assemble and compile the graph builder into a runnable application.
84+
85+
```typescript
86+
import { StateGraph } from "@langchain/langgraph";
87+
import { AgentStateAnnotation, AgentState } from "./state.js";
88+
import { callModelNode } from "./nodes.js";
89+
import { shouldContinue } from "./edges.js";
90+
91+
const workflow = new StateGraph(AgentStateAnnotation)
92+
.addNode("agent", callModelNode)
93+
.addEdge("__start__", "agent")
94+
.addConditionalEdges("agent", shouldContinue, {
95+
continue: "agent",
96+
end: "__end__",
97+
});
98+
99+
export const graph = workflow.compile();
100+
```

0 commit comments

Comments
 (0)