Rebuilding Query Understanding for E-Commerce Search with LLMs

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 rebuild query understanding for millions of imperfect grocery searches because traditional QU models struggled with broad and long-tail queries, noisy labels, and a collection of task-specific systems that were difficult to scale and evolve.

Constraints:

Architecture

Instacart moved QU to a layered LLM strategy. It uses context-engineered RAG and guardrails for category classification and head-query semantic role labeling, specialized rewrite prompts for substitutes/broader queries/synonyms, and a teacher/student SRL system where an offline RAG pipeline creates validated tags, a cache, and training data for a fine-tuned Llama-3-8B real-time model serving tail queries.

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

flowchart LR
  A["Search query stream"] --> B["Category classification pipeline"]
  A --> C["Query rewrite pipeline"]
  A --> D["SRL cache router"]
  B --> E["Converted category retriever"]
  B --> F["LLM re-ranker"]
  B --> G["Semantic similarity guardrail"]
  C --> G
  C --> H["Session engagement signals"]
  D --> I["Head-query tag cache"]
  D --> J["Fine-tuned real-time SRL model"]
  K["Offline RAG teacher"] --> L["Historical conversion data"]
  K --> M["Product catalog embeddings"]
  K --> N["Post-processing catalog guardrail"]
  K --> I
  K --> O["Training dataset"]
  O --> J
  J --> P["GPU serving infrastructure"]
  B --> Q["Search ranking and retrieval"]
  C --> Q
  D --> Q
Drag to pan · scroll or use +/− to zoom · reconstructed from the source's described component topology

Components

Component Role Interacts with
Search query stream Live customer search queries requiring intent interpretation. Category classification pipeline, Query rewrite pipeline, SRL cache router
Category classification pipeline Retrieves candidate categories, invokes an LLM re-ranker, and applies semantic guardrails. Search query stream, Converted category retriever, LLM re-ranker, Semantic similarity guardrail
Converted category retriever Provides top converted categories as candidate taxonomy paths. Category classification pipeline
LLM re-ranker Uses injected Instacart context to rank candidate categories or infer query tags. Category classification pipeline, Offline RAG teacher
Semantic similarity guardrail Filters category predictions or generated rewrites that fall below relevance thresholds. Category classification pipeline, Query rewrite pipeline
Query rewrite pipeline Runs specialized prompts for substitutes, broader queries, and synonyms. Search query stream, Semantic similarity guardrail, Session engagement signals
SRL cache router Routes high-frequency head queries to cached tags and cache misses to the real-time model. Search query stream, Head-query tag cache, Fine-tuned real-time SRL model
Offline RAG teacher Generates high-quality SRL tags with conversion history and catalog context. Historical conversion data, Product catalog embeddings, LLM re-ranker, Post-processing catalog guardrail, Head-query tag cache, Training dataset
Historical conversion data Provides converted brands/categories and engagement signals for context engineering. Offline RAG teacher, Query rewrite pipeline
Product catalog embeddings Provides product and brand similarity context for grounding query interpretation. Offline RAG teacher
Post-processing catalog guardrail Validates generated tags against the catalog and taxonomy. Offline RAG teacher
Head-query tag cache Serves validated tags for common head queries with low latency. SRL cache router, Offline RAG teacher
Training dataset Curriculum data generated by the offline teacher for fine-tuning. Offline RAG teacher, Fine-tuned real-time SRL model
Fine-tuned real-time SRL model Fast Llama-3-8B student model serving long-tail cache-miss queries. SRL cache router, Training dataset, GPU serving infrastructure
GPU serving infrastructure H100/adapter-merged/autoscaled serving layer for low-latency real-time inference. Fine-tuned real-time SRL model
Search ranking and retrieval Downstream search experience using QU outputs for recall, ranking, ads, and filters. Category classification pipeline, Query rewrite pipeline, SRL cache router

Data flow

Search queries first flow through LLM-powered QU tasks. For category classification, candidate categories are retrieved, an LLM re-ranks them with Instacart context, and semantic-similarity guardrails filter irrelevant outputs. For rewrites, dedicated prompts generate substitute, broader-query, and synonym rewrites, then guardrails validate relevance. For SRL, an offline RAG teacher enriches prompts with conversion and catalog context, validates tags, writes a low-latency cache for head queries, and produces training data for a fine-tuned real-time student model that handles cache misses and long-tail traffic.

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 chose a layered architecture because different QU tasks had different latency and knowledge requirements. RAG/context engineering gives the model business context without retraining, guardrails protect taxonomy relevance, offline teacher processing can spend more compute on head-query quality, and fine-tuning distills that quality into a fast real-time model for the long tail.

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)

Traditional bespoke QU models for each task

Rejected because: They struggled with long-tail generalization, required noisy labels, and created multiple systems with separate pipelines and serving architecture.

“we historically trained and maintained multiple independent models for individual QU tasks.”article, stated
“Each of these bespoke solutions demanded its own data pipeline, training and serving architecture.”article, stated

A simple single-model rewrite prompt

Rejected because: It was too ambiguous and generated valid synonyms that were not useful product-discovery rewrites.

“Our initial attempt involved a simple prompt asking a single model to generate rewrites for recall enhancement. This proved too ambiguous.”article, stated
“for “1% milk”, the model might return “one percent milk” — a valid synonym but not a useful rewrite for discovering alternative products.”article, stated

Running the large offline LLM for every query at request time

Rejected because: Live long-tail queries needed fast and cost-effective inference, so Instacart cached head-query tags and trained a smaller real-time student model for cache misses.

“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
“When a user’s query results in a cache miss (indicating a long-tail query), it is routed to our real-time model.”article, stated

