Revamping Query Understanding with LLMs in E-commerce Search

instacart · E-commerce · 2025 · source Agentic: no · promotional bias: low · source-backed · extracted with prompt 0.5.6 (schema v2)

Generated from extraction.json — do not edit by hand. Every claim cites verbatim source evidence; stated means the quote directly asserts the claim, implied means it supports it indirectly.

Problem

Domain: E-commerce search query understanding

Instacart needed to improve query understanding for broad, tail, and creatively phrased grocery searches because traditional task-specific ML systems struggled with noisy labels, sparse engagement data, and operational complexity.

Constraints:

Architecture

Instacart built a layered LLM query-understanding system. It uses context-engineered RAG to inject conversion history and catalog context, guardrails to filter hallucinated or taxonomy-incompatible outputs, specialized rewrite prompts, and an offline teacher pipeline that populates a head-query cache while generating training data for a fine-tuned Llama-3-8B real-time student model for long-tail cache misses.

Diagram confidence: explicit (topology described by the source).

flowchart LR
  A["Customer search queries"] --> B["Category classification pipeline"]
  A --> F["Query rewrite pipeline"]
  A --> G["SRL cache router"]
  B --> C["Converted category retriever"]
  B --> D["LLM reranker"]
  B --> E["Semantic similarity guardrail"]
  F --> E
  G --> I["Head-query tag cache"]
  G --> K["Fine-tuned real-time SRL model"]
  H["Offline RAG teacher"] --> D
  H --> I
  H --> J["Training dataset"]
  J --> K
  K --> L["GPU serving infrastructure"]
  K --> M["Search ranking and retrieval"]
Drag to pan · scroll or use +/− to zoom · reconstructed from the source's described component topology

Components

Component Role Interacts with
Customer search queries Live grocery search inputs that may be broad, rare, misspelled, or creatively phrased. Category classification pipeline, Query rewrite pipeline, SRL cache router
Category classification pipeline Retrieves candidate product categories and reranks them using an LLM with Instacart context. Converted category retriever, LLM reranker, Semantic similarity guardrail
Converted category retriever Provides top-K converted category candidates for a query. Category classification pipeline
LLM reranker Uses injected business context to rank categories or infer semantic role labels. Category classification pipeline, Offline RAG teacher
Semantic similarity guardrail Filters category predictions and rewrites that fall below relevance thresholds. Category classification pipeline, Query rewrite pipeline
Query rewrite pipeline Runs separate prompts for substitutes, broader queries, and synonyms. Customer search queries, Semantic similarity guardrail
SRL cache router Routes head queries to cached labels and cache misses to the fine-tuned real-time model. Head-query tag cache, Fine-tuned real-time SRL model
Offline RAG teacher Generates high-quality tags with catalog, conversion, product, brand, and query-performance context. LLM reranker, Head-query tag cache, Training dataset
Head-query tag cache Serves validated tags for common queries at low latency. SRL cache router
Training dataset Teacher-generated examples used to fine-tune the real-time student model. Fine-tuned real-time SRL model
Fine-tuned real-time SRL model Llama-3-8B student model serving long-tail semantic role labels for cache misses. GPU serving infrastructure, Search ranking and retrieval
GPU serving infrastructure Adapter-merged H100/autoscaled serving layer for low-latency inference. Fine-tuned real-time SRL model
Search ranking and retrieval Downstream search systems that use QU outputs to improve recall, ranking, ads, and filters. —

Data flow

Search queries enter QU tasks. Category classification retrieves top converted categories, reranks them with an LLM and injected Instacart context, then filters predictions through semantic-similarity guardrails. Query rewrite uses dedicated prompts for substitutes, broader queries, and synonyms. Semantic role labeling uses an offline RAG teacher for high-quality head-query tags, writes those tags into a cache, and trains a fine-tuned real-time model that handles cache misses.

3 supporting quotes
“first, we retrieve the top-K converted categories for each query as initial candidates; second, we use an LLM to re-rank them with injected Instacart context; and finally, we apply a post-processing guardrail.”article, stated
“The system’s flow, shown in the diagram below, is determined simply by a cache-hit.”article, stated
“A powerful offline process generates high-quality data that serves two purposes: populating a cache for our most common “head” queries and creating the training data for a fast, real-time model”article, stated

Why this architecture

Instacart used a layered architecture because each part of QU had different economics. RAG and context engineering could inject domain knowledge for offline or high-value tasks, guardrails protected taxonomy correctness, caching made common queries cheap, and a fine-tuned smaller model preserved enough teacher quality for real-time long-tail serving.

