---
title: "Ollama Web Search: Give Local Models Internet Access (Python)"
description: "Ollama models can't browse on their own. How to use Ollama's web_search and web_fetch API, how to give any Ollama model your own search tool with tool calling, a tested Python loop and its output, model choice, and when to use which."
category: "guides"
url: https://keirolabs.cloud/blog/ollama-web-search
published: 2026-11-24
updated: 2026-11-24
---

# Ollama web search: give local models internet access with tool calling

## The 60-second version

**Ollama models can't reach the internet on their own. You give them web search through tool calling:** the model asks for a search, your code runs it, and the results go back into the chat. There are two ways to supply the search:

| | Ollama's web search API | Your own search tool |
|---|---|---|
| Setup | Free Ollama account + `OLLAMA_API_KEY` | Any search API key |
| Python | `from ollama import web_search, web_fetch` | A function you pass as a tool |
| Results | 3 by default in the Python client; the API allows up to 10 | Whatever your provider returns |
| Pricing | No per-search pricing published by Ollama when we checked | Your provider's, e.g. $0.03 to $0.10 per 1,000 on Keirolabs plans |
| Zero data retention | Not documented | Depends on provider; Keirolabs: `"zdr": true` on paid plans |

**Both use the same loop.** We ran the code below end to end with `ollama-python` 0.6.3 and a tool-capable model. The output is further down.

## Does Ollama have internet access?

**No. A model running in Ollama only knows its training data.** Ask it about something recent and it will either say it doesn't know or confidently guess. Web search fixes that, but only if:

1. **The model supports tool calling.** Check the model's page for "tools". Small models call tools less reliably.
2. **Your code runs the loop:** send the chat with tools, run any tool calls, append the results, and call the model again until it answers.

## How do I use Ollama's built-in web search?

**Ollama runs a hosted web search and web fetch API.** Create an API key in your Ollama account, set `OLLAMA_API_KEY`, and the Python library exposes both as functions you can pass straight to the model as tools:

```python
from ollama import chat, web_search, web_fetch

messages = [{"role": "user", "content": "What's new in the latest Ollama release?"}]
tools = {"web_search": web_search, "web_fetch": web_fetch}

while True:
    resp = chat(model="qwen3:4b", messages=messages, tools=list(tools.values()))
    messages.append(resp.message)
    if not resp.message.tool_calls:
        print(resp.message.content)
        break
    for call in resp.message.tool_calls:
        result = tools[call.function.name](**call.function.arguments)
        messages.append({"role": "tool", "content": str(result)[:8000],   # cap long pages
                         "tool_name": call.function.name})
```

- **`web_search(query, max_results=3)`** returns results with `title`, `url` and `content`. Over the REST API (`POST https://ollama.com/api/web_search`), `max_results` defaults to 5 and caps at 10.
- **`web_fetch(url)`** returns a page's `title`, `content` and `links`.
- **Context:** search results are long. Ollama recommends a context window of about 32,000 tokens for search agents. Set it per request with `options={"num_ctx": 32768}` if your model's default is smaller.

## How do I give Ollama my own web search tool?

**Write a normal Python function with a docstring, and pass it as a tool.** The Ollama library turns the function's signature and docstring into the tool schema. Here it calls Keirolabs (our API):

```python
import os, requests
import ollama

def web_search(query: str) -> str:
    """
    Search the live web. Use it for anything recent, or anything you are not sure about.

    Args:
      query: A short search query, 2 to 10 words

    Returns:
      Numbered results with title, URL and snippet
    """
    r = requests.post("https://api.keirolabs.cloud/api/v2/search/lite",
                      headers={"Authorization": f"Bearer {os.environ['KEIRO_API_KEY']}"},
                      json={"query": query, "count": 6}, timeout=30)
    r.raise_for_status()
    return "\n\n".join(f"[{i+1}] {x['title']}\n{x['url']}\n{x.get('snippet', '')}"
                       for i, x in enumerate(r.json()["results"]))

messages = [
    {"role": "system", "content": "Today is 2026-11-24. Answer from search results and cite them as [n]."},
    {"role": "user", "content": "What is the latest stable version of Python?"},
]
for step in range(5):                                  # cap the loop
    resp = ollama.chat(model="glm-5.3-flash:cloud", messages=messages, tools=[web_search])
    messages.append(resp.message)
    if not resp.message.tool_calls:
        print(resp.message.content)
        break
    for call in resp.message.tool_calls:
        print("searching:", call.function.arguments["query"])
        messages.append({"role": "tool", "content": web_search(**call.function.arguments),
                         "tool_name": call.function.name})
```

**What it printed** when we ran it:

```text
searching: latest stable Python version 2026
searching: Python 3.15 latest release version number
searching: "Python 3.15.2" release download
The latest stable version of Python is **Python 3.15** (latest patch: **3.15.2**,
released October 25, 2026) [1][2]. ...
```

**The model searched three times on its own.** It found 3.15, checked the latest patch, then confirmed the exact version before answering. That refinement is the point of giving the model a tool rather than pasting in one fixed set of results.

**Details that matter:**

- **Every function argument becomes required.** The library marks all parameters as required, even those with defaults. Keep the tool to one `query` argument and set the result count inside the function.
- **The docstring is the tool description.** Say *when* to search ("anything recent"), and what a good query looks like ("2 to 10 words").
- **Put today's date in the system prompt.** Without it, models search for last year's information. In our logs of 163 OpenAI web search answers, 10% of the model's queries added an old year the user never asked for ([grounding AI agents](/blog/grounding-ai-agents-web-search)).
- **Return snippets, not pages.** A title, URL and snippet is about 65 tokens. Ten full pages can be 50,000+ tokens and swamp a local model. Add a second tool for full pages if the model needs them ([context engineering](/blog/context-engineering-web-search-agents)).
- **Cap the loop.** Small models sometimes repeat the same search. Five rounds is plenty.

## Can I use the OpenAI client with Ollama instead?

**Yes.** Ollama serves an OpenAI-compatible API at `http://localhost:11434/v1`. Point the OpenAI SDK there with any API key string, and the standard tool-calling loop works unchanged. The full version, with Claude and OpenAI variants, is in [how to give an LLM a web search tool](/blog/llm-web-search-tool-calling).

## Which Ollama models work best with web search?

- **Pick a model tagged for tools.** Without tool support, the model can't request a search at all.
- **Bigger is more reliable.** Small local models skip the tool, pass malformed arguments, or loop. Ollama's cloud models run large models through the same API if your hardware can't.
- **Test on your own questions.** Run 20 real questions and check that it searches when it should, and cites what it found.

## Ollama web search or your own search API?

**Use Ollama's web search** for personal projects and quick prototypes: no extra account, two functions, done.

**Use your own search API** when you need:

- **Predictable pricing and rate limits** for production.
- **Control over results:** filtering, domain lists, caching.
- **Page content or embeddings in the same call.** Keirolabs `/search/content` returns full text and optional embedding chunks.
- **Zero data retention** for sensitive queries: `"zdr": true` on Keirolabs paid plans ([web search APIs with ZDR](/blog/web-search-api-zero-data-retention)).
- **The same tool across Ollama, OpenAI and Claude.**

New Keirolabs accounts get up to 2,000 free credits with no card ([get a key](https://platform.keirolabs.cloud/register)).
