building-langgraph-agentslisted
Install: claude install-skill Krosebrook/loxihart-skills
# LangGraph Agent Construction
LangGraph is a state machine framework for agents. It's the right choice
when your agent needs explicit control over what happens next at each step.
**Choose LangGraph over LangChain (sequential) or CrewAI (role-based) when:**
- The agent needs to loop until a condition is met (not just chain steps)
- Different tool results should route to different next actions
- Error recovery requires specific retry or fallback paths
- Human-in-the-loop checkpoints need to be embedded in the flow
- You need to inspect or modify agent state mid-execution
---
## Core Concepts
### StateGraph
The central object. Nodes process state; edges define what runs next.
```python
from langgraph.graph import StateGraph, END
from typing import TypedDict, Annotated
import operator
# 1. Define your state schema
class AgentState(TypedDict):
messages: Annotated[list, operator.add] # reducer: new messages append
task: str
tools_called: Annotated[list, operator.add]
outcome: str | None
error: str | None
retry_count: int
# 2. Create the graph
graph = StateGraph(AgentState)
```
### Nodes
Functions that take state and return partial state updates.
```python
async def call_model(state: AgentState) -> dict:
response = await llm.ainvoke(state["messages"])
return {"messages": [response]}
async def execute_tool(state: AgentState) -> dict:
# Extract tool call from last message
last_msg = state["messages"][-1]
tool_name = last_msg.t