Skip to content

Conversation

zamalali
Copy link

Problem

The original sub_agent.py had two critical issues that disrupted workflows:

  1. State Corruption: The task tool overwrote the parent agent's state with state["messages"] = [...], erasing prior user/assistant messages. This broke multi-step workflows requiring preserved context.
  2. Schema Inconsistency: The general-purpose sub-agent was created without state_schema, defaulting to LangGraph's AgentState. This caused files and todos fields to be dropped during delegation, breaking state persistence.

Additionally, the tool passed the full state (including files and todos) to all sub-agents, causing unnecessary memory overhead for lightweight agents like analyzer.

Fix

To address these issues, I made the following changes to sub_agent.py:

  1. Isolated sub_state:
    • Replaced state["messages"] mutation with a new sub_state dictionary containing only the task description and selected fields.
    • Prevents corruption of the parent agent's state, preserving message history.
  2. Schema Consistency:
    • Added state_schema=DeepAgentState to the general-purpose sub-agent creation.
    • Ensures all sub-agents maintain files and todos fields consistently.
  3. Optional State Propagation:
    • Added include_files and include_todos flags to SubAgent (default: False).
    • Created a subagent_configs dictionary for efficient flag lookups at initialization.
    • Only propagate files/todos to sub_state and update in Command(update=...) if flagged.
    • Optimized Command return to reuse the existing config variable, removing a redundant lookup.
  4. Async Path:
    • Retained ainvoke for asynchronous execution, ensuring compatibility.

Behavior

  • Parent State Preservation: The parent agent's state is never modified, maintaining message history for multi-step workflows.
  • General-Purpose Agent: Always propagates and updates both files and todos for compatibility.
  • Custom Sub-Agents: Default to receiving/returning only messages unless include_files or include_todos is explicitly enabled, reducing memory overhead.

zamalali and others added 14 commits July 30, 2025 21:21
fix silly bug, let sub agents be None
* Cloudflare MCP used

* cleanup

* Add example output

* example log fixed, removed unused internet_search to not confuse example

* Remove edit to reqs

* Remove edit

* Update to simplify mcp adapter example, adds to readme only

* cleanup title and comments

* Make more uniform with previous example lang

* More direct linkage to lc-mcp-adapter repo

* clean up example output to match some examples from docs

* Update README.md

* Update README.md

* Update README.md

* Update README.md

---------

Co-authored-by: Harrison Chase <hw.chase.17@gmail.com>
* FROM ryaneggz/13-use-custom-model-with-ollama TO hwchase17/deepagents@main (langchain-ai#24)


Co-authored-by: Ryan Eggleston <kre8mymedia@gmail.com>
…chain-ai#34)

This pull request addresses a performance inefficiency in the example code located in README.md and examples/research/research_agent.py.

Changes:

The TavilyClient was previously being instantiated inside the internet_search tool function. This resulted in a new client object being created on every single tool call, which is inefficient.
This commit refactors the code to initialize the TavilyClient once at the module level. The single client instance is then reused across all subsequent calls to the internet_search tool.

Benefits:

Performance: Reduces the overhead of creating new client objects repeatedly in an agentic loop.
Best Practices: Aligns the example code with the best practice for handling API clients, providing a better template for users of the library.
@conkretelabs-prog

This comment was marked as spam.

Copy link

@conkretelabs-prog conkretelabs-prog left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent work on identifying and fixing these critical state management issues! This PR addresses fundamental problems that could break multi-step workflows.

Key Strengths:

  1. State Isolation: The sub_state approach prevents parent state corruption - crucial for maintaining conversation history
  2. Schema Consistency: Adding state_schema=DeepAgentState ensures proper field propagation
  3. Memory Optimization: Optional propagation flags reduce overhead for lightweight agents
  4. Backward Compatibility: Maintains async support and existing interfaces

Technical Review:

  • The isolated state approach is architecturally sound
  • Optional propagation flags provide good flexibility
  • Performance optimization through config reuse is well thought out

Suggestions:

  1. Consider adding unit tests for state isolation scenarios
  2. Documentation update for the new propagation flags would be helpful
  3. Migration guide for existing sub-agent implementations

This fix is essential for production stability. The state corruption issue could cause significant problems in real-world deployments.

@conkretelabs-prog

This comment was marked as spam.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants