Principle:Run llama Llama index Tool Definition
Overview
Tool Definition is the foundational concept in agentic AI that enables LLM agents to interact with external functions, data sources, and services. In LlamaIndex, tools are the bridge between an agent's reasoning capabilities and real-world actions -- they wrap arbitrary Python functions, query engines, and retrievers into a standardized interface that the LLM can discover, understand, and invoke through function calling.
The tool abstraction consists of three key elements: the callable logic (what the tool does), the metadata (how the LLM understands the tool), and the schema (what parameters the tool accepts). Together, these allow the agent to make informed decisions about which tool to use and how to call it correctly.
AI Agents Function Calling Tool Use LlamaIndex
The Tool Abstraction
Every tool in LlamaIndex implements a common interface defined by BaseTool and AsyncBaseTool. This interface requires:
- A metadata property returning a
ToolMetadataobject (name, description, schema, return_direct flag) - A call method for synchronous execution
- An acall method for asynchronous execution
The ToolMetadata dataclass is the critical piece that enables function calling:
@dataclass
class ToolMetadata:
description: str # Natural language description for the LLM
name: Optional[str] = None # Tool identifier used in function calls
fn_schema: Optional[Type[BaseModel]] = DefaultToolFnSchema # Pydantic schema for parameters
return_direct: bool = False # If True, tool output is returned directly without further LLM processing
The description is particularly important -- it is what the LLM reads to decide whether a tool is relevant for a given task. A well-written description dramatically improves agent accuracy.
Types of Tools
LlamaIndex provides three primary tool classes, each wrapping a different kind of callable:
| Tool Class | Wraps | Use Case |
|---|---|---|
| FunctionTool | Any Python function (sync or async) | General-purpose tool creation from arbitrary functions |
| QueryEngineTool | A BaseQueryEngine |
Natural language Q&A over indexed documents |
| RetrieverTool | A BaseRetriever |
Document retrieval from a knowledge base |
FunctionTool
The most flexible tool type. It wraps any Python callable and automatically generates the parameter schema by inspecting the function signature and docstring. It supports:
- Sync and async functions -- provide either or both; the missing variant is auto-generated
- Callbacks -- post-processing hooks that can override the tool output
- Partial parameters -- pre-bind certain arguments so they are excluded from the LLM's schema
- Workflow context injection -- functions annotated with
Contextautomatically receive the workflow context
QueryEngineTool
Wraps a query engine (e.g., a VectorStoreIndex or a KnowledgeGraphIndex) as a tool. The LLM provides a natural language query string, and the tool returns the query engine's response. This is the primary way to give an agent access to RAG (Retrieval-Augmented Generation) pipelines.
RetrieverTool
Similar to QueryEngineTool but operates at the retriever level, returning raw document nodes rather than synthesized responses. This gives the agent access to the retrieved documents themselves, which is useful when the agent needs to reason over multiple source passages.
Schema Generation
When creating a FunctionTool, the parameter schema is automatically derived from the function's type annotations using create_schema_from_function. This generates a Pydantic model that is then converted to a JSON Schema for the LLM's function calling interface.
Docstring parameter descriptions (in Sphinx, Google, or Javadoc style) are automatically extracted and added to the schema fields, giving the LLM richer context about each parameter:
def search_documents(query: str, max_results: int = 10) -> str:
"""Search the document database.
Args:
query (str): The search query to find relevant documents.
max_results (int): Maximum number of results to return.
"""
...
The generated schema will include the descriptions "The search query to find relevant documents." and "Maximum number of results to return." for the respective parameters.
Tool Output
All tools return a ToolOutput object containing:
- content -- the string representation of the output (what the LLM sees)
- blocks -- a list of
ContentBlockobjects (text, images, audio) - tool_name -- which tool produced the output
- raw_input -- the original arguments passed to the tool
- raw_output -- the unprocessed return value of the underlying function
- is_error -- whether the tool call resulted in an error
Knowledge Sources
LlamaIndex Tools Documentation ReAct Paper - Yao et al. 2022 LlamaIndex GitHub Repository
Implementation
Implementation:Run_llama_Llama_index_FunctionTool_From_Defaults