Adding Tools to Agents
Enhancing your agents with specialized capabilities
Tools give agents superpowers – the ability to search the web, do calculations, access APIs, and more.
Quick Start
- Install Dependencies
bash pip install praisonaiagents duckduckgo-search - Create a Tool
Any Python function can be a tool:
python from duckduckgo_search import DDGS def search_web(query: str) -> str: """Search the web for information""" results = DDGS().text(query, max_results=3) return str(results) - Add Tool to Agent
python from praisonaiagents import Agent agent = Agent( instructions="Search and summarize information", tools=[search_web] # Add your tool here ) agent.start("Search for AI trends in 2025")
How Tools Work
Creating Tools
Simple Tool
def calculate(expression: str) -> str:
"""Calculate a math expression"""
try:
return str(eval(expression))
except:
return "Error: Invalid expression"Tool with Multiple Parameters
def convert_temperature(value: float, from_unit: str, to_unit: str) -> str:
"""Convert temperature between Celsius and Fahrenheit"""
if from_unit == "C" and to_unit == "F":
return f"{value * 9/5 + 32}°F"
elif from_unit == "F" and to_unit == "C":
return f"{(value - 32) * 5/9}°C"
return "Unknown conversion"Web Search Tool
from duckduckgo_search import DDGS
def search_web(query: str) -> str:
"""Search the web for information"""
results = []
for r in DDGS().text(query, max_results=5):
results.append(f"- {r['title']}: {r['body']}")
return "\n".join(results)Complete Example
from praisonaiagents import Agent
from duckduckgo_search import DDGS
# Tool 1: Web search
def search_web(query: str) -> str:
"""Search the web for information"""
results = DDGS().text(query, max_results=3)
return str(results)
# Tool 2: Calculator
def calculate(expression: str) -> str:
"""Calculate a math expression"""
try:
return str(eval(expression))
except:
return "Error"
# Agent with multiple tools
agent = Agent(
name="ResearchAssistant",
instructions="Search the web and do calculations to help users",
tools=[search_web, calculate]
)
agent.start("What is the population of Tokyo? Calculate 10% of that number.")Built-in Tools
PraisonAI includes ready-to-use tools:
from praisonaiagents import Agent
# Built-in web search
agent = Agent(
instructions="Research topics",
web=True # Enable built-in web search
)
# Or use specific search providers
agent = Agent(
instructions="Research topics",
tools=["tavily", "duckduckgo"] # Named tools
)Multi-Agent with Tools
from praisonaiagents import Agent, AgentTeam
from duckduckgo_search import DDGS
def search_web(query: str) -> str:
"""Search the web"""
return str(DDGS().text(query, max_results=3))
# Researcher has the search tool
researcher = Agent(
instructions="Search for information",
tools=[search_web]
)
# Writer doesn't need tools
writer = Agent(
instructions="Write summaries based on research"
)
team = AgentTeam(agents=[researcher, writer])
team.start()Best Practices
Add Docstrings
Describe what the tool does so the agent knows when to use it
Use Type Hintsdef tool(x: str) -> str: helps the agent understand inputs
Return helpful error messages instead of crashing
One PurposeEach tool should do one thing well
Next: Building Multi-Agent Systems
Learn how to create teams of agents that work together.