LangChain Middleware API
AgentMiddleware base class hook methods
| Method | Signature | Execution frequency | Return value |
|---|---|---|---|
| before_agent | (state, runtime) → dict or None | 1 time | State update or None |
| abefore_agent | (state, runtime) → dict or None | 1 time | State update or None |
| before_model | (state, runtime) → dict or None | Per iteration | State update or None |
| abefore_model | (state, runtime) → dict or None | Per iteration | State update or None |
| wrap_model_call | (request, handler) → ModelResponse or AIMessage | Per iteration | Model call result |
| awrap_model_call | (request, handler) → ModelResponse or AIMessage | Per iteration | Model call result |
| after_model | (state, runtime) → dict or None | Per iteration | State update or None |
| aafter_model | (state, runtime) → dict or None | Per iteration | State update or None |
| wrap_tool_call | (request, handler) → ToolMessage or Command | Per tool call | Tool execution result |
| awrap_tool_call | (request, handler) → ToolMessage or Command | Per tool call | Tool execution result |
| after_agent | (state, runtime) → dict or None | 1 time | State update or None |
| aafter_agent | (state, runtime) → dict or None | 1 time | State update or None |
Decorator overview
| Decorator | Parameters | Description |
|---|---|---|
| @before_agent | state_schema, tools, can_jump_to, name | Executed before the agent starts |
| @after_agent | state_schema, tools, can_jump_to, name | Executed after the agent ends |
| @before_model | state_schema, tools, can_jump_to, name | Executed before model invocation |
| @after_model | state_schema, tools, can_jump_to, name | Executed after model invocation |
| @wrap_model_call | state_schema, tools, name | Intercept model execution |
| @wrap_tool_call | tools, name | Intercept tool execution |
| @dynamic_prompt | None | Dynamically generate system prompt |
ModelRequest key attributes
| Attribute | Type | Description |
|---|---|---|
| model | BaseChatModel | Current model |
| messages | list[AnyMessage] | Message list (excluding system messages) |
| system_message | SystemMessage or None | Current system message |
| tools | list | Available tools list |
| tool_choice | Any | Tool selection strategy |
| response_format | ResponseFormat or None | Structured output format |
| state | dict | Agent current state |
| runtime | Runtime | Runtime context |
ModelRequest's override() method is used to create a new modified request:
Example
new_request = request.override(
model=different_model,
system_message=SystemMessage(content="new prompt"),
tools=[new_tool],
)
model=different_model,
system_message=SystemMessage(content="new prompt"),
tools=[new_tool],
)
ModelResponse structure
| Attribute | Type | Description |
|---|---|---|
| result | list[BaseMessage] | Message list returned by the model |
| structured_response | Any or None | Structured output result |
Special return value ExtendedModelResponse
| Attribute | Description |
|---|---|
| model_response | Underlying ModelResponse |
| command | Optional Command object for additional state updates |