Skip to main content
Managed Deep Agents support the normal Deep Agents middleware configuration surface. Add LangChain middleware to define_deep_agent or defineDeepAgent to monitor tool calls, add guardrails, redact data, retry transient failures, or customize model calls.
Managed Deep Agents is in private beta, available on LangSmith Cloud in the US region only. Join the waitlist to request access.
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. For deeper hook, state, and context details, see Custom middleware.

Add a middleware module

Put middleware code under middleware/ in your project and import it from the agent entry. For the full project layout, see the CLI project file reference.
from collections.abc import Callable

from langchain.agents.middleware import wrap_tool_call
from langchain.messages import ToolMessage
from langchain.tools.tool_node import ToolCallRequest
from langgraph.types import Command


@wrap_tool_call
def log_tool_calls(
    request: ToolCallRequest,
    handler: Callable[[ToolCallRequest], ToolMessage | Command],
) -> ToolMessage | Command:
    print(f"Calling tool: {request.tool_call['name']}")
    result = handler(request)
    print(f"Finished tool: {request.tool_call['name']}")
    return result

Attach middleware to the agent

Import the middleware into the project-root agent entry and pass it in the middleware list.
from managed_deepagents import define_deep_agent

from middleware.audit import log_tool_calls

agent = define_deep_agent(
    model="openai:gpt-5.5",
    middleware=[log_tool_calls],
)
mda dev and mda deploy copy the project files into the compiled build. Your middleware imports should work the same way they do in a normal local Python or TypeScript project.

Use prebuilt middleware

You can also pass LangChain prebuilt middleware directly in the agent definition.
from langchain.agents.middleware import ModelCallLimitMiddleware, PIIMiddleware
from managed_deepagents import define_deep_agent

agent = define_deep_agent(
    model="openai:gpt-5.5",
    middleware=[
        PIIMiddleware("email", strategy="redact", apply_to_input=True),
        ModelCallLimitMiddleware(run_limit=50),
    ],
)
Middleware is the right place for cross-cutting behavior such as PII handling, rate limits, retry policies, model fallbacks, dynamic model selection, and tool-call monitoring.

Human-in-the-loop

Pause the agent before sensitive tool calls so a person can approve, edit, or reject them. Set interrupt_on (Python) or interruptOn (TypeScript) in the agent definition, and optionally set permissions to gate tool and filesystem access.
from managed_deepagents import define_deep_agent

from tools.customer import lookup_customer

agent = define_deep_agent(
    model="openai:gpt-5.5",
    tools=[lookup_customer],
    interrupt_on={"lookup_customer": True},
)
The interrupt_on field applies the same interrupt behavior as LangChain’s human-in-the-loop middleware. For decision types (approve, edit, reject), conditional interrupts, and permission rules, see the Deep Agents Human-in-the-loop and Permissions guides.

Respond to an interrupt

When a run hits an interrupt, it pauses and waits for a human response before continuing.
  • During local development, mda dev runs the agent in LangSmith Studio, which surfaces the interrupt so you can inspect the pending tool call and resume the run.
  • On a deployed agent, resume the paused run through the LangGraph server API with a Command(resume=...) payload. See Human-in-the-loop using server API.
During private beta, Managed Deep Agents is CLI-first and programmatic invocation is not yet documented. To resume runs programmatically from your own application, contact your LangChain team.
Human-in-the-loop needs durable thread state to pause and resume. The managed runtime owns the checkpointer, so no extra setup is required. For how threads persist, see How Managed Deep Agents work.

Use runtime context

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

Test and deploy

Test the project locally with mda dev, then deploy it with mda deploy. Open deployment traces in LangSmith to inspect model calls, tool calls, errors, and latency.