Self-Optimizing Enterprise RAG+SQL Knowledge Agents: Unified Retrieval, Schema-Aware Query Translation, Event Graph Reasoning, and Adaptive Model Routing
This paper presents a Self-Optimizing Enterprise RAG+SQL Knowledge Agent, a middleware architecture that unifies schema-aware SQL translation, hybrid document and graph retrieval, and adaptive model routing to treat complex enterprise question answering as a self-optimizing control problem over heterogeneous knowledge tools.
Original paper licensed under CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). This is an AI-generated explanation of the paper below. It is not written or endorsed by the authors. For technical accuracy, refer to the original paper. Read full disclaimer
Modern businesses do not keep their knowledge in a single place. Critical facts about customers, contracts, and financial performance live in structured databases, where data is organized into rows and columns. Meanwhile, the stories behind those numbers—incident reports, policy documents, and operational logs—reside in unstructured text files, spreadsheets, and PDFs. For decades, artificial intelligence systems have struggled to bridge this divide. One type of system, known as retrieval-augmented generation, excels at finding answers within vast libraries of text but cannot perform the precise calculations required by a database. Another type, capable of translating human questions into database queries, can crunch numbers but cannot read a policy manual or understand the sequence of events in a narrative report. This separation forces companies to choose between accuracy and context, often leaving them with incomplete answers to complex questions that require both.
A new approach, detailed in recent research by Harshil Lodhiya of Sliced Health, proposes a unified solution that treats enterprise knowledge as a single, interconnected ecosystem rather than a collection of isolated silos. The researcher introduces a system called the Self-Optimizing Enterprise RAG+SQL Knowledge Agent. This is not merely a tool that searches for text or writes code; it is a middleware architecture designed to act as a central conductor for diverse information sources. It takes a natural language question, breaks it down into specific tasks, and then orchestrates a sequence of actions: retrieving relevant documents, traversing a map of relationships between events, and executing precise database queries. Crucially, the system does not stop at generating an answer. It continuously monitors its own performance, learning from every interaction to improve how it selects tools, retrieves information, and validates its work. The result is a system that can answer questions like, "Which customers affected by last week's outage also have premium support in their contracts?" by simultaneously reading the incident report, checking the database for affected accounts, and verifying the contract terms, all while ensuring the final answer is safe, accurate, and fully sourced.
The core innovation lies in how the system plans its work before it begins. Instead of asking a single artificial intelligence model to guess the answer, the agent first decomposes the user's question into a typed evidence plan. This plan is a structured sequence of operations, similar to a checklist for a skilled investigator. For a complex query, the agent might first identify the specific incident in a log, then use that information to find the relevant customer names, and finally use those names to filter a database for contract details. Each step in this plan is assigned a specific tool: one for reading text, another for navigating a graph of relationships, and a third for writing and running database commands. By separating the planning phase from the execution phase, the system ensures that every piece of information it gathers is relevant and that the logic connecting them is auditable. This structure prevents the system from hallucinating facts or making unsupported leaps, as every claim in the final answer must be backed by a specific piece of evidence collected during the process.
To handle the vast and varied nature of enterprise data, the system employs a hybrid retrieval strategy that combines three distinct methods. It uses dense vector search to find text that is semantically similar to the question, even if the words do not match exactly. It employs lexical matching to find exact terms, which is vital for specific codes or names. Most importantly, it utilizes a graph-based approach to understand relationships. In this graph, events, documents, and database records are linked together. If a question asks about the chain of events leading to a failure, the system can traverse this graph to follow the causal path from the initial error to the final impact, rather than just searching for keywords. This allows the agent to reconstruct complex narratives, such as the sequence of events behind a failed payment batch, by connecting the dots between a log entry, a policy violation, and a specific database row.
Safety and accuracy are enforced through rigorous validation steps that occur before any final answer is presented to a user. When the system generates a database query, it does not simply run it. It first checks the query against a pruned version of the database schema, ensuring it only accesses tables and columns the user is permitted to see. The query is then tested in a read-only sandbox, a secure environment where it can be executed without the risk of altering or deleting any data. The system verifies that the query is syntactically correct and that the results make sense within the context of the question. If a query fails validation or if the retrieved evidence is insufficient, the system is designed to abstain from giving an answer rather than guessing. It will instead explain what information is missing or why it cannot proceed, a feature that is critical for maintaining trust in high-stakes environments like finance or compliance.
Perhaps the most dynamic aspect of this architecture is its ability to self-optimize. The system operates as a closed-loop control problem, constantly adjusting its behavior based on feedback from the real world. As the system processes thousands of queries, it collects telemetry data regarding the cost, speed, and accuracy of its decisions. It tracks which models performed best for specific types of tasks, which retrieval methods yielded the most relevant documents, and where validation thresholds needed adjustment. Using this data, the system automatically updates its internal policies. If a particular artificial intelligence model becomes slower or less accurate over time, the system learns to route fewer tasks to it. If a certain type of document retrieval consistently fails to find the right information, the system adjusts its search weights to prioritize other methods. This continuous learning cycle ensures that the agent does not remain static but evolves alongside the changing landscape of enterprise data, user needs, and available technology.
The research outlines a practical blueprint for building such a system using existing, robust technologies. It suggests using standard relational databases for storing metadata and graph structures, alongside vector databases for managing text embeddings. The architecture is modular, allowing organizations to adopt individual components, such as schema-aware query translation or event graph reasoning, before integrating the full self-optimizing loop. This flexibility means that even partial implementations can provide significant value by improving the accuracy of specific tasks, such as generating database reports or summarizing incident logs. The ultimate goal is not to replace human analysts but to provide them with a powerful, reliable assistant that can navigate the complexity of modern enterprise knowledge, turning fragmented data into coherent, actionable insights.
The findings suggest that treating enterprise question answering as a multi-tool control problem, rather than a simple text search, offers a path forward for reliable artificial intelligence in business. By combining the strengths of text retrieval, graph reasoning, and database execution within a single, self-correcting framework, the system addresses the fundamental limitations of previous approaches. It acknowledges that no single model or method can solve every problem and instead focuses on orchestrating a diverse set of tools to produce a unified result. The research demonstrates that with careful design, including strict validation, access control, and continuous optimization, it is possible to build agents that are not only intelligent but also safe, transparent, and capable of improving over time. This approach represents a significant step toward realizing the potential of artificial intelligence to truly understand and interact with the complex, multi-faceted reality of the modern enterprise.
Drowning in papers in your field?
Get daily digests of the most novel papers matching your research keywords — with technical summaries, in your language.