3 supporting quotes
“Our strategy was layered, moving from context-engineering with guardrails to our ultimate goal: fine-tuning”article, stated
“Because latency is not a concern here, we can use complex techniques to ensure the highest possible quality.”article, stated
“This hybrid approach gives us the best of both worlds: the raw power of massive LLMs, and the speed and efficiency of a lightweight, learnable model.”article, stated

Models used: Large Language Models · Llama-3-8B · FastText · LoRA fine-tuned model

Scale

Alternatives considered (roads not taken)

Continue building bespoke traditional QU models

Rejected because: The prior approach created separate pipelines and serving stacks for each task and struggled with long-tail language.

“Each bespoke solution demanded its own data pipeline, training architecture, and serving infrastructure. This heterogeneity introduced inconsistencies, slowed development cycles”summary, stated

Run an expensive large model online for every semantic role label query

Rejected because: The long tail is effectively infinite and online LLM processing is too expensive for every query.

“we can’t pre-compute results for every possible query because the “long-tail” of new and unique searches is effectively infinite, and offline LLM processing is expensive.”article, stated

Use one generic rewrite prompt

Rejected because: A broad recall-enhancement prompt was too ambiguous, so Instacart split the job into substitute, broader-query, and synonym prompts.

“This proved too ambiguous. For example, for “1% milk”, the model might return “one percent milk” — a valid synonym but not a useful rewrite for discovering alternative products.”article, stated

Deploy FP8 quantization for extra latency reduction

Rejected because: The extra 10% latency improvement caused a slight recall drop, so production used the unquantized model.

“We explored quantization (FP8), which cut latency by another 10% but with a slight drop in recall. We deployed the unquantized model to prioritize quality.”article, stated

Patterns

context-engineering (retrieval)

Instacart injected catalog, conversion, brand, and query-performance context into prompts so LLMs could act as grocery-domain specialists.

“We build data pipelines that retrieve and inject Instacart-specific context, such as conversion history and catalog data, directly into the prompt.”article, stated
“Context-engineering injects Instacart domain knowledge to ground the LLM’s inference and generate far more accurate intent signals.”article, stated

guardrails-io-filtering (safety-quality)

Post-processing guardrails filter hallucinations and enforce taxonomy, semantic relevance, and catalog validity before QU outputs reach search.

“2. Post-Processing Guardrails: We refine LLM outputs through validation layers. These guardrails filter out hallucinations and enforce alignment with Instacart’s product taxonomy.”article, stated
“Armed with this context, the model correctly infers the users’ intent: they are looking for a smoothie brand. After generation, a post-processing guardrail validates the tags against our catalog. This rigorous process has two critical outputs:”article, stated

fine-tuning-adaptation (model-adaptation)

A Llama-3-8B student model was fine-tuned with LoRA on offline teacher data to serve long-tail SRL in real time.

“For the real-time SRL model, we fine-tuned an open-source Llama-3–8B model using LoRA (Low-Rank Adaptation). The model was trained on the dataset from the offline “teacher” pipeline.”article, stated
“The model was trained on the dataset from the offline “teacher” pipeline. This process effectively distilled the knowledge and nuanced context from the larger model into the smaller, more efficient one.”article, stated

semantic-caching (serving)

Validated tags for common head queries are served from a cache, while cache misses route to the real-time model.

“1. A low-latency cache containing the validated, high quality tags for our most common queries.”article, stated
“High-frequency “head” queries are served instantly with cache, while “tail” queries are handled by a real-time, fine-tuned model.”article, stated

model-distillation (model-adaptation)

The offline teacher's larger-model behavior and domain context were distilled into the smaller real-time student.

“This process effectively distilled the knowledge and nuanced context from the larger model into the smaller, more efficient one.”article, stated

latency-optimization (serving)

Adapter merging, H100 GPUs, autoscaling, and avoiding recall-damaging quantization brought live inference to the latency target.

“Merging the LoRA adapter weights directly into the base model and upgrading to H100 GPUs got us to our 300ms target.”article, stated

Tradeoffs

Domain adaptation ladder

Chose fine-tuning plus context engineering and guardrails over prompting alone.

Rationale: Instacart found fine-tuning most effective, context engineering useful, and prompting the lightest layer, with each carrying different engineering costs.

“Through our work, we found a clear hierarchy of effectiveness, each with its own engineering trade-offs: Fine-tuning > Context-Engineering (RAG) > Prompting.”article, stated

Head and tail serving split

