# Middleware for Managed Deep Agents

## Executive summary

Middleware is a mechanism for extending the lifecycle of Managed Deep Agents, allowing developers to implement custom behaviors such as policy enforcement, fault tolerance, and rate limiting. The demonstration covers two primary use cases: using prebuilt middleware (like `PIIMiddleware`) to redact sensitive data before it reaches the LLM, and building custom middleware from scratch using decorators like `wrap_tool_call` for logging and auditing tool usage.

## Key takeaways

- Middleware Functionality: Middleware extends the agent's lifecycle to manage behaviors like policy enforcement, fault tolerance, and rate limits when interacting with tools or the LLM.
- PII Redaction Demo: Using prebuilt `PIIMiddleware` automatically detects and redacts sensitive information (e.g., customer emails) from the input, preventing the data from reaching the LLM or being stored in LangSmith.
- Custom Middleware Development: Custom middleware can be built using decorators (e.g., `wrap_tool_call`) and hooks to intercept and log events, such as every tool call, at specific points in the agent's process.

## Technical details

- Middleware Architecture: Middleware allows developers to inject custom logic into the agent's flow, managing interactions with tools and the LLM. Potential use cases include logging, prompt transformation, retries, and guardrails.
- PII Redaction Implementation: The `PIIMiddleware` was configured to block the 'email' attribute and apply a redaction strategy to the input passing through to the LLM, ensuring the input is modified in place before model consumption.
- Custom Logging Middleware: A custom logging middleware was implemented using the `wrap_tool_call` decorator to automatically wrap and log every tool call used by the agent, demonstrating granular control over the agent's execution path.

## Practical implications

- Build engineers can integrate middleware to enforce data governance (PII redaction) and compliance requirements directly into agent workflows.
- The ability to build custom middleware allows for robust, centralized logging and auditing of all tool calls and data inputs, which is critical for debugging and performance tracking.
- Middleware provides a standardized layer for adding cross-cutting concerns (like rate limiting or error handling) without modifying core agent logic.

## Topics

Middleware, Managed Deep Agents, LLM Agents, Policy Enforcement, Data Redaction, LangGraph, Tool Calling, Managed Deep Agents middleware docs, LangSmith, LangChain Academy

Source: https://www.youtube.com/watch?v=d6aNbE-3dxo
