A middleware wraps the agent’s LLM call. Return one fromDocumentation Index
Fetch the complete documentation index at: https://docs.ixo.world/llms.txt
Use this file to discover all available pages before exploring further.
getMiddlewares(ctx) and it runs on every model turn.
Build the middleware with createMiddleware
Import Canonical source: weather-middleware.ts.
createMiddleware from langchain. Set a name (appears in error messages) and any of the three hooks.Pick the right hook
Each hook receives the LangGraph state. Use the one whose timing matches your work.For state mutations, return a partial state object from the hook — same protocol as LangChain’s
AgentMiddleware.Return it from getMiddlewares(ctx)
The runtime appends your middleware after the four always-on framework middleware.See
getMiddlewares in weather.plugin.ts.What to know before shipping
- The four always-on middleware (
tool-validation,tool-retry,page-context,safety-guardrail) always run first. They are not removable. - Plugin middleware runs in topological dependency order — encode ordering via
dependsOnif it matters. - Middleware fires per LLM turn, not per individual tool invocation. Wrap the tool handler directly for per-tool behaviour.
- Closure-scoped timers interleave across concurrent model calls. For accurate timing, push start times onto a per-call ID.
- Don’t reimplement auth in a middleware — auth runs at the HTTP layer (
AuthHeaderMiddleware), not in the agent loop.
Where to read next
Add a sub-agent
Sub-agent-scoped middleware.
State schema
What’s in
state when your hooks fire.