#12846·onyx

Query expansion (_expand_queries_and_decide_scope) has no config flag to disable, dominates search latency

Author: cyrilS-devCreated Jul 10, 2026Updated Sep 28, 2026
LabelsenhancementStale

Is your feature request related to a problem?

The pre-retrieval phase inside SearchTool.run() issues multiple sequential/parallel LLM calls before any retrieval happens, and there is no config flag to reduce or disable them. On any setup where a single LLM call is not near-instant, these stacked calls dominate end-to-end chat latency.

Using timing logs, I traced a single chat query (~25s total) and found the largest cost is _expand_queries_and_decide_scope() in backend/onyx/tools/tool_implementations/search/search_tool.py, which fires 3 parallel LLM calls (semantic_query_rephrase, keyword_query_expansion, decide_search_scope) on self.llm. Wall time is dictated by the slowest of the three.

Environment

  • Onyx CE, version v4.3.2 (image onyxdotapp/onyx-backend:latest)
  • Helm chart 0.6.9
  • LLM: Qwen3 27B via an OpenAI-compatible endpoint (single completion call takes several seconds)
  • Single-connector Persona (ingestion_api), so decide_search_scope already short-circuits (< 2 sources)

Timing breakdown (from timing.py logs, single query)

Phase | Time -- | -- run() start → Internal search - source scope log (i.e. _expand_queries_and_decide_scope) | ~14s select_sections_for_expansion | ~2.3s classify_section_relevance | ~5–6s run() total | ~22s

What I tried (no-code), and results

  • MAX_LLM_CYCLES=2 → EmptyLLMResponseError, breaks the flow. Reverted to default (6).
  • Smaller model on the Persona (Mistral 7B, then Mistral Small 24B, both non-reasoning) → the pre-retrieval phase stayed ~12–14s. So this is not Qwen3 thinking overhead; it's per-call latency × number of calls.
  • chat_template_kwargs: {enable_thinking: false} via LITELLM_EXTRA_BODY → rejected by the endpoint ("feature not currently supported"; the endpoint enforces strict OpenAI spec).
  • MAX_CHUNKS_FED_TO_CHAT=25 → 10 → helped proportionally on the classify step (12.6s → 5.2s), but does not touch the ~14s expansion phase.
  • DISABLE_LLM_CHOOSE_SEARCH → no longer exists in the codebase (verified). Routing is now native tool_choice=auto, so once the tool call fires, _expand_queries_and_decide_scope always runs with no way to skip it.

Proposed feature

An env var (or Persona/search setting) to disable query expansion for the first SearchTool call, e.g. DISABLE_QUERY_EXPANSION.

Today skip_query_expansion exists in SearchToolOverrideKwargs, but it is only set to True from the 2nd SearchTool call onward (llm_loop.py, skip_search_query_expansion=has_called_search_tool), so the first (and most expensive) call always pays the full expansion cost. Exposing a config to skip expansion on the first call would let deployments trade a small recall hit for a large latency win, the raw LLM-authored queries from the tool call would be used directly.

Happy to open a PR if the maintainers agree on the flag name and placement.