How to beginner · 3 min read

LangChain tool calling agent

Quick answer
Use langchain to create a tool calling agent by defining tools with BaseTool and integrating them into an Agent with a language model like ChatOpenAI. The agent automatically decides when to call tools based on user input and returns combined results.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install langchain openai

Setup

Install the required packages and set your OpenAI API key as an environment variable.

  • Install LangChain and OpenAI SDK:
bash
pip install langchain openai
output
Collecting langchain
Collecting openai
Installing collected packages: ...
Successfully installed langchain-0.2.0 openai-1.0.0

Step by step

This example defines a simple tool that returns the current time, then creates a LangChain agent that can call this tool based on user queries.

python
import os
from langchain_openai import ChatOpenAI
from langchain.agents import Tool, initialize_agent, AgentType
from datetime import datetime

# Ensure your OpenAI API key is set in the environment
# export OPENAI_API_KEY=os.environ["OPENAI_API_KEY"]

# Define a simple tool
class TimeTool(Tool):
    name = "current_time"
    description = "Returns the current time as a string"

    def _run(self, query: str) -> str:
        return datetime.now().strftime("%Y-%m-%d %H:%M:%S")

    async def _arun(self, query: str) -> str:
        raise NotImplementedError("Async not implemented")

# Instantiate the tool
tools = [TimeTool()]

# Initialize the language model
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)

# Create the agent with tool calling capability
agent = initialize_agent(
    tools,
    llm,
    agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
    verbose=True
)

# Run the agent with a query that triggers the tool
response = agent.run("What time is it right now?")
print("Agent response:", response)
output
> Entering new AgentExecutor chain...
Thought: I need to use a tool to find the current time
Action: current_time
Action Input: None
Observation: 2026-04-27 15:42:10
Thought: I have the current time
Final Answer: The current time is 2026-04-27 15:42:10
> Finished chain.
Agent response: The current time is 2026-04-27 15:42:10

Common variations

  • Use async agent calls with await agent.arun() for asynchronous workflows.
  • Replace ChatOpenAI with other LLMs like Anthropic or OpenAI clients.
  • Define multiple tools for complex workflows, each with clear name and description.
  • Use different agent types like AgentType.CONVERSATIONAL_REACT_DESCRIPTION for chat-based tool calling.

Troubleshooting

  • If the agent does not call tools, ensure tools have proper name and description attributes.
  • Set verbose=True in initialize_agent to debug agent reasoning steps.
  • Check your OpenAI API key is correctly set in os.environ["OPENAI_API_KEY"].
  • For async errors, confirm your environment supports async and use await properly.

Key Takeaways

  • Define tools as subclasses of Tool with clear names and descriptions for LangChain agents.
  • Use initialize_agent with AgentType.ZERO_SHOT_REACT_DESCRIPTION to enable automatic tool calling.
  • Set verbose=True to see agent decision-making and debug tool calls.
  • Async tool calling requires _arun method and async agent invocation.
  • Multiple tools can be combined for complex agent workflows.
Verified 2026-04 · gpt-4o-mini
Verify ↗