Home / Function/ create_tool_calling_agent() — langchain Function Reference

create_tool_calling_agent() — langchain Function Reference

Architecture documentation for the create_tool_calling_agent() function in base.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  91db3d30_e0b7_05ce_4068_e3c677b6e9e6["create_tool_calling_agent()"]
  5e705163_f2ec_0cda_3a5c_58793e650cbe["base.py"]
  91db3d30_e0b7_05ce_4068_e3c677b6e9e6 -->|defined in| 5e705163_f2ec_0cda_3a5c_58793e650cbe
  style 91db3d30_e0b7_05ce_4068_e3c677b6e9e6 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/langchain/langchain_classic/agents/tool_calling_agent/base.py lines 18–117

def create_tool_calling_agent(
    llm: BaseLanguageModel,
    tools: Sequence[BaseTool],
    prompt: ChatPromptTemplate,
    *,
    message_formatter: MessageFormatter = format_to_tool_messages,
) -> Runnable:
    """Create an agent that uses tools.

    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 on the expected
            input variables.
        message_formatter: Formatter function to convert (AgentAction, tool output)
            tuples into FunctionMessages.

    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.

    Example:
        ```python
        from langchain_classic.agents import (
            AgentExecutor,
            create_tool_calling_agent,
            tool,
        )
        from langchain_anthropic import ChatAnthropic
        from langchain_core.prompts import ChatPromptTemplate

        prompt = ChatPromptTemplate.from_messages(
            [
                ("system", "You are a helpful assistant"),
                ("placeholder", "{chat_history}"),
                ("human", "{input}"),
                ("placeholder", "{agent_scratchpad}"),
            ]
        )
        model = ChatAnthropic(model="claude-opus-4-1-20250805")

        @tool
        def magic_function(input: int) -> int:
            \"\"\"Applies a magic function to an input.\"\"\"
            return input + 2

        tools = [magic_function]

        agent = create_tool_calling_agent(model, tools, prompt)
        agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)

        agent_executor.invoke({"input": "what is the value of magic_function(3)?"})

        # Using with chat history
        from langchain_core.messages import AIMessage, HumanMessage
        agent_executor.invoke(
            {
                "input": "what's my name?",
                "chat_history": [
                    HumanMessage(content="hi! my name is bob"),
                    AIMessage(content="Hello Bob! How can I assist you today?"),
                ],
            }
        )
        ```

    Prompt:
        The agent prompt must have an `agent_scratchpad` key that is a
            `MessagesPlaceholder`. Intermediate agent actions and tool output
            messages will be passed in here.

    Troubleshooting:
        - If you encounter `invalid_tool_calls` errors, ensure that your tool
          functions return properly formatted responses. Tool outputs should be
          serializable to JSON. For custom objects, implement proper __str__ or
          to_dict methods.
    """
    missing_vars = {"agent_scratchpad"}.difference(
        prompt.input_variables + list(prompt.partial_variables),
    )

Subdomains

Frequently Asked Questions

What does create_tool_calling_agent() do?
create_tool_calling_agent() is a function in the langchain codebase, defined in libs/langchain/langchain_classic/agents/tool_calling_agent/base.py.
Where is create_tool_calling_agent() defined?
create_tool_calling_agent() is defined in libs/langchain/langchain_classic/agents/tool_calling_agent/base.py at line 18.

Analyze Your Own Codebase

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

Try Supermodel Free