How to Configure Pydantic AI MariaDB Audit Log

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up a pydantic ai mariadb audit log by turning on the server audit plugin. Then you give your agent a dedicated database user role to track its queries. Many teams start by sharing one broad database login across all their apps and scripts. When an AI agent uses a broad role, it can read or change sensitive tables. You cannot tell which query came from a person and which came from the agent. How a pydantic ai mariadb audit log works in practice Pydantic AI lets developers build agents

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.

You set up a pydantic ai mariadb audit log by turning on the server audit plugin. Then you give your agent a dedicated database user role to track its queries. Many teams start by sharing one broad database login across all their apps and scripts. When an AI agent uses a broad role, it can read or change sensitive tables. You cannot tell which query came from a person and which came from the agent.

How a pydantic ai mariadb audit log works in practice

Pydantic AI lets developers build agents that call Python tools to fetch or update data. One common tool opens a MariaDB session and runs SQL queries on behalf of the model. The model reads the user prompt and picks the right tool and parameters. Then the Python app sends the SQL statement to the MariaDB server.

MariaDB includes a built-in server audit plugin that records database activity to a file or syslog. You turn on this plugin in the database settings and pick which events to record. You can log login attempts, queries, and table reads. To track your agent, you create a specific MariaDB user account just for the Python app. The audit plugin writes the database username, timestamp, and SQL text for every query.

Next, your security team sends these log files to a central log store for review. You can filter the logs by the agent database username to see its exact queries. This basic setup gives you a historical record of every command the agent ran.

What native logging solves and what it misses

Native database logging solves the basic problem of tracking past activity on your server. It shows you when the agent logged in and which tables it read. If an agent runs a bad query, you can find the exact SQL text later. This helps teams pass basic audits and debug broken agent tools.

Still, a native database log only records events after they happen. It decides who can log in, not what individual statements can do in flight. If a prompt trick makes the agent ask for all customer rows, the query still runs. The log records the leak, but the sensitive data already went to the language model.

AI Audit Trails + Audit Log Integrity: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Native logs also miss the real user identity behind the AI agent request. MariaDB only sees the static service account used by the Python app. It does not know which human user or business workflow triggered that specific agent run. Also, native logs do not mask sensitive fields in query results. If a table holds emails, phone numbers, card numbers, or national IDs, the agent gets them all. Only dynamic masking rewrites matched fields in results before they reach the model.

Best practices for agent database roles

Start by giving your AI agent the smallest set of table permissions it needs. Never let an agent log in with an admin account or a table owner role. If the agent only answers questions, give its MariaDB user read-only rights on specific views.

Separate your agent accounts by task so one log stream does not mix different workflows. A support bot and a billing bot should use two different database roles. This makes your audit logs much easier to read when you check for strange queries. You should also set strict query timeouts so a bad join cannot lock your server.

Add wire guardrails and masking beside MySQL

Database grants and TLS setup decide who can reach MySQL. Even so, an open session can run broad updates or read raw personal data from allowed tables. In practice, hoop.dev runs beside the database as a sidecar for your MySQL. It keeps your login setup and drivers as they are while checking queries on the wire.

With hoop.dev MySQL support, guardrails refuse bad queries before they run and return a readable MySQL error. Data masking rewrites matched sensitive values in result rows in memory. Furthermore, hoop.dev holds SOC 2 Type II and generates evidence for GDPR, HIPAA, and PCI DSS programs. Read the docs and source on GitHub.

Frequently asked questions

Does Pydantic AI have a built-in MariaDB audit log feature?

No, the framework only runs Python functions and tracks model messages in your app. You must set up query logging inside MariaDB or in your network layer.

Can the MariaDB audit plugin block dangerous AI queries in real time?

No, the native audit plugin only writes events to a log file after they run. It cannot pause a risky query for human approval or rewrite data in flight.

How do I keep customer PII out of my AI agent prompts?

Use dynamic data masking to rewrite emails, phone numbers, and card numbers in query results. This keeps raw personal data out of the model context while keeping the app working.

The query runs. The emails stay hidden.