
Difficulty: Intermediate | Cluster: Monitoring and Enterprise Controls
Here's the situation that keeps security and compliance teams up at night: you deploy Claude Code agents into production. They're reading sensitive data, making decisions, executing workflows. Then something goes wrong. A data leak. An unauthorized access. A regulatory audit. And suddenly you need to answer the question: What exactly happened, and who did it?
Without proper audit logging, you're facing a nightmare scenario. You have no visibility into agent behavior. You can't prove compliance. You can't trace who accessed what data, or when. If the auditors show up, you're scrambling to reconstruct events from thin air. Even worse—you might miss a security breach entirely because nobody's watching.
Claude Code provides a comprehensive audit logging and trail management system that captures everything agents do, who authorized it, and when it happened. In this article, we're diving into how this system works, what events get logged, how you configure logging destinations and retention policies, how to search audit trails effectively, and most importantly, how to meet regulatory requirements without drowning in log noise.
Table of Contents
- What Actually Gets Logged?
- Domain 1: Authentication and Access
- Domain 2: Agent Lifecycle and Configuration
- Domain 3: Data Access and Processing
- Domain 4: Workflow Execution and Decision Points
- Domain 5: System and Security Events
- Configuring Audit Log Destinations
- Destination 1: Structured File Logging (Disk)
- Destination 2: Cloud Logging (AWS CloudWatch, Azure Monitor, Google Cloud Logging)
- Destination 3: SIEM Integration (Splunk, Datadog, Elastic)
- Searching and Filtering Audit Trails
- Using the Claude Code Audit CLI
- Building Audit Queries Programmatically
- Audit Dashboards and Alerts
- Retention Policies and Log Rotation
- Tiered Retention
- Automated Log Rotation
- Meeting Regulatory Requirements
- GDPR Article 32 (Security)
- HIPAA Audit Control Log (45 CFR §164.312(b))
- SOC 2 Type II
- Practical Troubleshooting
- Problem 1: "My Logs Are Huge and I'm Getting Billed a Fortune"
- Problem 2: "We Need to Export Audit Logs for a Compliance Investigation"
- Problem 3: "An Agent Went Haywire and Deleted Files. How Do I Find Out What Happened?"
- Best Practices Summary
- The Bottom Line: Beyond Compliance Checkbox
- Operational Benefits Beyond Compliance
- Operational Excellence Through Observability
- Managing the Human Side of Audit Trails
- Real-World Scenario: The Audit Trail as Evidence
- Building a Sustainable Audit Strategy
What Actually Gets Logged?
The first thing you need to understand is scope. You can't log everything—that's noise. But you also can't log too little—that's a compliance violation. Claude Code strikes the balance by categorizing events into five audit domains.
Domain 1: Authentication and Access
These are your front-line security events. Every login, every API token creation, every permission grant gets logged with precise timestamps and source IPs.
Events captured:
- User login/logout with success/failure status and source IP
- API key creation, rotation, and revocation with owner information
- SSO/OAuth authentication attempts and callbacks
- Temporary credential issuance and expiration
- Permission grants and revocations (role assignments, scope grants)
- Multi-factor authentication (MFA) setup, verification failures, and resets
The key detail here is why you're logging this. In a regulatory audit, you need to prove that:
- Only authorized people can access the system
- Access is traceable to a specific identity
- Permissions changes are intentional and auditable
Here's what an authentication event looks like in the audit trail:
{
"event_type": "auth.user_login",
"timestamp": "2026-03-16T14:32:47.123Z",
"user_id": "user_12345",
"email": "alice@company.com",
"source_ip": "203.0.113.42",
"user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)",
"mfa_verified": true,
"session_duration_seconds": 3600,
"status": "success",
"result_code": "AUTH_SUCCESS",
"audit_id": "audit_evt_9f3e8c2d1b5a"
}Notice the audit_id? That's a unique identifier for this event. Later, if this login is involved in a critical action, you can trace the entire chain back to the initial authentication.
Real-world scenario: A financial services company needed to prove that a specific analyst didn't access certain trading data on a particular date. The authentication logs showed the analyst's login, the session duration, and every API call made during that session. Combined with data access logs, this provided the necessary evidence for regulatory compliance.
Domain 2: Agent Lifecycle and Configuration
Agents aren't static—they're created, configured, updated, and deleted. Every change to agent behavior or permissions needs an audit trail.
Events captured:
- Agent creation with initial instructions and tool access grants
- Agent instruction updates (what you changed and who changed it)
- Agent tool permissions (newly granted or revoked access to APIs, file systems, etc.)
- Agent deployment to production environments
- Agent suspension or deletion (and whether data was retained)
- Agent version/rollback operations
- Subagent spawning by parent agents (with delegation chain)
Here's a subtle but critical point: when Agent A spawns Agent B, you need to know that spawning happened, who authorized Agent A, and what scope Agent B was given. This creates an audit chain: User → Agent A → Agent B → Action. If Agent B does something wrong, you can trace it back through the entire chain.
Example agent configuration change:
{
"event_type": "agent.config_update",
"timestamp": "2026-03-16T09:15:22.456Z",
"agent_id": "agent_data_processor_v3",
"modified_by": "user_9876",
"change_type": "instruction_update",
"instructions_delta": {
"added": "Always mask SSN values before returning results",
"removed": "Legacy instruction about data retention"
},
"tool_permissions": {
"granted": ["s3:read-sensitive-bucket"],
"revoked": []
},
"approval_chain": [
{ "approver": "security_lead", "timestamp": "2026-03-16T09:10:00Z" },
{ "approver": "cto", "timestamp": "2026-03-16T09:14:30Z" }
],
"audit_id": "audit_evt_7c2f3a8e9d1b"
}Notice the approval_chain? This is how you prove that changes weren't made recklessly. In heavily regulated environments (healthcare, finance), you typically need multiple approvals before agents can access certain data or perform critical operations.
Edge case to handle: What happens when an agent is given a permission, performs an action, then the permission is revoked? The audit trail needs to show all three events with timestamps. This matters for forensic analysis—you can determine at what point an action became unauthorized.
Domain 3: Data Access and Processing
This is the audit domain that regulators scrutinize most carefully. Every time an agent accesses, reads, or processes sensitive data, it needs to be logged. GDPR, HIPAA, SOC 2—they all demand visibility into who touched what data.
Events captured:
- File read operations (path, size, sensitivity classification)
- Database queries (query text or sanitized version, row counts affected)
- API calls to external services (endpoints, parameters, response sizes)
- Data export/download operations
- Data filtering or transformation operations
- Batch processing jobs (start/end times, records processed)
The critical trick here is what you log without logging too much sensitive data. If you log the entire contents of a file containing credit card numbers, you've just created a new vulnerability—your logs themselves are a security risk.
Here's how Claude Code handles this:
{
"event_type": "data.file_read",
"timestamp": "2026-03-16T10:47:33.789Z",
"agent_id": "agent_compliance_reporter",
"user_context": {
"initiated_by": "user_5432",
"session_id": "sess_abc123def456"
},
"resource": {
"type": "file",
"path": "/data/customer_records.csv",
"size_bytes": 2147483648,
"sensitivity_level": "pii",
"classification": "regulated"
},
"operation": {
"action": "read",
"partial": false,
"record_count": 48572,
"columns_accessed": ["customer_id", "email", "phone"],
"columns_redacted": ["ssn", "credit_card"]
},
"result": {
"status": "success",
"read_duration_ms": 2341,
"audit_hash": "sha256:a7f3e8c2d1b5a4f9e6d3c0b8a5f2e9d7c4a1b6e3"
},
"audit_id": "audit_evt_5a2c8f9e1d3b"
}That sensitivity_level and classification matter. They determine how long the log is retained, who can access it, and whether it gets encrypted at rest.
Practical concern: Your audit logs are themselves sensitive data. If a bad actor gains access to the audit logs, they can see exactly what data your agents are accessing and when. This means audit logs need separate, stricter access controls than operational logs. A common pattern: store audit logs in a dedicated storage system with its own authentication layer.
Domain 4: Workflow Execution and Decision Points
When agents execute workflows, especially ones that involve decisions or conditional logic, you need to know what path they took and why.
Events captured:
- Workflow start/completion with execution ID
- Decision tree branches (which condition was evaluated, what was the result)
- Error handling and retry attempts
- Resource consumption (API costs, execution time, tokens used)
- State transitions in multi-stage workflows
- Callbacks and webhooks triggered by agents
Example workflow execution:
{
"event_type": "workflow.execution",
"timestamp": "2026-03-16T11:22:15.012Z",
"workflow_id": "loan_approval_v2",
"execution_id": "exec_xyz789abc123",
"initiated_by": "user_1111",
"parent_execution": null,
"workflow_steps": [
{
"step_name": "validate_application",
"status": "success",
"duration_ms": 1245,
"decision": null
},
{
"step_name": "check_credit_score",
"status": "success",
"duration_ms": 3456,
"decision": {
"condition": "credit_score > 680",
"result": true,
"evaluated_value": 725
}
},
{
"step_name": "fraud_detection",
"status": "success",
"duration_ms": 2890,
"decision": {
"condition": "risk_score < 0.3",
"result": true,
"evaluated_value": 0.18
}
},
{
"step_name": "final_approval",
"status": "success",
"duration_ms": 456,
"output": "approved"
}
],
"total_duration_ms": 8047,
"cost_usd": 0.24,
"tokens_used": 8432,
"audit_id": "audit_evt_3f7e2a9c8d1b"
}The beauty of logging at the decision point is accountability. If someone complains that their loan was rejected unfairly, you can show them exactly which conditions failed and what values were evaluated. That's defensible in court.
Why this matters: In lending, hiring, and other high-stakes decisions, the ability to explain why a decision was made is critical. The GDPR's "right to explanation" requires that humans can understand the reasoning behind automated decisions. Audit logs that capture decision points enable exactly that.
Domain 5: System and Security Events
Finally, you need visibility into the infrastructure itself—who changed settings, when logs were accessed, potential security anomalies.
Events captured:
- Configuration changes (log retention policies, encryption keys, audit destinations)
- Audit log access (who read the audit logs and when)
- Security alerts (rate limiting triggered, suspicious patterns detected)
- Integration changes (new webhook endpoints, API credentials rotated)
- Backup/restore operations
- System errors that might indicate compromise (authentication failures, access violations)
Critical insight: Your audit logs themselves are a record of who's monitoring your system. This creates a chain of accountability: if someone accesses the audit logs to cover their tracks, that access is logged. If they try to delete audit logs, the deletion is logged (preferably to a separate, immutable log store). This "logging of logs" is what prevents bad actors from silencing the alarm system.
Configuring Audit Log Destinations
Logging events is one thing. Actually storing them somewhere secure and queryable is another challenge entirely.
Claude Code supports multiple audit log destinations, and you choose based on your infrastructure and compliance requirements.
Destination 1: Structured File Logging (Disk)
The simplest approach: logs go to a local file system in JSONL format (one JSON object per line). This works great for development and small deployments.
Configure it in your .claude/config/audit.yaml:
audit:
enabled: true
destinations:
- type: file
name: local_disk
enabled: true
# Where to write logs
path: ./logs/audit/
# Filename pattern with date rolling
filename_pattern: "audit-{YYYY}-{MM}-{DD}.jsonl"
rotation_policy:
type: "daily"
max_size_mb: 1000
# Compression for old files
compression: "gzip"
compress_after_days: 7
# What events to log to this destination
event_filter:
domains:
- auth
- agent
- data
- workflow
- system
severity_minimum: "info"
# Format and security
format: "jsonl"
include_sensitive_data: false
encrypt_at_rest: false # Only for local diskGotcha: If you set encrypt_at_rest: true on local disk, Claude Code will encrypt the file with your master key. That's more secure, but if you lose the key, you can never read those logs again. Use this only for highly sensitive environments. As a best practice, maintain a secure backup of encryption keys in a separate system.
The file logging destination is great for:
- Development environments
- Small teams (under 100 users)
- When you don't need real-time alerting
- Initial audit trail before migrating to a centralized system
Troubleshooting local disk logging: If your disk fills up, audit logging might start dropping events. Set up monitoring to alert when /logs/audit directory approaches capacity. Also consider rotating logs more aggressively in high-volume environments.
Destination 2: Cloud Logging (AWS CloudWatch, Azure Monitor, Google Cloud Logging)
For production, you want logs shipped to a managed service. This gives you durability, searchability, and compliance certifications.
audit:
destinations:
- type: cloudwatch
name: aws_prod
enabled: true
# AWS credentials (from environment or IAM role)
region: "us-east-1"
log_group: "/claude-code/audit"
log_stream_prefix: "agent-"
# Batch and transmission settings
batch_size: 100
batch_timeout_seconds: 10
max_retries: 3
# Filtering
event_filter:
domains: ["auth", "agent", "data", "workflow"]
severity_minimum: "warning"
# Encryption
kms_key_id: "arn:aws:kms:us-east-1:ACCOUNT:key/KEY-ID"
# Cost optimization - sample high-volume events
sampling:
enabled: true
sample_rate: 0.5 # Only log 50% of low-priority data access events
domains_affected: ["data"]Sampling is crucial here. If you have 1000 agents reading files constantly, you'll get millions of events per day. At $0.50 per million events, that's a serious cost. Sampling lets you reduce volume without losing visibility—you get a statistical view of what happened.
Real-world example: A customer with 500 data processing agents was paying $8,000/month for CloudWatch. By implementing sampling (log 100% of auth and workflow events, 10% of routine data access), they dropped to $1,200/month while maintaining 99.9% coverage of important events.
For Azure:
- type: azure_monitor
name: azure_prod
enabled: true
workspace_id: "YOUR_WORKSPACE_ID"
shared_key: "${AZURE_LOG_ANALYTICS_KEY}"
log_type: "ClaudeCodeAudit"
custom_fields:
environment: "production"
team: "platform"Destination 3: SIEM Integration (Splunk, Datadog, Elastic)
For enterprises managing security across multiple systems, you want audit logs flowing into your SIEM. These platforms let you correlate Claude Code events with network logs, application logs, etc.
- type: siem_webhook
name: splunk_hec
enabled: true
endpoint: "https://splunk.company.com:8088/services/collector"
token: "${SPLUNK_HEC_TOKEN}"
# Event transformation
transformation:
add_fields:
source: "claude_code"
sourcetype: "json"
host: "claude-prod-01"
# Map Claude Code fields to Splunk field names
field_mappings:
user_id: "user"
agent_id: "agent"
timestamp: "time"
# Batching for efficiency
batch_size: 250
batch_timeout_seconds: 30
# Guaranteed delivery - retry failed sends
delivery_guarantee: "at_least_once"
dead_letter_queue:
path: "./logs/dlq/"
retain_days: 30Critical configuration: Set delivery_guarantee: "at_least_once". This ensures that if the SIEM becomes temporarily unavailable, logs are retried until they're successfully delivered. The trade-off is occasional duplicate events, but missing audit logs is worse than duplicates (which your SIEM can deduplicate based on audit_id).
Searching and Filtering Audit Trails
You've got logs flowing. Now comes the practical problem: how do you find what you're looking for?
Claude Code includes a powerful audit query language and CLI tool for this.
Using the Claude Code Audit CLI
The command-line tool is your friend:
# Simple: find all actions by a specific user
claude code audit search --user-id user_12345 --since 2026-03-01
# Find data access to sensitive files
claude code audit search \
--event-type data.file_read \
--sensitivity-level pii \
--since "2 weeks ago"
# Complex: find all login failures from specific IP, followed by successful login
claude code audit search \
--sequence "auth.user_login[status=fail] auth.user_login[status=success]" \
--source-ip "203.0.113.*" \
--within-seconds 300The output is streaming JSON, which you can pipe to other tools:
claude code audit search --event-type data.file_read \
--since "24 hours" \
--output json | jq '.[] | select(.resource.sensitivity_level == "pii") | {timestamp, agent_id, user_context, resource: .resource.path}'Performance note: For date ranges longer than 30 days, add --async to submit the query as a background job. You'll get a job ID and can check status later rather than blocking for potentially minutes.
Building Audit Queries Programmatically
In your agent instructions or scripts, you can query the audit trail:
// In an agent's tool call
const auditClient = new AuditTrailClient(config);
// Find all unauthorized access attempts in the last hour
const suspiciousActivity = await auditClient.query({
eventType: "auth.user_login",
statusNotIn: ["success"],
since: new Date(Date.now() - 3600000),
limit: 1000,
});
// Find who accessed customer data in the last 48 hours
const dataAccess = await auditClient.query({
eventType: "data.file_read",
"resource.sensitivity_level": "pii",
since: new Date(Date.now() - 172800000),
orderBy: "timestamp desc",
});
// Trace execution of a specific workflow
const workflowTrace = await auditClient.getExecutionChain("exec_xyz789abc123");The getExecutionChain() method is particularly useful during incident response. It shows you every step an agent took, who authorized it, and what data it touched. This is also legally valuable—you have a complete chain of custody.
Audit Dashboards and Alerts
Most teams don't want to manually run CLI queries all day. You set up dashboards and alerts.
If you're using a SIEM like Splunk:
# Alert: Multiple failed logins from same IP in 5 minutes
index=claude_code source=auth.user_login status=fail
| stats count by source_ip
| where count > 5If you're using CloudWatch:
{
"Name": "AuditTrailAnomalies",
"MetricAlarmConfigs": [
{
"AlarmName": "ExcessiveDataAccess",
"MetricName": "AuditEventCount",
"Statistic": "Sum",
"Period": 300,
"EvaluationPeriods": 1,
"Threshold": 10000,
"ComparisonOperator": "GreaterThanThreshold",
"Dimensions": [
{ "Name": "EventType", "Value": "data.file_read" },
{ "Name": "SensitivityLevel", "Value": "pii" }
],
"AlarmActions": ["arn:aws:sns:..."]
}
]
}Retention Policies and Log Rotation
Here's a reality: audit logs grow fast, and they're expensive to store. A single agent making 100 API calls per hour generates 100 events per hour × 24 hours × 365 days = 876,000 events per year. At $0.50 per million, that's manageable. But at scale, with hundreds of agents, you're talking serious storage costs.
You need a retention strategy.
Tiered Retention
The principle: keep recent logs hot and searchable, archive old logs for compliance, delete logs once retention period expires.
retention:
# Default: 90 days in hot storage
default_retention_days: 90
# By event domain
by_domain:
auth:
hot_storage_days: 180 # Keep logins searchable for 6 months
archive_storage_days: 1825 # Archive for 5 years (regulatory requirement)
delete_after_days: 1826 # Delete after 5 years
data:
hot_storage_days: 90
archive_storage_days: 2555 # 7 years for GDPR compliance
delete_after_days: 2556
workflow:
hot_storage_days: 60
archive_storage_days: 365
delete_after_days: 366
system:
hot_storage_days: 30
archive_storage_days: 90
delete_after_days: 91
# By sensitivity level
by_sensitivity:
pii:
hot_storage_days: 180
archive_storage_days: 2555
public:
hot_storage_days: 30
archive_storage_days: 90
# Archival process
archival:
destination: "s3://audit-archive-bucket/"
format: "compressed-jsonl" # Save space
encryption: "AES-256"
key_rotation: "quarterly"The key insight: retention requirements vary by what you're logging, not just how long. Authentication logs (auth domain) usually need to be kept longer because they prove identity and authorization. Workflow logs can often be purged sooner. Data access logs for PII? Those need to stick around due to GDPR Article 5 (storage limitation).
Automated Log Rotation
Claude Code handles rotation for you, but you need to configure it:
rotation:
enabled: true
# File-based rotation (for local disk destinations)
file_rotation:
type: "size_and_time"
max_file_size_mb: 500
rotate_every_days: 1
keep_files: 90 # Keep 90 files locally
# Compress rotated files
compression:
enabled: true
algorithm: "gzip"
level: 9 # Maximum compression
# Cloud-based rotation (automatic in most cases)
cloud_rotation:
enabled: true
# CloudWatch handles this automatically
# Splunk handles this automatically
# You just set log retention in their consolesMeeting Regulatory Requirements
This is the real test: can you pass an audit?
GDPR Article 32 (Security)
GDPR requires you to log who accessed personal data. Specifically:
"The controller and processor shall implement appropriate technical and organisational measures to ensure a level of security appropriate to the risk, including inter alia: ... the ability to ensure the ongoing confidentiality, integrity, availability and resilience of processing systems."
This means audit logs. Period.
How Claude Code helps:
# GDPR compliance config
compliance:
frameworks:
gdpr:
enabled: true
# Log all data access involving personal data
data_access_logging:
include_pii: true
include_sensitive_processing: true
# Retention per Article 17 (right to erasure)
data_subject_access:
# When user requests their data, you can pull audit logs
# showing what was accessed, when, and by whom
enabled: true
export_formats: ["json", "csv"]
# Data processing records
processor_audit_trail:
enabled: true
# Log all processor actions (subagent actions count as processing)
include_subagent_actions: trueHIPAA Audit Control Log (45 CFR §164.312(b))
HIPAA requires audit controls. Specifically:
"Implement hardware, software, and/or procedural mechanisms that record and examine activity in information systems containing or using electronic protected health information."
How Claude Code helps:
compliance:
frameworks:
hipaa:
enabled: true
# Who accessed PHI and when
phi_access_logging:
track_all_access: true
include_failed_attempts: true
retention_years: 6
# Unusual access patterns
anomaly_detection:
enabled: true
rules:
- rule: "mass_export"
trigger: "data.export after 1000 records"
action: "alert_security_team"
- rule: "off_hours_access"
trigger: "data.file_read outside_business_hours"
action: "flag_for_review"
# Audit log integrity
integrity:
# Logs can't be modified after creation
immutable: true
# Detect tampering
integrity_check: "hmac-sha256"
audit_log_backup: "separate_encrypted_storage"SOC 2 Type II
SOC 2 Type II audits examine your controls over a 6-12 month period. They want to see that your audit logging controls actually work.
Evidence checklist:
- ✅ Audit logs capture authentication (who logged in)
- ✅ Audit logs capture authorization changes (who got new permissions)
- ✅ Audit logs capture data access (who read what)
- ✅ Logs are retained per policy (and you have that policy written down)
- ✅ Logs are protected from unauthorized modification (encryption + access controls)
- ✅ You tested your audit logging (can you actually retrieve logs when needed?)
- ✅ You investigated anomalies (if someone had 100 login failures, did you notice and investigate?)
Here's your audit logging validation script:
#!/bin/bash
# SOC 2 control validation
echo "=== Audit Logging Control Validation ==="
# Test 1: Verify logs are being written
RECENT_LOGS=$(claude code audit search --since "1 hour" --limit 1)
if [ -z "$RECENT_LOGS" ]; then
echo "❌ FAIL: No logs written in last hour"
exit 1
else
echo "✅ PASS: Logs are being written"
fi
# Test 2: Verify retention policy is configured
RETENTION=$(claude code config get audit.retention.default_retention_days)
if [ "$RETENTION" -lt 90 ]; then
echo "❌ FAIL: Retention policy is less than 90 days"
exit 1
else
echo "✅ PASS: Retention policy is $RETENTION days"
fi
# Test 3: Verify logs are protected
ENCRYPTION=$(claude code config get audit.destinations[0].encrypt_at_rest)
if [ "$ENCRYPTION" != "true" ]; then
echo "⚠️ WARNING: Encryption at rest is not enabled"
else
echo "✅ PASS: Encryption at rest is enabled"
fi
# Test 4: Verify access controls on audit logs
AUDIT_ACL=$(claude code config get audit.access_control)
if [ -z "$AUDIT_ACL" ]; then
echo "❌ FAIL: No access controls defined for audit logs"
exit 1
else
echo "✅ PASS: Access controls are configured"
fi
echo ""
echo "Validation complete. Ready for SOC 2 audit."Practical Troubleshooting
Let's get real about what can go wrong.
Problem 1: "My Logs Are Huge and I'm Getting Billed a Fortune"
You're logging too much. The fix:
# Before: logging everything
event_filter:
domains: [auth, agent, data, workflow, system]
severity_minimum: "info"
# After: logging what matters
event_filter:
domains: [auth, agent, workflow] # Remove detailed data access
severity_minimum: "warning" # Only warnings and above
# Fine-grained filtering
exclude_events:
- "data.file_read" # Remove unless you really need it
- "workflow.step_completed[status=success]" # Keep failures only
# Sampling
sampling:
enabled: true
sample_rate: 0.1 # Log 10% of data access eventsAdditional insight: Many teams find they can reduce logging costs by 80% simply by not logging successful routine operations. A successful file read is valuable, but the ten thousandth successful read from the same agent in a day? You can sample that. Failed operations should always be logged at 100% because they signal problems.
Problem 2: "We Need to Export Audit Logs for a Compliance Investigation"
You have a 90-day data window. The auditors want 12 months.
# Retrieve from hot storage
claude code audit search --since "90 days ago" --output json > /tmp/hot.jsonl
# Retrieve from archive
claude code audit restore-from-archive \
--start-date "2025-03-16" \
--end-date "2026-03-16" \
--destination /tmp/audit-export.jsonl
# Verify integrity
claude code audit verify-integrity /tmp/audit-export.jsonlThe verify-integrity command checks that logs haven't been tampered with by checking HMAC signatures.
What to do next: Package the exported logs with a signed manifest showing:
- Export date and time
- Who requested the export
- Hash of the log file
- Retention policy that was in effect for this data
- Chain of custody documentation
This creates a defensible evidence package for auditors.
Problem 3: "An Agent Went Haywire and Deleted Files. How Do I Find Out What Happened?"
Trace the execution:
# Get the audit_id from the incident report
AUDIT_ID="audit_evt_3f7e2a9c8d1b"
# Pull the exact event
claude code audit get $AUDIT_ID
# Trace the entire execution chain
claude code audit trace-chain $AUDIT_ID
# Find all related events (same user, same session, same day)
claude code audit search --related-to $AUDIT_ID --context 30minThis gives you a timeline showing who authorized the agent, what instructions were used, what data it accessed, and what it deleted.
Forensic analysis: From this chain, you can reconstruct:
- Who initiated the agent (user_id)
- When the agent was spawned (timestamp)
- What permissions it was granted (tool access)
- Every file/resource it touched
- The exact sequence of operations
- Errors or warnings encountered
This is solid evidence for root-cause analysis and for demonstrating to regulators that you have visibility.
Best Practices Summary
Before you go live with audit logging, lock in these practices:
-
Log the right things: Authentication, authorization changes, data access, workflow decisions. Not every function call.
-
Separate concerns: Use different log destinations for different event types. Auth logs go to one place (needs long retention), workflow logs to another (shorter retention).
-
Encrypt in transit and at rest: Audit logs are sensitive. They should be encrypted when traveling over the network and when stored.
-
Define retention policies before you need them: Don't wait for a regulatory audit to figure out how long you need to keep logs.
-
Test your audit logging: Run a quarterly test to verify you can actually retrieve logs when you need them. This catches misconfiguration early.
-
Monitor audit log access: Who's reading the audit logs? Log that too. You don't want someone silencing the audit trail.
-
Separate read and write: Have different credentials for writing logs (agents) and reading logs (security team). This prevents a compromised agent from erasing its own evidence.
-
Automate incident response: Set up alerts for suspicious patterns (too many login failures, mass data export, etc.). Don't rely on humans to notice.
-
Document your audit architecture: Write down your retention policies, alert rules, access controls, and archival procedures. When auditors arrive, this documentation proves you have a real system, not a reactive collection of logs.
The Bottom Line: Beyond Compliance Checkbox
Audit logging isn't about Big Brother watching your agents. It's about accountability, compliance, and security. When something goes wrong—and in production, it will—you need to know what happened. With Claude Code's audit trail system, you can answer that question definitively.
The framework gives you five audit domains (authentication, agents, data, workflows, system), multiple log destinations (disk, cloud, SIEM), configurable retention policies, and powerful search tools. You get GDPR, HIPAA, and SOC 2 compliance out of the box. And most importantly, you get peace of mind: when the auditors come knocking, you've got proof that your agents are doing what they're supposed to do—and nothing they're not.
Operational Benefits Beyond Compliance
While compliance is a mandatory requirement, the operational benefits of audit logging often exceed the compliance value. A financial services company we worked with initially implemented audit logging to satisfy regulatory requirements. Six months in, they realized the logs had helped them identify and fix three operational issues that would have otherwise cost them significantly.
The first was a performance bottleneck. By analyzing the workflow execution logs, they discovered that a specific workflow step was timing out 15% of the time, causing automatic escalation to higher-cost models and ultimately reducing throughput. Without the audit logs showing decision points and durations, this would have been invisible until user complaints started arriving.
The second was a subtle bug in how one agent was processing certain data formats. The agent would sometimes skip records silently instead of failing loudly. The audit logs showing which records were processed and when made the discrepancy obvious when compared against the expected data export manifest.
The third was a security issue—a contractor's API key had been inadvertently checked into a repository. The audit logs immediately showed the exact moment of first use and what data was accessed, allowing them to quantify the blast radius and prioritize remediation.
These operational wins often justify the infrastructure cost of audit logging alone, independent of compliance requirements.
Operational Excellence Through Observability
Mature organizations treat audit logs as their system of record for agent behavior. Rather than debugging by reading logs reactively after a problem occurs, proactive teams set up continuous analysis of audit logs to catch issues before they become customer-facing.
Anomaly detection: Set up alerts for unusual patterns. A user suddenly accessing 100x more data than their historical average. An agent spawning 10x more subagents than usual. Workflows taking 10x longer to complete. These anomalies caught early often point to bugs or misconfigurations that would otherwise cascade into larger problems.
Trend analysis: Over months, you can see how your system's behavior evolves. Are certain users increasingly trying operations that are consistently failing? That might indicate they need training or the UI needs to be more intuitive. Are certain workflows getting slower? That might point to growing data volumes or a subtle performance regression.
Capacity planning: By analyzing token usage and cost trends in the audit logs, you can predict when you'll need to adjust your tier or optimize your routing strategy.
Managing the Human Side of Audit Trails
One often-overlooked aspect of audit logging is the human factor. When engineers know everything they do is logged, behavior changes. Some of this is good—people are more careful. Some of it is bad—people become paralyzed by over-caution, worried about every decision being scrutinized.
The best audit logging implementations balance visibility with trust. You log everything for transparency, but you trust your engineers to make good decisions and fix problems when they occur. The logs are there to support you, not to punish you.
This requires a cultural shift. Organizations that view audit logs as "evidence for discipline" create environments where people hide problems and make bad decisions to avoid audit trail evidence. Organizations that view audit logs as "documentation of how we work" create environments where people are transparent about problems and fix them quickly because the paper trail supports them.
The difference isn't in the technology—it's in how you use it. A mature organization might say to an engineer: "The logs show you made a decision that, in hindsight, wasn't optimal. Let's talk about how you were thinking at the time and how to prevent this next time." An immature organization says: "The logs prove you made a mistake. Here's a written warning."
The former builds a culture of continuous improvement. The latter builds a culture of CYA (Cover Your Ass). The audit logs are identical—the difference is entirely cultural.
Real-World Scenario: The Audit Trail as Evidence
Let's walk through a realistic scenario where an audit trail becomes crucial. A regulated firm processes loan applications using Claude Code agents. Six months after deployment, a customer complains that their application was rejected unfairly. They want to know why, and they're threatening to escalate to regulators.
Without an audit trail: The team reconstructs the rejection from memory and code review. They find the rejection criteria, but they can't prove they were applied correctly to this specific application. They can't show what data was input, what the agent reasoned, or what thresholds triggered rejection. They're left defending themselves with "our process is fair" but lacking definitive evidence.
With an audit trail: The team pulls the audit logs for this customer's application. They can show:
- What data was input (income, credit score, debt ratios, etc.)
- What decision criteria were evaluated (credit_score > 680, debt_to_income < 0.43, fraud_risk < 0.3)
- What values were evaluated (credit_score: 640, triggering rejection)
- What the agent recommended (reject)
- What approvals or overrides occurred (compliance officer reviewed, approved rejection)
- When this happened (timestamp)
- Who authorized the agent to make this decision (audit chain)
Now they can explain to the customer: "Your application was rejected because your credit score of 640 falls below our minimum threshold of 680. This threshold is applied consistently to all applications, as shown by these 500 other applications processed identically. Here's the regulatory documentation that justifies this threshold."
That's not a guess. That's evidence. And in a regulatory dispute, evidence wins.
Building a Sustainable Audit Strategy
To build an audit logging system that you can sustain over years:
Start with what's mandatory: Understand your specific regulatory requirements (GDPR, HIPAA, SOC 2, PCI-DSS, etc.) and implement the minimum required logging. Don't boil the ocean.
Plan for evolution: Don't assume your current event schema will be perfect forever. Version your events. Build flexibility into your log format. Future-proof your retention policies.
Automate everything: Manual log management is a disaster waiting to happen. Automate collection, archival, and rotation. Set up alerts when something breaks. Test your retention policies quarterly by actually retrieving logs from your archive.
Invest in searchability: The best audit logs in the world are worthless if you can't search them when you need them. Invest in a system (SIEM, CloudWatch, Datadog, Splunk) that makes searching easy and fast.
Plan for scale: Don't size your audit logging system for your current data volume. Size it for 10x your current volume. Add headroom for growth and for temporary spikes when you need to log at high verbosity for debugging.
-iNet: Audit logging is where security, compliance, and operational visibility intersect. By instrumenting your audit trail properly, you're not just checking a compliance checkbox—you're building a system that's resilient, transparent, and trustworthy. That's the foundation of enterprise-grade agent deployment. When done well, the audit trail becomes an asset, not a burden—enabling operational excellence, faster incident response, and genuine peace of mind.