How to define tools for LlamaIndex agent
Quick answer
To define tools for a
LlamaIndex agent, create Python functions or classes that encapsulate specific tasks, then register them as tools with the agent's toolkit. Use the Tool class from llama_index.tools to wrap these functions, enabling the agent to invoke them during execution.PREREQUISITES
Python 3.8+pip install llama-index>=0.6.0OpenAI API key (free tier works)pip install openai>=1.0
Setup
Install the llama-index package and set your OpenAI API key as an environment variable.
- Install with
pip install llama-index openai - Set environment variable in your shell:
export OPENAI_API_KEY='your_api_key'
pip install llama-index openai Step by step
Define a tool as a Python function, wrap it with Tool from llama_index.tools, then create an agent with these tools. The agent can call the tools by name during its reasoning process.
import os
from llama_index import GPTSimpleVectorIndex, SimpleDirectoryReader
from llama_index.tools import Tool
from llama_index.agent import Agent
# Define a simple tool function
def greet(name: str) -> str:
return f"Hello, {name}! Welcome to LlamaIndex agent."
# Wrap the function as a Tool
hello_tool = Tool(
func=greet,
name="greet",
description="Greets a user by name."
)
# Create an agent with the tool
agent = Agent(tools=[hello_tool])
# Use the agent to run a query that triggers the tool
response = agent.chat("Use the greet tool to say hello to Alice.")
print(response) output
Hello, Alice! Welcome to LlamaIndex agent.
Common variations
You can define tools as classes with a __call__ method for more complex logic. Also, integrate multiple tools by passing a list to the Agent. Use different models by configuring the agent's LLM parameter.
from llama_index.tools import Tool
from llama_index.agent import Agent
# Tool as a class
class CalculatorTool:
def __call__(self, expression: str) -> str:
try:
result = eval(expression, {}, {}) # simple eval for demo
return str(result)
except Exception:
return "Invalid expression"
calc_tool = Tool(
func=CalculatorTool(),
name="calculator",
description="Evaluates simple math expressions."
)
agent = Agent(tools=[hello_tool, calc_tool])
response = agent.chat("Calculate 5 * 7 using the calculator tool.")
print(response) output
35
Troubleshooting
- If the agent does not recognize your tool, ensure the
nameanddescriptionare clear and unique. - For errors during tool execution, add try-except blocks inside your tool functions.
- Verify your environment variable
OPENAI_API_KEYis set correctly to avoid authentication errors.
Key Takeaways
- Use the
Toolclass fromllama_index.toolsto define callable tools for the agent. - Register all tools in a list and pass them to the
Agentconstructor for integration. - Tools can be simple functions or callable classes depending on complexity.
- Clear naming and descriptions help the agent select the correct tool during execution.
- Always handle exceptions inside tools to prevent agent crashes.