Overview
Plugins implement theBasePlugin class and provide callback methods for various lifecycle events:
- User messages - Intercept and modify incoming user messages
- Agent execution - Before/after agent runs
- Model calls - Before/after LLM requests and on errors
- Tool execution - Before/after tool calls and on errors
- Events - Process agent events as they occur
BasePlugin
All plugins extend theBasePlugin class:
Available Callbacks
All callbacks are optional. Returnundefined to continue normal execution, or return a value to short-circuit:
Called when a user message is received. Can modify or replace the message.Parameters:
invocationContext- Current invocation contextuserMessage- The user’s Content message
Called before agent execution starts.Parameters:
invocationContext- Current invocation context
Called after agent execution completes.Parameters:
invocationContext- Current invocation contextresult- The execution result
Called for each event during execution.Parameters:
invocationContext- Current invocation contextevent- The event being processed
Called before an agent (including sub-agents) starts.Parameters:
agent- The agent about to executecallbackContext- Callback context with invocation details
Called after an agent completes.Parameters:
agent- The agent that executedcallbackContext- Callback contextresult- Agent result
Called before each LLM request. Can short-circuit with a cached response.Parameters:
callbackContext- Callback contextllmRequest- The LLM request about to be sent
Called after each LLM response.Parameters:
callbackContext- Callback contextllmResponse- The LLM response receivedllmRequest- The original request
Called when an LLM request fails. Can provide a fallback response.Parameters:
callbackContext- Callback contextllmRequest- The failed requesterror- The error that occurred
Called before tool execution. Can modify tool arguments.Parameters:
tool- The tool about to executetoolArgs- Tool argumentstoolContext- Tool execution context
Called after tool execution. Can modify tool results.Parameters:
tool- The tool that executedtoolArgs- Tool arguments usedtoolContext- Tool execution contextresult- Tool execution result
Called when a tool fails. Can provide fallback result.Parameters:
tool- The tool that failedtoolArgs- Tool arguments usedtoolContext- Tool execution contexterror- The error that occurred
Called when the plugin manager shuts down. Clean up resources here.
PluginManager
ThePluginManager manages plugin registration and execution:
Built-in Plugins
LangfusePlugin
Integrates with Langfuse for observability and tracing:- Traces entire agent execution hierarchies
- Tracks token usage and costs
- Records tool calls and model generations
- Supports multi-agent tracing
- Automatic span creation for sub-agents
LangfusePlugin requires the
langfuse package: npm install langfuseModelFallbackPlugin
Automatically retries failed LLM requests with fallback models:- Only handles rate limit errors (429)
- Retries same model up to
maxRetriestimes - Falls back to next model in the chain
- Preserves all request parameters during fallback
ReflectAndRetryToolPlugin
Automatically retries failed tool calls with reflection guidance:- Catches tool execution errors
- Provides structured reflection guidance to the agent
- Tracks retry counts per tool
- Suggests alternative approaches after failures
- Configurable tracking scope (per-invocation or global)
INVOCATION- Count retries per agent invocationGLOBAL- Count retries globally across all invocations
ToolOutputFilterPlugin
Automatically filters large tool outputs using LLM-generated JQ filters:- Automatically detects oversized tool outputs
- Uses LLM to generate smart JQ filters
- Iteratively reduces output size
- Preserves relevant data for the query
- Adds metadata about filtering
Trigger filtering when output exceeds this many characters
Trigger filtering when output has more than this many keys
Target size after filtering
Maximum number of filtering iterations
Maximum depth for schema extraction
Creating Custom Plugins
Advanced Examples
Caching Plugin
Cache LLM responses to save costs:Metrics Plugin
Track execution metrics:Best Practices
See Also
- Telemetry - Built-in telemetry integration
- Tool Development - Creating custom tools
- Agent Builder - Building agents with plugins