How do I audit LLM SQL queries for compliance (SOC 2, HIPAA)?
SOC 2 CC7.2 (system monitoring) and HIPAA 164.312(b) (audit controls) require contemporaneous, tamper-evident logging of access to in-scope data. For LLM SQL agents, an audit-grade log entry needs:
1. Timestamp (RFC 3339, UTC). 2. Subject — the authenticated user / API key on whose behalf the agent ran. 3. Agent identity — which model, which system prompt version. 4. The natural-language prompt. 5. The generated SQL (pre- and post-rewrite if the policy engine modified it). 6. Decision — allowed, rejected (with rule ID), or rewritten. 7. Result row count (not contents, to avoid logging PHI). 8. Hash of the previous log entry (for tamper-evidence; cf. Certificate Transparency Merkle log pattern).
QueryShield emits this as structured JSON to stdout, S3, or any SIEM (Datadog, Splunk, Elastic). For HIPAA specifically, keep logs for six years; for PCI DSS, one year online plus three archived. Do not log query *results* — log row counts and column names, not values.