Understanding AI Tools
A tool is a function that can be called by the model to perform a specific task. You can think of a tool like a program you give to the model that it can run as and when it deems necessary.Tools enable the AI to interact with external systems, databases, and APIs,
making it much more powerful than a simple text generator.
Adding the Resource Tool
Let’s create a tool to give the model the ability to create, embed, and save a resource to your agent’s knowledge base. Update your route handler with the following code:app/api/chat/route.ts
How the Tool Works
Tool Definition
Tool Definition
The
addResource
tool is defined with three key components: description,
input schema, and execute
function.Description
Description
The description tells the AI when and how to use this tool, influencing its
decision-making.
Input Schema
Input Schema
Zod schema defines the required input structure, ensuring type safety and
validation.
Execute Function
Execute Function
The async function that performs the actual work when the tool is called.
Testing the Tool
1
Test the Tool
Head back to the browser and tell the model your favorite food. You should
see an empty response in the UI.
2
Check the Database
Run the following command in a new terminal window:This will start Drizzle Studio where you can view the rows in your
database.
terminal
3
Verify Results
You should see a new row in both the embeddings and resources table with
your favorite food!
The tool call happens automatically when the AI determines it should add
information to your knowledge base. The empty response indicates the tool was
called successfully.
Updating the UI for Tool Calls
Let’s make a few changes in the UI to communicate to the user when a tool has been called. Update your root page (app/page.tsx
) with the following code:
app/page.tsx
Understanding Tool Call Flow
AI Decision
Model Evaluation: The AI decides whether to call a tool based on the
user’s input and tool descriptions.
Tool Execution
Function Call: The AI SDK automatically calls the execute function with
the extracted parameters.
Database Update
Data Persistence: The tool creates embeddings and stores the resource in
your database.
UI Feedback
User Communication: The UI shows which tool was called and its input
parameters.
Tool Call States
The tool call has different states that you can track:calling
: The tool is currently being executedoutput-available
: The tool has completed and returned results
Testing the Updated Interface
1
Save and Refresh
Save the file and head back to the browser. Refresh the page to see the
updated UI.
2
Test Tool Display
Tell the model your favorite movie. You should now see which tool is called
in place of the model’s typical text response.
3
Verify Tool Information
The UI will display the tool name, state, and input parameters in a
formatted JSON block.
With this change, you now conditionally render the tool that has been called
directly in the UI, providing transparency about what the AI is doing behind
the scenes.