Intercept execution and control when the handler is called. Use for retries, caching, and transformation.You decide if the handler is called zero times (short-circuit), once (normal flow), or multiple times (retry logic).Available hooks:
Middleware can extend the agent’s state with custom properties. This enables middleware to:
Track state across execution: Maintain counters, flags, or other values that persist throughout the agent’s execution lifecycle
Share data between hooks: Pass information from beforeModel to afterModel or between different middleware instances
Implement cross-cutting concerns: Add functionality like rate limiting, usage tracking, user context, or audit logging without modifying the core agent logic
Make conditional decisions: Use accumulated state to determine whether to continue execution, jump to different nodes, or modify behavior dynamically
State fields can be either public or private. Fields that start with an underscore (_) are considered private and will not be included in the agent’s result. Only public fields (those without a leading underscore) are returned.This is useful for storing internal middleware state that shouldn’t be exposed to the caller, such as temporary tracking variables or internal flags:
import { StateSchema } from "@langchain/langgraph";import * as z from "zod";const PrivateState = new StateSchema({ // Public field - included in invoke result publicCounter: z.number().default(0), // Private field - excluded from invoke result _internalFlag: z.boolean().default(false),});const middleware = createMiddleware({ name: "ExampleMiddleware", stateSchema: PrivateState, afterModel: (state) => { // Both fields are accessible during execution if (state._internalFlag) { return { publicCounter: state.publicCounter + 1 }; } return { _internalFlag: true }; },});const result = await agent.invoke({ messages: [new HumanMessage("Hello")], publicCounter: 0});// result only contains publicCounter, not _internalFlagconsole.log(result.publicCounter); // 1console.log(result._internalFlag); // undefined
Middleware can define a custom context schema to access per-invocation metadata. Unlike state, context is read-only and not persisted between invocations. This makes it ideal for:
User information: Pass user ID, roles, or preferences that don’t change during execution
Configuration overrides: Provide per-invocation settings like rate limits or feature flags
Tenant/workspace context: Include organization-specific data for multi-tenant applications
Request metadata: Pass request IDs, API keys, or other metadata needed by middleware
Define a context schema using Zod and access it via runtime.context in middleware hooks. Required fields in the context schema will be enforced at the TypeScript level, ensuring you must provide them when calling agent.invoke().
import { createAgent, createMiddleware, HumanMessage } from "langchain";import * as z from "zod";const contextSchema = z.object({ userId: z.string(), tenantId: z.string(), apiKey: z.string().optional(),});const userContextMiddleware = createMiddleware({ name: "UserContextMiddleware", contextSchema, wrapModelCall: (request, handler) => { // Access context from runtime const { userId, tenantId } = request.runtime.context; // Add user context to system message const contextText = `User ID: ${userId}, Tenant: ${tenantId}`; const newSystemMessage = request.systemMessage.concat(contextText); return handler({ ...request, systemMessage: newSystemMessage, }); },});const agent = createAgent({ model: "gpt-4.1", middleware: [userContextMiddleware], tools: [], contextSchema,});const result = await agent.invoke( { messages: [new HumanMessage("Hello")] }, // Required fields (userId, tenantId) must be provided { context: { userId: "user-123", tenantId: "acme-corp", }, });
Required context fields: When you define required fields in your contextSchema (fields without .optional() or .default()), TypeScript will enforce that these fields must be provided during agent.invoke() calls. This ensures type safety and prevents runtime errors from missing required context.
// This will cause a TypeScript error if userId or tenantId are missingconst result = await agent.invoke( { messages: [new HumanMessage("Hello")] }, { context: { userId: "user-123" } } // Error: tenantId is required);
Select relevant tools at runtime to improve performance and accuracy. This section covers filtering pre-registered tools. For registering tools that are discovered at runtime (e.g., from MCP servers), see Runtime tool registration.Benefits:
Shorter prompts - Reduce complexity by exposing only relevant tools
Better accuracy - Models choose correctly from fewer options
Permission control - Dynamically filter tools based on user access
Modify system messages in middleware using the systemMessage field in ModelRequest. It contains a SystemMessage object (even if the agent was created with a string systemPrompt).Example: Chaining middleware - Different middleware can use different approaches: