deepagents-architecture
Scannednpx machina-cli add skill existential-birds/beagle/deepagents-architecture --openclawDeep Agents Architecture Decisions
When to Use Deep Agents
Use Deep Agents When You Need:
- Long-horizon tasks - Complex workflows spanning dozens of tool calls
- Planning capabilities - Task decomposition before execution
- Filesystem operations - Reading, writing, and editing files
- Subagent delegation - Isolated task execution with separate context windows
- Persistent memory - Long-term storage across conversations
- Human-in-the-loop - Approval gates for sensitive operations
- Context management - Auto-summarization for long conversations
Consider Alternatives When:
| Scenario | Alternative | Why |
|---|---|---|
| Single LLM call | Direct API call | Deep Agents overhead not justified |
| Simple RAG pipeline | LangChain LCEL | Simpler abstraction |
| Custom graph control flow | LangGraph directly | More flexibility |
| No file operations needed | create_react_agent | Lighter weight |
| Stateless tool use | Function calling | No middleware needed |
Backend Selection
Backend Comparison
| Backend | Persistence | Use Case | Requires |
|---|---|---|---|
StateBackend | Ephemeral (per-thread) | Working files, temp data | Nothing (default) |
FilesystemBackend | Disk | Local development, real files | root_dir path |
StoreBackend | Cross-thread | User preferences, knowledge bases | LangGraph store |
CompositeBackend | Mixed | Hybrid memory patterns | Multiple backends |
Backend Decision Tree
Need real disk access?
├─ Yes → FilesystemBackend(root_dir="/path")
└─ No
└─ Need persistence across conversations?
├─ Yes → Need mixed ephemeral + persistent?
│ ├─ Yes → CompositeBackend
│ └─ No → StoreBackend
└─ No → StateBackend (default)
CompositeBackend Routing
Route different paths to different storage backends:
from deepagents import create_deep_agent
from deepagents.backends import CompositeBackend, StateBackend, StoreBackend
agent = create_deep_agent(
backend=CompositeBackend(
default=StateBackend(), # Working files (ephemeral)
routes={
"/memories/": StoreBackend(store=store), # Persistent
"/preferences/": StoreBackend(store=store), # Persistent
},
),
)
Subagent Architecture
When to Use Subagents
Use subagents when:
- Task is complex, multi-step, and can run independently
- Task requires heavy context that would bloat the main thread
- Multiple independent tasks can run in parallel
- You need isolated execution (sandboxing)
- You only care about the final result, not intermediate steps
Don't use subagents when:
- Task is trivial (few tool calls)
- You need to see intermediate reasoning
- Splitting adds latency without benefit
- Task depends on main thread state mid-execution
Subagent Patterns
Pattern 1: Parallel Research
┌─────────────┐
│ Orchestrator│
└──────┬──────┘
┌──────────┼──────────┐
▼ ▼ ▼
┌──────┐ ┌──────┐ ┌──────┐
│Task A│ │Task B│ │Task C│
└──┬───┘ └──┬───┘ └──┬───┘
└──────────┼──────────┘
▼
┌─────────────┐
│ Synthesize │
└─────────────┘
Best for: Research on multiple topics, parallel analysis, batch processing.
Pattern 2: Specialized Agents
research_agent = {
"name": "researcher",
"description": "Deep research on complex topics",
"system_prompt": "You are an expert researcher...",
"tools": [web_search, document_reader],
}
coder_agent = {
"name": "coder",
"description": "Write and review code",
"system_prompt": "You are an expert programmer...",
"tools": [code_executor, linter],
}
agent = create_deep_agent(subagents=[research_agent, coder_agent])
Best for: Domain-specific expertise, different tool sets per task type.
Pattern 3: Pre-compiled Subagents
from deepagents import CompiledSubAgent, create_deep_agent
# Use existing LangGraph graph as subagent
custom_graph = create_react_agent(model=..., tools=...)
agent = create_deep_agent(
subagents=[CompiledSubAgent(
name="custom-workflow",
description="Runs specialized workflow",
runnable=custom_graph
)]
)
Best for: Reusing existing LangGraph graphs, complex custom workflows.
Middleware Architecture
Built-in Middleware Stack
Deep Agents applies middleware in this order:
- TodoListMiddleware - Task planning with
write_todos/read_todos - FilesystemMiddleware - File ops:
ls,read_file,write_file,edit_file,glob,grep,execute - SubAgentMiddleware - Delegation via
tasktool - SummarizationMiddleware - Auto-summarizes at ~85% context or 170k tokens
- AnthropicPromptCachingMiddleware - Caches system prompts (Anthropic only)
- PatchToolCallsMiddleware - Fixes dangling tool calls from interruptions
- HumanInTheLoopMiddleware - Pauses for approval (if
interrupt_onconfigured)
Custom Middleware Placement
from langchain.agents.middleware import AgentMiddleware
class MyMiddleware(AgentMiddleware):
tools = [my_custom_tool]
def transform_request(self, request):
# Modify system prompt, inject context
return request
def transform_response(self, response):
# Post-process, log, filter
return response
# Custom middleware added AFTER built-in stack
agent = create_deep_agent(middleware=[MyMiddleware()])
Middleware vs Tools Decision
| Need | Use Middleware | Use Tools |
|---|---|---|
| Inject system prompt content | ✅ | ❌ |
| Add tools dynamically | ✅ | ❌ |
| Transform requests/responses | ✅ | ❌ |
| Standalone capability | ❌ | ✅ |
| User-invokable action | ❌ | ✅ |
Subagent Middleware Inheritance
Subagents receive their own middleware stack by default:
- TodoListMiddleware
- FilesystemMiddleware (shared backend)
- SummarizationMiddleware
- AnthropicPromptCachingMiddleware
- PatchToolCallsMiddleware
Override with default_middleware=[] in SubAgentMiddleware or per-subagent middleware key.
Architecture Decision Checklist
Before implementing:
- Is Deep Agents the right tool? (vs LangGraph directly, vs simpler agent)
- Backend strategy chosen?
- Ephemeral only → StateBackend (default)
- Need disk access → FilesystemBackend
- Need cross-thread persistence → StoreBackend or CompositeBackend
- Subagent strategy defined?
- Which tasks benefit from isolation?
- Custom subagents with specialized tools/prompts?
- Parallel execution opportunities identified?
- Human-in-the-loop points defined?
- Which tools need approval?
- Approval flow (approve/edit/reject)?
- Custom middleware needed?
- System prompt injection?
- Request/response transformation?
- Context management considered?
- Long conversations → summarization triggers
- Large file handling → use references
- Checkpointing strategy? (for persistence/resume)
Source
git clone https://github.com/existential-birds/beagle/blob/main/plugins/beagle-ai/skills/deepagents-architecture/SKILL.mdView on GitHub Overview
Guides architectural decisions for Deep Agents applications, including when to use Deep Agents vs alternatives, backend strategies, subagent design, and middleware choices. It covers long-horizon tasks, planning, file operations, persistent memory, human-in-the-loop, and context management to help build scalable, maintainable systems.
How This Skill Works
The skill helps you evaluate use cases, select appropriate backends (StateBackend, FilesystemBackend, StoreBackend, CompositeBackend), and apply structured patterns for subagents. It also provides a backend decision tree and routing example to implement persistent vs ephemeral data flows and parallel subagent execution where needed.
When to Use It
- You need long-horizon tasks with complex tool usage and planning capabilities.
- You require subagent delegation for isolated execution with separate context windows.
- You need persistent memory or long-term storage across conversations.
- You want human-in-the-loop approvals for sensitive operations.
- You manage long-running contexts that require auto-summarization and context management.
Quick Start
- Step 1: Assess your task requirements (horizon, planning, files, memory, parallelism, isolation).
- Step 2: Choose backends (StateBackend, FilesystemBackend, StoreBackend, or CompositeBackend) and define routes for persistent vs ephemeral data.
- Step 3: If appropriate, implement subagent patterns (Parallel Research or Specialized Agents) and wire them with clear prompts and tools.
Best Practices
- Define backend requirements up front: ephemeral work files vs. persistent data, memory, and routing needs.
- Choose a backend architecture based on persistence: StateBackend for no disk, FilesystemBackend for real files, StoreBackend for cross-thread memory, CompositeBackend for mixed patterns.
- Use explicit routing in CompositeBackend to separate memories and preferences from transient work data.
- Use subagents for complex, multi-step tasks only when isolation and parallelism add value to latency or clarity.
- Validate architecture with a minimal viable setup before scaling, then iterate on backend choices and subagent patterns.
Example Use Cases
- Research hub uses Parallel Research pattern to run multiple analyses in parallel and synthesize results.
- Code assistant employs Specialized Agents (e.g., a coder agent) to write and review code with dedicated system prompts.
- A user-facing assistant retains preferences and knowledge across sessions via StoreBackend for cross-session memory.
- An environment uses CompositeBackend to mix ephemeral working files with persistent memories and user data.
- A sensitive workflow enforces human-in-the-loop approvals before executing critical actions.