Add Web Search to an AI Agent With Brave

Diagram of adding web search to an AI agent with a Brave search loop

Adding web search to an AI agent comes down to one pattern: hand the model a search tool, run whatever searches it requests, and feed the results back until it can answer. A build that pairs the Brave Search API with Claude shows how little code that takes, and where costs creep in.

Why a model without search gets it wrong

Models answer from training text that ends at a cutoff date. Ask for the latest stable Python release and the first NumPy version to support it, and anything published after that date is invisible to the model.

The question needs two lookups, too. The Python release has to be found before the NumPy query can even be written. One-shot retrieval can’t manage that, so the build uses a loop.

A sequence between your code, the model and Brave. Your code sends the tool definition, the system line and the message list (#A). The model replies with a turn holding an answer or search calls (#B). Your code appends the turn (#C) and runs each search on Brave, which returns text chunks with links. Your code appends one message with each result under its search-call id (#D) and sends the longer list (#E).
Image: theaiengineer.substack.com

How the loop works

Every call to the model bundles a tool definition, a system line and the running message list. Only the list grows, since each reply and each result lands in it.

A single pass through the loop goes like this:

  1. The code sends all three parts to the model.
  2. The model replies with a turn. The stop reason tool_use signals search calls inside it, while end_turn signals a final answer. A turn may carry several searches.
  3. The code stores that turn unchanged, then runs every search on Brave and gets text chunks plus links.
  4. One new message carries all the results, each tagged with the id of the search it answers.
  5. The longer list returns to the model, which searches again or answers.

The system line gives four orders: search when unsure, continue until a source states the answer, cite links, and admit it when nothing is found.

A flow from the question to a model call. An answer branch ends the run as exit 1. A search call branch runs the search, appends the turn and the result, and returns to the model call. A counter of at most 4 rounds leads to 1 more call with the tools switched off, which ends the run as exit 2 with an answer from what the list holds.
Image: theaiengineer.substack.com

Picking the Brave endpoint

Brave sells two interfaces. Web Search is meant for human-facing result pages and gives ranked titles, URLs and brief snippets. LLM Context gives models what they can read: cleaned, shortened page text, each block labelled with its URL. The build picks LLM Context and caps every search at 2,048 tokens, against a default of 8,192 and an allowed range of 1,024 to 32,768.

Brave charges $5 for every 1,000 searches and credits $5 monthly, which pays for the first thousand. A credit card is required to turn the plan on. On the Python side, version 3.10 or newer is required by Anthropic’s SDK, and both Anthropic and Brave must issue you a key.

3 columns for model calls 1, 2 and 3. Each stacks the tool definition, the system line and the question. Column 2 adds turn 1, a search call, and result 1 on top. Column 3 adds turn 2, a search call, and result 2 on top of that.
Image: theaiengineer.substack.com

Capping the loop

An uncapped agent could search indefinitely. This one stops at 4 rounds, counting each model turn that requests searches as a round. Then comes one extra call with tools disabled, forcing an answer from the list as it stands. A run therefore never exceeds 5 model calls.

That ceiling applies to model calls only. One round may contain several searches, so the Brave bill can’t be capped the same way.

One model quirk shapes the code. Anthropic’s API notes say claude-sonnet-5-5 refuses a forced tool choice, so the loop uses the auto setting and leans on the system line to trigger searches. The none setting is still accepted, and the last call relies on it.

What it costs

Because the whole list is sent again on every call, each extra search inflates the next request. Anthropic counts the tool definition, system line and list as input tokens. After each call the build prints running input and output totals, so the bill is visible in your own runs. Results differ from run to run, so compare fields and rough counts, not exact figures.

Storage is restricted as well. Brave’s terms bar caching results beyond what the app needs to run, so this agent keeps them in memory for one run and writes nothing to disk. Read your own plan’s terms before keeping anything.

Guarding against invented links

Models also recall links from training, so a URL in an answer may come from a result or from memory. The final part of the build compares every link in the answer with the pages Brave returned in that run, which matters when adding web search to an AI agent that users will trust.

Testing the finished agent

Start with the Python and NumPy question and check that the scoreboard shows two searches. Then ask something the web can’t answer, such as the third customer of a company with no published customer list. A final line reading call=5 means the cap stopped the run. Anything lower means the model answered or reported finding nothing.

To watch the cap work, drop it to 1 and rerun the first question. Two scoreboard lines should appear, the second from the tools-off call. Restore it to 4 afterward, since that is the setting the rest of the build assumes for adding web search to an AI agent.

Source: theaiengineer.substack.com

Written by

Saksham Bhargava

Saksham Bhargava runs Alteroid and has been writing about consumer technology for over five years, covering iPhone, Android, Windows, AI tools and gaming. With an MCA and six years of hands-on web development experience, he tests the features he writes about on his own devices and focuses on clear, practical guides that solve real problems. Away from the screen, he enjoys travelling to places most tourists skip.