LangGraph State Loss in a heirarchial multi agent system with deep agents and agent
Problem Summary
I’m experiencing state loss when using nested deep agents (create_deep_agent) with a regular agent (create_agent) that has a custom state schema (subagentstate). The custom state updates made via ToolRuntime and Command.update within tools are lost when control returns from the agent to the parent deep agent.
Architecture
1_DeepAgent(State) → 2_DeepAgent(State) → 3_Agent(subagentstate) → Tool(ToolRuntime)
State Schemas
Top-Level State
class State(TypedDict):
subagentstate: Optional[subagentstate]
call_trace: Annotated[List[str], operator.add]
Custom Subagent State
class subagentstate(TypedDict):
user_id: str
Implementation
Top-Level Supervisor (Deep Agent 1)
top_level_supervisor = create_deep_agent(
model=llm,
system_prompt=supervisor_prompt,
subagents=[Subagent1, Subagent2],
context_schema=State,
checkpointer=SHORT_TERM_MEMORY,
store=LONG_TERM_MEMORY,
)
Second-Level Supervisor (Deep Agent 2)
second_level_supervisor = create_deep_agent(
model=llm,
system_prompt=second_supervisor_prompt,
subagents=[agent1, agent2],
context_schema=State
)
Agent with Custom State Schema (Regular Agent)
agent1 = create_agent(
model=llm,
system_prompt=agent_prompt,
tools=[
tool1,
tool2
],
middleware=[
TodoListMiddleware(
system_prompt=custom_todo_prompt,
tool_description=custom_todo_description,
),
SummarizationMiddleware(model=llm, summary_prompt=custom_summary_prompt)
],
response_format=customAgentResponse,
state_schema=subagentstate,
name="agent1",
)
Tool Implementation
@tool(
"tool1",
description="manipulates the user id",
args_schema=ToolInputschema
)
def tool1(
runtime: ToolRuntime,
) -> Command:
user_id = runtime.state.get("user_id")
tool_call_id = runtime.tool_call_id
return Command(
update={
"messages": [
ToolMessage(
content=f"user id updated",
tool_call_id=tool_call_id,
)
],
"user_id": "new_user_123"
},
)
Observed Behavior
- Tool executes and returns
Commandwith state updates - Updates are applied within the agent’s execution context
- When control returns from
agent1tosecond_level_supervisor, the custom state updates are lost - The
user_idfield insubagentstatereverts to its previous value or becomes None
Expected Behavior
Custom state updates made within tools should propagate back through the agent hierarchy and be accessible in the parent deep agent’s state.
Question
Is this expected behavior when nesting deep agents with a regular agent that uses a custom state schema? What’s the recommended approach for maintaining custom state across this hierarchy?