Multi-Agent Architecture with RAG and Dynamic Context Windows for Text-to-SQL Optimization
Paper · 2026 · Willgnner Ferreira Santos, Paulo Victor dos Santos, Marcella Scoczynski Ribeiro Martins et al.
Summary
LLM-based Text-to-SQL systems often put the entire database schema into every prompt. In enterprise databases with hundreds of tables, this is expensive and leads to errors. The authors address this for Brazilian legacy ERP systems on Firebird, which have sparse documentation, inconsistent naming and users who ask questions in Portuguese. Their multi-agent pipeline has two phases. Offline, an agent built with LangChain/LangGraph extracts the schema and uses Claude 3 Sonnet (via AWS Bedrock) to write Portuguese descriptions, saved as YAML/JSON metadata dictionaries. Online, a selector agent uses RAG over these dictionaries to keep only the tables and columns relevant to a question. A second agent then writes the SQL with Mistral Large from this reduced context. The method was tested on three production ERP databases, with ten Portuguese questions each, against a full-schema baseline. It cut processed tokens and per-query cost by roughly 84% while preserving or improving correctness. On the largest database, the baseline got no query right. The study argues that schema pruning matters for answer quality, not just cost.
Key points
- Data: three real Firebird ERP databases with 397, 400 and 448 tables (13,143 columns in total, about 4,381 per database), each queried with 10 Portuguese questions. The questions cover filtering, GROUP BY aggregation, multi-table JOINs and temporal filters. Two domain experts validated correctness.
- Efficiency: average tokens per query fell from 32,768 (full schema) to 5,112, and cost from $0.0493 to $0.0077 (−84.4% for both). Per-database reductions ranged from 81.2% to 86.7%.
- Context size: about 4–8 tables and 40–80 columns were kept, instead of an average of 415 tables and 4,381 columns (−97.6% and −98.2%).
- Accuracy: on the 448-table database, the baseline scored 0% and the proposed approach 60%.
- Resources: the metadata files INFO_TBLS.yaml and INFO_COLUMNS.yaml form the base for retrieval. Because of LGPD and confidentiality agreements, the code and data are available only on restricted request.
Fields, topics and concepts
📚 Shelves 2The field of science this work belongs to (OpenAlex fields).Computer Science · 16,667 worksArtificial Intelligence · 16,194 works
🧭 Topics 2Research subjects and languages the work is about.Semantic parsing & meaning representation · 185 worksCode & programming languages · 129 works
Cite
Cite as Willgnner Ferreira Santos, Paulo Victor dos Santos, Marcella Scoczynski Ribeiro Martins, Larissa Freire Lekakis, Frederico Lemes Rosa, Bruno Matheus Costa, Miguel Alves Pereira Filho and Isabella Alves Montalvão. 2026. Multi-Agent Architecture with RAG and Dynamic Context Windows for Text-to-SQL Optimization. Proceedings of the 17th International Conference on Computational Processing of Portuguese (PROPOR 2026) - Vol. 1.
@inproceedings{santos2026multi,
title = {{Multi-Agent Architecture with RAG and Dynamic Context Windows for Text-to-SQL Optimization}},
author = {Willgnner Ferreira Santos, Paulo Victor {dos Santos}, Marcella Scoczynski Ribeiro Martins, Larissa Freire Lekakis, Frederico Lemes Rosa, Bruno Matheus Costa, Miguel Alves Pereira Filho and Isabella Alves Montalvão},
booktitle = {Proceedings of the 17th International Conference on Computational Processing of Portuguese (PROPOR 2026) - Vol. 1},
year = {2026},
url = {https://aclanthology.org/2026.propor-1.100/}
}References
- Jiya Manchanda, Laura Boettcher, Matheus Westphalen, and Jasser Jasser. 2024. The open source advantage in large language models (llms). arXiv preprint arXiv:2412.12004. — [arXiv:2412.12004](https://arxiv.org/abs/2412.12004) find
- A. Bhattacharjee. 2025. A Practical Guide to Generative AI Using Amazon Bedrock: Building, Deploying, and Securing Generative AI Applications. Professional and Applied Computing. Apress. find
- Jan-Micha Bodensohn, Ulf Brackmann, Liane Vogel, Anupam Sanghi, and Carsten Binnig. 2025. Unveiling challenges for llms in enterprise data engineering. arXiv preprint arXiv:2504.10950. — [arXiv:2504.10950](https://arxiv.org/abs/2504.10950) find
- Chongjiong Fan, Zhicheng Pan, Wenwen Sun, Chengcheng Yang, and Wei-Neng Chen. 2024. Latuner: An llm-enhanced database tuning system based on adaptive surrogate model. In Joint European Conference on Machine Learning and Knowledge Discovery in Databases, pages 372–388. Springer. find
- Dawei Gao, Haibin Wang, Yaliang Li, Xiuyu Sun, Yichen Qian, Bolin Ding, and Jingren Zhou. 2023. Text-to-sql empowered by large language models: A benchmark evaluation. arXiv preprint arXiv:2308.15363.
- Yingqi Gao, Yifu Liu, Xiaoxia Li, Xiaorong Shi, Yin Zhu, Yiming Wang, Shiqi Li, Wei Li, Yuntao Hong, Zhiling Luo, and 1 others. 2024. A preview of xiyan-sql: A multi-generator ensemble framework for text-to-sql. arXiv preprint arXiv:2411.08599. — [arXiv:2411.08599](https://arxiv.org/abs/2411.08599) find
- Lara Rocha Garcia, Edson Aguilera-Fernandes, Rafael Augusto Moreno Gonçalves, and Marcos Ribeiro Pereira-Barretto. 2020. Lei Geral de Proteção de Dados (LGPD): guia de implantação. Editora Blucher. find
- Z. Hong, Z. Yuan, Q. Zhang, and 1 others. 2025. Next-generation database interfaces: A survey of llm-based text-to-sql. In IEEE Journals & Magazine. find
- Feiran Huang, Junnan Dong, Hao Chen, and Qinggang Zhang. 2025. Enhancing security in text-to-sql systems: A novel dataset & evaluation framework for prompt injection attacks. Natural Language Engineering. find
- Xiuwen Li, Qifeng Cai, Yang Shu, Chenjuan Guo, and Bin Yang. 2025. Aid-sql: Adaptive in-context learning of text-to-sql with difficulty-aware instruction and retrieval-augmented generation. In 2025 IEEE 41st International Conference on Data Engineering (ICDE), pages 3945–3957. IEEE. find
- Xinyu Liu, Shuyu Shen, Boyan Li, Peixian Ma, Runzhi Jiang, Yuxin Zhang, Ju Fan, Guoliang Li, Nan Tang, and Yuyu Luo. 2025. A survey of text-to-sql in the era of llms: Where are we, and where are we going? IEEE Transactions on Knowledge and Data Engineering. find
- Jovana Milosavljeva and Viktorija Jankulovska. 2025. Report for support in business development. Technical report, CARBONICA Project (Horizon Europe). Deliverable D4.3. find
- A. Mohammadjafari, A. S. Maida, and R. Gottumukkala. 2024. From natural language to sql: Review of llm-based text-to-sql systems. arXiv preprint arXiv:2410.01066. — [arXiv:2410.01066](https://arxiv.org/abs/2410.01066) find
- Eduardo Nascimento and 1 others. 2025. Llm-based text-to-sql for real-world databases. SN Computer Science, 6(1):1–18. find
- Deborah Karimi Njue. 2025. ENTERPRISE RE-SOURCE PLANNING SYSTEM INTEGRATION AND PERFORMANCE OF COMMERCIAL BANKS IN EMBU COUNTY, KENYA. Ph.D. thesis, Kenyatta University. find
- Sai S Nudurupati, Sofiane Tebboune, Patrizia Garengo, Richard Daley, and Julie Hardman. 2024. Performance measurement in data intensive organisations: resources and capabilities for decision-making process. Production Planning & Control, 35(4):373– 393. find
- Francisco Carlos Paletta and Jader Jaime Costa do Lago. 2021. Gestão da informação corporativa. Environmental smoke, 4(21):54–64. find
- Marcel Parciak, Brecht Vandevoort, Frank Neven, Liesbet M Peeters, and Stijn Vansummeren. 2024. Schema matching with large language models: an experimental study. arXiv preprint arXiv:2407.11852. — [arXiv:2407.11852](https://arxiv.org/abs/2407.11852) find
- Mohammadreza Pourreza and Davood Rafiei. 2023. Din-sql: Decomposed in-context learning of text-to-sql with self-correction. Advances in Neural Information Processing Systems, 36:36339–36348.
- F. Santos, L. Almeida, R. Pereira, and 1 others. 2025. Optimizing text-to-sql conversion techniques through the integration of intelligent agents and large language models. In Information Processing & Management. find
- Bing Wang and 1 others. 2025. Mac-sql: A multi-agent collaborative framework for text-to-sql. In Proceedings of the 2025 International Conference on Computational Linguistics (COLING), pages 540–557.
- Claes Wohlin, Per Runeson, Martin Höst, Magnus C. Ohlsson, Björn Regnell, and Anders Wesslén. 2000. Experimentation in Software Engineering: An Introduction, 1 edition. Kluwer Academic Publishers. find
Updated
This page in Markdown → https://md.research.elamur.ai/free/en/papers/santos-2026-multi-agent-architecture-rag-dynamic/