$npx -y skills add hanamizuki/solopreneur --skill langgraphUnderstand LangGraph v1.0 thoroughly
| 1 | # LangGraph Development Principles |
| 2 | |
| 3 | If you are coding with LangGraph, follow these principles and patterns. |
| 4 | |
| 5 | ## Critical Structure Requirements |
| 6 | |
| 7 | ### MANDATORY FIRST STEP |
| 8 | Before creating any files, **always search the codebase** for existing LangGraph-related files: |
| 9 | - Files with names like: `graph.py`, `main.py`, `app.py`, `agent.py`, `workflow.py` |
| 10 | - Files containing: `.compile()`, `StateGraph`, `create_react_agent`, `app =`, graph exports |
| 11 | - Any existing LangGraph imports or patterns |
| 12 | |
| 13 | **If any LangGraph files exist**: Follow the existing structure exactly. Do not create new agent.py files. |
| 14 | |
| 15 | **Only create agent.py when**: Building from completely empty directory with zero existing LangGraph files. |
| 16 | |
| 17 | - When starting from scratch, ensure all of the following: |
| 18 | 1. `agent.py` at project root with compiled graph exported as `app` |
| 19 | 2. `langgraph.json` configuration file in the same directory as the graph |
| 20 | 3. Proper state management defined with `TypedDict` or Pydantic `BaseModel` |
| 21 | 4. Test small components before building complex graphs |
| 22 | |
| 23 | ## Deployment-First Principles |
| 24 | |
| 25 | **CRITICAL**: All LangGraph agents should be written for DEPLOYMENT unless otherwise specified. |
| 26 | |
| 27 | ### Core Requirements: |
| 28 | - **NEVER ADD A CHECKPOINTER** unless explicitly requested by user |
| 29 | - Always export compiled graph as `app` |
| 30 | - Use prebuilt components when possible |
| 31 | - Follow model preference hierarchy: Anthropic > OpenAI > Google |
| 32 | - Keep state minimal (MessagesState usually sufficient) |
| 33 | |
| 34 | #### AVOID unless user specifically requests |
| 35 | ```python |
| 36 | # Don't do this unless asked! |
| 37 | from langgraph.checkpoint.memory import MemorySaver |
| 38 | graph = create_react_agent(model, tools, checkpointer=MemorySaver()) |
| 39 | ``` |
| 40 | |
| 41 | #### For existing codebases |
| 42 | - Always search for existing graph export patterns first |
| 43 | - Work within the established structure rather than imposing new patterns |
| 44 | - Do not create `agent.py` if graphs are already exported elsewhere |
| 45 | |
| 46 | ### Standard Structure for New Projects: |
| 47 | ``` |
| 48 | ./agent.py # Main agent file, exports: app |
| 49 | ./langgraph.json # LangGraph configuration |
| 50 | ``` |
| 51 | |
| 52 | ### Export Pattern: |
| 53 | ```python |
| 54 | from langgraph.graph import StateGraph, START, END |
| 55 | # ... your state and node definitions ... |
| 56 | |
| 57 | # Build your graph |
| 58 | graph_builder = StateGraph(YourState) |
| 59 | # ... add nodes and edges ... |
| 60 | |
| 61 | # Export as 'app' for new agents from scratch |
| 62 | graph = graph_builder.compile() |
| 63 | app = graph # Required for new LangGraph agents |
| 64 | ``` |
| 65 | |
| 66 | ## Prefer Prebuilt Components |
| 67 | |
| 68 | **Always use prebuilt components when possible** - they are deployment-ready and well-tested. |
| 69 | |
| 70 | ### Basic Agents - Use create_react_agent: |
| 71 | ```python |
| 72 | from langgraph.prebuilt import create_react_agent |
| 73 | |
| 74 | # Simple, deployment-ready agent |
| 75 | graph = create_react_agent( |
| 76 | model=model, |
| 77 | tools=tools, |
| 78 | prompt="Your agent instructions here" |
| 79 | ) |
| 80 | app = graph |
| 81 | ``` |
| 82 | |
| 83 | ### Multi-Agent Systems: |
| 84 | |
| 85 | #### Supervisor Pattern (central coordination): |
| 86 | ```python |
| 87 | from langgraph_supervisor import create_supervisor |
| 88 | |
| 89 | supervisor = create_supervisor( |
| 90 | agents=[agent1, agent2], |
| 91 | model=model, |
| 92 | prompt="You coordinate between agents..." |
| 93 | ) |
| 94 | app = supervisor.compile() |
| 95 | ``` |
| 96 | Documentation: https://langchain-ai.github.io/langgraph/reference/supervisor/ |
| 97 | |
| 98 | #### Swarm Pattern (dynamic handoffs): |
| 99 | ```python |
| 100 | from langgraph_swarm import create_swarm, create_handoff_tool |
| 101 | |
| 102 | alice = create_react_agent( |
| 103 | model, |
| 104 | [tools, create_handoff_tool(agent_name="Bob")], |
| 105 | prompt="You are Alice.", |
| 106 | name="Alice", |
| 107 | ) |
| 108 | |
| 109 | workflow = create_swarm([alice, bob], default_active_agent="Alice") |
| 110 | app = workflow.compile() |
| 111 | ``` |
| 112 | Documentation: https://langchain-ai.github.io/langgraph/reference/swarm/ |
| 113 | |
| 114 | ### Only Build Custom StateGraph When: |
| 115 | - Prebuilt components don't fit the specific use case |
| 116 | - User explicitly asks for custom workflow |
| 117 | - Complex branching logic required |
| 118 | - Advanced streaming patterns needed |
| 119 | |
| 120 | ## Model Preferences |
| 121 | |
| 122 | **LLM MODEL PRIORITY** (follow this order): |
| 123 | ```python |
| 124 | # 1. PREFER: Anthropic |
| 125 | from langchain_anthropic import ChatAnthropic |
| 126 | model = ChatAnthropic(model="claude-3-5-sonnet-20241022") |
| 127 | |
| 128 | # 2. SECOND CHOICE: OpenAI |
| 129 | from langchain_openai import ChatOpenAI |
| 130 | model = ChatOpenAI(model="gpt-4o") |
| 131 | |
| 132 | # 3. THIRD CHOICE: Google |
| 133 | from langchain_google_genai import ChatGoogleGenerativeAI |
| 134 | model = ChatGoogleGenerativeAI(model="gemini-1.5-pro") |
| 135 | ``` |
| 136 | |
| 137 | **NOTE**: Assume API keys are available in environment. |
| 138 | During development, ignore missing key errors. |
| 139 | |
| 140 | ## Message and State Handling |
| 141 | |
| 142 | ### CRITICAL: Extract Message Content Properly |
| 143 | ```python |
| 144 | # CORRECT: Extract message content properly |
| 145 | result = agent.invoke({"messages": state["messages"]}) |
| 146 | if result.get("messages"): |
| 147 | final_message = result["messages"][-1] # This is a message object |
| 148 | content = final_message.content # This is the string content |
| 149 | |
| 150 | # WRONG: Treating message objects as strings |
| 151 | content = result["messages"][-1] # This is an object, not a string! |
| 152 | if content.startswith("Error"): # Will fail - objects don't have startswith() |
| 153 | ``` |
| 154 | |
| 155 | ### State Updates Mu |