Skip to main content

Web search interception

Some providers offer a built-in web search tool, and others do not. Web search interception makes web search available to any model on the gateway. When a model that has been given the web search tool emits a search tool call, the gateway intercepts it, runs the query through one of its configured search tools, feeds the results back to the model, and continues the conversation. The client sees a normal response with search results, including the Anthropic style server_tool_use and web_search_tool_result blocks when the request came through the Messages API.

A real-world scenario​

A team runs Claude models on AWS Bedrock, which has no native web search there, and their agent asks for the Messages API web_search tool. The admin turns on web search interception for Bedrock with the company's Tavily search tool, and the agent's searches start working without a code change.

Steps​

  1. Register a search tool, see Search.

  2. In the Admin UI open Settings, then Admin Settings, and select the Web Search Interception tab. Turn on Enable Web Search Interception, pick bedrock under Providers, pick the tool under Search Tool, and click Save Settings. The same change through the API, with a proxy admin key:

    curl -X PATCH http://localhost:4000/update/websearch_interception_settings \
    -H "Authorization: Bearer $SPANWAY_MASTER_KEY" -H "Content-Type: application/json" \
    -d '{"enabled": true, "enabled_providers": ["bedrock"], "search_tool_name": "tavily-search", "max_agentic_loops": 3}'

    The answer is {"status": "success", ...} and every gateway instance picks the change up within about ten seconds. Or set the YAML under Configuration instead.

  3. Send a request with the web search tool to a Bedrock model.

    curl http://localhost:4000/v1/messages \
    -H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \
    -d '{"model": "bedrock-claude", "max_tokens": 500, "messages": [{"role": "user", "content": "What changed in the latest gateway release?"}],
    "tools": [{"type": "web_search_20250305", "name": "web_search"}]}'
  4. Open Logs and check that the request shows the search queries and the follow-up model calls under one request id.

Expected result​

The answer carries server_tool_use and web_search_tool_result blocks, and the spend log shows the search cost next to the model cost.

Configuration​

spanway_settings:
callbacks: ["websearch_interception"]
websearch_interception_params:
enabled_providers: ["bedrock", "vertex_ai"]
search_tool_name: "tavily-search"
max_agentic_loops: 5

search_tools:
- search_tool_name: tavily-search
spanway_params:
search_provider: tavily
api_key: os.environ/TAVILY_API_KEY

The callbacks: ["websearch_interception"] line is what turns interception on when you configure it in YAML. Without it the websearch_interception_params block is read by nothing. The Admin UI tab and the settings API do not need the line, because they store an enabled flag in the database instead.

SettingMeaning
enabledAdmin UI and settings API only. On means the gateway runs search tool calls for the listed providers. Off means requests go to the provider unchanged, so a model without native search answers without searching. Default off.
enabled_providersProvider names whose requests are intercepted. Leaving it unset means Bedrock only. In YAML an explicit empty list [] intercepts no provider at all, while an empty list saved from the Admin UI is treated as unset.
search_tool_nameSearch tool from search_tools to use. Defaults to the first configured tool. When no search tool is registered at all, the gateway falls back to the Perplexity search provider, and a key under general_settings.search_tool_deny_by_default may not use that fallback.
max_agentic_loopsHow many follow-up model calls one request may chain. Defaults to 3. Each loop is one more billed model call plus the search cost, so a higher value can multiply the cost of one answer.

Every search also goes through the caller's search tool grants. When the key, its team or its user has a search tool list, or search_tool_deny_by_default is on, a search with a tool the caller was not granted is refused, see Errors.

How a request flows​

  1. A client sends a request with the web search tool enabled, for example web_search_options on chat completions or the web_search tool on the Messages API.
  2. The gateway rewrites the request so the model receives a function tool it can call for searches.
  3. When the model calls it, the gateway runs the query through the search tool, with the query and, for providers that support richer input, an objective and several keyword queries.
  4. The results are added to the conversation and the model is called again, up to max_agentic_loops times.
  5. The final answer is returned with the search results attached in the format the client asked for.

Search errors are reported in the result block with one of three codes, so the model can explain the failure instead of hallucinating. too_many_requests means the search provider rate limited the call, invalid_tool_input means the provider rejected the query as a bad request, and unavailable covers every other failure, such as a wrong key, a timeout or a search the caller is not allowed to use.

Cost and logging​

Each search query is costed from the search provider's price in the bundled price map, and each follow-up model call is costed as usual. All of them are logged under the original request id, so the spend log shows the full cost of an answer that needed searches.

Search tools are described on the search page and the provider pages under it. For models with native web search, the gateway passes the provider's own tool through unchanged.

Errors​

What you seeWhat it meansFix
The model answers without searchingInterception is off, the YAML has no callbacks: ["websearch_interception"] line, or the provider is not in enabled_providers.Turn it on, add the callback line, or add the provider. Unset means Bedrock only, and an empty YAML list means no provider.
A result block with unavailableThe search tool failed, for example a wrong key, or the caller is not allowed to use it ("Key not allowed to access search tool: ...").Fix the search tool and test it on Search Tools, or grant the tool to the key or team.
A result block with too_many_requestsThe search provider rate limited the call.Wait, or raise the limit at the search provider.
A result block with invalid_tool_inputThe search provider rejected the query.Check the query the model sent in Logs.
The request fails with "Exceeded max_agentic_loops=3 for model=..."The model kept calling the search tool past max_agentic_loops.Raise the value, or tell the model in the prompt to search less.
Not running on the proxy that answered this page that does not go awayThe change did not reach that instance.Check that the instances share the database and Redis.
"Could not update settings"The save failed.Check the gateway log, and that you are a proxy admin.

Logs​

LevelLineWhat it meansAction
ERRORWebSearchInterception: Search failed: <error type>, WebSearchInterception: Search <n> failed: <error type> or WebSearchInterception: Responses search failed: <error type>One search call failed, and the model received a result block with an error code. The line carries the error type only, so the search query, which is built from the user's prompt, never reaches the log, even when the provider's error text quotes it.Normal now and then. When it repeats, test the search tool on Search Tools.
WARNINGFailed to reinitialize web search interception settings immediately: <error>A change saved on the settings page was stored, but this instance could not apply it at once. Every instance, this one included, applies it at its next settings check, within about 10 seconds.None if it happens once. When it repeats, check the error and that the instances share the database.
DEBUGWebSearchInterception: Search tool '<name>' not found in ..., falling back to first available or perplexitysearch_tool_name names a tool that is not registered. Shown only with detailed debug logging.Fix the name or register the tool.