Detailed interpretation of LangGraph status editing and manual feedback
📚 Overview
This document explains in detail the State Editing and Human Feedback mechanisms in LangGraph. This is the core technology for building human-machine collaborative AI systems, allowing us to pause, check, and modify the state during Agent execution, and introduce human wisdom to guide AI decision-making.
🎯 Core Concept
What is status editing?
State Editing is a capability provided by LangGraph that allows us to: during graph execution:
- Pause the execution of the graph (using breakpoints)
- Check current status
- Modify status data
- continue execution
This is a "time machine" capability that allows us to intervene in the execution process of the AI Agent.
Three modes of human-machine collaboration
In previous studies, we learned about the three main modes of human-machine collaboration:
Approval Mode (Approval)
- Pause before critical operations
- Show what the AI plans to do
- Waiting for human approval or rejection
- Example: Get confirmation before AI plans to delete data
Debugging mode (Debugging)
- Retrace the execution history of the graph
- Reproduce or avoid errors
- Analyze the root cause of the problem
- Example: When the AI makes an error, go back to the previous checkpoint and re-execute it
Editing Mode (Editing) ⭐ Key points of this lesson
- Directly modify the status of the graph
- Inject human feedback or knowledge
- Correct AI’s understanding bias
- Example: Fixing AI misinterpretation of user intent
Why do you need status editing?
Consider the following scenario:
Scenario 1: Correcting AI understanding
用户:"计算 2 × 3"
AI:准备执行...
[暂停]
人类:"不对,我想要 3 × 3"
[修改状态]
AI:执行修正后的计算 → 9Scenario 2: Injecting expert knowledge
AI:分析数据,发现异常值...
[暂停]
专家:"这个异常值是预期的,是季节性因素"
[添加上下文到状态]
AI:基于专家反馈调整分析...Scenario 3: Guiding the direction of exploration
研究 Agent:规划调研 10 个主题...
[暂停]
研究员:"聚焦前 3 个即可,其他不重要"
[修改待处理列表]
Agent:执行精简后的计划...🔧 Practical case 1: Basic status editing
System architecture
We build a mathematical calculation Agent that supports modifying user input before execution:
用户输入 "Multiply 2 and 3"
↓
[断点] interrupt_before=["assistant"]
↓
(检查状态)
↓
(修改状态) update_state()
↓
[assistant] 执行修正后的计算
↓
[tools] 调用工具
↓
[assistant] 返回结果Detailed explanation of code implementation
1. Define tools and models
from langchain_openai import ChatOpenAI
def multiply(a: int, b: int) -> int:
"""Multiply a and b.
Args:
a: first int
b: second int
"""
return a * b
def add(a: int, b: int) -> int:
"""Adds a and b.
Args:
a: first int
b: second int
"""
return a + b
def divide(a: int, b: int) -> float:
"""Divide a by b.
Args:
a: first int
b: second int
"""
return a / b
tools = [add, multiply, divide]
llm = ChatOpenAI(model="gpt-4o")
llm_with_tools = llm.bind_tools(tools)Key Points:
- Three mathematical tools defined: addition, multiplication, and division
- Each tool has a clear documentation string (docstring), LLM will use this to understand the purpose of the tool
bind_tools()binds tools to LLM, enabling it to call those tools
Python knowledge point: function documentation string (Docstring)
def multiply(a: int, b: int) -> int:
"""Multiply a and b. # 简短描述
Args: # 参数说明
a: first int
b: second int
"""
return a * bThis format follows the Google Style document specification, and LangChain will automatically parse and convert it into a tool description for LLM to understand.
2. Build the graph
from IPython.display import Image, display
from langgraph.checkpoint.memory import MemorySaver
from langgraph.graph import MessagesState
from langgraph.graph import START, StateGraph
from langgraph.prebuilt import tools_condition, ToolNode
from langchain_core.messages import HumanMessage, SystemMessage
# 系统消息
sys_msg = SystemMessage(content="You are a helpful assistant tasked with performing arithmetic on a set of inputs.")
# 助手节点
def assistant(state: MessagesState):
return {"messages": [llm_with_tools.invoke([sys_msg] + state["messages"])]}
# 构建图
builder = StateGraph(MessagesState)
# 定义节点
builder.add_node("assistant", assistant)
builder.add_node("tools", ToolNode(tools))
# 定义边
builder.add_edge(START, "assistant")
builder.add_conditional_edges(
"assistant",
tools_condition, # 自动判断:工具调用 → tools,否则 → END
)
builder.add_edge("tools", "assistant")
# 编译图(关键:设置断点)
memory = MemorySaver()
graph = builder.compile(
interrupt_before=["assistant"], # ⭐ 在 assistant 前暂停
checkpointer=memory
)LangGraph knowledge point: interrupt_before
graph = builder.compile(interrupt_before=["assistant"], checkpointer=memory)
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^
# 在指定节点前设置断点interrupt_before=["assistant"]: pause before executingassistantnodecheckpointer=memory: Save execution status, support recovery and modification- Breakpoint will stop the graph before the specified node and wait for manual intervention
**Why do you need checkpointer? **
Checkpointer is the core of status management:
MemorySaver() # 内存存储(开发/测试)
# 生产环境可用:
# PostgresSaver() # 数据库存储
# RedisSaver() # Redis 存储Without checkpointer, the graph cannot be paused and resumed!
3. Execute and trigger breakpoints
# 输入
initial_input = {"messages": "Multiply 2 and 3"}
# 线程配置(用于追踪会话)
thread = {"configurable": {"thread_id": "1"}}
# 运行图直到第一个断点
for event in graph.stream(initial_input, thread, stream_mode="values"):
event['messages'][-1].pretty_print()Output:
================================ Human Message =================================
Multiply 2 and 3Observations:
- Figure receives user message
- Pause before
assistantnode (because ofinterrupt_before=["assistant"]) - AI has not executed yet, we can intervene
4. Check status
state = graph.get_state(thread)
stateOutput:
StateSnapshot(
values={'messages': [HumanMessage(content='Multiply 2 and 3', id='e7edcaba-...')]},
next=('assistant',), # ⭐ 下一个要执行的节点
config={'configurable': {'thread_id': '1', ...}},
...
)LangGraph knowledge point: StateSnapshot
StateSnapshot is a complete snapshot of the graph at a certain point in time:
| Field | Description | Example |
|---|---|---|
values | Current status data | {'messages': [...]} |
next | Node to be executed | ('assistant',) |
config | Session Configuration | {'thread_id': '1'} |
tasks | Tasks to be executed | (PregelTask(...),) |
With next, we know which node the pause is before.
5. Modify status ⭐
graph.update_state(
thread,
{"messages": [HumanMessage(content="No, actually multiply 3 and 3!")]},
)Key Concept: Reducer Mechanism
The messages field of MessagesState uses the add_messages reducer:
class MessagesState(TypedDict):
messages: Annotated[list, add_messages] # 使用 add_messages reducerReducer behavior:
If message ID is provided: overwrite existing messages
python# 假设原消息 id='abc' update_state({"messages": [HumanMessage(content="新内容", id='abc')]}) # 结果:原消息被替换If message ID is not provided: Append new message
pythonupdate_state({"messages": [HumanMessage(content="新消息")]}) # 结果:新消息被追加到列表末尾
In our case, no ID was provided, so the new message is appended.
6. Verification status modification
new_state = graph.get_state(thread).values
for m in new_state['messages']:
m.pretty_print()Output:
================================ Human Message =================================
Multiply 2 and 3
================================ Human Message =================================
No, actually multiply 3 and 3!Observations:
- Original message retained
- New messages are appended
- The AI will see the context of both messages
7. Continue execution
for event in graph.stream(None, thread, stream_mode="values"):
event['messages'][-1].pretty_print()Important: Passing None means continuing from the current state
Output:
================================ Human Message =================================
No, actually multiply 3 and 3!
================================== Ai Message ==================================
Tool Calls:
multiply (call_Mbu8...)
Args:
a: 3
b: 3
================================= Tool Message =================================
Name: multiply
9Process Analysis:
- Resume from the breakpoint and the AI sees the modified message
- AI is understood as calculating 3 × 3
- Call the
multiplytool - Get result 9
8. Continue again (processing tool returns)
for event in graph.stream(None, thread, stream_mode="values"):
event['messages'][-1].pretty_print()Output:
================================= Tool Message =================================
Name: multiply
9
================================== Ai Message ==================================
3 multiplied by 3 equals 9.Note: Because interrupt_before=["assistant"] is set, it is paused again before the assistant node. You need to call stream(None, ...) again to get the final reply.
🎭 Practical Case 2: Using LangGraph Studio
Studio Introduction
LangGraph Studio is a visual development tool that can:
- Visual graph structure
- Interactive debugging
- Modify status in real time
- View execution history
Run Studio locally
cd /path/to/module-3/studio
langgraph devOutput:
🚀 API: http://127.0.0.1:2024
🎨 Studio UI: https://smith.langchain.com/studio/?baseUrl=http://127.0.0.1:2024
📚 API Docs: http://127.0.0.1:2024/docsOpen a browser to access the Studio UI to use the visual interface.
Using LangGraph SDK
Studio provides HTTP API, which we can call programmatically through the SDK:
1. Connect to Studio
from langgraph_sdk import get_client
client = get_client(url="http://127.0.0.1:2024")2. Create a thread and run it (with breakpoint)
initial_input = {"messages": "Multiply 2 and 3"}
thread = await client.threads.create()
async for chunk in client.runs.stream(
thread["thread_id"],
"agent", # agent 名称(定义在 studio/agent.py)
input=initial_input,
stream_mode="values",
interrupt_before=["assistant"], # ⭐ SDK 支持动态设置断点
):
print(f"Receiving new event of type: {chunk.event}...")
messages = chunk.data.get('messages', [])
if messages:
print(messages[-1])Important findings: Even if breakpoints are not defined in studio/agent.py, we can dynamically inject through the API!
Output:
Receiving new event of type: metadata...
--------------------------------------------------
Receiving new event of type: values...
{'content': 'Multiply 2 and 3', 'type': 'human', 'id': '...'}
--------------------------------------------------3. Get the current status
current_state = await client.threads.get_state(thread['thread_id'])
current_stateOutput:
{
'values': {
'messages': [{'content': 'Multiply 2 and 3', 'type': 'human', ...}]
},
'next': ['assistant'], # 下一个节点
'tasks': [...],
'metadata': {...},
...
}4. Extract and edit messages
# 获取最后一条消息
last_message = current_state['values']['messages'][-1]
last_messageOutput:
{
'content': 'Multiply 2 and 3',
'type': 'human',
'id': '882dabe4-...',
...
}Modify message content:
last_message['content'] = "No, actually multiply 3 and 3!"Python knowledge points: dictionary modification
# 字典是可变对象,直接修改会影响原数据
msg = {'content': 'old'}
msg['content'] = 'new'
print(msg) # {'content': 'new'}5. Update status to server
await client.threads.update_state(
thread['thread_id'],
{"messages": last_message}
)Key: Because we retain the id of the message, the add_messages reducer will overwrite the original message instead of appending it.
6. Resume execution
async for chunk in client.runs.stream(
thread["thread_id"],
assistant_id="agent",
input=None, # ⭐ 传递 None 表示从当前状态继续
stream_mode="values",
interrupt_before=["assistant"],
):
print(f"Receiving new event of type: {chunk.event}...")
messages = chunk.data.get('messages', [])
if messages:
print(messages[-1])Output:
Receiving new event of type: values...
{'content': 'No, actually multiply 3 and 3!', 'type': 'human', ...}
--------------------------------------------------
Receiving new event of type: values...
{'content': '', 'tool_calls': [{'name': 'multiply', 'args': {'a': 3, 'b': 3}, ...}], ...}
--------------------------------------------------
Receiving new event of type: values...
{'content': '9', 'type': 'tool', 'name': 'multiply', ...}
--------------------------------------------------7. Final result
async for chunk in client.runs.stream(
thread["thread_id"],
assistant_id="agent",
input=None,
stream_mode="values",
interrupt_before=["assistant"],
):
...Output:
{'content': 'The result of multiplying 3 by 3 is 9.', 'type': 'ai', ...}🚀 Practical case 3: Waiting for user input node
Design Pattern: Human Feedback Node
Earlier we manually called update_state after a breakpoint. Now we design a specialized node to receive human feedback.
System architecture
START
↓
[human_feedback] <─┐ (断点:等待人工输入)
↓ │
[assistant] │
↓ │
[tools] ──────────┘Core idea:
human_feedbackis an "empty node" (no-op), only used as a breakpoint- Pause in front of this node and wait for user input
- Use
update_state(..., as_node="human_feedback")to inject feedback - Continue execution
Code implementation
1. Define nodes
from langchain_core.messages import HumanMessage, SystemMessage
from langgraph.graph import MessagesState, START, StateGraph
from langgraph.prebuilt import tools_condition, ToolNode
from langgraph.checkpoint.memory import MemorySaver
sys_msg = SystemMessage(content="You are a helpful assistant tasked with performing arithmetic on a set of inputs.")
# ⭐ 人工反馈节点(空操作)
def human_feedback(state: MessagesState):
pass
# 助手节点
def assistant(state: MessagesState):
return {"messages": [llm_with_tools.invoke([sys_msg] + state["messages"])]}Important: The human_feedback node does nothing but serves as a pause point.
2. Build the graph
builder = StateGraph(MessagesState)
# 添加节点
builder.add_node("assistant", assistant)
builder.add_node("tools", ToolNode(tools))
builder.add_node("human_feedback", human_feedback)
# 定义边
builder.add_edge(START, "human_feedback")
builder.add_edge("human_feedback", "assistant")
builder.add_conditional_edges("assistant", tools_condition)
builder.add_edge("tools", "human_feedback") # ⭐ 工具执行后回到反馈节点
# 编译
memory = MemorySaver()
graph = builder.compile(
interrupt_before=["human_feedback"], # 在 human_feedback 前暂停
checkpointer=memory
)Execution process of the diagram:
1. START → human_feedback [暂停]
2. (人工输入) → assistant
3. assistant → tools (如果需要调用工具)
4. tools → human_feedback [暂停]
5. (人工确认) → assistant
6. assistant → END3. Interactive execution
initial_input = {"messages": "Multiply 2 and 3"}
thread = {"configurable": {"thread_id": "5"}}
# 运行到第一个断点
for event in graph.stream(initial_input, thread, stream_mode="values"):
event["messages"][-1].pretty_print()
# 获取用户输入
user_input = input("Tell me how you want to update the state: ")
# ⭐ 作为 human_feedback 节点更新状态
graph.update_state(
thread,
{"messages": user_input},
as_node="human_feedback" # 关键参数
)
# 继续执行
for event in graph.stream(None, thread, stream_mode="values"):
event["messages"][-1].pretty_print()LangGraph knowledge point: as_node parameter
graph.update_state(
thread,
{"messages": user_input},
as_node="human_feedback" # ⭐ 模拟该节点的输出
)Function:
- Treat status updates as output from the
human_feedbacknode - The graph will consider that the
human_feedbacknode has been executed - Automatically flow to the next node (
assistant)
If as_node is not specified:
- The state will be updated, but the graph does not know which node to continue from
- Requires manual management of execution process
4. Execution results
User input: no, multiply 3 and 3
Output:
================================ Human Message =================================
Multiply 2 and 3
[暂停,等待输入]
================================ Human Message =================================
no, multiply 3 and 3
================================== Ai Message ==================================
Tool Calls:
multiply (call_...)
Args:
a: 3
b: 3
================================= Tool Message =================================
Name: multiply
9continue again:
for event in graph.stream(None, thread, stream_mode="values"):
event["messages"][-1].pretty_print()Output:
================================= Tool Message =================================
Name: multiply
9
================================== Ai Message ==================================
The result of multiplying 3 and 3 is 9.🎓 Summary of core knowledge points
LangGraph-specific concepts
1. Breakpoint mechanism (Interrupt)
| Parameters | Description | Purpose |
|---|---|---|
interrupt_before=["node"] | Pause before node execution | Manual approval, status check |
interrupt_after=["node"] | Pause after node execution | Verify results, debug |
graph = builder.compile(
interrupt_before=["assistant"], # 执行前暂停
checkpointer=memory
)2. Status Management API
| Method | Description | Example |
|---|---|---|
get_state(config) | Get current status | state = graph.get_state(thread) |
update_state(config, values) | Update status | graph.update_state(thread, {"messages": msg}) |
update_state(..., as_node="node") | Update as specified node | graph.update_state(thread, data, as_node="human") |
3. Reducer mechanism
Behavior of add_messages reducer:
from langgraph.graph import MessagesState
# MessagesState 内部定义:
class MessagesState(TypedDict):
messages: Annotated[list[BaseMessage], add_messages]Rules of Conduct:
| Scenario | Example | Results |
|---|---|---|
| New message (no ID) | update({"messages": [HumanMessage("Hi")]}) | Append to end of list |
| Overwrite message (with ID) | update({"messages": [HumanMessage("Hi", id="123")]}) | Replace message with ID "123" |
| Delete message | update({"messages": [RemoveMessage(id="123")]}) | Delete specified message |
4. Session management (Thread)
thread = {"configurable": {"thread_id": "1"}}The role of Thread:
- Isolate the status of different users/sessions -Support multiple rounds of dialogue
- Track execution history
Analogy understanding:
# 类似于数据库的会话 ID
user_1 = {"configurable": {"thread_id": "user_1"}}
user_2 = {"configurable": {"thread_id": "user_2"}}
# 两个用户的状态完全隔离
graph.stream(input1, user_1) # 用户 1 的会话
graph.stream(input2, user_2) # 用户 2 的会话5. Checkpointer persistence
| Type | Applicable scenarios | Persistence |
|---|---|---|
MemorySaver() | Development, testing | Memory only, lost after restart |
SqliteSaver() | Local application | File storage |
PostgresSaver() | Production environment | Database |
RedisSaver() | High performance scenario | Redis |
from langgraph.checkpoint.postgres import PostgresSaver
# 生产环境示例
checkpointer = PostgresSaver(connection_string="postgresql://...")
graph = builder.compile(checkpointer=checkpointer)Python-specific knowledge points
1. TypedDict vs Pydantic BaseModel
| Properties | TypedDict | BaseModel |
|---|---|---|
| Type checking | Static (IDE) | Runtime |
| Data Validation | None | Yes |
| Performance | Fast | Slightly Slow |
| Purpose | State Definition | Data Model |
from typing_extensions import TypedDict
from pydantic import BaseModel
# TypedDict - 用于图状态
class MyState(TypedDict):
messages: list
count: int
# BaseModel - 用于结构化输出
class Response(BaseModel):
answer: str
confidence: float2. Function documentation string (Docstring)
def multiply(a: int, b: int) -> int:
"""Multiply a and b.
Args:
a: first integer
b: second integer
Returns:
The product of a and b
Examples:
>>> multiply(2, 3)
6
"""
return a * bBest Practices:
- First line: short function description
Args: Parameter descriptionReturns: Return value descriptionExamples: usage example (optional)
3. Type Hints
from typing import Annotated
from langgraph.graph import MessagesState
def assistant(state: MessagesState) -> dict:
# ^^^^^^^^^^^^^^ ^^^^
# 输入类型 输出类型
return {"messages": [...]}**Why is it important? **
- IDE auto-completion
- static type checking (mypy)
- Code readability
4. async/await asynchronous programming
LangGraph SDK uses asynchronous API:
# 异步函数定义
async def main():
# 等待异步操作
state = await client.threads.get_state(thread_id)
# 异步迭代
async for chunk in client.runs.stream(...):
print(chunk)
# 运行异步函数
import asyncio
asyncio.run(main())In Jupyter:
# 直接使用 await(Jupyter 自动处理)
state = await client.threads.get_state(thread_id)💡 Best Practices
1. When to use status editing?
✅ Applicable scenarios:
- Correction of AI misunderstanding: User’s expression is unclear and requires manual clarification
- Inject domain knowledge: AI lacks professional knowledge, experts provide guidance
- Dynamic Adjustment Strategy: Adjust execution plan based on intermediate results
- Error recovery: AI error, manually correct the status and try again
❌ Not applicable scenarios:
- Simple parameter validation (should be processed before input)
- Frequent user interaction (consider process redesign)
- Automate processes (break automation features)
2. Breakpoint setting strategy
Strategy 1: Key Decision Points
# 在 AI 做重要决策前暂停
interrupt_before=["delete_data", "send_email", "make_payment"]Strategy 2: Phased Approval
# 多个检查点
interrupt_before=["plan", "execute", "finalize"]Strategy 3: Dynamic breakpoints (Studio API)
# 根据运行时条件设置断点
if is_production:
interrupt_before = ["risky_operation"]
else:
interrupt_before = []
client.runs.stream(..., interrupt_before=interrupt_before)3. Status update mode
Mode 1: Additional feedback
# 不提供 ID,追加新消息
graph.update_state(
thread,
{"messages": [HumanMessage("Additional info: ...")]},
as_node="human_feedback"
)Mode 2: Correction Message
# 提供 ID,覆盖原消息
state = graph.get_state(thread)
last_msg = state.values['messages'][-1]
last_msg['content'] = "Corrected message"
graph.update_state(
thread,
{"messages": [last_msg]}, # 保留 ID
as_node="human_feedback"
)Mode 3: Partial status update
# 只更新特定字段
graph.update_state(
thread,
{"retry_count": 0, "error": None}, # 不影响 messages
as_node="error_handler"
)4. User experience design
Principle 1: Clear pause prompts
print("⏸ AI is paused. Current plan:")
print(f" - {state.values['planned_action']}")
user_input = input("Approve? (yes/no/edit): ")Principle 2: Provide context
# 展示完整上下文,帮助用户决策
for msg in state.values['messages']:
print(f"{msg.type}: {msg.content}")Principle 3: Support multiple operations
choice = input("Choose: (1) Approve (2) Edit (3) Cancel (4) Restart: ")
if choice == "1":
graph.stream(None, thread) # 继续
elif choice == "2":
new_input = input("Enter correction: ")
graph.update_state(thread, {"messages": new_input})
graph.stream(None, thread)
elif choice == "3":
# 取消执行
pass
elif choice == "4":
# 重新开始(新线程)
thread = {"configurable": {"thread_id": str(uuid.uuid4())}}5. Error handling
Tip 1: Verify status updates
try:
graph.update_state(thread, {"messages": user_input})
print("✅ State updated successfully")
except Exception as e:
print(f"❌ Failed to update state: {e}")Tip 2: State rollback
# 保存原始状态
original_state = graph.get_state(thread)
try:
graph.update_state(thread, new_data)
result = graph.stream(None, thread)
except Exception as e:
# 恢复原状态
graph.update_state(thread, original_state.values)
print(f"Rolled back due to error: {e}")Tip 3: Timeout handling
import asyncio
async def run_with_timeout():
try:
# 等待用户输入,最多 60 秒
user_input = await asyncio.wait_for(
get_user_input_async(),
timeout=60.0
)
graph.update_state(thread, {"messages": user_input})
except asyncio.TimeoutError:
# 超时,使用默认值
graph.update_state(thread, {"messages": "timeout - using default"})🚀 Advanced skills
1. Multiple feedback loops
# 允许用户多次修正
max_iterations = 3
for i in range(max_iterations):
# 执行到断点
for event in graph.stream(None, thread, stream_mode="values"):
print(event)
# 用户确认
satisfied = input("Satisfied? (yes/no): ")
if satisfied == "yes":
break
# 修正
correction = input("Enter correction: ")
graph.update_state(thread, {"messages": correction}, as_node="human_feedback")2. Conditional breakpoint
def conditional_interrupt(state: MessagesState):
# 只在特定条件下暂停
if state.get("confidence", 1.0) < 0.7:
# 低置信度,需要人工确认
return "human_feedback"
else:
# 高置信度,直接执行
return "execute"
builder.add_conditional_edges("assistant", conditional_interrupt)3. Collaborative status editing
# 多个专家同时提供反馈
state = graph.get_state(thread)
expert_1_feedback = "Consider approach A because..."
expert_2_feedback = "I suggest approach B because..."
# 合并反馈
combined_feedback = f"""
Expert 1: {expert_1_feedback}
Expert 2: {expert_2_feedback}
"""
graph.update_state(
thread,
{"messages": [HumanMessage(combined_feedback)]},
as_node="expert_review"
)4. Version control and auditing
import json
from datetime import datetime
# 记录每次状态修改
def update_with_audit(thread, new_state, as_node=None):
# 获取修改前状态
before = graph.get_state(thread)
# 执行更新
result = graph.update_state(thread, new_state, as_node=as_node)
# 获取修改后状态
after = graph.get_state(thread)
# 记录审计日志
audit_log = {
"timestamp": datetime.now().isoformat(),
"before": before.values,
"after": after.values,
"changes": new_state,
"node": as_node
}
with open("audit.jsonl", "a") as f:
f.write(json.dumps(audit_log) + "\n")
return result📊 Status editing vs other modes
Comparison: Three human-machine collaboration modes
| Features | Approval Mode | Debug Mode | Edit Mode |
|---|---|---|---|
| Main purpose | Confirm AI operation | Reproduce/avoid errors | Fix/guide AI |
| Status modification | None | Roll back to historical status | Modify current status directly |
| Interaction timing | Before operation | After error | Any time |
| Technical implementation | Breakpoints | Historical traceback | update_state |
| Applicable scenarios | High-risk operations | Troubleshooting | Dynamic adjustments |
Example comparison
Approval Mode
# 仅确认,不修改
for event in graph.stream(input, thread):
print(event)
confirm = input("Approve? (yes/no): ")
if confirm == "yes":
graph.stream(None, thread) # 继续执行
else:
# 取消
passDebug mode
# 回到之前的检查点
history = graph.get_state_history(thread)
for state in history:
print(state.config['checkpoint_id'])
# 回滚到特定检查点
graph.update_state(history[2].config, history[2].values)Edit mode (focus of this lesson)
# 直接修改状态
state = graph.get_state(thread)
state.values['messages'][-1].content = "Modified"
graph.update_state(thread, state.values)🎯 Practical application cases
Case 1: Customer service agent misunderstanding correction
# 用户:"我要退货"
# AI 理解:"用户要查询退货政策"
# 暂停在 assistant 前
state = graph.get_state(thread)
print(f"AI 理解:{state.values['intent']}")
# 客服修正
graph.update_state(
thread,
{"intent": "process_return", "clarification": "用户要办理退货,非查询"},
as_node="human_supervisor"
)
# 继续处理
graph.stream(None, thread)Case 2: Research Agent direction adjustment
# AI 规划:调研 10 个技术方向
# 专家:"只需要关注 LLM 和 RAG"
state = graph.get_state(thread)
original_topics = state.values['research_topics']
print(f"原计划:{original_topics}")
# 精简计划
graph.update_state(
thread,
{"research_topics": ["LLM", "RAG"]},
as_node="expert_review"
)
# 执行精简后的计划
graph.stream(None, thread)Case 3: Code Generation Agent Requirements Clarification
# 用户:"写一个排序函数"
# AI:"准备写快速排序..."
# 暂停,让用户明确需求
state = graph.get_state(thread)
clarification = input("需要哪种排序算法?(bubble/quick/merge): ")
performance = input("数据规模?(small/large): ")
# 注入澄清信息
graph.update_state(
thread,
{
"messages": [HumanMessage(f"Use {clarification} sort for {performance} dataset")],
"algorithm": clarification,
"scale": performance
},
as_node="requirement_clarification"
)
graph.stream(None, thread)🔍 Frequently Asked Questions
Q1: Do I need to call stream(None) after update_state?
**need! ** update_state only modifies the status and does not advance execution. stream(None, thread) must be called to continue.
# ❌ 错误:状态更新了,但图没继续执行
graph.update_state(thread, {"messages": "new message"})
# ✅ 正确:更新后继续执行
graph.update_state(thread, {"messages": "new message"})
graph.stream(None, thread) # 从当前状态继续Q2: What is the use of the as_node parameter?
Function: Specify which node the status update comes from, affecting the subsequent execution flow.
# 不指定 as_node:状态更新,但图不知道该执行哪个节点
graph.update_state(thread, data)
# 指定 as_node:模拟该节点已执行,自动流向下一个节点
graph.update_state(thread, data, as_node="human_feedback")
# → 图认为 human_feedback 已完成,执行其后续节点Q3: How do breakpoints and as_node work together?
Typical mode:
# 1. 设置断点
graph = builder.compile(interrupt_before=["human_feedback"])
# 2. 执行到断点
graph.stream(input, thread)
# 3. 作为 human_feedback 节点更新状态
graph.update_state(thread, data, as_node="human_feedback")
# 4. 继续执行(跳过 human_feedback,执行下一个节点)
graph.stream(None, thread)Q4: How to overwrite a message instead of appending it?
Method: id that retains the message.
# 追加(无 ID)
graph.update_state(thread, {"messages": [HumanMessage("new")]})
# 覆盖(有 ID)
state = graph.get_state(thread)
msg = state.values['messages'][-1]
msg.content = "modified"
graph.update_state(thread, {"messages": [msg]}) # msg 保留了 IDQ5: Will data be lost after MemorySaver is restarted?
**meeting! ** MemorySaver only exists in memory and will be lost when the process is restarted.
Production environment plan:
from langgraph.checkpoint.sqlite import SqliteSaver
# 持久化到文件
checkpointer = SqliteSaver.from_conn_string("checkpoints.db")
graph = builder.compile(checkpointer=checkpointer)📖 Further reading
- LangGraph status editing official document
- Complete Guide to Human-in-the-Loop
- LangGraph Studio usage tutorial
- Checkpointer persistence solution
🎓 Summary of this chapter
Core Harvest
- Breakpoint mechanism: Pause execution at key nodes through
interrupt_before/after - Status Management: Use
get_stateto check andupdate_stateto modify the status - Reducer mechanism:
add_messagesdecides to append or overwrite based on the message ID - as_node parameter: simulate node output and control execution process
- Manual feedback node: Design a special "empty node" as a human-computer interaction point
- LangGraph Studio: Visual tool that supports dynamic breakpoints and status editing
Design patterns
| Mode | Description | Applicable Scenarios |
|---|---|---|
| Checkpoint Approval | Pause before node, waiting for confirmation | High risk operation |
| Feedback Injection | Dedicated nodes receive human input | Multiple rounds of collaboration |
| Status Correction | Directly modify the error status | Correct AI misunderstandings |
| Dynamic breakpoint | Determine whether to pause based on conditions | Adaptive approval |
Next step
Mastering state editing and human feedback, you already have the ability to build a highly controllable AI Agent. Next, you can learn:
- Parallel processing: Multiple Agents work together
- Subgraph: modularized complex processes
- Persistence Strategy: Production-level state management
- Advanced routing: dynamic decision-making and conditional execution
State editing is the cornerstone of human-machine collaboration. Mastering it will make your AI system more intelligent, reliable, and controllable!