arXiv:2608.22830cs.AIAug 2026

Manuscript Offprint

arXiv:2608.22830

Beyond the Harness: End-to-End Optimization of Context Artifacts for Enterprise Text-to-SQL

Kate Gwimm, Carson Eisenach

Abstract

Deploying LLMs for enterprise Text-to-SQL is bottlenecked less by the model than by what context reaches it: business logic spans thousands of tables, and no model can ingest a full catalog at once. We argue that the most effective place to intervene is therefore the knowledge-base context the model consumes, and that this context should be constructed from historical usage rather than tuned for as a fixed input. Using a query-DAG decomposition—the same family of intermediates that enterprise benchmarks like BEAVER annotate, here recovered from production SQL—we compare the value of oracle query graphs versus retrieved knowledge-base context. In this ablation, retrieved knowledge-base context provides the largest marginal improvement when added to the full oracle graph. Building on this, we optimize a distillation procedure that turns historical query profiles into reusable SQL reference cards. On a benchmark of 5176 production queries from a major online retailer, optimizing these context artifacts yields larger gains ( ∼\sim12–25% AST similarity) than optimizing the retrieval harness ( ∼\sim3–12%). On the public BEAVER benchmark, which lacks the production-usage signals available in our internal setting, the picture is more mixed: table cards alone perform about the same as raw historical SQL. The best optimized variant retrieves both cards and raw SQL, scoring 9.00% versus 6.33% (p-value 0.12) for the comparable baseline on a held-out N=300 subset, using retrieved context and harness changes but no agentic loop.

1 Introduction

Successful real-world deployments of large language model (LLM) systems depend not just on the model but also on the information that reaches the model and the harness that manages the model's interaction with the external world. A growing body of work optimizes this harness—the executable scaffolding that decides what an LLM application stores, retrieves, and presents—automatically (Lee et al., 2026; Hu et al., 2024).

In this work we consider the complementary problem of optimizing the context artifacts that the model uses for downstream tasks. Concurrent work refines knowledge bases and memory artifacts after they are built (Huang et al., 2026), or accumulates them online with hand-designed update rules (Biswal et al., 2026). We instead ask how to construct the artifacts from raw production traces in the first place. Appendix A gives an extended discussion of related work.

We distinguish raw traces (prior SQL usage records, excluding held-out queries), distilled artifacts (the reusable SQL reference cards synthesized from those traces), the harness (retrieval and generation scaffolding), and the injected context the model actually sees. This distinction matters because our claim is not that every useful context item must be summarized first, but that historical SQL-usage signal should be treated as an optimizable input to the Text-to-SQL system.

Enterprise Text-to-SQL is hard for reasons academic benchmarks rarely capture. Business logic spans thousands of tables and intermediate views, long-horizon planning remains brittle even for reasoning models (Valmeekam et al., 2024), and accuracy degrades as the input grows despite nominally large context windows (Hsieh et al., 2024; Bai et al., 2024). Benchmarks such as Spider (Yu et al., 2018), BIRD (Li et al., 2024), and even Spider 2.0 (Lei et al., 2024) evaluate on schemas far simpler than production data lakes. Because no model can ingest a full enterprise catalog at once, deciding which evidence to surface for a given query becomes the central bottleneck.

We study the Text-to-SQL-specific version of this problem and show, in a production enterprise setting, that SQL reference-card context artifacts distilled from historical query profiles outperform prompt/tool harness optimization under the same search procedure. We treat the knowledge-base context the model consumes as the primary optimization target: we represent SQL queries as a DAG of sub-problems, which makes critical subtasks—such as identifying which tables are relevant—independently measurable, and we distill historical query profiles into reusable SQL reference-card artifacts. Unlike fixed workload-mining recipes (Vaidya et al., 2025; Baek et al., 2025), our distillation function is optimized end-to-end against downstream SQL quality using an AlphaEvolve-style search (Novikov et al., 2025).

Our contribution is a distillation-time optimization method that learns to convert historical data-warehouse queries into reusable SQL reference-card artifacts. To make enterprise context bottlenecks independently measurable we adopt a query-DAG supervision view—the same family of intermediates that enterprise benchmarks like BEAVER annotate (Chen et al., 2024), here recovered from production SQL rather than hand-labeled—and use it to ablate which subtasks and intermediate graph information the model is given—table linkage, output schemas, and the surrounding graph structure—to locate where the bottlenecks actually lie. We then quantify the trade-off between the two optimization surfaces: on the internal benchmark, optimizing context artifacts yields larger relative end-to-end AST gains within each model than prompt/tool harness optimization ( ∼\sim12% vs. ∼\sim3% for Sonnet; ∼\sim25% vs. ∼\sim12% for Qwen). Finally, we test the procedure on BEAVER (Chen et al., 2024), a public enterprise SQL benchmark graded by execution accuracy. Because BEAVER lacks the production-usage signals available internally, it serves as a conservative transfer check. The result is more nuanced than the internal benchmark: cards-only and raw-query retrieval are statistically indistinguishable on our held-out N=300 subset. The best-scoring held-out variant retrieves both cards and raw SQL, scoring 9.00% versus 6.33% for the comparable baseline, using a single generation call and no agentic exploration. This difference is directional rather than statistically significant.