Get startedGet started for free

Creating an Agent With Custom Tools

1. Creating an Agent With Custom Tools

In the previous lesson, you built your first agent with built-in tools. Now, let's take it a step further by creating custom tools!

2. Why Build Custom Tools?

You might wonder: "If code agents can already write and run Python and use built-in tools, why would I need to create custom tools at all?" It's true; code agents are powerful on their own. But custom tools give you important advantages, for example...

3. Benefits of Custom Tools

Reliability: You can write, test, and validate logic instead of letting the agent guess how to do something every time. Reusability: Once built, a custom tool can be used in different projects or agents. Consistency: Your approach stays the same across different runs, which makes your agents more predictable and easier to debug. And finally, controlled access: Custom tools let you give agents access to specific resources, like CSV files, APIs, or databases. In short, tools help you turn one-off logic into reliable building blocks. And they're often the bridge between your agents and the outside world.

4. Scenario: You Run a Retail Store

Consider the following scenario: You run a small retail store and want to check inventory levels. Your data is stored in a CSV file that includes product details such as size, color, quantity in stock, and price. Although code agents can generate Python to read a CSV, they don't automatically have access to the files. So, you can give the agent controlled access by wrapping file-reading logic inside a custom tool.

5. Anatomy of a Custom Tool

Let's break down the anatomy of a custom tool. The @tool decorator from the smolagents library turns a regular Python function into a tool the agent can call. In this example, the check_inventory() function takes a product name, reads the inventory from a CSV file, and returns the quantity in stock.

6. Best Practices for Custom Tools

To work properly, a custom tool should include: clear input parameters that are explicitly defined, type hints for each parameter; these help the LLM understand the expected input and output types, and a docstring that explains what the function does.

7. How the Agent Uses Your Tool

When you ask the agent, "Do we have any t-shirts in stock?" here's what happens behind the scenes: It analyzes the question and recognizes you're asking about inventory. It matches your question to the check_inventory() tool. Then, it executes check_inventory() with "t-shirt" as the input parameter. And finally, it takes the tool result to formulate a helpful response. This is why clear docstrings and type hints matter. They guide the agent in how to use tools correctly, just like good documentation helps human developers.

8. Registering a Custom Tool with Your Agent

To set up the agent with the check_inventory() custom tool, you just need to add it to the tools list. Since the tool relies on the pandas library to read and process the CSV file, we need to explicitly allow this package, and we do that with the additional_authorized_imports parameter. If the custom tool doesn't require any external libraries and only uses Python's built-in libraries, you can omit the additional_authorized_imports parameter.

9. Custom Tools in Production Projects

In a production scenario, your data likely won't live in a simple CSV file. Instead, you might connect to: a PostgreSQL database, an API, or a cloud storage service. But the good news is the same principles still apply. Your custom tool becomes the bridge between the agent and the external information it needs to access whether that's local or remote.

10. Let's practice!

In the next exercises, you'll create your own custom tools.

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.