Middleware

Contents

Middleware#

The LangChain middleware enables more specific workflows to be built with LangChain.

from typing import Any
from langchain_ollama import ChatOllama
from langchain.agents import create_agent
from langchain.tools import ToolRuntime
from langchain.agents.middleware import AgentState, after_agent

Agent State#

The agent state is an object that describes the history of steps taken by the agent.


To view what Agent State is the following cell defines the agent with the special middleware that saves it AgentState to the global scope.

@after_agent
def mw(state: AgentState, runtime: ToolRuntime) -> dict[str, Any] | None:
    global g_state
    g_state = state
    return None

agent = create_agent(
    model=ChatOllama(model="llama3.2:1b", temperature=0),
    middleware=[mw]
)

messages = agent.invoke({"messages": [
    {"role": "user", "content": "What is middle ware?"}
]})

The following cell prvides an overview of the state object.

g_state
{'messages': [HumanMessage(content='What is middle ware?', additional_kwargs={}, response_metadata={}, id='b5a1203e-3800-4423-b055-7a951147e9f3'),
  AIMessage(content='Middleware is a type of software that acts as an intermediary between two systems, applications, or networks. It provides a layer of abstraction and facilitates communication between different components, allowing them to work together seamlessly.\n\nThink of middleware like a bridge between two buildings. Just as a bridge connects two structures, middleware connects two systems, enabling them to exchange data, share resources, and perform tasks that would be difficult or impossible on their own.\n\nMiddleware can perform various functions, such as:\n\n1. Data integration: Migrating data from one system to another.\n2. Security: Protecting data and applications from unauthorized access.\n3. Messaging: Enabling communication between systems using standardized protocols (e.g., messaging queues).\n4. Application development: Providing a platform for building custom applications that interact with other systems.\n\nMiddleware can be used in various contexts, including:\n\n1. Enterprise software integration\n2. Web services\n3. Cloud computing\n4. Database management\n5. Cybersecurity\n\nExamples of middleware include:\n\n* Message queues (e.g., Apache Kafka, RabbitMQ)\n* Data integration tools (e.g., Talend, Informatica PowerCenter)\n* Security gateways (e.g., AWS WAF, Google Cloud Armor)\n* Messaging platforms (e.g., Amazon SQS, Microsoft Azure Service Bus)\n\nIn summary, middleware is a software layer that facilitates communication and interaction between systems, applications, or networks, enabling them to work together more efficiently.', additional_kwargs={}, response_metadata={'model': 'llama3.2:1b', 'created_at': '2025-11-05T10:29:11.329200079Z', 'done': True, 'done_reason': 'stop', 'total_duration': 16750098745, 'load_duration': 1837456465, 'prompt_eval_count': 30, 'prompt_eval_duration': 414470722, 'eval_count': 291, 'eval_duration': 14497020618, 'model_name': 'llama3.2:1b', 'model_provider': 'ollama'}, id='lc_run--d315a830-ef4c-4007-969c-b95bd4713ded-0', usage_metadata={'input_tokens': 30, 'output_tokens': 291, 'total_tokens': 321})]}

It looks exatly like as the output of the agent’s invokation - it actually is.

g_state == messages
True

However, the objects are different.

g_state is messages
False