SkillsMiddleware¶
SkillsMiddleware
¶
Bases: AgentMiddleware
Middleware for loading and exposing agent skills to the system prompt.
Loads skills from backend sources and injects them into the system prompt using progressive disclosure (metadata first, full content on demand).
Skills are loaded in source order with later sources overriding earlier ones.
Example
| PARAMETER | DESCRIPTION |
|---|---|
backend
|
Backend instance for file operations
TYPE:
|
sources
|
List of skill source paths. Source names are derived from the last path component. |
| METHOD | DESCRIPTION |
|---|---|
__init__ |
Initialize the skills middleware. |
modify_request |
Inject skills documentation into a model request's system message. |
before_agent |
Load skills metadata before agent execution (synchronous). |
abefore_agent |
Load skills metadata before agent execution (async). |
wrap_model_call |
Inject skills documentation into the system prompt. |
awrap_model_call |
Inject skills documentation into the system prompt (async version). |
before_model |
Logic to run before the model is called. |
abefore_model |
Async logic to run before the model is called. |
after_model |
Logic to run after the model is called. |
aafter_model |
Async logic to run after the model is called. |
after_agent |
Logic to run after the agent execution completes. |
aafter_agent |
Async logic to run after the agent execution completes. |
wrap_tool_call |
Intercept tool execution for retries, monitoring, or modification. |
awrap_tool_call |
Intercept and control async tool execution via handler callback. |
state_schema
class-attribute
instance-attribute
¶
The schema for state passed to the middleware nodes.
name
property
¶
name: str
The name of the middleware instance.
Defaults to the class name, but can be overridden for custom naming.
__init__
¶
Initialize the skills middleware.
| PARAMETER | DESCRIPTION |
|---|---|
backend
|
Backend instance or factory function that takes runtime and returns a backend.
Use a factory for StateBackend:
TYPE:
|
sources
|
List of skill source paths (e.g., ["/skills/user/", "/skills/project/"]). |
modify_request
¶
modify_request(request: ModelRequest) -> ModelRequest
Inject skills documentation into a model request's system message.
| PARAMETER | DESCRIPTION |
|---|---|
request
|
Model request to modify
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ModelRequest
|
New model request with skills documentation injected into system message |
before_agent
¶
before_agent(
state: SkillsState, runtime: Runtime, config: RunnableConfig
) -> SkillsStateUpdate | None
Load skills metadata before agent execution (synchronous).
Runs before each agent interaction to discover available skills from all configured sources. Re-loads on every call to capture any changes.
Skills are loaded in source order with later sources overriding earlier ones if they contain skills with the same name (last one wins).
| PARAMETER | DESCRIPTION |
|---|---|
state
|
Current agent state.
TYPE:
|
runtime
|
Runtime context.
TYPE:
|
config
|
Runnable config.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
SkillsStateUpdate | None
|
State update with |
abefore_agent
async
¶
abefore_agent(
state: SkillsState, runtime: Runtime, config: RunnableConfig
) -> SkillsStateUpdate | None
Load skills metadata before agent execution (async).
Runs before each agent interaction to discover available skills from all configured sources. Re-loads on every call to capture any changes.
Skills are loaded in source order with later sources overriding earlier ones if they contain skills with the same name (last one wins).
| PARAMETER | DESCRIPTION |
|---|---|
state
|
Current agent state.
TYPE:
|
runtime
|
Runtime context.
TYPE:
|
config
|
Runnable config.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
SkillsStateUpdate | None
|
State update with |
wrap_model_call
¶
wrap_model_call(
request: ModelRequest, handler: Callable[[ModelRequest], ModelResponse]
) -> ModelResponse
Inject skills documentation into the system prompt.
| PARAMETER | DESCRIPTION |
|---|---|
request
|
Model request being processed
TYPE:
|
handler
|
Handler function to call with modified request
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ModelResponse
|
Model response from handler |
awrap_model_call
async
¶
awrap_model_call(
request: ModelRequest, handler: Callable[[ModelRequest], Awaitable[ModelResponse]]
) -> ModelResponse
Inject skills documentation into the system prompt (async version).
| PARAMETER | DESCRIPTION |
|---|---|
request
|
Model request being processed
TYPE:
|
handler
|
Async handler function to call with modified request
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ModelResponse
|
Model response from handler |
before_model
¶
abefore_model
async
¶
after_model
¶
aafter_model
async
¶
after_agent
¶
aafter_agent
async
¶
wrap_tool_call
¶
wrap_tool_call(
request: ToolCallRequest,
handler: Callable[[ToolCallRequest], ToolMessage | Command[Any]],
) -> ToolMessage | Command[Any]
Intercept tool execution for retries, monitoring, or modification.
Async version is awrap_tool_call
Multiple middleware compose automatically (first defined = outermost).
Exceptions propagate unless handle_tool_errors is configured on ToolNode.
| PARAMETER | DESCRIPTION |
|---|---|
request
|
Tool call request with call Access state via
TYPE:
|
handler
|
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ToolMessage | Command[Any]
|
|
The handler Callable can be invoked multiple times for retry logic.
Each call to handler is independent and stateless.
Examples:
Modify request before execution
Retry on error (call handler multiple times)
awrap_tool_call
async
¶
awrap_tool_call(
request: ToolCallRequest,
handler: Callable[[ToolCallRequest], Awaitable[ToolMessage | Command[Any]]],
) -> ToolMessage | Command[Any]
Intercept and control async tool execution via handler callback.
The handler callback executes the tool call and returns a ToolMessage or
Command. Middleware can call the handler multiple times for retry logic, skip
calling it to short-circuit, or modify the request/response. Multiple middleware
compose with first in list as outermost layer.
| PARAMETER | DESCRIPTION |
|---|---|
request
|
Tool call request with call Access state via
TYPE:
|
handler
|
Async callable to execute the tool and returns Call this to execute the tool. Can be called multiple times for retry logic. Can skip calling it to short-circuit.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ToolMessage | Command[Any]
|
|
The handler Callable can be invoked multiple times for retry logic.
Each call to handler is independent and stateless.
Examples: