Tool calling with Claude
Using Claude’s “Tool Use” Feature with Exa Search Integration.
This guide will show you how to properly set up and use Anthropic’s and Exa’s API client, and utilise Claude’s function calling or “tool use” feature to perform Exa search integration. Here are the steps:
- Install the prerequisite packages and set up API keys as environment variables
- Understand how Claude’s “tool use” feature works
- Use Exa within the tool use feature
Get Started
Prerequisites and installation
Before you can use this guide you will need to have python3 and pip installed on your machine.
For the purpose of this guide we will need to install:
anthropic
library to perform Claude API calls and completionsexa_py
library to perform Exa searchrich
library to make the output more readable
Install the libraries.
To successfully use the Exa search client and Anthropic client you will need to have your ANTHROPIC_API_KEY
and EXA_API_KEY
set as environment variables.
To get an Anthropic API key, you will first need an Anthropic account, visit the Anthropic console to generate your API key.
Similarly, to get the Exa API key, you will first need an Exa account, visit the Exa dashboard to generate your API key.
Get your Exa API key
Be safe with your API keys. Make sure they are not hardcoded in your code or added to a git repository to prevent leaking them to the public.
You can create an .env
file in the root of your project and add the following to it:
Make sure to add your .env
file to your .gitignore
file if you have one.
Understanding Claude's Tool Use Feature
Claude LLMs can call a function you have defined in your code; this is called tool use. To do this, you first need to describe the function you want to call to Claude’s LLM. You can do this by defining a description object of the format:
When this description is sent to Claude’s LLM, it returns an object with a string, which is the function name defined in your code, and the arguments that the function takes. This does not execute or call functions on Anthropic’s side; it only returns the function name and arguments which you will have to parse and call yourself in your code.
We will use the object of this format to call the exa_search
function we define.
Use Exa Search as Claude tool
First, we import and initialise the Anthropic and Exa libraries and load the stored API keys.
Next, we define the function and the function schema so that Claude knows how to use it and what arguments our local function takes:
Finally, we’ll define the primer SYSTEM_MESSAGE
, which explains to Claude what it is supposed to do:
We can now start writing the code needed to perform the LLM calls and the search. We’ll create the exa_search
function that will call Exa’s search_and_contents
function with the query:
Next, we create a function to process the tool use:
Lastly, we’ll create a main
function to bring it all together, and handle the user input and interaction with Claude:
The implementation creates a loop that continually prompts the user for search queries, uses Claude’s tool use feature to determine when to perform a search, and then uses the Exa search results to provide an informed response to the user’s query.
We also use the rich library to provide a more visually appealing console interface, including coloured output and markdown rendering for the responses.
Full code
We have now written an advanced search tool that combines the power of Claude’s language models with Exa’s semantic search capabilities, providing users with informative and context-aware responses to their queries.
Running the code
Save the code in a file, e.g. claude_search.py
, and make sure the .env
file containing the API keys we previously created is in the same directory as the script.
Then run the script using the following command from your terminal:
You should see a prompt:
Let’s test it out.
That’s it, enjoy your search agent!