# How Uber Scaled Text-to-SQL to 1.2M Monthly Queries: Inside QueryGPT's Multi-Agent Architecture

> Source: <https://dev.to/chethanramaswamy/how-uber-scaled-text-to-sql-to-12m-monthly-queries-inside-querygpts-multi-agent-architecture-592j>
> Published: 2026-11-25 17:07:05+00:00

**Recommendation:** Scope retrieval to curated domain workspaces and prune columns before generating SQL; Uber found naive top-3 table RAG overflowed 128K tokens and returned zero-row queries from wrong enum guesses.
More in the playbook "Designing agent systems": <https://wpnews.pro/playbooks/agent-architecture>

Most Text-to-SQL demos look impressive on a 5-table toy database and fall apart the moment they meet an enterprise data warehouse. The bottleneck isn't SQL syntax—it's **context bloat, ambiguous domain boundaries, and tribal schema knowledge**.

In their engineering deep dive, [*QueryGPT – Natural Language to SQL Using Generative AI*](https://www.uber.com/in/en/blog/query-gpt/), Uber shared how they evolved a 2023 hackathon prototype through 20+ iterations into a production system serving **~1.2 million interactive queries per month** (with Operations alone driving ~36% of volume), cutting average query authoring time from **~10 minutes down to ~3 minutes**.

Here is an architectural breakdown of why naive RAG failed at Uber's scale—and the multi-agent pipeline they built to fix it.

## 
  
  
  1. Why V1 Naive RAG Collapsed at Scale

Uber's initial May 2023 prototype (*Hackdayz V1*) followed a classic single-pass RAG pattern:

1. Embed the user's natural language prompt.
2. Run k-nearest neighbor (k-NN) similarity search across a small pool of **7 Tier-1 tables** and**20 sample SQL queries** .
3. Retrieve the **top 3 tables** and**top 7 SQL samples** , combine them with custom Uber business instructions (such as date formatting rules), and generate SQL in one LLM call.

``` php
flowchart LR
    P["User Natural Language Prompt"] --> E["Vector Embedding + k-NN Search"]
    E --> R1["Top 3 Table DDLs"]
    E --> R2["Top 7 SQL Samples"]
    C["Custom Uber Date & Business Rules"] --> LLM["Single LLM Call"]
    R1 --> LLM
    R2 --> LLM
    LLM --> OUT["SQL Query + Explanation"]
```

As Uber onboarded more datasets into production, three systemic bottlenecks broke this architecture:

- 
**DDL Embedding Mismatch:** Vector similarity between a plain-English business question (*"trips completed yesterday in Seattle"* ) and raw`CREATE TABLE` DDL statements degraded rapidly as hundreds of tables were added.
- 
**Context Window Exhaustion:** Wide Tier-1 tables at Uber often exceed**200+ columns** , consuming**40K–60K tokens per table** . Retrieving just three tables blew past early 32K token limits—and continued causing intermittent overflows even after upgrading to**GPT-4 Turbo (1106) with a 128K token window** .
- 
**Silent Semantic Hallucinations:** Queries frequently compiled and executed without syntax errors while returning zero rows because the LLM guessed wrong enum values (e.g., filtering`status = 'Finished'` when the table stores`'Completed'` ).

## 
  
  
  2. Production Architecture: A Progressive Filtering Funnel

Instead of dumping raw schemas into a single prompt, Uber redesigned QueryGPT as a **multi-stage agentic pipeline** that progressively narrows the search radius before generating SQL.

```
flowchart TD
    U["User Prompt: Tesla trips completed in Seattle yesterday"] --> IA["1. Intent Agent"]
    IA --> WS["Domain Workspace Routing: Mobility selected out of 12 System Workspaces"]
    WS --> RAG["Scoped Vector Search: Mobility Tables & SQL Samples"]
    RAG --> TA["2. Table Agent"]
    TA --> HITL{"Human-in-the-Loop Checkpoint"}
    HITL -- "User edits table list" --> TA
    HITL -- "User clicks Looks Good" --> CPA["3. Column Prune Agent"]
    CPA --> SLIM["Pruned Schemas: Irrelevant Columns Stripped"]
    SLIM --> GEN["4. SQL Generation LLM + Custom Date Rules"]
    GEN --> FINAL["Verified SQL Query + Plain-English Explanation"]
```

### 
  
  
  Stage 1: Domain Workspaces + Intent Agent

Uber partitioned its data catalog into **12 curated System Workspaces**—** Mobility** plus 11 other core domains including **Ads** (Uber Advertising), **Core Services**, **Platform Engineering**, and **IT** (Information Technology)—alongside user-defined **Custom Workspaces** for specialized teams.

Every request first hits an **Intent Agent** (an LLM classifier) that maps the prompt to one or more workspaces. By locking onto the *Mobility* workspace first, QueryGPT avoids searching unrelated *Ads* or *IT* schemas altogether.

### 
  
  
  Stage 2: Table Agent with Human-in-the-Loop (HITL)

Early user feedback revealed that when upstream table retrieval missed the mark, the final SQL was guaranteed to fail. Uber introduced a **Table Agent** paired with an explicit UI checkpoint:

- The agent proposes candidate tables (e.g., `uber.trips_data` and`uber.dim_city` ).
- The user can either click **"Looks Good"** to proceed or manually add/remove tables before SQL synthesis begins.

### 
  
  
  Stage 3: Column Prune Agent

To eliminate 128K token overflows and reduce inference latency, Uber added a **Column Prune Agent**. Before generating SQL, an intermediate LLM call inspects the user's question and strips out irrelevant columns from each selected table's DDL—shrinking a 200-column schema down to only the handful of fields needed for joins, filters, and projections.

## 
  
  
  3. Complete End-to-End Walkthrough

Here is how a real request flows through every stage of the pipeline *(note: `uber.trips_data` is an illustrative table name used in Uber's blog)*:

1. 
**User Prompt:** >*"Write a query to find the number of trips that were completed by Teslas in Seattle yesterday"*
2. 
**Intent Agent Output:** Maps prompt to the**`Mobility`** system workspace.
3. 
**Table Agent + User Confirmation:** Suggests`uber.trips_data` and`uber.dim_city` → User clicks**"Looks Good"** .
4. 
**Column Prune Agent Output:** Reduces`uber.trips_data` from 200+ columns down to`trip_id` ,`city_id` ,`vehicle_make` ,`trip_status` , and`datestr` (partition key).
5. 
**Final Generated SQL (incorporating Uber's custom date instructions):**

*(Accompanied by a plain-English explanation of the join, filter conditions, and date partition logic.)*

## 
  
  
  4. How Uber Evaluates QueryGPT (Vanilla vs. Decoupled)

Because LLM outputs are non-deterministic (~5% run-to-run variance), Uber built a golden question-to-SQL benchmark and tracks regressions across two test modes:

- 
**Vanilla Flow:** End-to-end execution where QueryGPT infers intent, tables, and SQL autonomously.
- 
**Decoupled Flow:** Injects ground-truth intent and tables directly into the generator so engineers can isolate SQL synthesis quality from upstream retrieval errors.

``` php
flowchart LR
    G["Golden Benchmark Question"] --> M1["1. Intent Accuracy"]
    G --> M2["2. Table Overlap Score: 0.0 to 1.0"]
    G --> M3["3. Successful Run: Compiles & Executes"]
    G --> M4["4. Run Has Output: Rows Greater Than 0"]
    G --> M5["5. Qualitative Query Similarity: LLM Judge 0.0 to 1.0"]
```

Each benchmark run captures **5 distinct signals**:

1. 
**Intent Accuracy:** Did the Intent Agent select the right workspace?
2. 
**Table Overlap (0 to 1):** Measures retrieval precision. For example, if*"How many trips were canceled by drivers last week in Los Angeles?"* requires`[fact_trip_state, dim_city]` and QueryGPT selects`[dim_city, fact_eats_trip]` , the overlap score is**0.5** (1 of 2 required tables matched).
3. 
**Successful Run:** Does the SQL execute on the engine without syntax or missing-partition errors?
4. 
**Run Has Output (`rows > 0`):** Catches silent semantic bugs—such as` WHERE status = 'Finished'` instead of`'Completed'` —where a query runs cleanly but returns zero records.
5. 
**Qualitative Query Similarity (0 to 1):** An LLM judge compares the generated SQL against the golden SQL to verify whether joins, columns, and aggregations are structurally sound even when a minor rule (like a missing partition filter) causes execution to fail.

## 
  
  
  Key Architectural Takeaways

1. 
**Bound your retrieval space first:** Domain workspaces + an Intent Agent outperform flat vector search across monolithic enterprise catalogs.
2. 
**Put humans at high-leverage checkpoints:** Confirming 2–3 table names takes a user two seconds and prevents cascading hallucination in the final SQL.
3. 
**Prune schemas aggressively:** Treating DDL pruning as its own lightweight agentic step cuts token cost, prevents context overflows, and improves SQL attention accuracy.
4. 
**Measure silent failures, not just syntax:** A Text-to-SQL query that compiles with zero rows returned is a failure—track non-empty execution alongside structural similarity.
