How to Create Tools with LangChain – The Ultimate Guide
Programming

How to Create Tools with LangChain – The Ultimate Guide

Learn how to create, register, and use custom tools in LangChain with practical examples, best practices, and step-by-step instructions.

Amer AlaliMay 22, 20252 min read

How to Create Tools with LangChain – The Ultimate Guide

Introduction

LangChain is a powerful framework for building applications with large language models (LLMs). One of its most exciting features is the ability to create custom tools that extend the capabilities of your agents. In this guide, you’ll learn how to create, register, and use custom tools in LangChain, with practical examples in both Python and JavaScript.


What Are LangChain Tools?

LangChain tools are functions or classes that allow your LLM-powered agents to interact with external data, APIs, or perform specific tasks. Tools can be as simple as a calculator or as complex as a web search or database query.


Why Create Custom Tools?

  • Extend LLM capabilities: Go beyond text generation.
  • Integrate with APIs: Fetch real-time data, automate workflows.
  • Build smarter agents: Enable reasoning, planning, and action-taking.

Step 1: Setting Up Your Environment

Python:

pip install langchain openai

JavaScript:

npm install langchain openai

Step 2: Creating a Simple Tool

Python Example

The recommended way is to use the @tool decorator:

from langchain.tools import tool

@tool
def get_weather(city: str) -> str:
    """Returns the weather for a given city."""
    # Imagine this calls a real API
    return f"The weather in {city} is sunny."

JavaScript Example

import { Tool } from "langchain/tools";

const getWeather = new Tool({
  name: "get_weather",
  description: "Returns the weather for a given city.",
  func: async (city) => {
    // Imagine this calls a real API
    return `The weather in ${city} is sunny.`;
  },
});

Step 3: Registering Tools with an Agent

Python

from langchain.agents import initialize_agent, AgentType
from langchain.llms import OpenAI

llm = OpenAI()
tools = [get_weather]
agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION)

JavaScript

import { initializeAgent, AgentType } from "langchain/agents";
import { OpenAI } from "langchain/llms/openai";

const llm = new OpenAI();
const tools = [getWeather];
const agent = initializeAgent(tools, llm, AgentType.ZERO_SHOT_REACT_DESCRIPTION);

Step 4: Best Practices for Tool Creation

  • Clear Names & Descriptions: Make it easy for the LLM to understand when to use your tool.
  • Input/Output Schemas: Use JSON schemas for complex tools.
  • Error Handling: Always handle exceptions gracefully.
  • Testing: Test tools independently before integrating.

Step 5: Advanced Tools

You can create tools that:

  • Call external APIs (weather, finance, news, etc.)
  • Query databases
  • Perform calculations
  • Interact with files or cloud storage

Example (Python, API call):

@tool
def get_crypto_price(symbol: str) -> str:
    """Returns the current price of a cryptocurrency."""
    # Call a real API here
    return f"The price of {symbol} is $50,000."

Conclusion

Custom tools are the backbone of powerful, real-world LLM applications. With LangChain, you can easily create, register, and use tools to build agents that do more than just chat—they act!


Resources

LangChainAIToolsAgentsPythonJavaScriptGuide
Share

Start your own AI agency

  • Chat and voice agents under your brand
  • Your domain, dashboard and prices
  • We run the platform for you
Start your AI agency
White label AI platform

Start your AI agency today

Sell AI chat and voice agents under your own brand, on your own domain, at your own prices. We run the platform; you own the clients.

Start your AI agency