LLM Gateway
Features

Native Web Search

Enable real-time web search capabilities to get up-to-date information from the internet.

LLM Gateway supports native web search capabilities that allow models to access real-time information from the internet. This feature is useful for answering questions about current events, recent news, live data, and other time-sensitive information that may not be in the model's training data.

How It Works

When you include the web_search tool in your request, the model can search the web to gather relevant information before generating a response:

  1. You send a request with the web_search tool enabled
  2. The model determines if web search is needed based on the query, unless you require one
  3. If needed, the model performs web searches to gather current information
  4. The model synthesizes the search results and generates a response
  5. Citations are included in the response to show information sources

Need raw ranked results instead of a model-written answer? Use the Search API.

Supported Providers

The web_search tool works on every chat model. Models with native web search, listed on our models page, search through their provider. For every other chat model, LLM Gateway runs the search itself. If a request with native search fails, it falls back only to providers with native search; after a gateway-run search, it falls back to any provider with the searched prompt.

To check programmatically, read each entry of a model's providers array in GET /v1/models: web_search is true when the web_search tool works on that provider, web_search_native is true when the provider searches natively, and web_search_forced_only is true when that native search only runs when you require a search.

When the provider serving the request has no native web search, LLM Gateway gives the model a web_search function and runs the searches it asks for with a search engine. Like native search, the model decides whether a turn needs a search, writes its own queries, reads the results, can search again, and then answers. The request and response look the same as native search, with these differences:

  • Each model call is its own request in your activity log, with the request ID plus -ws0, -ws1, …, and is billed as usual. A turn that needs no search takes one call. Each search is billed on the call that reads its results; the response's usage covers all calls.
  • Text the model writes before a search streams as it is written, and the sources of each search arrive as url_citation annotations before the text that cites them.
  • max_uses caps the searches per request (default 5). search_context_size sets the number of results per search (low 3, medium 5, high 10). allowed_domains, blocked_domains, and user_location.country are passed to the search.
  • /v1/responses requests, models without tool calling, requests with n above 1, required searches on models that cannot be forced to call a function, and requests whose own function is named web_search get one search before the model answers, on new user turns only. A small model (Gemini Flash-Lite on Google AI Studio) writes up to three queries from the latest turns and, for an offered tool, decides whether the turn needs a search; the results are appended to your latest user message. If that model is unavailable, your latest user message is the query.

The search engine is an organization setting under Settings → Preferences → Web Search. It applies only when the serving provider has no native web search:

EnginePrice per searchData handling
Perplexity Search API (default)$0.005No training on queries; retention not zero
Exa$0.007Queries may be retained and used for training

If the search fails, the request continues without results and no search is billed. No search runs when tool_choice is "none", or when your organization's or API key's compliance policy rejects the engine: the engine receives the search queries, so the policy applies to it like to a provider. For models without tool calling, the query model is checked the same way as the google-ai-studio provider; when the policy rejects it, your latest user message is the query. On your own provider keys, the search is charged to your credits, and it is skipped when you have none. With tool_choice: {"type": "web_search"}, a search that cannot run returns an error instead of an answer without results. Only organization owners can change the engine. Your activity log shows which engine ran each search.

Perplexity Sonar changed on September 25, 2026. Perplexity retired its Sonar chat completions API on September 27. On September 25, perplexity/sonar moved to Perplexity's Agent API: the model id, the request shape and the response fields stayed the same, and the flat per-request fee was replaced by per-search billing. perplexity/sonar-pro and perplexity/sonar-reasoning-pro have no equivalent on that API and stopped being routable on September 27. Read the full announcement.

Basic Usage

To enable web search, add the web_search tool to your request:

curl -X POST "https://api.llmgateway.io/v1/chat/completions" \
  -H "Authorization: Bearer $LLM_GATEWAY_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-5.2",
    "messages": [
      {
        "role": "user",
        "content": "What is the current weather in San Francisco?"
      }
    ],
    "tools": [
      {
        "type": "web_search"
      }
    ]
  }'

Example Response

{
	"id": "chatcmpl-abc123",
	"object": "chat.completion",
	"created": 1234567890,
	"model": "openai/gpt-5.2",
	"choices": [
		{
			"index": 0,
			"message": {
				"role": "assistant",
				"content": "The current weather in San Francisco is 57°F (14°C) with mostly cloudy skies...",
				"annotations": [
					{
						"type": "url_citation",
						"url": "https://weather.com/...",
						"title": "San Francisco Weather"
					}
				]
			},
			"finish_reason": "stop"
		}
	],
	"usage": {
		"prompt_tokens": 15,
		"completion_tokens": 150,
		"total_tokens": 165,
		"cost": 0.0315
	}
}

