# Web search

A provider's own search runs in its infrastructure and returns citations; a scraper gets a bot challenge after a handful of requests. So each request resolves search from two facts: the session's Search switch (`web_search` on the session run config) and whether the API provider advertises `supports_web_search` (`_StreamFnProvider`, `ApiProviderSnapshot`).

| API | Request tool | Advertised |
| --- | --- | --- |
| `openai-responses`, `openai-codex` | `{"type": "web_search"}` | Yes |
| xAI over `openai-responses` | The same tool; the local `web_search` function is left out of the request, because xAI answers 400 “Duplicate tool names” to a function named like its search | Yes |
| `anthropic-messages` | `{"type": "web_search_20250305", "name": "web_search"}` | Yes |
| `google-generative-ai`, `gemini-subscription` | `{"google_search": {}}`, only in a request without function tools | No |
| DeepSeek, Kimi, local models, OpenAI-compatible endpoints | None | No |

With the switch on and the capability advertised, the runtime sets the request option `web_search=True` and removes the local `web_search` tool. Otherwise the local tool stays. Gemini's `generateContent` rejects grounding next to function declarations (Gemini 3 combines them only on the Interactions API) and an agent request always carries function tools; a tool-free completion with `web_search=True` still uses grounding.

## Local tool

| Case | Behaviour |
| --- | --- |
| Backends | Sixteen, for models without a provider search |
| No key | Only the DuckDuckGo page scraper |
| Unpinned fallback | Skips `arxiv`, a paper index; pinning still reaches it |
| Every backend fails | The error tells the model a search key fixes it (Settings > Search; Brave and Tavily have free tiers) and that it may open a search page with browser tools if it has them |

## From provider item to transcript

| Stage | Behaviour |
| --- | --- |
| Stream | Stream processors turn search items into a `ServerToolUse` block and `servertool_start` / `servertool_end` events. |
| Agent loop | Forwards them without executing. |
| Client | `tool_use` / `tool_result` with `tool: "web_search"`, `provider_executed: true`, `input` `{"query": ...}`, `result` one `title — url` per line, or `completed` when the provider reports no sources. |
| Persisted | Same shape in the reply's blocks, so a reloaded transcript shows “Searched the web: …” in place. |
| Citations | `url_citation` (Responses), `citations` (Anthropic), `groundingMetadata` (Gemini) become `citations: [{url, title, start, end}]` on `TextContent`, forwarded with `text_end`, persisted, rendered as a sources row under the answer. |

## Replay

| Next request goes to | The block |
| --- | --- |
| Responses, same API | Resent as a `web_search_call` item |
| Anthropic, same API | Resent as `server_tool_use` plus the raw `web_search_tool_result`, whose hits carry `encrypted_content` that must go back verbatim; a block without the raw result is dropped |
| Gemini | Grounding blocks get synthetic ids and are never resent |
| A different API | Dropped, like foreign thinking signatures |

**Wire contract**

Content block, `openprogram/models/providers/types.py`:

```
class ServerToolUse(BaseModel):
    type: Literal["serverToolUse"] = "serverToolUse"
    id: str                 # provider item id
    name: str               # "web_search"
    input: dict             # {"query": str}
    status: str             # "searching" | "completed" | "failed"
    results: list[dict]     # [{"title": str, "url": str}]
    raw: Any | None = None  # provider item, for replay
```

| Name | Shape |
| --- | --- |
| Text block | `TextContent.citations: list[Citation] \| None`, `Citation(url, title, start, end)` |
| Message events | `EventServerToolStart(type="servertool_start", content_index, partial)`, `EventServerToolEnd(type="servertool_end", content_index, server_tool: ServerToolUse, partial)` |
| Client events | `apps/web/lib/net/ws/chat-stream.ts`: `citations` on the text block at `text_end`; the search block arrives in the reply row of a `transcript_update` |
| Persisted block | `{"type": "tool", "tool": "web_search", "provider_executed": true, "input": "{\"query\": ...}", "result": ...}` |

**What comes from other agents**

| Agent | Its search | Taken |
| --- | --- | --- |
| Claude Code | Provider `web_search` server tool, results with citations | Provider tool first, citations on the text |
| Codex | `--search` enables the Responses tool; one “Searched the web: query” line per search | One visible step per search |
| Gemini CLI | `google_web_search` is grounding; numbered sources at the end | A sources row under the answer |
| OpenProgram adds | The local tool, and a session-level switch that follows the session's model | The local tool, and a session-level switch that follows the session's model |

## Implementation status

| Area | State |
| --- | --- |
| Capability flag | `openai-responses`, `openai-codex`, `anthropic-messages` |
| Request builders | All five APIs |
| Stream processing | Responses (shared processor), Anthropic, Gemini |
| Runtime | Intent to request option, local tool removal, event projection, persistence, local tool error text and fallback order |
| Client | Provider-executed search cards and citation rows |
