langchain-middleware

Implement custom middleware for LangChain agents with hooks and wrap methods.

3|1|Updated Jan 31, 2026
One-click install
npx skills add https://github.com/christian-bromann/langchain-skills --skill langchain-middleware
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: langchain-middleware
Source: https://github.com/christian-bromann/langchain-skills/tree/main/skills/langchain-middleware/python
Command: npx skills add https://github.com/christian-bromann/langchain-skills --skill langchain-middleware

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Middleware provides extensibility to customize LangChain agent behavior by injecting logic at key execution points such as before_model, after_model, wrap_model_call, and wrap_tool_call.

Core Features & Use Cases

  • Hook-based customization: run code before and after model/tool calls for validation, logging, or input shaping.
  • Wrap methods: intercept and modify requests and responses to route through dynamic logic or guardrails.
  • Composability: chain multiple middleware components into a single, flexible workflow.
  • Flexible definitions: implement as a class-based middleware or decorator-based hooks, with support for async operation and context.
  • Real-world scenarios: enforce guardrails, dynamically switch models, or filter available tools based on user context.

Quick Start

Create a simple LoggingMiddleware class extending AgentMiddleware and attach it to your agent to observe before_model behavior.

Frequently Asked Questions about langchain-middleware

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I add custom logic before or after LangChain model calls?

You can inject custom logic before or after LangChain model calls by implementing middleware hooks. These hooks allow you to run validation, logging, or input shaping code at key execution points within your agent workflows.

What is the best way to intercept and modify LangChain tool calls?

The best way to intercept and modify LangChain tool calls is by using the wrap_tool_call middleware method. This allows you to intercept requests and responses to route them through dynamic logic or enforce guardrails during agent execution.

Can I chain multiple middleware components together in a LangChain agent?

Yes, you can chain multiple middleware components together into a single flexible workflow. This composability allows you to combine various class-based or decorator-based hooks for complex agent behavior.

Does LangChain middleware support asynchronous operations and context?

Yes, LangChain middleware supports asynchronous operation and context. You can define your middleware using class-based structures or decorator-based hooks while leveraging async capabilities for diverse agent workflows.

How do I enforce guardrails or dynamically switch models in Python agents?

You can enforce guardrails or dynamically switch models by designing custom middleware for LangChain agents. By wrapping model calls, you intercept requests to apply guardrails or route logic based on user context.