keepcalculator

06 Numbers and Powers

How to Connect an Ai Assistant to a Database Without Replacing Its Query Engine

Card No. 06·36939 · Updated · Standard formula, shown below

You can connect an AI assistant to a database while keeping the application’s existing query engine in charge of database access. The assistant interprets a request and proposes a structured operation; your existing engine validates, executes, and returns the result. Before you begin, identify the engine’s supported query interface and decide which data the assistant is allowed to request.

What You’ll Need

  • An existing application with a working database query engine, such as an ORM, repository layer, query builder, or approved service API.
  • Access to the assistant’s tool-calling or structured-output interface, plus the ability to define a small set of tools.
  • A development or test database with representative, non-sensitive data.
  • A defined permission policy: which tables, fields, operations, and users are in scope.
  • Logging and tests for tool inputs, authorization decisions, queries, and results. Avoid logging secrets or unnecessary personal data.

Step-by-Step

  1. Map the current query path. Trace how a normal application request reaches the database: for example, route or service, repository, ORM, then database. Note where authentication, tenant filtering, validation, and transactions happen. Your goal is to add the assistant at the application boundary, not give it a second, unrestricted route to the database.
  2. Choose a narrow task for the assistant. Start with a request such as “show the status of order 123” or “count open tickets this week.” Decide which questions are supported and what the user must be authorized to see. Do not start with unrestricted prompts such as “query anything in the database.”
  3. Define a small tool contract. Expose application-level operations, not raw database credentials. A tool might accept a customer ID and a date range, or a named report and a few filters. Specify required fields, data types, allowed values, maximum result count, and a clear error response. Keep the contract independent of SQL syntax so the existing engine remains replaceable and testable.
  4. Have the assistant return structured arguments. Ask it to select a tool and supply arguments matching the contract. Treat its output as untrusted input: validate the schema, reject unknown fields and invalid ranges, and verify that IDs and requested operations are permitted for the current user. A natural-language explanation is not a substitute for validation.
  5. Pass validated arguments to the existing engine. Implement the tool handler by calling the same service, repository, ORM, or query builder the application already uses. Use that layer’s parameter binding and established filters. Do not concatenate model-generated text into SQL, pass a generated query to an execution function, or let a model choose a connection string.
  6. Apply identity and access controls on the server. Derive the user and tenant context from the authenticated application session, not from assistant-supplied arguments. Enforce authorization and row-level restrictions in the trusted application or database layer. If access is denied, return a limited error rather than retrying through a more privileged connection.
  7. Constrain database work. Start with read-only operations. Limit result rows, selected fields, date spans, and execution time; use pagination where appropriate. For reports that need writes, create separate, narrowly scoped tools with explicit confirmation and existing transaction and audit mechanisms. Never give the assistant general-purpose database-admin privileges.
  8. Return a minimal, clear result. Convert database records into a predictable response with only fields the user is allowed to see. Summarize or paginate large results rather than sending entire tables to the assistant. Distinguish “no matching records” from validation, authorization, and database errors.
  9. Test the whole path before release. Test valid requests, malformed arguments, unauthorized IDs, cross-tenant attempts, empty results, timeouts, and overly broad filters. Confirm that each call goes through the original query engine and its normal controls. Then deploy behind a feature flag, monitor latency and failures, and expand the supported tools gradually.

Common Mistakes

  • Allowing generated SQL to run directly. Even plausible-looking queries can expose data or perform expensive operations. Keep query construction in trusted application code and use parameterized inputs.
  • Trusting the assistant’s permissions claims. The assistant may produce an ID or tenant value that looks valid. Authorize it against the signed-in user on every request.
  • Creating a parallel database layer. A direct connection from the assistant integration can bypass ORM rules, audit logging, tenant filters, or transaction handling. Call the existing engine instead.
  • Returning too much data. Large result sets increase cost and may expose fields unrelated to the request. Select and return only the minimum necessary information.
  • Using vague tools. A tool named “run database query” invites broad, hard-to-validate requests. Prefer specific operations with bounded arguments.
  • Ignoring errors and retries. Retrying every failure can repeat writes or overload the database. Use idempotency controls for actions that change data, and retry only known transient failures under a limit.

Tips

  • Keep tool definitions small and describe each argument with its meaning, format, and limits.
  • Use a read-only database role where practical, but do not rely on it instead of application authorization.
  • Set sensible query timeouts and result limits in the engine or service layer, not only in the assistant prompt.
  • Record tool name, validated arguments, user context, outcome, and timing while minimizing sensitive content in logs.
  • For analytics, consider a read-only view or reporting service that the existing query engine can use, rather than exposing operational tables.
  • Make the assistant say when a request is unsupported or ambiguous instead of guessing a filter or broadening the search silently.

Troubleshooting

The assistant chooses the wrong tool: Narrow the tool descriptions, use distinct names, and provide examples of supported requests. Add server-side checks so a mistaken choice cannot cause an unsafe query.

Arguments fail validation: Inspect the structured response and schema error. Clarify required formats, allowed values, and date conventions in the tool definition; do not relax validation just to make a call succeed.

Results differ from the existing application: Compare the tool handler with the application’s normal service call. Check user context, tenant filters, soft-delete rules, timezone handling, and default scopes.

Queries are slow or return too much: Tighten date ranges and filters, cap rows, select fewer columns, and inspect the query through the existing engine’s normal diagnostics. Do not solve this by granting broader database access.

Calls time out or fail intermittently: Check application and database logs, connection-pool capacity, and timeout settings. Return a clear temporary failure when appropriate; avoid unlimited retries, especially for operations that may change data.

Frequently Asked Questions

Does the assistant need direct database credentials?
No. Prefer a tool handler in your application that calls the existing query engine under established credentials and controls.

Can I keep using my ORM or query builder?
Yes. Have the tool handler call the same repository or service methods the rest of the application uses.

Is it safe to let the assistant write data?
It can be, but use separate narrowly scoped operations, enforce authorization, validate every field, and require confirmation for consequential changes. Begin with read-only access.

Can the assistant generate SQL if I review it?
That still creates risks from unsafe or expensive queries. Prefer structured arguments mapped to fixed application operations; if SQL generation is required for a specialized system, isolate and validate it outside the general request path.

How do I support questions that span several tables?
Implement a specific service operation or approved report using the existing engine. Return only the fields and rows needed for the answer.

What should I log?
Log the tool, request context, validation and authorization outcome, duration, and result status. Keep sensitive values and returned records out of logs unless there is a justified, protected need.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →