Skip to main content
New to Exa? Try the Coding Agent Quickstart to get started in under a minute.

OpenAI recommends using the Responses API for all new projects. See the guide.
OpenAI’s tool calling allows LLMs to call functions that are defined in your code. This guide will show you how to utilise tool calling to call Exa’s search, with the following steps:
  1. Install prerequisite packages and set up the environment
  2. Use Exa within an OpenAI tool call

Get Started

1

Pre-requisites and installation

Install the:
  • openai library to perform OpenAI API calls and completions
  • exa_py library to perform Exa search
  • rich library to make the output more readable
Python
2

Set up the environment variables

Create an .env file in the root of your project and set the EXA_API_KEY and OPENAI_API_KEY environment variable to your API keys respectively. Visit the OpenAI playground and the Exa dashboard to generate your API keys.

Get your Exa API key

Shell
3

Use Exa Search as an OpenAI tool

First, we import and initialise the OpenAI and Exa libraries and load the stored API keys.
Python
Next, we define the function and the function schema so that OpenAI knows how to use it and what arguments our local function takes:
Python
Finally, we’ll define the primer SYSTEM_MESSAGE, which explains to OpenAI what it is supposed to do:
Python
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 function with the query:
Python
Next, we create a function to process the tool calls:
Python
Lastly, we’ll create a main function to bring it all together, and handle the user input and interaction with OpenAI:
Python
The implementation creates a loop that continually prompts the user for search queries, uses OpenAI’s tool calling 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.
4

Running the code

Save the code in a file, e.g. openai_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:
Bash
You should see a prompt:
Bash
Let’s test it out.
Bash
That’s it, enjoy your search agent!

Full code

Python
Last modified on May 6, 2026