Skip to main content
This cookbook demonstrates how to build an agentic web search loop — PLAN → SEARCH → ANALYZE → refine or FINISH → ANSWER with Mercury as the agent. Because each planning turn returns in a few hundred milliseconds, most of the wall clock goes to the search API rather than the model. We walk you through how to build:
  1. A pluggable search function and an evidence store with stable citation IDs
  2. A JSON action contract and the plan-search-refine loop it drives
  3. A fully cited answer with a latency breakdown

Run the Search Agent

Directly via Google Colab or download the .ipynb to run it locally in Jupyter

Dependencies

API Keys

You need an Inception key plus a key for your search backend. Only the key for the backend you select is prompted for:

The Search Backend

The loop only ever sees {heading: [docs]}, where a doc is {url, title, highlights[], publishedDate}. Keeping that shape fixed means swapping providers — Brave, Tavily, Serper, or your own vector index over internal docs — is one function plus a branch in search().

Evidence Bookkeeping

The agent’s memory. Two details are what make multi-round search work:
  • Stable source IDs. A URL keeps the same [n] for the whole run, across rounds. That is what lets the final answer cite [3] and have it mean something.
  • Append-only transcript. Each round is appended, never rewritten, so the prompt prefix stays byte-identical between rounds.

The Agent Prompt

The prompt the planner sees every round. Three things are doing the work: a strict action contract of exactly one JSON object per turn, an enumerate-then-verify strategy, and hard evidence rules.

Model Calls

A plain OpenAI-compatible chat call, with two Mercury-specific notes:
reasoning_effort is the speed/quality dial: instant, low, medium, or high.Use response_format={"type": "json_object"} for the planner only. The final answer is free-form markdown, and forcing JSON there would coerce a perfectly good table into an empty object.
Finally, a wall-clock ledger, so a run can show how much of its time was the model and how much was waiting on the web.

The Loop

Plan, fan out searches in parallel, append evidence, and repeat until the agent says finish or the round cap is hit. Then one grounded synthesis call:

Run It

Three rounds of planning, 32 sources, and a synthesized table in about 7.5 seconds:
Planning cost roughly 900 ms across all three rounds. The rest was spent waiting on the web, which is the point: at Mercury speeds, the agent’s own deliberation stops being the bottleneck, so you can afford more rounds of it.

Your Own Question