

read-only. In production — strict RBAC. No exceptions.Let’s be honest: connecting large language models (LLMs) to databases is the moment when a “toy” turns into a tool. At ASCN.AI, over the past 8 years, we have tried many approaches. We started with simple chatbots that just chatted about nothing, and moved on to autonomous systems that actually work.
“The industry’s main conclusion is simple: an agent isolated from data remains a toy. An autonomous agent connected to a database becomes an employee capable of closing transactions. In our tests, implementing such systems reduces application processing time from 4 hours to 5 minutes.”— Founder of ASCN.AI
Imagine: an agent dives into SQL or NoSQL on its own, processes transactions, generates analytics, and manages infrastructure without your involvement. Sounds like science fiction? In fact, it’s already routine. To understand how to implement such systems in your business, it’s worth starting with a basic understanding of workflow automation. But there are pitfalls here.
In the technical specification Article An ASCN Agent is not just a communication interface. Let’s be honest: it is an autonomous system capable of more than simply answering questions.
A standard chatbot is limited to a text response. A Tool-use Agent changes the state of your system. You get an executor that performs work on request. Learn more about applying such systems for companies in the article on AI agents for business.
The primary challenge at the intersection of AI and databases lies in data processing methodology. Large Language Models (LLMs) work with probabilistic tokens, predicting the next word. Meanwhile, databases (especially relational ones like PostgreSQL) require strict precision, typing, and schema compliance.
This creates a gap (Impedance Mismatch): the model may produce a “beautiful” sentence that technically violates strict SQL syntax. The role of middleware is to translate natural language into safe code. Without this validation layer, you will get a high rate of parsing errors and SQL injections instead of correct queries. It is important to understand this before writing code.
┌───────────────┐ ┌───────────────────────┐ ┌───────────────────┐
│ Пользователь │ ───▶ │ LLM Agent (Core Logic)│ ───▶ │ Middleware / Parser │
└───────────────┘ └───────────────────────┘ └───────────────────┘
│ │
│ (Function Call) │ SQL Validation
│ │
┌───────────────────────┐ ┌───────────────────┐
│ Vector DB (RAG) │◀────▶│ SQL / NoSQL DB │
└───────────────────────┘ └───────────────────┘
Diagram: Data flow from the user through the LLM agent and validation layer to the database.
Below is an algorithm for developers based on the Python + LangChain stack. The code is adapted for copying and use. If you are looking for how to connect ai agents to databases, then this is exactly the section you need.
First, ensure your libraries are compatible. Install the necessary packages via the pip package manager:
pip install langchain langchain-community sqlalchemy psycopg2-binary
Choosing the right driver is critical for context formation. A native driver works faster, but ORM (Object-Relational Mapping) provides more abstraction and security. To start, it is recommended to use SQLAlchemy is the industry gold standard. More details on standards and approaches to building agents are described in the article on creating an AI agent.
Never store connection parameters (Connection Strings) directly as strings in code. Use environment variables in the .env file with the dotenv library for secure authentication. Be sure to configure connection pooling via the pool_size and max_overflowparameters. This will prevent service failure during peak requests from dozens of users simultaneously. Trust us, without a pool your database will simply “go down”.
The agent must be able to call tools. In LangChain for SQL, use the ready-made class SQLDatabaseToolkit. Example of full initialization with protection:
from langchain.utilities import SQLDatabase
from langchain.agents import create_sql_agent
from langchain.agents.agent_toolkits import SQLDatabaseToolkit
from langchain.llms import OpenAI
db = SQLDatabase.from_uri("sqlite:///chinook.db")
llm = OpenAI(temperature=0)
# Инициализация тулсета
toolkit = SQLDatabaseToolkit(db=db, llm=llm)
tools = toolkit.get_tools()
agent_executor = create_sql_agent(
llm=llm,
tools=tools,
verbose=True
)
Configure access rights immediately. At ASCN.AI, we always start testing in read-only mode (read-only) so that the agent does not accidentally modify the database structure and break production.
“At ASCN.AI, we always start with read-only so the agent doesn’t break production. If the agent shouldn’t delete data, technically block this capability at the database driver level.”— Founder of ASCN.AI
Pass the table structure (Schema) to the LLM to avoid hallucinations where the model invents non-existent field names. Use dynamic metadata loading on demand (Selective Context Retrieval)—this works better than a static description of the entire database in the prompt. The code snippet should include examples (Few-Shot Examples) so the model learns the response format.
Comparative analysis saves months of development. Below, we compare existing approaches and the new industry standard—Model Context Protocol. Connecting AI Agents to Databases is not just about code; it is about choosing the right architecture.
MCP (Model Context Protocol) is a new open standard for connecting AI to data systems, similar to USB for peripherals. Instead of writing custom connectors and validation for each database for every agent, you deploy a single MCP server.
The agent becomes a client and uses a unified interface to call tools. This reduces maintenance complexity and improves security (a single server logs all requests). Protecting capital in crypto and data starts with architectural security.
Example of creating a secure MCP server (Python):
from mcp.server.fastmcp import FastMCP
import sqlite3
import re
mcp = FastMCP("safe-db-server")
def execute_safe_query(db_path, query):
# Запрет любых команд, кроме SELECT (Read Only)
if not re.match(r"^\s*SELECT\b", query, re.IGNORECASE):
return {"error": "Only SELECT queries allowed"}
try:
with sqlite3.connect(db_path) as conn:
cursor = conn.execute(query)
return cursor.fetchall()
except Exception as e:
return {"error": str(e)}
# Регистрация инструмента (Tool)
@mcp.tool()
async def query_database(query: str):
"""Execute a read-only SQL query against the database."""
return execute_safe_query("business_data.db", query)
if __name__ == "__main__":
mcp.run()
The LLM generates raw SQL code, which the database driver executes directly. It offers maximum flexibility for complex JOIN queries but carries a high risk of injection. Suitable only for internal use in a secure environment.
The LLM calls pre-prepared functions (endpoints). This ensures better security control (abstracting the database schema). Limitation: the agent can only perform actions described in the API.
The agent places a task in a queue (RabbitMQ/Kafka). The system executes the operation asynchronously. Ideal for heavy computations, but not suitable for fast chat responses (0.5–2 sec delay). High fault tolerance.
Before generating SQL or a response, the agent searches for meaning in a vector store of semantically relevant records.
| Method | Security | Flexibility | Implementation complexity |
|---|---|---|---|
| Direct SQL | Low | Maximum | Medium |
| MCP (New standard) | High | Medium | High (first time) |
| API Wrapper | Maximum | Depends on API | High (coding) |
| No-Code (ASCN.AI) | High (business) | Ready-made templates | Low (2 hours) |
Different databases require different optimization approaches. To avoid reinventing the wheel, study the material on automated databases.
Use SQLDatabaseChain in LangChain. It is critical to configure the Self-Correction mechanism: if the model receives an SQL error from the database, it should return it to the LLM and attempt to rewrite the query instead of failing with an error.
Use the PyMongo library. Agents often get confused by the nesting of JSON documents. Validation on the code side is critical because the schema in MongoDB is flexible.
For semantic search within SQL databases, connect extensions like pgvector. Example query for finding similar records:
SELECT * FROM documents
ORDER BY embedding_vector <-> '[0.1, 0.2, ...]' LIMIT 5;
We use this for real-time sentiment analysis of crypto assets. It completely changes the game.
“We actively use document vectorization for RAG. This allows us not to overload the model’s context window with unnecessary data, loading only relevant text fragments. This saves tokens and increases response accuracy.”— Founder of ASCN.AI
To learn how to properly index data for such systems, read our detailed guide on building RAG systems.
Important: This information is for educational purposes only and does not replace consultation with a data cybersecurity specialist. Any changes to database structures (BDDL) are performed at your own risk.
Validation methods for incoming requests are mandatory. The most reliable approach is a combination of prompt and code. Block dangerous commands (DROP, DELETE, TRUNCATE) at the regular expression level before sending to the database.
Example of Regex validation on the Python side:
import re
def protect_db(query):
# Если найдены запрещенные слова - возвращаем False
if re.search(r'\b(DROP|DELETE|INSERT|UPDATE|ALTER)\b', query, re.IGNORECASE):
return False
return True
The Chain of Thought (CoT) technique for verifying SQL logic before execution prevents data loss. Limit the number of returned rows via the directive LIMIT 10. The model must never dump a million records at once — it will crash the application’s memory. Seriously, do not do this.
Create a specific role (User Role) exclusively for the AI agent. It needs read-only rights for selected tables. No root or admin rights for the agent. Solving this "pain point" is a key security parameter for any production environment. Read about optimization and security strategies here.
For an expert audience preparing the system for load:
SELECT *. Train the agent to request only the necessary columns. This speeds up the response several times over and lowers the query cost.pool_recycle and retry logic.The most common mistake is expecting an instant response from SQL. When generating a complex SQL query, a database deadlock may occur. Always wrap the call db.run(query) in a try-except block with exponential backoff for retries.
Question: Can an AI agent delete data if I did not ask it to?
Answer: Yes, if the agent has WRITE privileges in the database. Always enforce restrictions via a Read-Only database user role.
Question: What is the difference between connecting an agent and simple RAG?
Answer: RAG searches and summarizes data (read mode). An agent performs actions in your system, changing the database state or sending messages.
Question: Which architecture should I choose for a high-load system?
Answer: For enterprise, use an API Layer and Message Queues (Kafka/RabbitMQ). Avoid direct connections from LLM to the database if you have more than 100 concurrent users. A direct SQL connection will not withstand such loads. See the example of algorithmic trading.
Question: Which LLMs handle SQL best?
Answer: Models fine-tuned specifically on code (CodeLlama, gpt-4-turbo, Anthropic Claude). They perform more stably than standard chat models. Standard models often invent non-existent syntax, leading to errors.
Who this section is for: Entrepreneurs, traders, business owners.
In the ASCN.AI AI platform project, we see how automation turns into real money. If you are a developer, you use the code above. If you are in business, our no-code platform allows you to deploy ready-made agents without programming. You select an automation template, connect the exchange or database API, and launch. The agent monitors limits, sends notifications, and executes the strategy on its own. Below are confirmed practical examples.
Problem: Traders lost money on manual search for arbitrage opportunities due to reaction delays (human latency).
Solution: We implemented an ASCN.AI Agent that parsed price differences between exchanges and the order database in real time.
Result: Clients closed spreads within seconds. In the case of the Falcon Finance crash the agent managed to detect the anomaly and signal action, allowing to lock in a profit of over $1000 using just 2 prompts. This is not magic, but the result of AI decision-making speed.
Situation: Nighttime flash crash on October 11. The market panicked, volatility increased 10-fold.
Action: Unlike traders, ASCN Agents continued to monitor liquidity and find hidden orders.
Result: Opening profitable positions at the moment when the market was paralyzed by panic. A full analysis of this event is available in the article on earning from a flash crash.
Disclaimer: Cryptocurrency trading involves risks. Past results (as in the cases above) do not guarantee future profits. Use capital management tools.
We often hear the question: "Can this be used for trading or marketing?". Yes, automation of trading strategies or sales requires up-to-date data.
Let’s compare the time required to launch:
With ASCN (No-Code): Building an agent in the visual builder. Cost — hours. You get an executor that changes the system state at your request today.