SpillwaveSolutions/mastering-langgraph-agent-skill
mastering-langgraph
VerifiedBuild stateful AI agents and agentic workflows with LangGraph in Python. Covers tool-using agents with LLM-tool loops, branching workflows, conversation memory, human-in-the-loop oversight, and production monitoring. Use when - (1) building agents that use tools and loop until task complete, (2) creating multi-step workflows with conditional branches, (3) adding persistence/memory across turns with checkpointers, (4) implementing human approval with interrupt(), (5) debugging via time-travel or LangSmith. Covers StateGraph, nodes, edges, add_conditional_edges, MessagesState, thread_id, Command objects, and ToolMessage handling. Examples include chatbots, calculator agents, and structured workflows.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 9.9 KB |
| references/core-api.md | markdown | 6.3 KB |
| references/debugging-monitoring.md | markdown | 4.2 KB |
| references/hitl-patterns.md | markdown | 5.3 KB |
| references/multi-agent-patterns.md | markdown | 9.4 KB |
| references/official-resources.md | markdown | 10.9 KB |
| references/persistence-memory.md | markdown | 5.1 KB |
| references/production-deployment.md | markdown | 8.3 KB |
| references/tool-agent-pattern.md | markdown | 6.4 KB |
| references/workflow-patterns.md | markdown | 6.3 KB |
references/persistence-memory.md
View on GitHub# Persistence and Memory
Enable conversation memory, survive crashes, and debug with time-travel.
## Contents
- [Why Persistence?](#why-persistence)
- [Checkpointers](#checkpointers)
- [Thread IDs](#thread-ids)
- [Multi-Turn Conversations](#multi-turn-conversations)
- [Time Travel](#time-travel)
- [Durable Execution](#durable-execution)
---
## Why Persistence?
| Feature | Benefit |
|---------|---------|
| Conversation memory | Agent remembers previous turns |
| Crash recovery | Resume from last checkpoint |
| Time-travel | Debug by replaying from any point |
| Audit trail | Full history of state changes |
---
## Checkpointers
### InMemorySaver (Development)
```python
from langgraph.checkpoint.memory import InMemorySaver
chain = graph.compile(checkpointer=InMemorySaver())
```
### PostgresSaver (Production)
```python
from langgraph.checkpoint.postgres import PostgresSaver
DB_URI = "postgresql://user:pass@localhost:5432/langgraph"
checkpointer = PostgresSaver.from_conn_string(DB_URI)
chain = graph.compile(checkpointer=checkpointer)
```
### SQLiteSaver
```python
from langgraph.checkpoint.sqlite import SqliteSaver
checkpointer = SqliteSaver.from_conn_string("checkpoints.db")
chain = graph.compile(checkpointer=checkpointer)
```
---
## Thread IDs
Identify separate conversations:
```python
chain = graph.compile(checkpointer=InMemorySaver())
config = {"configurable": {"thread_id": "user-123"}}
# First message
chain.invoke({"messages": [HumanMessage(content="I'm Alice")]}, config=config)
# Second message - remembers Alice
chain.invoke({"messages": [HumanMessage(content="What's my name?")]}, config=config)
# Different thread - doesn't know Alice
other_config = {"configurable": {"thread_id": "user-456"}}
chain.invoke({"messages": [HumanMessage(content="What's my name?")]}, config=other_config)
```
### Thread ID Strategies
| Strategy | Example | Use Case |
|----------|---------|----------|
| User ID | `"user-123"` | One conversation per user |
| Session ID | `"user-123-session-5"` | Multiple per user |
| UUID | `str(uuid4())` | Unique per conversation |
---
## Multi-Turn Conversations
```python
from langgraph.graph import StateGraph, START, END
from langgraph.checkpoint.memory import InMemorySaver
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage, AnyMessage
from typing_extensions import TypedDict, Annotated
import operator
class State(TypedDict):
messages: Annotated[list[AnyMessage], operator.add]
llm = ChatOpenAI(model="gpt-4")
def chat(state: State) -> dict:
response = llm.invoke(state["messages"])
return {"messages": [response]}
graph = StateGraph(State)
graph.add_node("chat", chat)
graph.add_edge(START, "chat")
graph.add_edge("chat", END)
chain = graph.compile(checkpointer=InMemorySaver())
config = {"configurable": {"thread_id": "demo"}}
# Turn 1
chain.invoke({"messages": [HumanMessage(content="My color is blue.")]}, config=config)
# Turn 2 - remembers
result = chain.invoke({"messages": [HumanMessage(content="What's my color?")]}, config=config)
# "Your color is blue."
```
---
## Time Travel
### Viewing History
```python
for state in chain.get_state_history(config):
print(f"Checkpoint: {state.config['configurable']['checkpoint_id']}")
print(f"Next: {state.next}")
```
### Resuming from Checkpoint
```python
checkpoint_id = "abc123..."
resume_config = {
"configurable": {
"thread_id": "my-thread",
"checkpoint_id": checkpoint_id
}
}
result = chain.invoke(None, config=resume_config)
```
### Modifying and Resuming
```python
state = chain.get_state(config)
modified = dict(state.values)
modified["messages"].append(HumanMessage(content="Try again."))
chain.update_state(config, modified)
result = chain.invoke(None, config=config)
```
### Forking
```python
fork_config = {
"configurable": {
"thread_id": "forked-thread",
"checkpoint_id": checkpoint_id
}
}
result = chain.invoke(new_input, config=fork_config)
```
---
## Durable Execution
Survive crashes and resume:
### Idempotency
```python
# ⚠️ Problem: re-sends on resume
def send_email(state):
send(state["draft"])
return {"sent": True}
# ✓ Fix: check first
def send_email(state):
if not state.get("sent"):
send(state["draft"])
return {"sent": True}
# ✓ Or use @task
from langgraph.func import task
@task
def send_email(state):
send(state["draft"])
return {"sent": True}
```
### Long-Running Workflows
```python
chain = graph.compile(checkpointer=PostgresSaver.from_conn_string(DB_URI))
config = {"configurable": {"thread_id": "long-job-1"}}
chain.invoke(initial_state, config=config)
# If server restarts, invoke again - resumes from checkpoint
chain.invoke(None, config=config)
```
---
## Production Setup
```python
import os
ENV = os.getenv("ENVIRONMENT", "development")
if ENV == "production":
from langgraph.checkpoint.postgres import PostgresSaver
checkpointer = PostgresSaver.from_conn_string(os.getenv("DATABASE_URL"))
else:
from langgraph.checkpoint.memory import InMemorySaver
checkpointer = InMemorySaver()
chain = graph.compile(checkpointer=checkpointer)
```
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| reverse-engineer-anythingmorluto/reaReverse engineer native, managed, Electron/JavaScript, packaged, firmware, and browser targets with REA, and build agent environments (AgentEnv bundles) of web applications from a live instance or a capture, and of Linux desktop apps with a task from a request. Use shipped-artifact or requested runtime evidence to explain features, compare versions, decompile code, or guide a reconstruction. Skip REA for ordinary source-repository architecture analysis. | Stars: 469k | Updated | Claude CodeCodex | Verified | Install |
| diagnosing-superpowersobra/superpowersUse when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness. | Stars: 321k | Updated | Claude CodeCodex | Verified | Install |
| dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependencies | Stars: 321k | Updated | Claude CodeCodex | Verified | Install |
| executing-plansobra/superpowersUse when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is available | Stars: 321k | Updated | Claude CodeCodex | Verified | Install |