Why AI Coding Tools Slow Down on Large-Scale Web Search, and How to Optimize It
This article explains why AI coding tools with web search slow down when queries span multiple sites and topics, and how to fix it. It breaks down the default legacy retrieval channel, which generates a full answer before fetching documents in a serial, blocking flow that inflates token usage and tail latency. The article then presents optimization strategies: parallelizing retrieval and generation, layered retrieval with recall convergence, caching and result reuse, streaming progress feedback, and usage budget guardrails. It also covers observability metrics and general deployment advice for cloud environments such as Baidu AI Cloud.
1. The Problem: Why Broad Searches Feel Like Flipping Through Pages One by One
When using AI coding tools with web retrieval, many developers notice the same pattern: narrow, well-scoped queries return almost instantly, but once the scope expands across sites and topics, responses slow down and results stop arriving as a single conclusion and start trickling out one item at a time. This difference is not caused by network bandwidth, nor by a sudden drop in model capability. It is determined by the execution order of the retrieval pipeline and its resource consumption model.
The core issue is that some tools enable a legacy retrieval channel by default, which uses a serial “generate everything first, then fetch sources” mode. The model writes a complete, coherent block of text around the question, and only after that text is finished does the system actually start fetching documents. Under this design, retrieval parallelism is forced to zero, and the “page-by-page flipping” users perceive is the external manifestation of this serial fetching.
2. Mechanism Breakdown: Where the Legacy Channel Loses Time
2.1 Serial Blocking: Generation and Fetching Wait on Each Other
The legacy channel binds “language generation” and “information retrieval” into two consecutive steps. The generation stage does not depend on external sources but monopolizes the time window; the fetching stage could run in parallel with generation but is scheduled afterward. The larger the retrieval scope, the more pages must be fetched, and the more noticeable the accumulated serial waiting becomes.
The execution order can be abstracted as follows:
Legacy channel (serial):generate_full_answer() -> fetch_documents() -> merge()wait time ≈ generation time + Σ(per-page fetch time)Ideal channel (parallel):fetch_documents() ┐generate_outline() ┘ -> merge_and_refine()wait time ≈ max(fetch time, generation time) + merge time
2.2 Usage Amplification: Invalid Prefixes Add Extra Cost
Because a complete paragraph must be produced first, each retrieval comes with a fairly long block of generated content. Feedback from plugin authors in the industry indicates that a single retrieval can consume on the order of thousands of tokens (about 9.6K tokens), and during peak hours it may be billed separately by token count. This means that even if only a handful of sources are ultimately needed, the user has already paid for the “leading long text.” The official documentation also clearly notes that when the wait time and usage of a single retrieval become the main cost, this channel should be avoided.
2.3 No Reuse: Repeated Questions Are Billed Repeatedly
The legacy channel typically does not retain intermediate retrieval results. The same sub-question is executed repeatedly across consecutive follow-ups, which both lengthens tail latency and amplifies token consumption.
3. Optimization Approach: Decoupling Retrieval from Generation
3.1 Parallelize Retrieval and Generation
Move document fetching earlier so it runs at the same time as outline generation, then perform a single fusion and refinement pass at the end. In engineering terms, this can be implemented with asynchronous task orchestration:
import asyncioasync def fast_search(query, fetcher, planner):# Fetch and outline run in parallel without blocking each otherdocs_task = asyncio.create_task(fetcher.fetch(query, top_k=8))plan_task = asyncio.create_task(planner.outline(query))docs, plan = await asyncio.gather(docs_task, plan_task)return merge_and_refine(plan, docs)
After parallelization, total wait time changes from “adding two segments together” to “taking the maximum of the two segments,” and the benefit is especially significant for broad searches.
3.2 Layered Retrieval and Recall Convergence
First use lightweight recall (keyword/vector coarse ranking) to narrow the candidate set, then use fine ranking to select highly relevant fragments, avoiding linear page-by-page fetching. The recall scale should adapt to the query scope rather than always fetching the full set.
3.3 Caching and Result Reuse
Build a cache key from “query fingerprint + time window” and return immediately on a hit; pool results for high-frequency sub-questions. Example:
cache_key = hash(normalize(query) + window)if cache_key in cache and not expired:return cache[cache_key]
Caching reduces both tail latency and token usage at the same time, making it one of the most cost-effective optimizations.
3.4 Streaming Output and Visible Progress
Even if the backend still needs time, progress such as “retrieving item N of M” should be fed back in a streaming manner to avoid leaving users facing a long blank. Streaming output is a common industry interaction practice and can significantly improve the subjective sense of waiting.
3.5 Usage and Budget Guardrails
Set token and fetch-count limits per session, and automatically degrade to “summary + key citations” when exceeded, preventing costs from spiraling during peak hours.
4. Observability and the Tuning Loop
Optimization cannot rely on guesswork; it needs to be metrics-driven. At minimum, collect: time to first byte, retrieval completion time, number of fetched items, cache hit rate, token consumption per request, and retry failure rate. General-purpose logging services and monitoring/alerting capabilities can be used to build dashboards, analyze P95 tail latency and usage trends, and form a “measure - locate - optimize - regress” loop.
5. Deployment Recommendations on Baidu AI Cloud
If running in a cloud environment such as Baidu AI Cloud, the retrieval service and generation service can be split into independently deployed units, with the retrieval side scaled horizontally to absorb the concurrency pressure of broad queries; intermediate results can be persisted and reused via general capabilities such as object storage or caching; and full-chain instrumentation can be connected to the logging and monitoring system. These are all general cloud product categories that do not involve any proprietary interfaces, making them easy to port and reuse across different environments.
6. Conclusion
The root cause of slow broad searches is the combination of the default legacy retrieval channel’s serial “generate first, fetch later” design and token-based billing. By decoupling retrieval from generation, introducing parallel orchestration, layered recall, cache reuse, and streaming feedback, and pairing them with usage guardrails and observability, you can reduce wait time and cost to an acceptable range without sacrificing result quality, approaching the ideal instant retrieval experience.