Chose offline teacher/cache for head queries and real-time student for tail queries over one live large-model path for every query.

Rationale: The split spends compute where latency does not matter and uses the smaller model for real-time cache misses.

“Because latency is not a concern here, we can use complex techniques to ensure the highest possible quality. The core of this is context-engineering: enriching the prompt with deep Instacart-specific knowledge.”article, stated
“This hybrid approach gives us the best of both worlds: the raw power of massive LLMs, and the speed and efficiency of a lightweight, learnable model.”article, stated

Latency versus recall

Chose unquantized model over FP8 quantized model.

Rationale: The additional latency reduction was not worth the recall loss for production search.

“We explored quantization (FP8), which cut latency by another 10% but with a slight drop in recall. We deployed the unquantized model to prioritize quality.”article, stated

Gotchas (what bit them)

Traditional QU labels were noisy because search intent is upstream of purchase behavior.

Root cause: Pseudo-labels from behavior can be misleading when a user searches for one thing but buys another.

Resolution: Instacart used domain-context LLM inference and guardrails instead of relying only on engagement-trained labels.

“* Lack of Labeled Data: QU operates upstream and doesn’t benefit from direct feedback like clicks or conversions. The pseudo-labels we derive from user behaviors are inherently noisy — a user might search for “bread” but ultimately purchase bananas.”article, stated
“The pseudo-labels we derive from user behaviors are inherently noisy — a user might search for “bread” but ultimately purchase bananas.”article, stated

A single broad rewrite prompt was under-specified.

Root cause: Synonyms, substitutes, and broader queries serve different user needs even though all improve recall.

Resolution: Instacart built three specialized prompts with chain-of-thought, few-shot examples, and relevance filters.

“This led us to design specialized prompts for three distinct rewrite types: Substitutes, Broader queries, and Synonyms.”article, stated
“To ensure the results are logical and useful, we apply post-processing guardrails, including filters for semantic relevance.”article, stated

The real-time student model was too slow out of the box.

Root cause: Initial serving latency was nearly 700ms on A100 GPUs.

Resolution: The team merged LoRA adapters, upgraded to H100 GPUs, evaluated FP8, and autoscaled GPU capacity.

“Having a great model is only half the battle; serving it in production with a latency target in the low hundreds of milliseconds was a significant engineering challenge. The out-of-the-box latency was nearly 700ms with A100 GPU. We reduced latency through a series of crucial optimizations:”article, stated
“Merging the LoRA adapter weights directly into the base model and upgrading to H100 GPUs got us to our 300ms target.”article, stated

Quantization improved speed but reduced recall.

Root cause: FP8 saved latency at the cost of search-quality signal.

Resolution: Instacart deployed the unquantized model to prioritize quality.

“* Quantization Trade-Offs: We explored quantization (FP8), which cut latency by another 10% but with a slight drop in recall. We deployed the unquantized model to prioritize quality.”article, stated
“* Quantization Trade-Offs: We explored quantization (FP8), which cut latency by another 10% but with a slight drop in recall. We deployed the unquantized model to prioritize quality.”article, stated

Evaluation

Instacart evaluated the QU rebuild through rewrite coverage and precision, model precision/recall/F1, latency, A/B search-quality measurements, scroll depth, cold-start query volume, and complaint reduction.

Metrics: Query rewrite coverage over 95% with 90%+ precision · Fine-tuned 8B precision 96.4% versus 95.4% baseline · Fine-tuned 8B recall 95% versus 96.2% baseline · Fine-tuned 8B F1 95.7% versus 95.8% baseline · Latency reduced from nearly 700ms to a 300ms target · Average scroll depth reduced by 6% · Poor tail-query complaint volume reduced by 50%

3 supporting quotes
“This structured approach increased our query rewrite coverage to over 95% with 90%+ precision across all three types.”article, stated
“Our production model (orange) has higher precision 96.4% vs 95.4%, lower recall 95% vs 96.2%, and on-par F1 score 95.7% vs 95.8%.”article, stated
“With the new SRL tagging for the tail queries, we reduce “average scroll depth” by 6% (users find items faster), with only a marginal latency increase.”article, stated

Outcomes

The system operates over millions of customers, billions of catalog items, live low-latency search traffic, and millions of cold-start queries per week.

“The system is now live, serving millions of cold-start queries weekly and reducing user complaints related to poor search results for tail queries by 50%.”article, stated
“The success of the real-time “student” model in our SRL system was more than just a win for one project; it proved the viability of a new foundational capability for Instacart”article, stated