What are tools?
A tool is an API or function you expose to the model. Instead of guessing/hallucinating values, the model can call your tool and use the results to generate UI. Examples of tools:- Fetching live stock prices from a finance API
- Querying a database for users or orders
- Calling an internal microservice
- Running a calculation or simulation
How tools fit into the flow
Tool calling / Function calling behaves in the same way as any standardized LLM endpoint. To learn more in depth refer to the OpenAI Guide on how it works.Example: Integrating Web Search
This guide demonstrates how to equip an agent with a web search tool, enabling it to provide up-to-the-minute information.1. Define a tool for the agent to use
To get started, we need to define the tool and how the agent should use it. For our company’s research assistant, a web search tool is crucial for gathering current information. This guide adds a web search tool powered by Tavily to search the web. You may need to install additional dependencies such aszod
, zod-to-json-schema
, and @tavily/core
. You can install them using npm:
cli
app/api/chat/tools.ts
2. Instruct the agent to use the tool
Now that the agent has a tool, we need to teach it when and how to use it. A system prompt is the perfect way to provide these instructions. We can tell the agent to use theweb_search
tool whenever it needs current information to answer a question about a company.
Here’s a sample system prompt:
app/api/chat/systemPrompt.ts
3. 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.
src/app/api/route.ts