This directory contains examples of extending Mellea with custom plugins—hooks for observability, modification, and control over the generation pipeline.
These examples require a running Ollama instance:
ollama serveuv run quickstart.pyDemonstrates:
- Registering a single hook function
- Using
HookType.GENERATION_PRE_CALLto log before LLM calls - Minimal plugin setup (30 lines)
uv run standalone_hooks.pyDemonstrates:
- Function-based hooks without a class
- Token budget enforcement
- Generation latency monitoring
- Multiple hook types in one program
uv run class_plugin.pyDemonstrates:
- Organizing hooks in a Plugin subclass
- PII protection patterns
- Input blocking before execution
- Output scanning (observe-only)
uv run execution_modes.pyDemonstrates:
- All five PluginMode execution strategies:
SEQUENTIAL— serial, can block and modifyTRANSFORM— serial, modify-onlyAUDIT— serial, observe-onlyCONCURRENT— parallel, can blockFIRE_AND_FORGET— background, observe-only
uv run plugin_set_composition.pyDemonstrates:
- Grouping related hooks into PluginSets
- Global vs. per-session registration
- Organizing by concern (security, observability)
uv run session_scoped.pyDemonstrates:
- Global hooks (fire for all sessions)
- Per-session plugins (fire only in specific session)
- Content policy enforcement by session
uv run plugin_scoped.pyDemonstrates:
- Plugin activation for specific code blocks
plugin_scope()context manager for multiple plugins- Plugin subclasses as context managers
- PluginSet as a context manager
- Automatic deregistration on block exit
uv run payload_modification.pyDemonstrates:
- Using
modify()to change payload fields - Model copying with
model_copy(update={...}) - Handling read-only vs. writable fields
uv run tool_hooks.pyDemonstrates:
TOOL_PRE_INVOKEandTOOL_POST_INVOKEhooks- Tool allow-listing
- Argument validation and sanitization
- Tool call auditing
uv run testing_plugins.pyDemonstrates:
- Unit-testing hooks without a live session
- Constructing payloads manually
- Direct hook invocation with
await - Testing blocking and pass-through behavior
Mellea supports hooks at various stages of the generation pipeline:
GENERATION_PRE_CALL— Before LLM callGENERATION_POST_CALL— After LLM callSESSION_CREATE— On session creationSESSION_DESTROY— On session cleanup- (See
mellea.plugins.HookTypefor complete list)
Hooks: Functions that execute at specific pipeline stages, allowing observation and modification of Mellea's behavior.
Payload Modification: Hooks can transform request/response payloads before/after LLM calls.
Session Scope: Plugins can be registered globally or per-session.
Async Support: Hooks can be synchronous or asynchronous.
- ../telemetry/ — Monitoring and metrics collection
- ../requirements/ — Custom validation logic
- Documentation:
mellea.pluginsmodule