How to Configure Mastra Neon Audit Log

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up a mastra neon audit log by turning on database logging in Neon. Next, you link your Mastra agent workflows to a specific Postgres role. Most teams start by sharing one broad database key across all agents. This shortcut lets every agent read or change any table in the database. When an agent runs a bad query, the team cannot tell which workflow caused it. How a mastra neon audit log works in practice Mastra is a TypeScript framework that helps teams build and run AI agent workflows

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 mastra neon audit log by turning on database logging in Neon. Next, you link your Mastra agent workflows to a specific Postgres role. Most teams start by sharing one broad database key across all agents. This shortcut lets every agent read or change any table in the database. When an agent runs a bad query, the team cannot tell which workflow caused it.

How a mastra neon audit log works in practice

Mastra is a TypeScript framework that helps teams build and run AI agent workflows. These agents often need to read and write data inside a Neon Postgres database. Neon runs Postgres on serverless compute that scales up and down based on traffic. To track what agents do, you must set up logging at both layers.

First, you create a dedicated Postgres role in Neon just for your Mastra app. You give this role permission to read or write only the tables it needs. Then you turn on the standard Postgres logging extension inside your Neon project settings. This extension records SQL statements and role names inside the database log files.

On the app side, Mastra agents run tools that send SQL queries to Neon. Mastra also keeps its own local logs of agent steps and tool calls. When you pair both records, you can trace a query back to an agent. You see when the agent ran the tool and when Neon ran the query.

Where native database logging falls short for AI agents

Native database logs help you check what happened after an incident is already over. They show which Postgres role logged in and which SQL statement ran in Neon. Still, native setup only checks who logs in at the start of a session. It does not inspect or block what individual statements do while in flight.

Because Neon compute instances scale down to zero, local log state can reset. Teams must ship those logs to an outside storage tool before the compute stops. Even with log shipping, standard logs capture raw data returned or sent in queries. If an agent queries emails, phone numbers, or card numbers, those values stay exposed. Data masking rewrites matched fields in results so raw emails or card numbers stay safe.

Audit Log Integrity + End-to-End Encryption: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Best practices for agent roles and query tracking

Start by splitting your database roles by agent task instead of sharing one login. Give read-only roles to research agents that only need to fetch reference rows. Give write roles only to specific tools that must update state or save results. This simple split limits the damage if an AI prompt goes off track.

You should also pass a clear app name in the database settings from Mastra. Postgres records this app name tag right next to every logged SQL query. When you check the logs later, you can filter by the exact Mastra workflow. Finally, review your log volume often so verbose query logs do not slow performance.

Run guardrails and masking on the Postgres connection

Role grants and firewall rules are needed setup. Setup alone cannot check single SQL statements or mask sensitive rows in a response. By design, hoop.dev is an open-source proxy that sits beside Postgres, RDS, or Aurora as a sidecar for your Postgres. Clients and agent tools keep their credentials and point their connection string at the listener.

On each query, hoop.dev guardrails refuse disallowed drops, deletes, or table reads before they run. Next, hoop.dev data masking rewrites sensitive fields in returned rows that match your rules. Furthermore, hoop.dev holds SOC 2 Type II and logs every statement and verdict per resource. Check out the project on GitHub.

Frequently asked questions

Does Neon keep Postgres audit logs forever by default?

No, Neon serverless compute instances shut down when idle and clear local log files. You must export your database logs to an outside log store for long-term retention.

How can I tell which Mastra agent ran a specific SQL query?

Give each Mastra agent workflow its own Postgres role or custom app name tag. Neon will record that role or tag alongside every SQL statement in the log.

Can native Neon logs hide sensitive data from Mastra agents?

No, native logs only record queries and do not change query results in flight. You need dynamic data masking to rewrite emails, phone numbers, or national IDs in results.

The query runs. The emails stay hidden.