
Implementing deepagents: a technical walkthrough
LangChain
Overview
This video provides a technical walkthrough of the 'deep agents' Python package, built on the LangGraph framework. It explains the core components and implementation details, focusing on how deep agents extend basic agent capabilities with longer-term planning and deeper problem-solving. The walkthrough covers the agent's state management, including messages, to-dos, and a virtual file system, as well as the underlying ReAct agent loop. It details the built-in tools for planning and file system interaction, and the crucial 'create task' tool for managing sub-agents. Finally, it demonstrates how to instantiate and use a deep agent with custom tools and instructions.
Save this permanently with flashcards, quizzes, and AI chat
Chapters
- Deep agents are designed for complex problem-solving over extended time horizons.
- They build upon foundational agent concepts like planning, file system access, sub-agents, and detailed system prompts.
- The 'deep agents' package is implemented using LangGraph, an agent runtime for creating agents as graphs.
- The core algorithm is a ReAct (Reasoning and Acting) loop: LLM call -> decide stop or action -> execute action -> get feedback -> repeat.
- Agents in LangGraph utilize a defined state object to track information over time.
- The `deep_agent_state` inherits from LangGraph's `agent_state` and adds specific attributes.
- Key attributes include `messages` (human, AI, tool feedback), `last_step`, `remaining_steps` (for iteration tracking), `todos` (for planning), and `files` (a virtual file system).
- The virtual file system is a dictionary mapping file names to content, chosen for scalability over a real file system.
- A `file_reducer` is implemented to merge file updates from parallel agent executions, though it doesn't yet resolve edit conflicts.
- The system prompt for deep agents is intentionally kept simple in the base configuration.
- Most of the detailed instructions and context are provided through rich, descriptive tool definitions.
- This approach leverages the LLM's ability to understand complex instructions embedded within tool descriptions.
- The complexity of the agent's behavior is thus distributed between the core prompt and the specific functionalities of its tools.
- The `create_deep_agent` function is the primary interface for instantiating a deep agent.
- It accepts custom tools, custom instructions, an optional custom model, sub-agent definitions, and a state schema.
- If no model is provided, it defaults to `ChatAnthropic` (Claude Sonnet) with a high `max_tokens` setting.
- The final agent is created by calling `create_react_agent` with the configured model, prompt, tools, and state schema.
- Deep agents come with five built-in tools: `write_todos` (planning) and four file system tools (`write_file`, `read_file`, `ls`, `edit_file`).
- The `write_todos` tool updates the agent's to-do list and logs the update as a message.
- File system tools operate on the virtual file system stored in the agent's state.
- `read_file` has specific logic for handling line limits, truncation, and formatting.
- `edit_file` performs exact string replacements, leveraging Anthropic's trained `str_replace` function for better model performance.
- The `create_task_tool` enables the main agent to delegate tasks to sub-agents.
- Sub-agents are defined by their name, description, prompt, and a list of tools they can access.
- A default 'general purpose' sub-agent is provided, also using the ReAct loop.
- When a sub-agent is called, its state is reset to focus solely on the assigned task, with only the task description as its initial message.
- The sub-agent's output is primarily its final message, though it can return files that are merged into the main agent's state.
- The `deep_agents` package can be installed via pip (`pip install deep_agents`).
- Users can create custom tools (e.g., a `search_tool`) and custom instructions to define the agent's role and behavior.
- Built-in tools like file operations and sub-agent management do not need to be redefined.
- A deep agent is instantiated by passing the custom tools and instructions to `create_deep_agent`.
- The resulting agent can then be invoked like any other LangGraph graph.
Key takeaways
- Deep agents extend basic agent functionality by enabling planning over longer horizons and deeper task decomposition.
- The ReAct loop (Reasoning and Acting) is the fundamental execution model for these agents.
- Agent state, encompassing messages, to-dos, and a virtual file system, is critical for maintaining context and managing resources.
- Complex instructions are effectively conveyed through detailed tool descriptions rather than solely relying on a lengthy system prompt.
- The `create_deep_agent` function acts as a flexible factory for configuring agents with custom tools, instructions, and models.
- Built-in tools for planning and file system interaction, along with the powerful sub-agent delegation mechanism, provide core capabilities.
- Sub-agents allow for task specialization, with their execution environment focused on the specific delegated task.
- The `deep_agents` package offers a user-friendly interface for creating sophisticated, task-specific agents.
Key terms
Test your understanding
- How does the ReAct loop enable an agent to perform actions and receive feedback?
- What are the key components of the `deep_agent_state` and why is each important?
- Why are tool descriptions used to convey complex instructions instead of a long system prompt?
- How does the `create_task_tool` facilitate the delegation of work to sub-agents, and what is the purpose of resetting the sub-agent's state?
- What are the advantages of using a virtual file system over a real file system for agent state management?