0PricingLogin
AI Engineering Academy · Lesson

Defining Tools for Your Agent

Create custom tools with the @tool decorator, write clear descriptions the LLM uses to decide when to call each tool, and add input validation with Pydantic.

Tools Give Agents Superpowers

An agent without tools can only reason about what it already knows — it cannot search the web, query a database, or send an email. Tools are Python functions that extend the agent's capabilities by letting it take real-world actions and retrieve fresh information. Defining tools clearly is one of the most important steps in building a reliable agent.

The @tool Decorator in LangChain

LangChain's @tool decorator transforms any Python function into a tool the agent can call. The function's docstring becomes the tool description that the LLM uses to decide when to call it. A clear, specific description dramatically improves the agent's tool selection accuracy.

from langchain_core.tools import tool

@tool
def get_weather(city: str) -> str:
    '''Get the current weather conditions for a given city.
    Use this tool when the user asks about weather in a specific location.
    Input should be just the city name, e.g. 'London' or 'New York'.
    '''
    # Real implementation would call a weather API
    return f'The weather in {city} is 18 degrees Celsius and partly cloudy.'

print(get_weather.name)         # 'get_weather'
print(get_weather.description)  # The docstring above

All lessons in this course

  1. The ReAct Framework: Think, Act, Observe
  2. Defining Tools for Your Agent
  3. Building a ReAct Agent with LangChain
  4. Handling Agent Failures and Loops
← Back to AI Engineering Academy