Home / Articles

Tool Calling in LangChain: The Loop Behind Agentic Apps

Register Python functions as LangChain tools and run the four-step loop where the model picks tools, they execute externally and results return as messages.

Publisher: REACTAPP.TOP6 min read1280 words

This article is published in English.

Sources: Agentic AI with LangChain — Part 3: Tool Calling in LangChain

This is the third piece in the Agentic AI with LangChain series. Part 2 built an agentic RAG workflow with LangGraph. The next step is to grow into more domains, cooking, swimming, classical music, and even live weather for cities around the world, without drowning the graph in new nodes.

Following Part 2's pattern, you could add one retrieval node per domain, each with its own Pinecone index, plus a weather node that hits a public API. As nodes multiply, wiring and branching multiply with them, and the graph gets hard to maintain.

Tool calling is the way out. Instead of a node per function, register those functions as LangChain tools and let the model decide when to call them, how to use the results and how to fold them into the final answer. That simplifies the workflow and extends the model with accurate calculations, database queries, web search and real-time APIs.

What follows covers the core ideas and a tool-calling loop in LangChain. That foundation prepares the next article on ReAct. With ReAct understood, the Part 2 workflow can be simplified using the same ideas. Code lives in the GitHub repo.

Topics:

  • What tool calling is and why it matters.
  • How to implement LangChain tools.
  • How the four-step tool-calling loop works.

Vocabulary

  • LangChain tool: a Python function decorated with @tool that exposes its schema and usage instructions to the model.
  • Tool calling: the mechanism by which the model requests that an external function run.
  • Tool message: the message type that carries tool results back to the model.
  • Tool-calling loop: the cycle in which the model selects a tool, the tool runs outside the model, the result returns and the model incorporates it into the answer.

Setup

Create a .env with OpenAI and Tavily keys.

OPENAI_API_KEY="<Your OpenAI API Key>"
TAVILY_API_KEY=<Your TAVILY API Key>

Load them through a Pydantic settings class.

class BaseConfig(BaseSettings):
    OPENAI_API_KEY: Optional[str]
    PINECONE_API_KEY: Optional[str]
    TAVILY_API_KEY: Optional[str]

model_config = SettingsConfigDict(env_file=".env", extra="ignore")

The project keeps tools under a tools package, with simple examples such as a sum helper and a weather tool.

project/
|
├── tools
│   ├── get_sum.py    # a simple LangChain tool example
|   └── weather.py    # get_weather LangChain tool using open source APIs
|
|── tool_call.py      # implement tool-calling loop using LangChain tools
├── config.py         # pydantic BaseConfig
├── .env              # environment variable definition
├── .gitignore
└── requirements.txt  # package requirements

What a LangChain tool is

A tool is an ordinary Python function with the @tool decorator. The decorator wraps the function, builds a schema from the docstring and type hints and gives the model instructions for calling it.

from langchain.tools import tool


@tool
def get_sum(a: int, b: int) -> int:
    """
    get the summation of two integers
    :param a: int, input integer
    :param b: int, input integer
    :return: int, the sum of a and b
    """
    return a + b

A weather tool

get_weather uses the free Open-Meteo API. It looks up a city's coordinates, then fetches current weather. That pattern, resolve then fetch, is typical for real-time tools.

The tool-calling loop

Once tools exist, the next question is how the model uses them. The answer is a four-step cycle.

  1. The user sends a request.
  2. The model decides which tool or tools to call and emits a message with names and arguments.
  3. The tools run outside the model, and their results return as tool messages.
  4. The model incorporates those results and produces a final answer.

The model never executes tools itself. Everything runs externally, and outputs come back as tool messages that the model then reasons over.

The model can also request several tools in one response, known as parallel tool calls. Asked for Boston's temperature and the sum of three and five, it may emit both get_weather and get_sum at once; the loop runs both before answering.

Implementing the loop in LangChain

A WeatherAssistant class wraps weather and web-search capabilities using get_weather and tavily_search.

LLM and tools

The constructor sets up an OpenAI model, a dictionary of tools, a bound LLM that can call them, a message list and a system prompt that tells the model when to use each tool.

class WeatherAssistant:
    def __init__(self):

        # initialize llm
        self.llm = ChatOpenAI(api_key=api_key, model="gpt-4o-mini", temperature=0)

        # initialize a tool dictionary
        self.tools = {"get_weather": get_weather,
                      "tavily_search": TavilySearch(max_results=3, tavily_api_key=TAVILY_API_KEY)}
        # bind LangChain tools to llm
        self.llm_with_tools = self.llm.bind_tools(list(self.tools.values()))

        # initialize messages to store message list
        self.messages = []

        # System prompt
        self.system_prompt = f"""You are a helpful assistant for question-answering tasks.
        When users ask about weather, use the get_weather tool to get weather. For other questions,
        use web_search. If you don't know the answer, just say that you don't know.
        Be conversational and helpful in your responses."""

        self.messages.append(SystemMessage(content=self.system_prompt))

The chat method

chat appends the user message, invokes the model, and if tool calls are present runs each tool in a loop, appends each result as a ToolMessage, then invokes the model again for the final response. That pattern handles both single and parallel calls.

async def chat(self, message: str):
    # Wrap User message in a HumanMessage and add it to message list
    self.messages.append(HumanMessage(content=message))

    # Get AI response (it may or may not contain tool calls)
    response = await self.llm_with_tools.ainvoke(self.messages)
    self.messages.append(response)

    # If there is any tool calls in the AI response
    if response.tool_calls:

        # process tool calls
        for tool_call in response.tool_calls:

            # retrieve function name from tool_call, then
            # retrieve the tool from tool dictionary, and invoke it,
            # append resulting tool message to message list
            tool = self.tools[tool_call["name"]]
            tool_result = await tool.ainvoke(tool_call)
            self.messages.append(tool_result)

        # Get final response after tool execution
        final_response = await self.llm_with_tools.ainvoke(self.messages)
        self.messages.append(final_response)

A quick test

A small async main creates the assistant and asks for Tokyo's temperature.

async def main():
    print("hello tool calling!")
    assistant = WeatherAssistant()
    message = "What is the temperature in Tokyo?"
    await assistant.chat(message)
    for msg in assistant.messages:
        msg.pretty_print()
if __name__ == "__main__":
    asyncio.run(main())

The message list then shows a clear sequence: a human message with the question, an AI message with a get_weather tool call, a tool message with the weather payload and a final AI message with the spoken answer.

A note on errors and retries\n\nIf a tool raises, the loop should catch the exception, turn it into a clear tool message and let the model decide whether to try again or apologize. Blind retries of the same bad arguments waste time; better to surface the failure text so the model can adjust.\n\n## Why this matters for the series

Tool calling is the primitive underneath ReAct and underneath any agent that decides its own actions. Once the loop is solid, adding a new capability means writing another @tool function rather than another graph node. That is the simplification Part 2's crowded workflow was waiting for. With tools registered once, new domains stop being new graph nodes and start being new functions the model can discover from their schemas and docstrings. The next article builds on this loop with the ReAct pattern, then returns to the Part 2 graph to show how much wiring disappears once tool calling carries the decisions.