-
Notifications
You must be signed in to change notification settings - Fork 19.6k
feat(langchain): add deepagents middleware and prebuilt #33405
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
nhuang-lc
wants to merge
16
commits into
nc/9oct/file-tools-middleware
Choose a base branch
from
nh/subagent-middleware
base: nc/9oct/file-tools-middleware
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 10 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
bd5ea78
feat(langchain_v1): Add Anthropic tools middleware with text editor, …
nfcampos 89d0fff
Add subagents middleware
nhuang-lc 544a755
Fix linting
nhuang-lc 6b6057f
Resolve merge conflicts
nhuang-lc 290e838
resolve merge conflicts
nhuang-lc 203323d
Remove claude file
nhuang-lc 7eedbf2
Merge remote-tracking branch 'origin/nc/9oct/file-tools-middleware' i…
nhuang-lc 7340cb5
Share code between anthropic tools and filesystem, add deepagents
nhuang-lc 9cab0be
Update imports
nhuang-lc c7a52e8
Fix formatting
nhuang-lc f682a9e
Fix and simplify filesystem substantially - add integration tests for…
nhuang-lc 1713a14
Address comments
nhuang-lc 206fa15
Add dependencies to tests
nhuang-lc bcbf2ff
Fix import in test
nhuang-lc 918e2ee
Update default model
nhuang-lc ecbd43b
Change how namespace is built
nhuang-lc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,237 @@ | ||
| """Deepagents come with planning, filesystem, and subagents, along with other supportive middlewares..""" | ||
| # ruff: noqa: E501 | ||
|
|
||
| from collections.abc import Callable, Sequence | ||
| from typing import Any | ||
|
|
||
| from langchain_anthropic import ChatAnthropic | ||
| from langchain_core.language_models import BaseChatModel | ||
| from langchain_core.tools import BaseTool | ||
| from langgraph.store.base import BaseStore | ||
| from langgraph.types import Checkpointer | ||
|
|
||
| from langchain.agents.factory import create_agent | ||
| from langchain.agents.middleware import AgentMiddleware | ||
| from langchain.agents.middleware.filesystem import FilesystemMiddleware | ||
| from langchain.agents.middleware.human_in_the_loop import HumanInTheLoopMiddleware, ToolConfig | ||
| from langchain.agents.middleware.planning import PlanningMiddleware | ||
| from langchain.agents.middleware.prompt_caching import AnthropicPromptCachingMiddleware | ||
| from langchain.agents.middleware.subagents import ( | ||
| CustomSubAgent, | ||
| DefinedSubAgent, | ||
| SubAgentMiddleware, | ||
| ) | ||
| from langchain.agents.middleware.summarization import SummarizationMiddleware | ||
|
|
||
| BASE_AGENT_PROMPT = """ | ||
| In order to complete the objective that the user asks of you, you have access to a number of standard tools. # noqa: E501 | ||
nhuang-lc marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """ | ||
|
|
||
|
|
||
| def get_default_model() -> ChatAnthropic: | ||
| """Get the default model for deep agents. | ||
|
|
||
| Returns: | ||
| ChatAnthropic instance configured with Claude Sonnet 4. | ||
| """ | ||
| return ChatAnthropic( | ||
| model_name="claude-sonnet-4-20250514", | ||
| timeout=None, | ||
| stop=None, | ||
| model_kwargs={"max_tokens": 64000}, | ||
| ) | ||
|
|
||
|
|
||
| def agent_builder( | ||
| tools: Sequence[BaseTool | Callable | dict[str, Any]], | ||
| instructions: str, | ||
| middleware: list[AgentMiddleware] | None = None, | ||
| tool_configs: dict[str, bool | ToolConfig] | None = None, | ||
| model: str | BaseChatModel | None = None, | ||
| subagents: list[DefinedSubAgent | CustomSubAgent] | None = None, | ||
| context_schema: type[Any] | None = None, | ||
| checkpointer: Checkpointer | None = None, | ||
| store: BaseStore | None = None, | ||
| *, | ||
| use_longterm_memory: bool = False, | ||
| is_async: bool = False, | ||
| ) -> Any: | ||
| """Build a deep agent with standard middleware stack. | ||
|
|
||
| Args: | ||
| tools: The tools the agent should have access to. | ||
| instructions: The instructions for the agent system prompt. | ||
| middleware: Additional middleware to apply after standard middleware. | ||
| tool_configs: Optional tool interrupt configurations. | ||
| model: The model to use. Defaults to Claude Sonnet 4. | ||
| subagents: Optional list of subagent configurations. | ||
| context_schema: Optional schema for the agent context. | ||
| checkpointer: Optional checkpointer for state persistence. | ||
| store: Optional store for longterm memory. | ||
| use_longterm_memory: Whether to enable longterm memory features. | ||
| is_async: Whether to create async subagent tools. | ||
|
|
||
| Returns: | ||
| A configured agent with deep agent middleware stack. | ||
| """ | ||
| if model is None: | ||
| model = get_default_model() | ||
|
|
||
| deepagent_middleware = [ | ||
| PlanningMiddleware(), | ||
| FilesystemMiddleware( | ||
| use_longterm_memory=use_longterm_memory, | ||
| ), | ||
| SubAgentMiddleware( | ||
| default_subagent_tools=tools, | ||
| default_subagent_model=model, | ||
| subagents=subagents if subagents is not None else [], | ||
| is_async=is_async, | ||
| ), | ||
| SummarizationMiddleware( | ||
| model=model, | ||
| max_tokens_before_summary=120000, | ||
| messages_to_keep=20, | ||
| ), | ||
| AnthropicPromptCachingMiddleware(ttl="5m", unsupported_model_behavior="ignore"), | ||
| ] | ||
| if tool_configs is not None: | ||
| deepagent_middleware.append(HumanInTheLoopMiddleware(interrupt_on=tool_configs)) | ||
| if middleware is not None: | ||
| deepagent_middleware.extend(middleware) | ||
|
|
||
| return create_agent( | ||
| model, | ||
| system_prompt=instructions + "\n\n" + BASE_AGENT_PROMPT, | ||
| tools=tools, | ||
| middleware=deepagent_middleware, | ||
| context_schema=context_schema, | ||
| checkpointer=checkpointer, | ||
| store=store, | ||
| ) | ||
|
|
||
|
|
||
| def create_deep_agent( | ||
| tools: Sequence[BaseTool | Callable | dict[str, Any]] | None = None, | ||
| instructions: str = "", | ||
nhuang-lc marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| middleware: list[AgentMiddleware] | None = None, | ||
| model: str | BaseChatModel | None = None, | ||
| subagents: list[DefinedSubAgent | CustomSubAgent] | None = None, | ||
| context_schema: type[Any] | None = None, | ||
| checkpointer: Checkpointer | None = None, | ||
| store: BaseStore | None = None, | ||
| *, | ||
| use_longterm_memory: bool = False, | ||
| tool_configs: dict[str, bool | ToolConfig] | None = None, | ||
| ) -> Any: | ||
| """Create a deep agent. | ||
|
|
||
| This agent will by default have access to a tool to write todos (write_todos), | ||
| four file editing tools: write_file, ls, read_file, edit_file, and a tool to call | ||
| subagents. | ||
|
|
||
| Args: | ||
| tools: The tools the agent should have access to. | ||
| instructions: The additional instructions the agent should have. Will go in | ||
| the system prompt. | ||
| middleware: Additional middleware to apply after standard middleware. | ||
| model: The model to use. | ||
| subagents: The subagents to use. Each subagent should be a dictionary with the | ||
| following keys: | ||
| - `name` | ||
| - `description` (used by the main agent to decide whether to call the | ||
| sub agent) | ||
| - `prompt` (used as the system prompt in the subagent) | ||
| - (optional) `tools` | ||
| - (optional) `model` (either a LanguageModelLike instance or dict | ||
| settings) | ||
| - (optional) `middleware` (list of AgentMiddleware) | ||
| context_schema: The schema of the deep agent. | ||
| checkpointer: Optional checkpointer for persisting agent state between runs. | ||
| store: Optional store for persisting longterm memories. | ||
| use_longterm_memory: Whether to use longterm memory - you must provide a store | ||
| in order to use longterm memory. | ||
| tool_configs: Optional Dict[str, HumanInTheLoopConfig] mapping tool names to | ||
| interrupt configs. | ||
|
|
||
| Returns: | ||
| A configured deep agent. | ||
| """ | ||
| if tools is None: | ||
| tools = [] | ||
| return agent_builder( | ||
| tools=tools, | ||
| instructions=instructions, | ||
| middleware=middleware, | ||
| model=model, | ||
| subagents=subagents, | ||
| context_schema=context_schema, | ||
| checkpointer=checkpointer, | ||
| store=store, | ||
| use_longterm_memory=use_longterm_memory, | ||
| tool_configs=tool_configs, | ||
| is_async=False, | ||
| ) | ||
|
|
||
|
|
||
| def async_create_deep_agent( | ||
| tools: Sequence[BaseTool | Callable | dict[str, Any]] | None = None, | ||
| instructions: str = "", | ||
| middleware: list[AgentMiddleware] | None = None, | ||
| model: str | BaseChatModel | None = None, | ||
| subagents: list[DefinedSubAgent | CustomSubAgent] | None = None, | ||
| context_schema: type[Any] | None = None, | ||
| checkpointer: Checkpointer | None = None, | ||
| store: BaseStore | None = None, | ||
| *, | ||
| use_longterm_memory: bool = False, | ||
| tool_configs: dict[str, bool | ToolConfig] | None = None, | ||
| ) -> Any: | ||
| """Create an async deep agent. | ||
|
|
||
| This agent will by default have access to a tool to write todos (write_todos), | ||
| four file editing tools: write_file, ls, read_file, edit_file, and a tool to call | ||
| subagents. | ||
|
|
||
| Args: | ||
| tools: The tools the agent should have access to. | ||
| instructions: The additional instructions the agent should have. Will go in | ||
| the system prompt. | ||
| middleware: Additional middleware to apply after standard middleware. | ||
| model: The model to use. | ||
| subagents: The subagents to use. Each subagent should be a dictionary with the | ||
| following keys: | ||
| - `name` | ||
| - `description` (used by the main agent to decide whether to call the | ||
| sub agent) | ||
| - `prompt` (used as the system prompt in the subagent) | ||
| - (optional) `tools` | ||
| - (optional) `model` (either a LanguageModelLike instance or dict | ||
| settings) | ||
| - (optional) `middleware` (list of AgentMiddleware) | ||
| context_schema: The schema of the deep agent. | ||
| checkpointer: Optional checkpointer for persisting agent state between runs. | ||
| store: Optional store for persisting longterm memories. | ||
| use_longterm_memory: Whether to use longterm memory - you must provide a store | ||
| in order to use longterm memory. | ||
| tool_configs: Optional Dict[str, HumanInTheLoopConfig] mapping tool names to | ||
| interrupt configs. | ||
|
|
||
| Returns: | ||
| A configured deep agent with async subagent tools. | ||
| """ | ||
| if tools is None: | ||
| tools = [] | ||
| return agent_builder( | ||
| tools=tools, | ||
| instructions=instructions, | ||
| middleware=middleware, | ||
| model=model, | ||
| subagents=subagents, | ||
| context_schema=context_schema, | ||
| checkpointer=checkpointer, | ||
| store=store, | ||
| use_longterm_memory=use_longterm_memory, | ||
| tool_configs=tool_configs, | ||
| is_async=True, | ||
| ) | ||
nhuang-lc marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.