Code mode for agentic retrieval
Agents can write retrieval code. The hard part is exposing the primitives that decide what comes back, how it is scored, and what survives into context.
Search interfaces for humans hid retrieval behind one input box: type a few words, let the backend run a bespoke retrieval and ranking pipeline, and get back a ranked list of links. That contract made sense for human users. It makes less sense for agents.
Advanced search exposed more controls: Boolean operators, field filters, date constraints, exact phrases, source restrictions, ranking controls. Those controls mattered to power users, but almost nobody wanted to operate the advanced form directly.
Agents change that constraint. Frontier models are good at writing code: they can plan across many retrieval calls, keep intermediate state outside their context window, and apply logic before deciding what evidence to keep for the next turn. For agents, advanced mode is code, not an advanced search form.
Code also compresses context: a program can fan out queries, filter results, and return compact evidence instead of dragging every intermediate tool call result back into the model context.
That shifts the abstraction from search(query: str) to search(code: str): code uses a harness-specific SDK to assemble the retrieval loop.
We made this feedback-loop argument in How we build a retrieval engine for agents: agents can configure, test, correct, and optimize retrieval when it looks like code. Anthropic showed the context-management side one layer up in Code execution with MCP , reporting a workflow that fell from 150,000 to 2,000 tokens once tool calls became code in a sandbox. Cloudflare shipped the same pattern as "Code Mode" , and Perplexity's Search as Code applies it to web search.
Once search(code) is the interface, the obvious work is the harness: the sandbox, the SDK, and the instructions that teach a model to use them. That layer gives the agent a retrieval language and a way to manage context without dragging every tool call result into the model context, where it compounds with each agentic turn. That only works if the retrieval primitives work well.
An agent can fan out twenty queries, dedupe, filter by date, and keep the survivors. If retrieval recall is low, no harness engineering recovers the documents the primitives never surfaced. The retrieval primitives decide what the code loop can inspect. BrowseComp-Plus gives a clean example: GPT-4.1 scored 93.49% when prompted with all labeled positive documents and 14.58% when the same model had to use BM25 retrieval, a 78.91-point gap with the model and corpus held fixed.
Source: BrowseComp-Plus , Table 1 (gpt-4.1 + BM25) and Section 4.8.1 (oracle retrieval).
That is why code mode for retrieval cannot stop at the query loop. Agents should be able to program retrieval behavior itself: impact weights, freshness functions, scoring functions, and other controls that determine what gets returned.
Code execution lets the agent inspect intermediate results before spending context tokens on them. Anyone who has watched a coding agent preserve context with shell tools has seen the pattern: keep bulky intermediate state outside the model context, operate on it with code, and bring back only what matters.
Result processing needs that shape: inspect fields, page through candidates, join lists, apply predicates, and write only the surviving evidence into context.
The useful primitive is a structured result set the agent can test, filter, and use to decide what to try next, rather than a top-k list of pre-split chunks.
Code mode for retrieval has to run close to the corpus: the public web, enterprise PDFs, source code, internal knowledge bases, or documents of any kind. Web-scale search and private retrieval both need relevance primitives an agent can inspect and control: freshness, scoring, weighting, filters, and structured results.
Relevance is still the horizontal challenge, but the new user changes the target: longer queries, structured retrieval calls, and many more operations inside one task. Plausible distractors can become premises, pollute later turns, and make the loop confidently wrong. Retrieval has to become something agents can configure, test, and correct.
The agent can write the retrieval code. Hornet makes the code useful by exposing the primitives that decide what comes back, how it is scored, and what survives into context.
We're building Hornet for teams working on agentic retrieval. For new posts, benchmarks, and early product notes, .