How langchain-ai/deepagentsjs works
Project type and purpose
• This is an open-source TypeScript library (an NPM package) named “deepagents”.
• Its purpose is to provide a general-purpose framework for building “deep” AI agents on top of LangChain / LangGraph, combining planning, subagent spawning, file system tools, memory backends, and custom middleware.
High-level architecture inferred from the file tree and README
• Public API surface (src/index.ts) exposing createDeepAgent.
• Core agent assembly (src/agent.ts) that wires together:
• Middleware layers (src/middleware/*)
• Backend implementations (src/backends/*)
• Tool orchestration and subagent orchestration
• Backends (state, store, filesystem, composite) manage memory and workspace.
• Middleware modules implement planning (To-Do lists), file system tools, subagents, and patching of tool calls.
• Tests (tests/unit, tests/integration) validate each backend and middleware component.
• Examples directory demonstrates usage patterns (examples/backends, examples/research).
Key components
A. Agent Core
• createDeepAgent API
• Agent instance (LangGraph graph)
B. Middleware Layer
• TodoListMiddleware
• FilesystemMiddleware
• SubAgentMiddleware
• (Any custom middleware)
C. Backend Layer
• StateBackend (in-memory)
• StoreBackend (LangGraph persistent store)
• FilesystemBackend (local FS)
• CompositeBackend (combination of state + store)
D. External Integrations
• LangChain tools (user-defined tools like internet_search via Tavily)
• Zod schemas for tool validation
• LangGraph Store & MemorySaver for persistence
E. User Code / Subagents
• User-supplied systemPrompt, tools array
• SubAgent definitions (name, description, tools, model, middleware)
• Example agents in examples/
Relationships and interactions
• The client calls createDeepAgent({ tools, systemPrompt, backend, middleware, subagents, interruptOn }).
• createDeepAgent → instantiates Agent Core
• Agent Core → attaches default middleware (planning, FS, subagents) + any custom middleware
• Middleware Layer ↔ Backend Layer (tools invoked by middleware will call through to backends for file I/O or state access)
• Agent Core → calls LangChain graph operations and LLM models
• Agent Core ↔ External Tools (e.g., internet_search) via Schema-validated tool interfaces
• SubAgentMiddleware → spawns child agents with isolated contexts, communicating back results
• Persistent storage (StoreBackend + checkpointer) records conversation state and memory checkpoints
Architectural patterns and design principles
• Modular middleware pipeline (chain of responsibility)
• Strategy pattern for backends (pluggable storage strategies)
• Inversion of control / dependency injection for tools, backends, middleware, models
• Composition over inheritance: agent behavior assembled from small reusable pieces
• Clear separation between “agent control logic” (planning, subagents) and “data storage” (backends)
Detailed element checklist
• Public API: createDeepAgent
• Agent Core: LangGraph agent instance
• Middleware: list each default middleware and any custom ones
• Backend components: StateBackend, StoreBackend, FilesystemBackend, CompositeBackend
• Tool invocation path: LangChain tools → Zod schema → LLM call or external API
• Subagent launch: show main agent delegating to subagent with isolated context
• Persistent memory: StoreBackend ↔ MemorySaver checkpointing
• Example flows: research agent example invoking internet_search → FilesystemMiddleware writing to FS