Deploying the FP8 quantized model

Rejected because: Although it reduced latency, it slightly reduced recall, so Instacart deployed the unquantized model to prioritize quality.

“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 conversion history, catalog data, product similarity, and engagement signals into prompts so LLMs could behave like 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

rag-retrieval (retrieval)

The offline teacher uses retrieval-augmented generation to enrich prompts for high-frequency queries before generating tags and training data.

“For our high-frequency “head” queries, we run an offline Retrieval-Augmented Generation (RAG) and caching pipeline.”article, stated
“Consider the query “verdant machine”. Without context, an LLM might assume it’s for machinery. Our offline pipeline, however, automatically enriches the prompt with crucial context from our internal data systems, including:”article, stated

guardrails-io-filtering (safety-quality)

Post-processing guardrails validate model outputs against semantic relevance, product taxonomy, and catalog constraints before they affect search.

“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)

Instacart fine-tuned a smaller Llama-3-8B model with LoRA on offline teacher-generated data so it could serve long-tail SRL queries 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

model-distillation (model-adaptation)

The offline teacher pipeline produced a curriculum that distilled larger-model and domain-context behavior into the faster student model.

“This process effectively distilled the knowledge and nuanced context from the larger model into the smaller, more efficient one.”article, stated
“the smaller model learns to replicate accuracy of its much larger counterpart, along with the domain context we injected.”article, stated

semantic-caching (serving)

Head queries are served from a validated low-latency cache, while cache misses route to the fine-tuned real-time model.

“1. A low-latency cache containing the validated, high quality tags for our most common queries.”article, stated
“Fig 3. Architecture of the hybrid SRL system. _Live traffic is routed based on a cache-hit. High-frequency “head” queries are served instantly with cache, while “tail” queries are handled by a real-time, fine-tuned model.”article, stated

latency-optimization (serving)

The serving path reduced model latency with adapter merging, H100 GPUs, and selective avoidance of quality-hurting quantization.

“Merging the LoRA adapter weights directly into the base model and upgrading to H100 GPUs got us to our 300ms target.”article, stated
“The Model is Only Half the Battle: A great model is useless if it can’t serve traffic at scale.”article, stated

Tradeoffs

Domain adaptation strategy

Chose fine-tuning plus context engineering and guardrails over prompting alone or another bespoke traditional model.

Rationale: The team found a hierarchy where fine-tuning most deeply embeds domain expertise, with RAG and prompting as progressively lighter adaptation layers.

“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-query and tail-query serving

Chose offline teacher plus cache for head queries and fine-tuned student for tail queries over one expensive live LLM path for all queries.

Rationale: This split gave high-quality processing where latency did not matter and fast inference where live search demanded it.

“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

Rewrite generation design

Chose three specialized rewrite prompts with guardrails over one broad recall-enhancement prompt.

Rationale: The broad prompt was ambiguous; specialized prompts made rewrite intent explicit and improved coverage/precision.

“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
“This structured approach increased our query rewrite coverage to over 95% with 90%+ precision across all three types.”article, stated

Quantization deployment

Chose unquantized model over FP8 quantized model.

Rationale: The latency win was not worth the recall loss for production search 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

Gotchas (what bit them)

Traditional labels were noisy because QU sits upstream of conversion.

Root cause: User behavior can imply misleading pseudo-labels, such as a user searching for one item and buying another.

Resolution: Instacart used LLMs with domain context and guardrails rather than relying only on engagement-trained traditional models.

“* 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

Long-tail queries broke models trained mostly on engagement data.

Root cause: Rare, specific queries have limited historical clicks or conversions, so conventional models generalize poorly.

Resolution: The hybrid SRL system uses an offline teacher to create data and a fine-tuned real-time student for tail cache misses.

“Highly specific or rare searches like “red hot chili pepper spice” or “2% reduced-fat ultra-pasteurized chocolate milk” suffer from data sparsity.”article, stated
“When a user’s query results in a cache miss (indicating a long-tail query), it is routed to our real-time model.”article, stated

A simple rewrite prompt produced technically valid but commercially unhelpful rewrites.

Root cause: Recall-enhancement was under-specified, so the model returned synonyms instead of useful discovery alternatives.

Resolution: Instacart split rewrites into substitute, broader-query, and synonym prompts with COT, 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

Real-time LLM latency was initially too high for production search.

Root cause: The fine-tuned model still had nearly 700ms latency on A100 GPUs out of the box.

Resolution: Instacart merged adapters into the base model, upgraded to H100 GPUs, avoided recall-damaging quantization, 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 hurt quality.

Root cause: FP8 quantization cut latency at the cost of a slight recall drop.

Resolution: The production deployment kept the unquantized model to prioritize search 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 with task-level precision/recall/F1, rewrite coverage/precision, serving latency, A/B search-quality metrics, scroll depth, cold-start serving volume, and user complaint reduction.

Metrics: Rewrite coverage over 95% with 90%+ precision · Fine-tuned 8B model precision 96.4% versus 95.4% baseline · Fine-tuned 8B model recall 95% versus 96.2% baseline · Fine-tuned 8B model F1 95.7% versus 95.8% baseline · Latency reduced from nearly 700ms on A100 toward 300ms on H100 with adapter merging · Average scroll depth reduced by 6% · Poor-tail-search-result complaints 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 in a high-scale grocery search environment with millions of customers, billions of catalog items, millions of weekly cold-start queries, and a hard real-time latency budget.

3 supporting quotes
“This structured approach increased our query rewrite coverage to over 95% with 90%+ precision across all three types.”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
“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