How to Configure OpenAI Agents SDK SQL Server Audit Log

Published
Length
3 min read

Luis Felipe Dias

All posts

To set up an openai agents sdk sql server audit log, give your agent a dedicated database user and turn on native database tracking. Next, link your agent run IDs to the database queries so your team can review every action. Teams often start by giving an AI agent a shared login with broad permissions. When a role is too broad, an agent can read private tables or change live rows by mistake. Tracking every query and scoping down the user role are key first steps for safety. How an openai agent

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

To set up an openai agents sdk sql server audit log, give your agent a dedicated database user and turn on native database tracking. Next, link your agent run IDs to the database queries so your team can review every action. Teams often start by giving an AI agent a shared login with broad permissions. When a role is too broad, an agent can read private tables or change live rows by mistake. Tracking every query and scoping down the user role are key first steps for safety.

How an openai agents sdk sql server audit log works

The OpenAI Agents SDK lets models call Python functions as tools to do work. To reach SQL Server, a developer writes a tool function that opens a database session and runs a query. That tool uses a login and a database user to sign in.

In SQL Server, a database user represents the identity of a login inside a specific database. When an agent needs to reach many databases on one server, teams often create a login in the master database. When an agent only needs one database, an admin can enable contained databases and make a contained user with a password.

After creating the user, you assign a role. Fixed database roles control what the agent can do, and you cannot change the permissions built into those fixed roles. A read role lets the user read all data from all user tables and views. A write role lets the user add, delete, or change data across user tables. An owner role can run all maintenance tasks and even drop the database.

When the agent runs a tool, SQL Server checks this user role and records the query in the native log. To trace which agent run caused a query, your tool function can add a comment with the agent trace ID to the SQL text. That way, the database log shows both the database user and the agent run ID.

What native roles and logs solve and what they miss

Dedicated users and native database logs solve the first layer of control. You know which agent app logged in, and you have a record of the queries that ran on the server. Using a deny-write role also stops an agent from changing or deleting table rows.

Still, native setup only checks who logs in and which broad role they hold. It does not inspect what individual statements do in flight beyond table permissions. For example, a fixed read role lets the agent read every user table and view in that database.

Audit Log Integrity + Kubernetes API Server Access: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

If an agent writes a broad select query, it can pull emails, phone numbers, card numbers, or national IDs into the model prompt. The native database log will show that the select statement ran, but only after the raw rows left the server. Also, if multiple agents share one service login, the database log cannot tell which human user started the agent run.

Why runtime query control matters for AI agents

AI agents generate queries on the fly based on user prompts. Even with good system instructions, a model can write a slow join or ask for sensitive columns. Relying only on after-the-fact log reviews means your team finds mistakes too late.

Teams need a way to check queries before they hit the database and rewrite sensitive fields in the results. When data masking rewrites matched fields like emails or card numbers in query results, the agent still finishes its task safely. Combining identity tracking with live data masking closes the gap left by static database roles.

Add runtime guardrails beside SQL Server

Role grants and network rules are needed setup for SQL Server. Still, an allowed user or agent tool can send a bad delete or read raw customer fields over a valid session. In practice, hoop.dev runs beside the database as a sidecar for your SQL Server and checks TDS traffic without changing how clients log in.

With hoop.dev SQL Server controls, rules refuse banned statements before they run and can hold risky queries for human review. Matched sensitive values in supported result columns are masked before they reach the client. Furthermore, hoop.dev holds SOC 2 Type II and generates evidence for HIPAA, PCI DSS, SOX, and GDPR audits. See the source on GitHub.

Frequently asked questions

How do we restrict an OpenAI agent to read-only queries in SQL Server?

Create a dedicated database user for the agent and place it in the fixed read role. You can also add thefixed deny-write role so the user cannot add, change, or delete data in any user table.

Can we use a contained database user for an AI agent?

Yes, if the agent only needs one or a few databases, an admin can turn on contained databases on the server. Then you can create a contained SQL user with a password that is not tied to a login in master.

Why is a read role not enough to protect sensitive columns from agents?

The fixed read role in SQL Server grants read permissions across all user tables and views in the database. Unless you mask results in flight, any select query from the agent can return emails, phone numbers, or card numbers.

The query runs. The emails stay hidden.