Home / Class/ Agent Class — langchain Architecture

Agent Class — langchain Architecture

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

Entity Profile

Dependency Diagram

graph TD
  37fcc3f3_2798_3648_915c_2bffdd19bff7["Agent"]
  2066c331_3189_1880_ba09_f5a3c375c553["BaseSingleActionAgent"]
  37fcc3f3_2798_3648_915c_2bffdd19bff7 -->|extends| 2066c331_3189_1880_ba09_f5a3c375c553
  21bd3731_3333_8ace_7023_5dd43ed308cf["PromptTemplate"]
  37fcc3f3_2798_3648_915c_2bffdd19bff7 -->|extends| 21bd3731_3333_8ace_7023_5dd43ed308cf
  4a8cd875_a116_c859_9f43_6737c2811ee4["FewShotPromptTemplate"]
  37fcc3f3_2798_3648_915c_2bffdd19bff7 -->|extends| 4a8cd875_a116_c859_9f43_6737c2811ee4
  ed938178_9a8a_e288_c89c_a0dfb4927ae8["AgentFinish"]
  37fcc3f3_2798_3648_915c_2bffdd19bff7 -->|extends| ed938178_9a8a_e288_c89c_a0dfb4927ae8
  0faae8c7_2812_be15_1073_b6537539cea8["agent.py"]
  37fcc3f3_2798_3648_915c_2bffdd19bff7 -->|defined in| 0faae8c7_2812_be15_1073_b6537539cea8
  4c9c9d79_8072_45ff_5de8_57ab9fd0b1c1["dict()"]
  37fcc3f3_2798_3648_915c_2bffdd19bff7 -->|method| 4c9c9d79_8072_45ff_5de8_57ab9fd0b1c1
  6b79fda4_77b9_fd1f_b200_b7c558fbcf57["get_allowed_tools()"]
  37fcc3f3_2798_3648_915c_2bffdd19bff7 -->|method| 6b79fda4_77b9_fd1f_b200_b7c558fbcf57
  827bb6dd_1d2a_33f1_e6fa_c3937e702f52["return_values()"]
  37fcc3f3_2798_3648_915c_2bffdd19bff7 -->|method| 827bb6dd_1d2a_33f1_e6fa_c3937e702f52
  1a20b7eb_9ee7_092d_f4a2_82f48788bfae["_stop()"]
  37fcc3f3_2798_3648_915c_2bffdd19bff7 -->|method| 1a20b7eb_9ee7_092d_f4a2_82f48788bfae
  f61de998_69fc_f269_1fb6_58fe712b9d99["_construct_scratchpad()"]
  37fcc3f3_2798_3648_915c_2bffdd19bff7 -->|method| f61de998_69fc_f269_1fb6_58fe712b9d99
  6f6336fa_b69b_7bc5_3826_a2de86c2f1b9["plan()"]
  37fcc3f3_2798_3648_915c_2bffdd19bff7 -->|method| 6f6336fa_b69b_7bc5_3826_a2de86c2f1b9
  44cf1bf5_a498_e468_d14a_0aa8267d73fb["aplan()"]
  37fcc3f3_2798_3648_915c_2bffdd19bff7 -->|method| 44cf1bf5_a498_e468_d14a_0aa8267d73fb
  398e54db_7082_c561_dd79_f8ba427c6a37["get_full_inputs()"]
  37fcc3f3_2798_3648_915c_2bffdd19bff7 -->|method| 398e54db_7082_c561_dd79_f8ba427c6a37
  b23b7a19_1b36_2ebd_e003_c7636a04fe36["input_keys()"]
  37fcc3f3_2798_3648_915c_2bffdd19bff7 -->|method| b23b7a19_1b36_2ebd_e003_c7636a04fe36

Relationship Graph

Source Code

libs/langchain/langchain_classic/agents/agent.py lines 704–980

class Agent(BaseSingleActionAgent):
    """Agent that calls the language model and deciding the action.

    This is driven by a LLMChain. The prompt in the LLMChain MUST include
    a variable called "agent_scratchpad" where the agent can put its
    intermediary work.
    """

    llm_chain: LLMChain
    """LLMChain to use for agent."""
    output_parser: AgentOutputParser
    """Output parser to use for agent."""
    allowed_tools: list[str] | None = None
    """Allowed tools for the agent. If `None`, all tools are allowed."""

    @override
    def dict(self, **kwargs: Any) -> builtins.dict:
        """Return dictionary representation of agent."""
        _dict = super().dict()
        del _dict["output_parser"]
        return _dict

    def get_allowed_tools(self) -> list[str] | None:
        """Get allowed tools."""
        return self.allowed_tools

    @property
    def return_values(self) -> list[str]:
        """Return values of the agent."""
        return ["output"]

    @property
    def _stop(self) -> list[str]:
        return [
            f"\n{self.observation_prefix.rstrip()}",
            f"\n\t{self.observation_prefix.rstrip()}",
        ]

    def _construct_scratchpad(
        self,
        intermediate_steps: list[tuple[AgentAction, str]],
    ) -> str | list[BaseMessage]:
        """Construct the scratchpad that lets the agent continue its thought process."""
        thoughts = ""
        for action, observation in intermediate_steps:
            thoughts += action.log
            thoughts += f"\n{self.observation_prefix}{observation}\n{self.llm_prefix}"
        return thoughts

    def plan(
        self,
        intermediate_steps: list[tuple[AgentAction, str]],
        callbacks: Callbacks = None,
        **kwargs: Any,
    ) -> AgentAction | AgentFinish:
        """Given input, decided what to do.

        Args:
            intermediate_steps: Steps the LLM has taken to date,
                along with observations.
            callbacks: Callbacks to run.
            **kwargs: User inputs.

        Returns:
            Action specifying what tool to use.
        """
        full_inputs = self.get_full_inputs(intermediate_steps, **kwargs)
        full_output = self.llm_chain.predict(callbacks=callbacks, **full_inputs)
        return self.output_parser.parse(full_output)

    async def aplan(
        self,
        intermediate_steps: list[tuple[AgentAction, str]],
        callbacks: Callbacks = None,
        **kwargs: Any,
    ) -> AgentAction | AgentFinish:
        """Async given input, decided what to do.

        Args:
            intermediate_steps: Steps the LLM has taken to date,
                along with observations.

Frequently Asked Questions

What is the Agent class?
Agent is a class in the langchain codebase, defined in libs/langchain/langchain_classic/agents/agent.py.
Where is Agent defined?
Agent is defined in libs/langchain/langchain_classic/agents/agent.py at line 704.
What does Agent extend?
Agent extends BaseSingleActionAgent, PromptTemplate, FewShotPromptTemplate, AgentFinish.

Analyze Your Own Codebase

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

Try Supermodel Free