Web Search Options

The web_search tool accepts optional configuration parameters:

User Location

Provide location context to get more relevant local search results:

{
	"type": "web_search",
	"user_location": {
		"city": "San Francisco",
		"region": "California",
		"country": "US",
		"timezone": "America/Los_Angeles"
	}
}

Search Context Size

Control the amount of web content retrieved (OpenAI only):

{
	"type": "web_search",
	"search_context_size": "medium"
}

Available values:

  • low - Minimal search context, faster responses
  • medium - Balanced context (default)
  • high - Maximum search context, more comprehensive

Max Uses

Limit the number of searches per request (provider-dependent):

{
	"type": "web_search",
	"max_uses": 3
}

Domain Filters

Restrict which domains the model may search with allowed_domains, or exclude domains with blocked_domains. The two fields are mutually exclusive: a web_search tool that sets both (non-empty) is rejected with a 400 (code: "unsupported_parameter_combination"). Provider support varies — currently the filters reach Anthropic-served requests, and other providers ignore them:

{
	"type": "web_search",
	"allowed_domains": ["example.com"]
}

OpenAI's external_web_access: false asks for a search of OpenAI's cached index only, without fetching live pages. Codex CLI sends it on every request unless its web_search setting is "live" or "disabled":

{
	"type": "web_search",
	"external_web_access": false
}

The gateway forwards it to OpenAI's Responses API, which bills each search it runs like a live one. No other provider has a cache-only mode, so there the gateway drops the tool: the request is validated, routed and billed as if it had no web search, and works on models without native web search. Omitted or true means live search.

Shorthand: web_search: true

As a shortcut, set the top-level web_search body field to true instead of adding the tool — the gateway injects a default web_search tool for you (it has no effect if the tool is already present):

{
	"model": "gpt-5.2",
	"messages": [{ "role": "user", "content": "What happened in tech today?" }],
	"web_search": true
}

OpenAI web_search_options

Chat Completions also accepts OpenAI's web_search_options body field. It is the same request as a web_search tool with the same search_context_size and user_location (OpenAI's nested approximate form); if you send both, the tool's own fields win:

{
	"model": "gpt-5.2",
	"messages": [{ "role": "user", "content": "What happened in tech today?" }],
	"web_search_options": {
		"search_context_size": "low",
		"user_location": {
			"type": "approximate",
			"approximate": { "country": "US", "city": "San Francisco" }
		}
	}
}

Responses API

/v1/responses takes the same tool as tools: [{"type": "web_search"}]. OpenAI's other names for it, web_search_preview and dated variants such as web_search_2025_08_26, are accepted too, as is filters.allowed_domains. tool_choice: {"type": "web_search"} (or web_search_preview) requires a search (see Requiring a Search for the exception).

By default the web_search tool offers the model a search and lets it judge whether the question needs one. To require a search on every request, set tool_choice:

{
	"model": "...",
	"messages": [{ "role": "user", "content": "What shipped in AI this week?" }],
	"tools": [{ "type": "web_search" }],
	"tool_choice": { "type": "web_search" }
}

DeepSeek ignores a forced search: on a request it serves, the model still decides whether to search. Pin another provider when every request must search.

Reach for this sparingly. A forced search is billed on every request that carries it, and the retrieved snippets are appended to your prompt, so they are billed as input tokens too — on a question the model could have answered from memory, you pay for both and gain nothing. If you are building a chat interface with a "web search" toggle, leaving the tool attached with the default tool_choice is usually what you want, so that follow-ups like "shorter, please" do not trigger a search.

Claude cannot be forced to search while extended thinking runs with a token budget (reasoning.max_tokens on budget-based models); there the search stays an offer.

A few upstreams have no model-elected search at all and can only search when asked to. Requiring a search is the only way to use their search; without it they behave like a model that decided not to search, and the gateway prefers to route a merely offered tool to a provider that can make that decision for itself. You can find the models with native web search on the models page.

Using with SDKs

OpenAI SDK (Python)

from openai import OpenAI

client = OpenAI(
    base_url="https://api.llmgateway.io/v1",
    api_key="your-api-key"
)

response = client.chat.completions.create(
    model="gpt-5.2",
    messages=[
        {"role": "user", "content": "What are the latest news headlines today?"}
    ],
    tools=[{"type": "web_search"}]
)

print(response.choices[0].message.content)

OpenAI SDK (TypeScript)

import OpenAI from "openai";

const client = new OpenAI({
	baseURL: "https://api.llmgateway.io/v1",
	apiKey: "your-api-key",
});

