Home / Class/ AgentMiddleware Class — langchain Architecture

AgentMiddleware Class — langchain Architecture

Architecture documentation for the AgentMiddleware class in types.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  6d9956c5_7c84_9ec3_58a3_b6b89f42fd0b["AgentMiddleware"]
  53203ed4_0c68_b6b0_e0b3_c11fef5ff00b["_DefaultAgentState"]
  6d9956c5_7c84_9ec3_58a3_b6b89f42fd0b -->|extends| 53203ed4_0c68_b6b0_e0b3_c11fef5ff00b
  fb1284e2_76fc_661f_c16d_e084e8c2b175["types.py"]
  6d9956c5_7c84_9ec3_58a3_b6b89f42fd0b -->|defined in| fb1284e2_76fc_661f_c16d_e084e8c2b175
  796a0807_ca98_b07a_d0f5_9495bad2b904["name()"]
  6d9956c5_7c84_9ec3_58a3_b6b89f42fd0b -->|method| 796a0807_ca98_b07a_d0f5_9495bad2b904
  f4c018ad_738e_c2eb_d178_a395eaa9ebc9["before_agent()"]
  6d9956c5_7c84_9ec3_58a3_b6b89f42fd0b -->|method| f4c018ad_738e_c2eb_d178_a395eaa9ebc9
  068a7e77_8c45_691a_e837_d650d5a2797c["abefore_agent()"]
  6d9956c5_7c84_9ec3_58a3_b6b89f42fd0b -->|method| 068a7e77_8c45_691a_e837_d650d5a2797c
  8fa24211_9ced_7f13_b4af_b1f4c22856a2["before_model()"]
  6d9956c5_7c84_9ec3_58a3_b6b89f42fd0b -->|method| 8fa24211_9ced_7f13_b4af_b1f4c22856a2
  f01cadce_fd2e_3dd5_eff7_5dee90ecd7a2["abefore_model()"]
  6d9956c5_7c84_9ec3_58a3_b6b89f42fd0b -->|method| f01cadce_fd2e_3dd5_eff7_5dee90ecd7a2
  e7c83c2d_7dc7_2565_c4cb_854e8f48a97b["after_model()"]
  6d9956c5_7c84_9ec3_58a3_b6b89f42fd0b -->|method| e7c83c2d_7dc7_2565_c4cb_854e8f48a97b
  dfcea0b7_24dc_be4c_3942_e9e857ef3d8f["aafter_model()"]
  6d9956c5_7c84_9ec3_58a3_b6b89f42fd0b -->|method| dfcea0b7_24dc_be4c_3942_e9e857ef3d8f
  6eadf5ca_8c6e_ee3c_3f83_0ccdc4ee0b31["wrap_model_call()"]
  6d9956c5_7c84_9ec3_58a3_b6b89f42fd0b -->|method| 6eadf5ca_8c6e_ee3c_3f83_0ccdc4ee0b31
  a5919324_c737_4023_cd74_fb04759a1de2["awrap_model_call()"]
  6d9956c5_7c84_9ec3_58a3_b6b89f42fd0b -->|method| a5919324_c737_4023_cd74_fb04759a1de2
  a9b1f3b7_b0d0_1b5c_ed9c_e09d3a556f7e["after_agent()"]
  6d9956c5_7c84_9ec3_58a3_b6b89f42fd0b -->|method| a9b1f3b7_b0d0_1b5c_ed9c_e09d3a556f7e
  8b8c9617_7bb4_fc68_0d3f_f664e53c7251["aafter_agent()"]
  6d9956c5_7c84_9ec3_58a3_b6b89f42fd0b -->|method| 8b8c9617_7bb4_fc68_0d3f_f664e53c7251
  bf7a947a_6d95_7dc6_410a_d7c9ae0c3903["wrap_tool_call()"]
  6d9956c5_7c84_9ec3_58a3_b6b89f42fd0b -->|method| bf7a947a_6d95_7dc6_410a_d7c9ae0c3903

Relationship Graph

Source Code

libs/langchain_v1/langchain/agents/middleware/types.py lines 380–798

class AgentMiddleware(Generic[StateT, ContextT, ResponseT]):
    """Base middleware class for an agent.

    Subclass this and implement any of the defined methods to customize agent behavior
    between steps in the main agent loop.

    Type Parameters:
        StateT: The type of the agent state. Defaults to `AgentState[Any]`.
        ContextT: The type of the runtime context. Defaults to `None`.
        ResponseT: The type of the structured response. Defaults to `Any`.
    """

    state_schema: type[StateT] = cast("type[StateT]", _DefaultAgentState)
    """The schema for state passed to the middleware nodes."""

    tools: Sequence[BaseTool]
    """Additional tools registered by the middleware."""

    @property
    def name(self) -> str:
        """The name of the middleware instance.

        Defaults to the class name, but can be overridden for custom naming.
        """
        return self.__class__.__name__

    def before_agent(self, state: StateT, runtime: Runtime[ContextT]) -> dict[str, Any] | None:
        """Logic to run before the agent execution starts.

        Args:
            state: The current agent state.
            runtime: The runtime context.

        Returns:
            Agent state updates to apply before agent execution.
        """

    async def abefore_agent(
        self, state: StateT, runtime: Runtime[ContextT]
    ) -> dict[str, Any] | None:
        """Async logic to run before the agent execution starts.

        Args:
            state: The current agent state.
            runtime: The runtime context.

        Returns:
            Agent state updates to apply before agent execution.
        """

    def before_model(self, state: StateT, runtime: Runtime[ContextT]) -> dict[str, Any] | None:
        """Logic to run before the model is called.

        Args:
            state: The current agent state.
            runtime: The runtime context.

        Returns:
            Agent state updates to apply before model call.
        """

    async def abefore_model(
        self, state: StateT, runtime: Runtime[ContextT]
    ) -> dict[str, Any] | None:
        """Async logic to run before the model is called.

        Args:
            state: The agent state.
            runtime: The runtime context.

        Returns:
            Agent state updates to apply before model call.
        """

    def after_model(self, state: StateT, runtime: Runtime[ContextT]) -> dict[str, Any] | None:
        """Logic to run after the model is called.

        Args:
            state: The current agent state.
            runtime: The runtime context.

Frequently Asked Questions

What is the AgentMiddleware class?
AgentMiddleware is a class in the langchain codebase, defined in libs/langchain_v1/langchain/agents/middleware/types.py.
Where is AgentMiddleware defined?
AgentMiddleware is defined in libs/langchain_v1/langchain/agents/middleware/types.py at line 380.
What does AgentMiddleware extend?
AgentMiddleware extends _DefaultAgentState.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free