MemoryMiddleware¶
MemoryMiddleware
¶
Bases: AgentMiddleware
Middleware for loading agent memory from AGENTS.md files.
Loads memory content from configured sources and injects into the system prompt.
Supports multiple sources that are combined together.
| PARAMETER | DESCRIPTION |
|---|---|
backend
|
Backend instance or factory function for file operations.
TYPE:
|
sources
|
List of |
| METHOD | DESCRIPTION |
|---|---|
__init__ |
Initialize the memory middleware. |
before_agent |
Load memory content before agent execution (synchronous). |
abefore_agent |
Load memory content before agent execution. |
modify_request |
Inject memory content into the system message. |
wrap_model_call |
Wrap model call to inject memory into system prompt. |
awrap_model_call |
Async wrap model call to inject memory into system prompt. |
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 memory 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 memory file paths to load (e.g., |
before_agent
¶
before_agent(
state: MemoryState, runtime: Runtime, config: RunnableConfig
) -> MemoryStateUpdate | None
Load memory content before agent execution (synchronous).
Loads memory from all configured sources and stores in state. Only loads if not already present in state.
| PARAMETER | DESCRIPTION |
|---|---|
state
|
Current agent state.
TYPE:
|
runtime
|
Runtime context.
TYPE:
|
config
|
Runnable config.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
MemoryStateUpdate | None
|
State update with memory_contents populated. |
abefore_agent
async
¶
abefore_agent(
state: MemoryState, runtime: Runtime, config: RunnableConfig
) -> MemoryStateUpdate | None
Load memory content before agent execution.
Loads memory from all configured sources and stores in state. Only loads if not already present in state.
| PARAMETER | DESCRIPTION |
|---|---|
state
|
Current agent state.
TYPE:
|
runtime
|
Runtime context.
TYPE:
|
config
|
Runnable config.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
MemoryStateUpdate | None
|
State update with memory_contents populated. |
modify_request
¶
modify_request(request: ModelRequest) -> ModelRequest
Inject memory content into the system message.
| PARAMETER | DESCRIPTION |
|---|---|
request
|
Model request to modify.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ModelRequest
|
Modified request with memory injected into system message. |
wrap_model_call
¶
wrap_model_call(
request: ModelRequest, handler: Callable[[ModelRequest], ModelResponse]
) -> ModelResponse
Wrap model call to inject memory into 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
Async wrap model call to inject memory into system prompt.
| 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: