Adding Tools to Agents

In this lesson, we’ll learn how to enhance your agents by adding tools. Tools allow agents to perform specific actions beyond just generating text, making them much more useful for real-world tasks.

What You’ll Learn

By the end of this lesson, you’ll understand:

  • What agent tools are and why they’re useful
  • How to create simple tools for your agents
  • How to integrate tools with your agents
  • Best practices for tool design

Understanding Agent Tools

Tools give your agents the ability to:

  • Perform calculations
  • Access specific information
  • Interact with external systems
  • Generate different types of content
1

Install PraisonAI

Install the core package:

Terminal
pip install praisonaiagents duckduckgo-search
2

Configure Environment

Terminal
export OPENAI_API_KEY=your_openai_key

Generate your OpenAI API key from OpenAI Use other LLM providers like Ollama, Anthropic, Groq, Google, etc. Please refer to the Models for more information.

3

Create Agent with Tool

Create app.py

4

Start Agents

Execute your script:

Terminal
python app.py

Creating Custom Tool

1

Create any function that you want to use as a tool, that performs a specific task.

from duckduckgo_search import DDGS

def internet_search_tool(query: str):
    results = []
    ddgs = DDGS()
    for result in ddgs.text(keywords=query, max_results=5):
        results.append({
            "title": result.get("title", ""),
            "url": result.get("href", ""),
            "snippet": result.get("body", "")
        })
    return results
2

Assign the tool to an agent

    data_agent = Agent(
        instructions="Search about AI job trends in 2025",
        tools=[internet_search_tool], # <-- Tool Assignment
    )

That's it!

You have created a custom tool and assigned it to an agent.

When Does the Agent Use Tools?

The agent decides when to use tools based on:

  1. The nature of the user’s request
  2. The tools available to it
  3. The instructions you’ve provided

If your agent isn’t using tools when expected, you might need to:

  • Make your instructions more explicit about when to use tools
  • Provide more descriptive tool definitions
  • Ensure the tools are appropriate for the task

Tool Best Practices

Specific Purpose

Each tool should have a clear, specific purpose

Error Handling

Tools should handle errors gracefully

Descriptive Names

Use clear names that indicate functionality

Clear Documentation

Include detailed descriptions and examples

Real-World Tool Examples

In production systems, you might create tools for:

Database Access

Retrieve or store information in databases

API Integration

Connect to external services

File Operations

Read from or write to files

Image Generation

Create images based on descriptions

Advanced Tool Concepts

As you become more comfortable with tools, you can explore:

  • Tools that call other tools
  • Tools with complex parameters
  • Tools that maintain state
  • Dynamically generated tools

In the next lesson, we’ll explore how to create a multi-agent system for more complex tasks.