Skip to main content
This cookbook shows how to build a grounded research agent that can:
  • Search the web for current information with Exa
  • Hand the results to a Cerebras model through tool calling
  • Return answers with inline citations and a clean source list
Exa search returns clean page content (highlights) with every result, so a single search tool is enough to ground your AI agent.

Prerequisites

Before you begin, ensure you have:
  • A Cerebras API key
  • An Exa API key
  • Python 3.10+ or Node.js 18+
Install the dependencies:
The Node.js examples use ES modules and top-level await. Save them with a .mjs extension (or set "type": "module" in your package.json) and run them with node file.mjs.
Then store your API keys in a .env file:
Get your keys here: Cerebras and Exa.

Step 1: Initialize the Clients

We use Exa for search and the OpenAI client against Cerebras’ OpenAI-compatible API for agent reasoning and tool use.

Step 2: Define the Exa Search Tool

The agent gets one tool: exa_search. It returns clean highlights for each result, with each source tagged [n] so the model can cite it. A finalize helper cleans up the model’s output and appends a numbered source list, so every answer ends with reliable citations.

Step 3: Register the Tool for the Model

The schema exposes the three search types so the model can choose faster or deeper search per query. Only query is required; everything else is optional.

Step 4: Run the Agent Loop

The core pattern is:
  1. Ask the model what it needs
  2. Let it call the search tool
  3. Feed tool results back into the conversation
  4. Stop when the model returns a final answer
The loop has a step limit, calls tools safely, and passes any tool error back to the model as a tool message so it can fix its input instead of crashing.

Step 5: Try It on a Real Question

Now you can ask for a grounded answer. The agent searches the web, then writes a cited answer.
Inline [n] markers map to the numbered Sources list at the end of the answer. Non-consecutive citations like [1], [2], and [4] are expected when the model cites only some of the results.

Complete Example

The full agent in a single file. Copy it into agent.py (or agent.mjs) and run it.

Summary

What We Built

A grounded research agent with:
  • Exa search for current source discovery, with page content (highlights) returned inline
  • Cerebras tool calling to plan searches and write cited answers
  • Reliable inline citations backed by a numbered source list

Next Steps

  • Use fast for low-latency chat assistants and deep for broader research tasks
  • Lower max_age_hours for newsy queries that need fresher content
  • Try other Exa API config in Exa API Dashboard

Resources

Acknowledgements

Thank you to Ishan Goswami from Exa for his collaboration and feedback during the development of this cookbook.