Core Concepts
- Strategy / Provider: Each middleware implements an
IAgentMiddlewareStrategy, marked with@AgentMiddlewareStrategy('<provider>')and registered toAgentMiddlewareRegistry(plugin-sdk). - Middleware Instance: The
AgentMiddlewareobject returned bycreateMiddleware, containing state Schema, context Schema, optional tools, and lifecycle hooks. - Node-based Integration: Connect
WorkflowNodeTypeEnum.MIDDLEWAREnodes to Agents in the workflow graph. At runtime, they are loaded and executed in connection order viagetAgentMiddlewares. - Configuration & Metadata:
TAgentMiddlewareMetadescribes name, i18n labels, icon, description, and configuration Schema. The UI renders configuration panels accordingly.
Lifecycle Hooks & Capabilities
| Hook | Trigger Timing | Typical Use Cases |
|---|---|---|
beforeAgent | Before Agent starts, triggered once | Initialize state, inject system prompts, fetch external context |
beforeModel | Before each model call | Dynamically assemble messages/tools, truncate or compress context |
afterModel | After model returns, before tool execution | Adjust tool call parameters, record logs/metrics |
afterAgent | After Agent completes | Persist results, cleanup resources |
wrapModelCall | Wraps model invocation | Custom retry, caching, prompt protection, model switching |
wrapToolCall | Wraps tool invocation | Authentication, rate limiting, result post-processing, return Command for flow control |
stateSchema: Persistable middleware state (Zod object/optional/with defaults).contextSchema: Runtime-only readable context, not persisted.tools: Dynamically injectedDynamicStructuredToollist.JumpToTarget: ReturnjumpToin hooks to control jumps (e.g.,model,tools,end).
Writing a Middleware
- Define Strategy & Metadata
- Implement
createMiddleware
- Register as Plugin Module
- Runtime Integration
- During development/deployment, add the plugin to plugin environment variables.
- In the workflow editor, add middleware nodes to the Agent and configure order (execution order can be adjusted by arrangement).
Built-in Examples
- SummarizationMiddleware
Detects conversation length inbeforeModel, triggers compression and replaces historical messages; supports triggering by token/message count or context ratio, and records compression to execution trace viaWrapWorkflowNodeExecutionCommand. - todoListMiddleware
Injects
write_todostool, and appends system prompts inwrapModelCallto guide LLM in planning complex tasks. Tool returnsCommandto update Agent state.
Best Practices
- Implement only necessary hooks, keep them idempotent, avoid heavy blocking operations within hooks.
- Use
stateSchemato strictly declare persistent data, preventing state interference between different middleware/Agents. - In
wrapModelCall/wrapToolCall, prioritize calling the passedhandlerto ensure default chain works, then add custom logic. - For ratio-triggered logic, rely on model
profile.maxInputTokens; fallback to absolute token limits when unavailable (see Summarization example). - Following LangChain Middleware approach: decompose cross-cutting concerns like logging, auditing, caching, rate limiting, and gradual model switching into independent middleware, composing them by connection order.