Tool Calling
Learn how to use tool calling to extend the capabilities of your agents
This guide assumes that you have completed the Quickstart.
Tool calling is a feature that allows you to call tools or functions from your agent. This is useful for a variety of use cases, such as:
- Retrieving information from a database
- Performing a calculation
- Performing an API call
- … and much more!
The following is a step-by-step guide on how to use tool calling. For the purpose of demonstration, this guide implements a web search tool to enable the agent to fetch up-to-date information from the web.
Define a tool for the agent to use
First, you need to tell the agent how to use the tool. This is done by defining a tool call function. To implement a web search tool, you can use an API such as Exa. You can create or copy your API key from the Exa dashboard.
Once you have the API key, you can set it in an environment variable. The following code block implements web search using Exa and uses an environment variable
named EXA_API_KEY
to store the API key.
Note that you may need to install additional dependencies such as zod
, zod-to-json-schema
and exa-js
to use this code. If you use npm for package management, you can install the dependencies using the following command:
Instruct the agent to use the tool
After defining the tool call function, you need to instruct the agent to use the tool. You can do this with a system prompt. The system prompt also allows you to set certain guidelines and rules for the agent to follow, such as how to use the tool call output in its responses.
If you are unfamiliar with system prompts, you can learn more about them in the Using System Prompts guide.
Here’s a sample system prompt:
Pass the tool to the agent
Now you just need to pass the tool call function to the agent so it can start using the tool. If you’ve followed the Quickstart guide, you can pass the tool call function to the agent by making a couple of small changes:
- Import the
tools
andsystemPrompt
to yourroute.ts
file. - Replace the
create
call in yourroute.ts
file with a convenientrunTools
call that takes the list of tools available to the agent.
Here’s an example of how to do this:
To view the full route.ts
code, you can expand the following codeblock:
Test it out!
You can now test out your agent, newly equipped with a web search tool: