> ## Documentation Index
> Fetch the complete documentation index at: https://docs.anyapi.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Web Search

Give your AI models access to real-time web data. Instead of relying on training data alone, models can search the web and ground their responses in current information.

## What This Unlocks

* **Live web data** — No more "I don't know about recent events"
* **Current information** — Break free from training cutoff dates
* **Automatic integration** — Search results blend seamlessly into responses
* **Configurable depth** — Control how much web context to include

## How It Works

Add `web_search_options` to your API request. AnyAPI passes the search request to the model's provider, which performs the web search and returns a response grounded in real-time data. The exact search mechanism depends on the provider — Anthropic uses its native web search tool, Google uses grounding with search, OpenAI uses its built-in search, and so on.

## Chat Completions API

### Basic Example

<CodeGroup>
  ```bash cURL theme={"system"}
  curl -X POST "https://api.anyapi.ai/v1/chat/completions" \
    -H "Authorization: Bearer your_api_key" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "anthropic/claude-sonnet-4",
      "messages": [
        {
          "role": "user",
          "content": "What are the latest developments in AI research this week?"
        }
      ],
      "web_search_options": {
        "search_context_size": "medium"
      }
    }'
  ```

  ```python Python theme={"system"}
  import requests

  response = requests.post(
      "https://api.anyapi.ai/v1/chat/completions",
      headers={
          "Authorization": "Bearer your_api_key",
          "Content-Type": "application/json"
      },
      json={
          "model": "anthropic/claude-sonnet-4",
          "messages": [
              {
                  "role": "user",
                  "content": "What are the latest developments in AI research this week?"
              }
          ],
          "web_search_options": {
              "search_context_size": "medium"
          }
      }
  )

  print(response.json()["choices"][0]["message"]["content"])
  ```

  ```javascript JavaScript theme={"system"}
  const response = await fetch('https://api.anyapi.ai/v1/chat/completions', {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer your_api_key',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      model: 'anthropic/claude-sonnet-4',
      messages: [
        {
          role: 'user',
          content: 'What are the latest developments in AI research this week?'
        }
      ],
      web_search_options: {
        search_context_size: 'medium'
      }
    })
  });

  const data = await response.json();
  console.log(data.choices[0].message.content);
  ```
</CodeGroup>

### The `web_search_options` Parameter

| Parameter             | Type   | Description                                                                                        |
| --------------------- | ------ | -------------------------------------------------------------------------------------------------- |
| `search_context_size` | string | Amount of web context to include: `"low"`, `"medium"` (default), or `"high"`                       |
| `user_location`       | object | Optional location for localized search results (supported by OpenAI, Anthropic, and Google models) |

### Localized Search with `user_location`

Pass a location to get region-specific search results:

```python Python theme={"system"}
response = requests.post(
    "https://api.anyapi.ai/v1/chat/completions",
    headers={
        "Authorization": "Bearer your_api_key",
        "Content-Type": "application/json"
    },
    json={
        "model": "anthropic/claude-sonnet-4",
        "messages": [
            {"role": "user", "content": "What's the weather forecast for this weekend?"}
        ],
        "web_search_options": {
            "search_context_size": "low",
            "user_location": {
                "type": "approximate",
                "city": "San Francisco",
                "region": "California",
                "country": "US",
                "timezone": "America/Los_Angeles"
            }
        }
    }
)
```

## Responses API

You can also use web search through the `/v1/responses` endpoint with the `web_search_preview` tool:

<CodeGroup>
  ```python Python theme={"system"}
  import requests

  response = requests.post(
      "https://api.anyapi.ai/v1/responses",
      headers={
          "Authorization": "Bearer your_api_key",
          "Content-Type": "application/json"
      },
      json={
          "model": "openai/gpt-5",
          "input": "What is happening in the stock market today?",
          "tools": [
              {
                  "type": "web_search_preview",
                  "search_context_size": "medium"
              }
          ]
      }
  )

  print(response.json())
  ```

  ```javascript JavaScript theme={"system"}
  const response = await fetch('https://api.anyapi.ai/v1/responses', {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer your_api_key',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      model: 'openai/gpt-5',
      input: 'What is happening in the stock market today?',
      tools: [
        {
          type: 'web_search_preview',
          search_context_size: 'medium'
        }
      ]
    })
  });

  const data = await response.json();
  console.log(data);
  ```
</CodeGroup>

## Search Context Levels

| Level      | Context  | Speed    | Best For                            |
| ---------- | -------- | -------- | ----------------------------------- |
| `"low"`    | Minimal  | Fastest  | Quick fact-checks, simple questions |
| `"medium"` | Balanced | Moderate | News updates, general research      |
| `"high"`   | Maximum  | Slowest  | Deep analysis, complex research     |

## Real-World Examples

### Latest News

```python Python theme={"system"}
response = requests.post(
    "https://api.anyapi.ai/v1/chat/completions",
    headers={
        "Authorization": "Bearer your_api_key",
        "Content-Type": "application/json"
    },
    json={
        "model": "google/gemini-2.5-flash",
        "messages": [
            {"role": "user", "content": "What are the top tech news stories today?"}
        ],
        "web_search_options": {
            "search_context_size": "medium"
        }
    }
)
```

### Deep Research with Perplexity

```python Python theme={"system"}
response = requests.post(
    "https://api.anyapi.ai/v1/chat/completions",
    headers={
        "Authorization": "Bearer your_api_key",
        "Content-Type": "application/json"
    },
    json={
        "model": "perplexity/sonar-pro",
        "messages": [
            {"role": "user", "content": "Analyze current trends in renewable energy investments"}
        ],
        "web_search_options": {
            "search_context_size": "high"
        }
    }
)
```

### Reasoning Over Web Results

```python Python theme={"system"}
response = requests.post(
    "https://api.anyapi.ai/v1/chat/completions",
    headers={
        "Authorization": "Bearer your_api_key",
        "Content-Type": "application/json"
    },
    json={
        "model": "openai/o3",
        "messages": [
            {"role": "user", "content": "Compare the latest quarterly earnings of NVIDIA vs AMD"}
        ],
        "web_search_options": {
            "search_context_size": "high"
        }
    }
)
```

## Supported Models

Web search works with 800+ models on AnyAPI, including:

* **OpenAI:** [GPT-5](https://anyapi.ai/ai-models/openai-gpt-5), [GPT-5 Mini](https://anyapi.ai/ai-models/openai-gpt-5-mini), [GPT-4o](https://anyapi.ai/ai-models/openai-gpt-4o), [GPT-4o Search Preview](https://anyapi.ai/ai-models/openai-gpt-4o-search-preview), [o3-Pro](https://anyapi.ai/ai-models/openai-o3-pro), and more
* **Anthropic:** [Claude Opus 4.6](https://anyapi.ai/ai-models/anthropic-claude-opus-4-6), [Claude Sonnet 4.6](https://anyapi.ai/ai-models/anthropic-claude-sonnet-4-6), [Claude Haiku 4.5](https://anyapi.ai/ai-models/anthropic-claude-haiku-4-5), and more
* **Google:** [Gemini 2.5 Pro](https://anyapi.ai/ai-models/google-gemini-2-5-pro), [Gemini 2.5 Flash](https://anyapi.ai/ai-models/google-gemini-2-5-flash), [Gemini 3 Pro/Flash Preview](https://anyapi.ai/ai-models/gemini-3-pro-preview), and more
* **xAI:** [Grok 4](https://anyapi.ai/ai-models/xai-grok-4), [Grok 4.1 Fast](https://anyapi.ai/ai-models/xai-grok-4-1-fast), [Grok 3](https://anyapi.ai/ai-models/xai-grok-3), and more
* **Perplexity:** Sonar, [Sonar Pro](https://anyapi.ai/ai-models/perplexity-sonar-pro), Sonar Deep Research, [Sonar Reasoning Pro](https://anyapi.ai/ai-models/perplexity-sonar-reasoning-pro)
* **DeepSeek:** [DeepSeek V3.2](https://anyapi.ai/ai-models/deepseek-deepseek-v3-2), [DeepSeek R1](https://anyapi.ai/ai-models/deepseek-r1), and more
* **Qwen:** [Qwen3 Max](https://anyapi.ai/ai-models/qwen-qwen3-max), [Qwen3 Coder Plus](https://anyapi.ai/ai-models/owen-qwen3-coder-plus), Qwen3 235B, and more
* **Meta:** [Llama 4 Maverick](https://anyapi.ai/ai-models/meta-llama-4-maverick), Llama 4 Scout, [Llama 3.1 405B](https://anyapi.ai/ai-models/meta-llama-3-1-405b-instruct), and more
* **Mistral:** [Mistral Large](https://anyapi.ai/ai-models/mistral-large), [Codestral](https://anyapi.ai/ai-models/mistral-codestral-2508), Devstral, Magistral Small, and more

Check the [model database](https://dash.anyapi.ai/?page=model-hub) for the full list of models with web search support.

## Pricing

Web search incurs an additional per-search cost on top of regular token pricing. The exact cost depends on the model and provider.

## Pro Tips

1. **Match context to your needs:** Quick questions = `"low"`, research = `"high"`
2. **Be specific in your prompts:** "Tesla Q4 2025 earnings" beats "stock market"
3. **Use with reasoning models:** Models like o3, DeepSeek R1, and Claude with extended thinking can reason over web results for deeper analysis
4. **Use Perplexity for research:** Sonar models are built for search and return citations by default

## Things to Keep in Mind

* **Slight latency:** Web search adds a moment to response time
* **Cost scaling:** Higher `search_context_size` uses more tokens and increases cost
* **Rate limits:** Standard rate limits apply to requests with web search

***

*Give your AI models internet access. Web search turns any model into a research assistant that knows what happened five minutes ago.*
