From createAgent to DeepAgents: Middleware, Virtual File Permissions, and Subagents in Node.js
Most agent tutorials stop at a single tool call. This one lays out the runtime machinery—middleware ordering, permission denials, subagent isolation, and checkpointing—that determines whether an agent survives contact with a production codebase.
This guide starts with the bare `createAgent` loop and then adds middleware hooks one by one: logging model calls, rewriting system prompts inside `wrapModelCall`, short-circuiting before the model runs, and wrapping tool results with `Command` state updates. Each hook is placed in the lifecycle, and the article is careful about state versus context, hook ordering, and preserving `tool_call_id` when rebuilding ToolMessages.
From there it moves into DeepAgents: a pre-assembled harness that adds a virtual filesystem with path-based permission rules, a `virtualMode` backend that hides real paths, and subagents that default to isolated context. The closing sections cover production boundaries: recursion limits, retry semantics, prompt-as-policy traps, structured output failure handling, middleware tests, and a decision table for when to use `createAgent`, DeepAgents, or an explicit LangGraph graph.
Middleware order becomes business logic when multiple wrappers mutate messages or system prompts, so tests should pin the order.
The article treats 'prompt is not a permission system' as a core principle: real safety comes from tool exposure, policy checks in middleware, and sandboxing.
DeepAgents doesn't replace LangGraph; it is a pre-assembled harness that still compiles to a native LangGraph agent, so Checkpointer, Store, LangSmith, and streaming remain available.
The isolated versus fork subagent distinction reflects a token-cost tradeoff: isolation keeps the main context small but forces self-contained task descriptions.
Most of what the article calls 'production boundaries' are not agent-specific; they are the usual distributed-systems concerns of retry semantics, idempotency, and audit, applied to a model-driven loop.