Skip to main content
Middleware adds behavior around model calls, tool calls, and the agent lifecycle. Like custom tools, MDA does not discover middleware automatically. Import it and pass it to the agent definition.
Managed Deep Agents is in public beta and available on LangSmith Cloud in the US region only.
Put custom middleware under middleware/, import it into the agent entry, and pass it to the agent definition:
For the full project layout, see Project structure. The managed runtime still owns backend, store, checkpointer, memory, skills, and the system prompt. Middleware should focus on agent behavior around model calls, tool calls, and lifecycle hooks.

Add middleware

Use middleware to redact PII, enforce call limits, retry failures, fall back between models, select models dynamically, or log and inspect tool calls.
1

Use prebuilt middleware

You can use LangChain prebuilt middleware directly in the agent definition:
agent.ts
2

Define custom middleware (Optional)

For a more advanced option, define custom middleware in a local module.
middleware/audit.ts
Import the middleware into the project-root agent entry and pass it in the middleware list:
agent.ts
Your middleware imports should work the same way they do in a normal local TypeScript project.

Use runtime context

Middleware can read per-run context through the normal LangChain runtime APIs. Use context for user IDs, organization IDs, feature flags, request metadata, or credentials that should not be part of the model prompt by default. For examples, see Custom middleware.

Deployment

mda dev and mda deploy copy project files into the compiled build, including modules under middleware/. Middleware is not synced to Context Hub; it ships with the agent code.

When to use middleware

For more information, see Project structure.