Web Search¶
Use grounding=True on an LLMStep when a field needs live data. The provider searches the web and answers in the same call, and any sources the provider returns come back in a sources field.
For larger runs, web_search() splits the work into two steps: search first, then analyse. That costs one extra step, and in return the analysis step can use batch pricing and the research is cached on its own.
Grounding (the default)¶
from accrue import LLMStep, Pipeline
pipeline = Pipeline([
LLMStep("research",
fields={
"summary": "Summarize recent news about this company",
"stage": {"prompt": "Funding stage", "enum": ["Seed", "Series A", "Series B+", "Public", "Unknown"]},
},
model="claude-haiku-5-5",
grounding=True,
),
])
result = pipeline.run([{"company": "Snowflake"}])
Each provider uses its own search: OpenAI's Responses API web_search tool, Anthropic's web search server tool, or Google Search. On OpenAI and Anthropic the strict JSON schema still applies, so enums are enforced while the model searches. On Google, grounding falls back to JSON mode: accrue parses and validates the JSON, but the decoder doesn't enforce the schema.
Configuration¶
Pass True for defaults, or a dict (or GroundingConfig) for more control:
from accrue import GroundingConfig, LLMStep
LLMStep("research",
fields={"summary": "Summarize recent news"},
grounding={
"allowed_domains": ["crunchbase.com", "sec.gov", "bloomberg.com"],
"user_location": {"country": "GB", "city": "London"},
"max_searches": 3,
},
)
LLMStep("research",
fields={"summary": "Summarize recent news"},
grounding=GroundingConfig(
allowed_domains=["crunchbase.com"],
provider_kwargs={"search_context_size": "high"}, # OpenAI-specific
),
)
Provider support¶
Options a provider doesn't support are ignored, with a warning in the log.
| Feature | OpenAI | Anthropic | |
|---|---|---|---|
allowed_domains |
Yes | Yes | No (warns) |
blocked_domains |
Yes | Yes | Yes |
user_location |
Yes | Yes | No (warns) |
max_searches |
No (warns) | Yes | No (warns) |
provider_kwargs |
Merged into tool config | Merged into tool config | Merged into tool config |
| Strict JSON schema | Yes | Yes | No (JSON mode) |
Anthropic takes allowed_domains or blocked_domains, not both: accrue fails the row (or web_search() at construction) rather than send a request the API rejects. OpenAI takes up to 100 of each.
Sources on Gemini¶
Gemini can leave the sources out of a JSON answer, even when it searched. A grounded step always asks for JSON, so on Gemini expect sources to be missing from many rows (the key is absent); in checks on gemini-3.1-flash-lite and gemini-3.5-flash-lite with short answers, all 10 rows seen to search came back without it. A missing sources does not mean the model answered from memory, and Google bills the searches either way. If you need URLs on Gemini, use web_search(): its answer is plain text, and Gemini returns sources with it.
Rows answered without a search¶
Each provider lets the model decide whether to search. When it answers without searching, the row succeeds and looks like any other, so accrue logs one warning per step with the number of rows and their indices:
Step 'research': 12 of 40 grounded rows show no sign of a web search, so those answers may come from the model's own knowledge (row index 0, 3, 7, 8, 11, 12, 15, 20, 22, 23, ...).
accrue reads this from the search calls in the OpenAI and Anthropic responses, and on Gemini 3 and 4 by asking the API to return its server-side tool calls (this adds no tokens or calls). Where it can't tell, the row isn't counted: Gemini 2.x and the -latest names, Vertex AI, a custom Gemini base URL, an OpenAI gateway set through OPENAI_BASE_URL (unless the answer shows sources or a search call), OpenAI-compatible endpoints set with base_url= (which get no search tool), google-genai versions without the option, requests that set tools, tool_config or http_options through provider_kwargs, and custom clients. Rows read from the cache aren't counted either, and neither is web_search(). A gateway that strips the search blocks from Anthropic responses makes searched rows look unsearched. A search showing up doesn't mean the answer used it.
Long searches on Anthropic¶
A search turn that runs many searches can hit Anthropic's per-request limit on its server-side tool loop. The response then ends with stop_reason: "pause_turn". accrue resumes it: it sends the request again with the paused content appended, up to 5 times. The step's usage and sources cover every request, and a strict schema is parsed from the final answer. Change the limit with AnthropicClient(max_pause_continuations=...) (0 turns resuming off). If the turn is still paused at the limit, the answer so far is kept with a warning; a step with a schema and no JSON answer yet fails once rather than retrying. An API error on a continuation fails the call, and a retry starts the turn again. A lower max_searches keeps turns short.
Citations¶
Sources go into the step output under sources. Each one has url, title and snippet.
LLMStep("research", fields={...}, grounding=True)
# Output row: {"summary": "...", "sources": [{"url": "...", "title": "...", "snippet": "..."}]}
LLMStep("research", fields={...}, grounding=True, sources_field="refs") # rename
LLMStep("research", fields={...}, grounding=True, sources_field=None) # drop
With a strict schema the providers don't always return inline citations. Anthropic returns none, and OpenAI sometimes doesn't. In that case sources lists the pages the search returned (Anthropic) or consulted (OpenAI), with an empty snippet. That is every result, not only the ones the answer used.
sources_field is not part of the cache key, so changing it does not invalidate cached results.
Search then analyse: web_search()¶
web_search() returns an async function for a FunctionStep. It runs the search on its own and returns the findings as text, which the next LLMStep reads.
from accrue import FunctionStep, LLMStep, Pipeline, web_search
pipeline = Pipeline([
FunctionStep("research",
fn=web_search("Research {company}: market position, recent news",
model="claude-haiku-5-5"),
fields=["__web_context", "sources"],
),
LLMStep("analyze",
fields={"summary": "Summarize the findings"},
model="claude-sonnet-5-5",
depends_on=["research"],
batch=True,
),
])
Use it when:
- The analysis step should run at batch prices. A grounded step can't use
batch=True; an analysis step that only reads the research can, at 50% off. - The research should be reused. The search step is cached on its own, so changing the analysis prompt or model doesn't search again.
- The query should be explicit. The query is a template you can read and audit, rather than whatever the model decides to search for.
__web_context (double underscore) reaches the next step through prior_results and is left out of the final output. sources (a list of URLs) appears in the output. As with grounding, when the answer has no inline citations, sources lists every page the search returned or consulted.
Cost example¶
A rounded estimate for 1,000 companies, assuming one search and about 10,000 tokens of search results per row:
| Pipeline | Search | Analysis | Total |
|---|---|---|---|
One step: claude-sonnet-5-5, grounding=True |
$10 searches + ~$20 result tokens | ~$3 | ~$33 |
Two steps: web_search() on claude-haiku-5-5, analysis on claude-sonnet-5-5 with batch=True |
$10 searches + ~$1 result tokens | ~$4.50 | ~$15.50 |
In the two-step version, changing the analysis prompt reruns only the analysis step (about $4.50). These are rounded estimates: for exact prices see the model and price table and the providers' pricing pages.
Choosing the provider¶
The model name picks the provider, the same way LLMStep does:
| Model | Provider | Default model | Key |
|---|---|---|---|
claude-* |
Anthropic web search (accrue[anthropic]) |
claude-haiku-5-5 |
ANTHROPIC_API_KEY |
gemini-* |
Google Search (accrue[google]) |
gemini-3.1-flash-lite |
GOOGLE_API_KEY |
| anything else | OpenAI Responses web_search |
gpt-6-luna |
OPENAI_API_KEY |
With no model, the search runs on OpenAI with gpt-6-luna. Pass client= to use a configured adapter instead (for example AnthropicClient(timeout=...)); with a built-in adapter and no model, its provider's default model is used.
Parameters¶
web_search(
query="Research {company}: {topic}", # {field} placeholders from the row and prior results
model=None, # picks the provider; see the table above
client=None, # an adapter to use instead of building one
api_key=None, # else the provider's environment variable
include_sources=True, # return the cited URLs in "sources"
allowed_domains=["sec.gov"], # OpenAI and Anthropic
blocked_domains=None, # all three (Anthropic: one list or the other)
user_location={"country": "US"}, # OpenAI and Anthropic
max_searches=2, # Anthropic only
search_context_size="medium", # OpenAI only: "low", "medium", "high"
tool_type="web_search", # OpenAI only: or "web_search_preview" (legacy)
max_tokens=8192, # thinking and reasoning tokens count towards it
provider_kwargs=None, # merged into the provider call, as on LLMStep
)
Options a provider doesn't support are dropped with one warning when the step is built. A missing placeholder in query raises ValueError.
Return value¶
__web_context is wrapped in a random boundary tag (<untrusted-…>…</untrusted-…>), so the next step treats page content as data rather than instructions.
Rate limits, timeouts and 5xx errors are retried twice. If the search still fails, the step logs a warning and returns {"__web_context": "", "sources": []}, and the next step runs on the row data alone.
Gotchas¶
groundingandbatch=Truecan't be combined on one step; accrue raises an error when the step is built. Put the search in aweb_search()step and batch the analysis.- On Google, grounding falls back to JSON mode, and many rows have no
sources. For a strict schema with Gemini, search in aweb_search()step and analyse without grounding. - The
sources_fieldname must not match a declared field. Rename the field, or setsources_fieldto another name orNone. - Grounding settings are part of the cache key, so changing them reruns the step.
sources_fieldis not. - Google Search grounding isn't available on the Gemini API free tier for Gemini 3 and later.