
Share
This article delves into creating a smarter Text-to-SQL agent that learns from past queries and context, mimicking expert data analysts' efficiency and reducing repetitive errors.
If you've ever worked on a Text-to-SQL project, you know the common pitfalls. Most agents start from scratch every time, describing tables, columns, and finding join keys-essentially repeating every mistake over and over. This is in stark contrast to how experienced data engineers or analysts operate. They leverage tribal knowledge, past queries, and established usage patterns to get things done efficiently.
In this article, we'll explore how to build a self-improving Text-to-SQL agent using dynamic context and "poor-man's continuous learning." The goal is to make the agent more robust by providing it with the same kind of contextual information that human experts use. Here’s how we can break down the problem:
Most Text-to-SQL agents fail because they lack context and tribal knowledge. They start from scratch every time, which leads to several common issues:
The key to improvement is providing the agent with the same tribal knowledge that human engineers have. This allows it to re-use established queries and search for usage patterns at runtime.
Dynamic context means the model has access to relevant information (schemas, joins, past queries, metric definitions, gotchas) at runtime. This can be achieved through techniques like Retrieval-Augmented Generation (RAG) or Agentic RAG, where the model retrieves and uses this context to generate more accurate SQL.
Continuous learning in this context means the agent learns from its successful runs and updates its knowledge base accordingly. This creates a self-improving feedback loop without requiring complex retraining processes. Here’s how it works:

This approach keeps the online path stable while gradually improving the agent's performance over time.
The architecture of our self-improving Text-to-SQL agent can be broken down into two main components:
The knowledge base should be structured to store:
To make the agent deployable, consider the following:
Tags
Original Sources
About the author
Kai built ML infrastructure at a Bay Area startup before developing an obsession with transformer architectures and inference optimisation that eventually pulled him out of product work entirely. A stint at a compute research lab sharpened his instinct for what actually matters in a model release versus what is marketing. He writes from the inside — from the perspective of someone who has debugged the systems he is describing at three in the morning. He is allergic to hype and instinctively drawn to the unglamorous plumbing questions that everyone else skips over.
More from The Engineer →This Week's Edition
16 December 2025
133 articles
Related Articles
Related Articles
More Stories