Home / Class/ AgentExecutor Class — langchain Architecture

AgentExecutor Class — langchain Architecture

Architecture documentation for the AgentExecutor class in agent.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  ec1d7866_e6a8_7cbe_b150_f82463cf2c7f["AgentExecutor"]
  f3cef70e_11b0_61c9_7ec0_7308f4b45056["Chain"]
  ec1d7866_e6a8_7cbe_b150_f82463cf2c7f -->|extends| f3cef70e_11b0_61c9_7ec0_7308f4b45056
  4a62481c_02cb_a5de_1833_50669d5351a6["Runnable"]
  ec1d7866_e6a8_7cbe_b150_f82463cf2c7f -->|extends| 4a62481c_02cb_a5de_1833_50669d5351a6
  21559aaf_24e1_3178_8193_1719e6ef11ba["AgentFinish"]
  ec1d7866_e6a8_7cbe_b150_f82463cf2c7f -->|extends| 21559aaf_24e1_3178_8193_1719e6ef11ba
  daca522e_1d40_6f23_5fc4_253b7aed0c06["AgentStep"]
  ec1d7866_e6a8_7cbe_b150_f82463cf2c7f -->|extends| daca522e_1d40_6f23_5fc4_253b7aed0c06
  53553bfd_1550_1d23_d247_d865657bb39e["AgentAction"]
  ec1d7866_e6a8_7cbe_b150_f82463cf2c7f -->|extends| 53553bfd_1550_1d23_d247_d865657bb39e
  0ebc163d_9dba_a521_0e2e_0dd45f356b3e["agent.py"]
  ec1d7866_e6a8_7cbe_b150_f82463cf2c7f -->|defined in| 0ebc163d_9dba_a521_0e2e_0dd45f356b3e
  2f032c7d_dbed_338b_0f1c_e8a0153b3603["from_agent_and_tools()"]
  ec1d7866_e6a8_7cbe_b150_f82463cf2c7f -->|method| 2f032c7d_dbed_338b_0f1c_e8a0153b3603
  67c19ee5_afc1_345e_5c95_d54fc7753816["validate_tools()"]
  ec1d7866_e6a8_7cbe_b150_f82463cf2c7f -->|method| 67c19ee5_afc1_345e_5c95_d54fc7753816
  9a46227b_f176_308f_d291_4b9030dbf42c["validate_runnable_agent()"]
  ec1d7866_e6a8_7cbe_b150_f82463cf2c7f -->|method| 9a46227b_f176_308f_d291_4b9030dbf42c
  683924f4_71dc_3460_d5e9_388914c2234b["_action_agent()"]
  ec1d7866_e6a8_7cbe_b150_f82463cf2c7f -->|method| 683924f4_71dc_3460_d5e9_388914c2234b
  2c0beeee_11ac_2f5c_3c1e_d620dfb030ee["save()"]
  ec1d7866_e6a8_7cbe_b150_f82463cf2c7f -->|method| 2c0beeee_11ac_2f5c_3c1e_d620dfb030ee
  77ed720d_156a_96b5_fac1_3fa9c1502a09["save_agent()"]
  ec1d7866_e6a8_7cbe_b150_f82463cf2c7f -->|method| 77ed720d_156a_96b5_fac1_3fa9c1502a09
  a8de6699_0db2_d0aa_833a_416463999c63["iter()"]
  ec1d7866_e6a8_7cbe_b150_f82463cf2c7f -->|method| a8de6699_0db2_d0aa_833a_416463999c63
  0fae352e_4923_b6eb_ef9e_1f50e7d509e3["input_keys()"]
  ec1d7866_e6a8_7cbe_b150_f82463cf2c7f -->|method| 0fae352e_4923_b6eb_ef9e_1f50e7d509e3

Relationship Graph

Source Code

libs/langchain/langchain_classic/agents/agent.py lines 1012–1792

class AgentExecutor(Chain):
    """Agent that is using tools."""

    agent: BaseSingleActionAgent | BaseMultiActionAgent | Runnable
    """The agent to run for creating a plan and determining actions
    to take at each step of the execution loop."""
    tools: Sequence[BaseTool]
    """The valid tools the agent can call."""
    return_intermediate_steps: bool = False
    """Whether to return the agent's trajectory of intermediate steps
    at the end in addition to the final output."""
    max_iterations: int | None = 15
    """The maximum number of steps to take before ending the execution
    loop.

    Setting to 'None' could lead to an infinite loop."""
    max_execution_time: float | None = None
    """The maximum amount of wall clock time to spend in the execution
    loop.
    """
    early_stopping_method: str = "force"
    """The method to use for early stopping if the agent never
    returns `AgentFinish`. Either 'force' or 'generate'.

    `"force"` returns a string saying that it stopped because it met a
        time or iteration limit.

    `"generate"` calls the agent's LLM Chain one final time to generate
        a final answer based on the previous steps.
    """
    handle_parsing_errors: bool | str | Callable[[OutputParserException], str] = False
    """How to handle errors raised by the agent's output parser.
    Defaults to `False`, which raises the error.
    If `true`, the error will be sent back to the LLM as an observation.
    If a string, the string itself will be sent to the LLM as an observation.
    If a callable function, the function will be called with the exception as an
    argument, and the result of that function will be passed to the agent as an
    observation.
    """
    trim_intermediate_steps: (
        int | Callable[[list[tuple[AgentAction, str]]], list[tuple[AgentAction, str]]]
    ) = -1
    """How to trim the intermediate steps before returning them.
    Defaults to -1, which means no trimming.
    """

    @classmethod
    def from_agent_and_tools(
        cls,
        agent: BaseSingleActionAgent | BaseMultiActionAgent | Runnable,
        tools: Sequence[BaseTool],
        callbacks: Callbacks = None,
        **kwargs: Any,
    ) -> AgentExecutor:
        """Create from agent and tools.

        Args:
            agent: Agent to use.
            tools: Tools to use.
            callbacks: Callbacks to use.
            kwargs: Additional arguments.

        Returns:
            Agent executor object.
        """
        return cls(
            agent=agent,
            tools=tools,
            callbacks=callbacks,
            **kwargs,
        )

    @model_validator(mode="after")
    def validate_tools(self) -> Self:
        """Validate that tools are compatible with agent.

        Args:
            values: Values to validate.

        Returns:
            Validated values.

Frequently Asked Questions

What is the AgentExecutor class?
AgentExecutor is a class in the langchain codebase, defined in libs/langchain/langchain_classic/agents/agent.py.
Where is AgentExecutor defined?
AgentExecutor is defined in libs/langchain/langchain_classic/agents/agent.py at line 1012.
What does AgentExecutor extend?
AgentExecutor extends Chain, Runnable, AgentFinish, AgentStep, AgentAction.

Analyze Your Own Codebase

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

Try Supermodel Free