const response = await client.chat.completions.create({
	model: "gpt-5.2",
	messages: [{ role: "user", content: "What are the latest tech news?" }],
	tools: [{ type: "web_search" }],
});

console.log(response.choices[0].message.content);

Streaming

Web search works with streaming responses. Citations are included in the final chunks:

curl -X POST "https://api.llmgateway.io/v1/chat/completions" \
  -H "Authorization: Bearer $LLM_GATEWAY_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-5.2",
    "messages": [
      {"role": "user", "content": "What is the current stock price of Apple?"}
    ],
    "tools": [{"type": "web_search"}],
    "stream": true
  }'

Citations and Sources

Web search responses include citations to show where information was sourced from. These appear in the annotations field of the message:

{
	"annotations": [
		{
			"type": "url_citation",
			"url": "https://example.com/article",
			"title": "Article Title",
			"start_index": 0,
			"end_index": 50,
			"date": "2026-09-16",
			"last_updated": "2026-09-17"
		}
	]
}

Citation format may vary slightly between providers, but LLM Gateway normalizes them into a consistent structure. date and last_updated are only present when the provider reports them for that source.

Search-grounded models that return their sources as a set rather than as inline citations also include a top-level search_results array alongside choices, with one entry per source (url, title, snippet, date, last_updated). Streaming responses carry it on the chunk that delivers the sources.

Cost Tracking

Web search costs are rolled into the total cost reported in the usage object:

{
	"usage": {
		"prompt_tokens": 15,
		"completion_tokens": 150,
		"total_tokens": 165,
		"cost": 0.0125,
		"cost_details": {
			"upstream_inference_cost": 0.0115,
			"upstream_inference_prompt_cost": 0.0015,
			"upstream_inference_completions_cost": 0.01,
			"total_cost": 0.0125,
			"input_cost": 0.0015,
			"output_cost": 0.01,
			"web_search_cost": 0.001
		}
	}
}

Web search is billed per search call at the model's web search price — for example, $0.01 per call on OpenAI reasoning models (GPT-5, o-series). The web search charge is included in the top-level cost value and surfaced separately as cost_details.web_search_cost. A gateway-run search is billed once per search, at the price of your organization's engine.

On Airside listings that declare a web search format, a search is billed only when the response shows one ran: url_citation annotations, a search counter in usage (server_tool_use.web_search_requests or num_search_queries), or reported prompt tokens beyond what the request itself could encode to — the retrieved pages the endpoint added to the prompt. Offering or requiring a search bills nothing on its own.

When the upstream reports its billable server-side operations in usage.server_tool_use (tool_search, tool_fetch), every successful search and page fetch is billed as one search call. Failed searches and searches that return no results are not billed.

Combining with Function Tools

You can use web search alongside regular function tools:

{
	"tools": [
		{ "type": "web_search" },
		{
			"type": "function",
			"function": {
				"name": "get_weather",
				"description": "Get weather for a location",
				"parameters": {
					"type": "object",
					"properties": {
						"location": { "type": "string" }
					}
				}
			}
		}
	]
}

Some dedicated search models only support web search and do not support additional function tools. Use gpt-5.2 or other GPT-5 series models if you need both web search and function tools.

Use Cases

Current Events and News

{
	"messages": [
		{ "role": "user", "content": "What are the major news stories today?" }
	],
	"tools": [{ "type": "web_search" }]
}

Real-Time Data

{
	"messages": [
		{ "role": "user", "content": "What is the current price of Bitcoin?" }
	],
	"tools": [{ "type": "web_search" }]
}

Research and Fact-Checking

{
	"messages": [
		{
			"role": "user",
			"content": "What are the latest findings on climate change?"
		}
	],
	"tools": [{ "type": "web_search" }]
}

Local Information

{
	"messages": [
		{
			"role": "user",
			"content": "What restaurants are open near me right now?"
		}
	],
	"tools": [
		{
			"type": "web_search",
			"user_location": {
				"city": "New York",
				"country": "US"
			}
		}
	]
}

Best Practices

  1. Use GPT-5.2: For the best web search experience with full tool support, use gpt-5.2
  2. Provide location context: When queries are location-dependent, include user_location for more relevant results
  3. Monitor costs: Web search incurs per-query costs in addition to token costs
  4. Check citations: Always review the citations in responses to verify information sources
  5. Use streaming: For user-facing applications, enable streaming to show responses as they're generated

Error Handling

Models that do not produce chat completions (image, video, embedding, speech and realtime models) reject the web_search tool with a 400 invalid_request_error.

How is this guide?

Last updated on

On this page

Ready for production?

Ship to production with SSO, audit logs, spend controls, and guardrails your security team will approve.

Explore Enterprise