- Rewrite LLM provider request kwargs before Mibyan calls the provider.
- Rewrite tool arguments before guardrails, approval checks, hooks, and tool execution see them.
- Wrap the actual LLM execution callback while preserving Mibyan retry, streaming, interrupt, and hook behavior.
- Wrap the actual tool execution callback while preserving Mibyan guardrails, approval, post-tool hooks, and tool-result transformation.
Contract
Plugins register middleware fromregister(ctx):
telemetry_schema_version: currentlymibyan.observer.v1middleware_schema_version: currentlymibyan.middleware.v1- Runtime context such as
session_id,task_id,turn_id,api_request_id,provider,model,api_mode,tool_name, andtool_call_idwhen applicable.
Request middleware can return optional trace fields:
middleware_trace.
Execution middleware receives a next_call callback. Call it to continue the
chain:
Execution Order
LLM Calls
For each provider request, Mibyan applies middleware in this order:- Build provider kwargs from the current conversation.
- Apply
llm_requestmiddleware. - Emit
pre_api_requestobserver hooks with the effective request. - Run provider execution through
llm_executionmiddleware. - Emit
post_api_requestorapi_request_errorobserver hooks.
messages or
Responses API input, model settings, tool definitions, stream options, and
provider-specific options. Execution middleware receives the same effective
request plus next_call.
Tool Calls
For each tool call, Mibyan applies middleware in this order:- Parse and coerce model-provided tool arguments.
- Apply
tool_requestmiddleware. - Run the normal Mibyan pre-execution path against the effective arguments: tool availability checks, observer block directives, guardrails, and approval checks.
- Run tool execution through
tool_executionmiddleware. - Emit
post_tool_callobserver hooks. - Apply
transform_tool_resulthooks before the result is appended back into conversation context.
Enablement
Middleware only runs for enabled plugins. For a bundled plugin:mibyan_HOME for plugin enablement and the
agent run:
Generic Plugin Examples
The examples below are intentionally small. They show the middleware contract shape without depending on NeMo Relay.LLM Request Middleware
This plugin tags provider requests and records a middleware trace entry:pre_api_request, provider execution, and
post_api_request.
Tool Request Middleware
This plugin constrainsterminal calls to a known working directory:
workdir.
LLM Execution Middleware
This plugin wraps the provider call and preserves the raw provider response:Tool Execution Middleware
This plugin wraps tool execution while preserving the tool result:next_call(modified_args) to pass a changed
payload to later middleware and the base tool dispatcher.
Plugin-specific examples should live with the plugin that owns the behavior.
NeMo Relay execution middleware is installed through Relay’s discovered user
and system configuration, or through an explicit plugins.toml selected with
mibyan_NEMO_RELAY_PLUGINS_TOML; see
Relay shared metrics.
Safety Notes
- Middleware should be deterministic for the same input unless it is explicitly routing to a dynamic external system.
- Request middleware should return complete replacement payloads, not partial patches.
- Execution middleware should call
next_call(...)exactly once unless it is intentionally short-circuiting execution. - If execution middleware raises before calling
next_call(...), Mibyan treats that as middleware failure and continues with the remaining middleware chain and base execution. - If execution middleware calls
next_call(...)successfully and then raises during post-processing, Mibyan preserves the downstream result and does not run the provider or tool a second time. - If downstream provider or tool execution fails, middleware may let that error
propagate or translate it deliberately. Mibyan does not convert downstream
failure into a successful
Noneresult. - Tool request middleware runs before approvals. If it mutates file paths, commands, URLs, or arguments, the mutated values are what guardrails and approvals evaluate.
- Observer hooks remain the right place for read-only telemetry. Use middleware only when a plugin needs to alter or wrap behavior.

