AI Hints
LangChain · Lesson 38 of 120

The tool Decorator

Functions become tools.

⏱ 2 min read▶ Video at 41:33🎓 Free
Watch this lesson in the LangChain courseWatch on YouTube ↗Full LangChain course

The easiest way to make a tool is the tool decorator. Put it above a normal Python function, with type hints and a docstring, and it becomes a tool.

The tool Decorator
From the video: The tool Decorator
  • @tool
  • Type hints
  • A docstring

Making a tool

Here's a stock checker. The function takes a book title, and returns the number of copies in stock, from a small dictionary standing in for a database. The tool decorator turns it into a tool. Run it. It has a name, a description taken from the docstring, and argument types taken from the type hints. And we can still invoke it ourselves.

first_tool.pypython
from langchain.tools import tool

STOCK = {"Dune": 3, "Circe": 0, "The Hobbit": 7}

@tool
def check_stock(title: str) -> int:
    """Return how many copies of a book Riverside Books has in stock."""
    return STOCK.get(title, 0)

print("name:", check_stock.name)
print("description:", check_stock.description)
print("args:", check_stock.args)
print("Dune ->", check_stock.invoke({"title": "Dune"}))
Output
name: check_stock
description: Return how many copies of a book Riverside Books has in stock.
args: {'title': {'title': 'Title', 'type': 'string'}}
Dune -> 3

Writing good tools

Good tools have a clear name, like check stock. A docstring that says what the tool does, and when to use it. Simple argument types, like strings and numbers. And a short, clear return value. Think of the docstring as instructions for the model.

keyvalue
Nameverb + noun
Docstringwhat and when
Argssimple types
Returnshort and clear

Summary

The tool decorator turns a typed, documented function into a tool, with a name, description, and arguments. Next, what the model actually sees.

Key takeaways
  • @tool
  • Docstring = description
  • Type hints = args
Next up: Tool Schemas — what the model sees