Skip to main content
🔧 You are viewing: API Specification - Complete technical reference for the @tool decoratorSee also: Conceptual Guide

Overview

The @tool decorator is a simple, powerful mechanism for registering Python functions as agent tools. It automatically extracts function signatures, type annotations, and docstrings to create tool definitions that LLMs can understand and use. What it does:
  • Registers functions in the global tool registry
  • Extracts function signature and parameters
  • Infers parameter types from type annotations
  • Captures docstrings for tool descriptions
  • Generates JSON schema for LLM function calling
  • Supports both @tool and @tool() syntax
Why use it:
  • Minimal boilerplate (single decorator)
  • Automatic type inference from Python annotations
  • Clear separation between tool definition and implementation
  • Compatible with standard Python typing

Purpose and Use Cases

When to Use

  1. Registering Agent Tools
    • Every function you want the agent to call
    • Tools that perform actions (database queries, file operations, API calls)
    • Tools that retrieve information (search, list, get status)
  2. Simple Tool Definition
    • Quick tool prototyping
    • Small to medium agents with 3-20 tools
    • Standard Python function signatures
  3. Type-Safe Tools
    • Tools with clear parameter types
    • Tools requiring type validation
    • Tools used by multiple agents

When NOT to Use

  • Functions not meant to be called by the agent
  • Internal helper methods
  • Methods that require agent instance state (use instance methods in _register_tools() instead)

API Specification

Decorator Definition

Tool Registry

Type Mapping


Code Examples

Example 1: Basic Tool

Example 2: Atomic Tool (Simple Operations)

Example 3: Tool with Optional Parameters

Example 4: Tool with Complex Types

Example 5: Tool in Agent Context

Example 6: Both Decorator Syntaxes


Implementation Details

Type Inference

Docstring Parsing

Registry Management


Testing Requirements

Unit Tests

File: tests/agents/base/test_tools.py

Dependencies

Required Packages

No External Dependencies

The tool decorator uses only Python standard library.

Best Practices

Good Tool Docstrings

Clear Parameter Names

Consistent Return Types


Limitations

No Advanced Type Hints

No Parameter Validation


Future Enhancements

  • Support for Union types
  • Support for Optional types
  • Support for List[Type], Dict[Key, Value]
  • Pydantic model integration
  • Parameter validation decorators
  • Tool categories/tags
  • Tool versioning
  • Automatic example generation from docstring

@tool Decorator Technical Specification