AI agents that process personal data, make decisions, or interact with external systems need audit logs. Not โnice to haveโ logs. Compliance-grade audit trails that regulators can inspect.
Hereโs how to implement audit logging that meets GDPR, SOC 2, HIPAA, and EU AI Act requirements.
Why audit logging matters
Regulators are catching up to AI. The EU AI Act (effective 2025-2026) requires logging of high-risk AI systems. GDPR requires records of data processing. SOC 2 requires access controls and audit trails.
If your AI agent processes personal data, makes decisions affecting users, or operates in a regulated industry, you need audit logs.
What to log
Required events
| Event | Data Required | Retention |
|---|---|---|
| Agent start | Agent ID, task description, user ID, timestamp | 1-3 years |
| Model invocation | Model name, input tokens, output tokens, cost, timestamp | 1-3 years |
| Tool use | Tool name, input, output, success/failure, timestamp | 1-3 years |
| Data access | Data source, data type, purpose, timestamp | 1-3 years |
| Decision | Decision made, reasoning, confidence, timestamp | 1-3 years |
| Error | Error type, context, resolution, timestamp | 1-3 years |
| Agent end | Status, total cost, duration, output summary, timestamp | 1-3 years |
Optional but recommended
- Prompt and response (for debugging, may contain PII)
- Chain of thought (reasoning trace)
- User feedback (quality ratings)
- Performance metrics (latency, token usage)
Implementation
Log format
Use structured JSON logging:
import json
from datetime import datetime
class AgentAuditLogger:
def __init__(self, agent_id, user_id):
self.agent_id = agent_id
self.user_id = user_id
def log_event(self, event_type, data):
entry = {
'timestamp': datetime.utcnow().isoformat(),
'event_type': event_type,
'agent_id': self.agent_id,
'user_id': self.user_id,
'data': self.sanitize(data), # Remove PII if needed
}
self.write_to_log(entry)
self.send_to_monitoring(entry)
def sanitize(self, data):
"""Remove or mask PII from log data."""
sanitized = data.copy()
# Mask email addresses
if 'email' in sanitized:
sanitized['email'] = self.mask_email(sanitized['email'])
# Remove raw prompts (may contain PII)
if 'prompt' in sanitized:
sanitized['prompt_hash'] = self.hash(sanitized['prompt'])
del sanitized['prompt']
return sanitized
Storage
Audit logs must be:
- Append-only: No modification or deletion
- Tamper-evident: Hash chain or digital signatures
- Encrypted: At rest and in transit
- Retained: Per regulatory requirements (1-3 years)
class SecureAuditStorage:
def __init__(self, storage_backend):
self.backend = storage_backend
self.previous_hash = None
def write(self, entry):
# Add hash chain
entry['previous_hash'] = self.previous_hash
entry['hash'] = self.compute_hash(entry)
self.previous_hash = entry['hash']
# Write to append-only storage
self.backend.append(entry)
Querying
Audit logs need to be searchable for investigations and compliance reviews:
class AuditQuery:
def __init__(self, storage):
self.storage = storage
def query_agent(self, agent_id, start_date, end_date):
"""Get all events for a specific agent."""
return self.storage.query(
filter={'agent_id': agent_id, 'timestamp': {'$gte': start_date, '$lte': end_date}}
)
def query_data_access(self, data_type, start_date, end_date):
"""Get all data access events for a specific data type."""
return self.storage.query(
filter={'event_type': 'data_access', 'data.data_type': data_type, 'timestamp': {'$gte': start_date, '$lte': end_date}}
)
def query_decisions(self, user_id, start_date, end_date):
"""Get all decisions affecting a specific user."""
return self.storage.query(
filter={'event_type': 'decision', 'user_id': user_id, 'timestamp': {'$gte': start_date, '$lte': end_date}}
)
Compliance requirements
GDPR (Article 30)
Requirements:
- Records of processing activities
- Purpose of processing
- Categories of data subjects
- Retention periods
Implementation:
# Log data processing activities
audit_logger.log_event('data_processing', {
'purpose': 'customer_support',
'data_categories': ['email', 'name', 'support_ticket'],
'data_subjects': 'customers',
'retention_days': 365,
})
SOC 2 (Type II)
Requirements:
- Access controls
- Audit logging
- Change management
- Incident response
Implementation:
# Log access control events
audit_logger.log_event('access_control', {
'resource': 'customer_database',
'action': 'read',
'user_id': 'agent_123',
'result': 'allowed',
})
HIPAA
Requirements:
- PHI access logging
- Audit controls
- Integrity controls
- Transmission security
Implementation:
# Log PHI access
audit_logger.log_event('phi_access', {
'patient_id': 'masked_id',
'data_type': 'medical_record',
'purpose': 'treatment',
'user_id': 'agent_123',
})
EU AI Act (High-Risk Systems)
Requirements:
- Logging of AI system behavior
- Record-keeping for at least 6 months
- Traceability of decisions
- Human oversight documentation
Implementation:
# Log AI decisions
audit_logger.log_event('ai_decision', {
'decision': 'approve_loan',
'reasoning': 'credit_score_above_threshold',
'confidence': 0.92,
'human_review': False,
})
Retention policy
| Regulation | Minimum Retention | Recommended |
|---|---|---|
| GDPR | Duration of processing + 1 year | 3 years |
| SOC 2 | 1 year | 3 years |
| HIPAA | 6 years | 7 years |
| EU AI Act | 6 months | 2 years |
My take
Audit logging is not optional for production AI agents. The regulatory landscape is tightening, and the cost of non-compliance (fines, lawsuits, reputation damage) far exceeds the cost of implementation.
Start with the basics:
- Log every agent action (start, tool use, decision, end)
- Use structured JSON format
- Store in append-only, encrypted storage
- Retain for 3 years (covers most regulations)
Add compliance-specific logging (GDPR, HIPAA) as needed for your industry.
The biggest mistake is logging too little. You can always delete logs later. You canโt retroactively create logs for events you didnโt capture.
FAQ
Do I need audit logging for a single agent?
If the agent processes personal data, makes decisions affecting users, or operates in a regulated industry: yes. For internal-only agents with no sensitive data: itโs still recommended.
How long should I retain audit logs?
3 years covers most regulations. HIPAA requires 6 years. EU AI Act requires 6 months minimum. Check your specific regulatory requirements.
Can I store audit logs in the same system as the agent?
No. Audit logs should be stored separately, in append-only, tamper-evident storage. If the agentโs system is compromised, the audit logs must remain intact.
How do I handle PII in audit logs?
Mask or remove PII before logging. Hash prompts and responses. Use tokenization for identifiers. Never log raw personal data in audit logs.
Whatโs the minimum audit logging for SOC 2?
Access controls (who accessed what), change management (what changed), and incident response (what went wrong). Log every access, every change, and every incident.
Related Articles
- AI Agent Security
- AI Agent Governance Framework
- Production Agent Deployment Checklist
- AI Compliance Automation
- Agent Sandboxing at Scale