Code Examples
This page shows reusable Python and Node.js plugin patterns for request interception, event logging, multi-surface policies, and framework-neutral behavior. The same plugin contract applies to Rust. Refer to the Header Plugin Example for Rust-specific registration guidance.
Dynamic Header Injection
Use an LLM request intercept when a plugin needs to inject tenant or routing metadata into every provider request.
LLM request intercepts receive name, request, and annotated. Python
intercepts receive an immutable request, so they return a new request with the
required changes. Node.js intercepts receive a normal JavaScript request
object; return a complete outcome containing the intended request rather than
relying on in-place mutation. Rust intercepts receive a mutable request.
The following complete examples define, register, validate, and activate a plugin that adds a request header. Each example clears the active configuration during shutdown.
Python
Node.js
This pattern is useful for:
- Tenant identity
- Trace correlation
- Region or deployment routing
Subscriber Logging Pattern
Use a subscriber-oriented plugin when the component should watch the full lifecycle rather than rewrite requests. The following examples log each event in Python and Node.js. They do not transform events into OpenInference spans. Refer to OpenInference for the built-in OpenInference exporter.
Python
Node.js
This is the right pattern when the component:
- Logs events across tools and LLMs
- Adds application-specific logging or filtering
- Should not change execution behavior
Multi-Surface Policy Bundle
A plugin can register more than one runtime surface when one configuration document controls a related behavior bundle.
The following Python example installs a subscriber and an LLM request intercept from one component configuration:
This bundle registers:
- An event-logging subscriber
- An LLM request intercept that adds correlation metadata
Use this pattern when one component makes the configured behavior easier to reason about than several unrelated plugin components. Keep each registered surface small. Make the component configuration explicit about which surfaces are enabled.
Framework-Neutral Plugin Design
Plugins can stay framework-agnostic if they operate on the normalized runtime data rather than framework-specific objects.
Good examples:
- Rewrite provider headers
- Emit tracing data
- Attach scheduling hints
- Apply cross-framework safety policies