Home / Function/ create_react_agent() — langchain Function Reference

create_react_agent() — langchain Function Reference

Architecture documentation for the create_react_agent() function in agent.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  08a6b1c9_3fbf_094e_7607_707e89cc20e8["create_react_agent()"]
  2802721f_971c_bf69_b405_2e0a4f956ecb["agent.py"]
  08a6b1c9_3fbf_094e_7607_707e89cc20e8 -->|defined in| 2802721f_971c_bf69_b405_2e0a4f956ecb
  style 08a6b1c9_3fbf_094e_7607_707e89cc20e8 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/langchain/langchain_classic/agents/react/agent.py lines 16–150

def create_react_agent(
    llm: BaseLanguageModel,
    tools: Sequence[BaseTool],
    prompt: BasePromptTemplate,
    output_parser: AgentOutputParser | None = None,
    tools_renderer: ToolsRenderer = render_text_description,
    *,
    stop_sequence: bool | list[str] = True,
) -> Runnable:
    r"""Create an agent that uses ReAct prompting.

    Based on paper "ReAct: Synergizing Reasoning and Acting in Language Models"
    (https://arxiv.org/abs/2210.03629)

    !!! warning

        This implementation is based on the foundational ReAct paper but is older and
        not well-suited for production applications.

        For a more robust and feature-rich implementation, we recommend using the
        `create_agent` function from the `langchain` library.

        See the
        [reference doc](https://reference.langchain.com/python/langchain/agents/)
        for more information.

    Args:
        llm: LLM to use as the agent.
        tools: Tools this agent has access to.
        prompt: The prompt to use. See Prompt section below for more.
        output_parser: AgentOutputParser for parse the LLM output.
        tools_renderer: This controls how the tools are converted into a string and
            then passed into the LLM.
        stop_sequence: bool or list of str.
            If `True`, adds a stop token of "Observation:" to avoid hallucinates.
            If `False`, does not add a stop token.
            If a list of str, uses the provided list as the stop tokens.

            You may to set this to False if the LLM you are using
            does not support stop sequences.

    Returns:
        A Runnable sequence representing an agent. It takes as input all the same input
        variables as the prompt passed in does. It returns as output either an
        AgentAction or AgentFinish.

    Examples:
        ```python
        from langchain_classic import hub
        from langchain_openai import OpenAI
        from langchain_classic.agents import AgentExecutor, create_react_agent

        prompt = hub.pull("hwchase17/react")
        model = OpenAI()
        tools = ...

        agent = create_react_agent(model, tools, prompt)
        agent_executor = AgentExecutor(agent=agent, tools=tools)

        agent_executor.invoke({"input": "hi"})

        # Use with chat history
        from langchain_core.messages import AIMessage, HumanMessage

        agent_executor.invoke(
            {
                "input": "what's my name?",
                # Notice that chat_history is a string
                # since this prompt is aimed at LLMs, not chat models
                "chat_history": "Human: My name is Bob\nAI: Hello Bob!",
            }
        )
        ```

    Prompt:

        The prompt must have input keys:
            * `tools`: contains descriptions and arguments for each tool.
            * `tool_names`: contains all tool names.
            * `agent_scratchpad`: contains previous agent actions and tool outputs as a
                string.

Subdomains

Frequently Asked Questions

What does create_react_agent() do?
create_react_agent() is a function in the langchain codebase, defined in libs/langchain/langchain_classic/agents/react/agent.py.
Where is create_react_agent() defined?
create_react_agent() is defined in libs/langchain/langchain_classic/agents/react/agent.py at line 16.

Analyze Your Own Codebase

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

Try Supermodel Free