Compliance-Ready Audit Trails for AI Agents
Created by Alex Galle-From | substrateagnostic
AgentLedger is an SDK for building tamper-evident, compliance-ready audit trails for AI agents in regulated industries. Think "OpenTelemetry for AI accountability."
Organizations deploying AI agents in regulated contexts face emerging accountability requirements:
- FINRA 4511/3110: Financial services must reproduce data "exactly as it was at specific points in time"
- EU AI Act Article 12: High-risk AI systems require automatic logging of events
- HIPAA: Healthcare AI must maintain audit trails with PHI protection
- SOC 2: Service organizations need evidence of AI governance controls
No uniform standardized logging exists.
npm install agentledger-core agentledger-openaiimport { createLedger } from 'agentledger-core';
import { createAuditedOpenAI } from 'agentledger-openai';
import OpenAI from 'openai';
// Create audit ledger
const ledger = createLedger({
orgId: 'acme-corp',
agentId: 'customer-service-agent',
environment: 'production',
compliance: ['SOC2', 'GDPR'],
});
// Wrap OpenAI client
const openai = new OpenAI();
const audited = createAuditedOpenAI(openai, { ledger });
// Start session
await ledger.start({ type: 'user', identifier: 'user_123' });
// All calls are automatically logged
const response = await audited.chat.completions.create({
model: 'gpt-4o',
messages: [{ role: 'user', content: 'Hello!' }],
});
// Close and sign
const log = await ledger.close();
console.log(`Merkle root: ${log.merkle_root}`);- Hash Chains: Every entry links to the previous via SHA-256
- Merkle Trees: Efficient range verification and tamper detection
- Ed25519 Signatures: Organization attestation on session close
- Session metadata (org, agent, environment, compliance contexts)
- Model calls (provider, version, parameters, tokens, cost)
- Tool invocations (inputs, outputs, duration, resources accessed)
- Decision points (options considered, selection, reasoning)
- Human approvals (approver, role, attestation)
- State snapshots (checkpoints, metrics)
- OpenAI SDK: Wrapper for chat completions with streaming support
- Anthropic SDK: Wrapper for messages API with tool use tracking
- LangChain: CallbackHandler for chains, agents, and retrievers
- Generic:
@auditeddecorator for any async function
- JSONL: Raw structured logs
- Splunk CIM: Common Information Model for Splunk
- Elastic ECS: Elastic Common Schema for Elasticsearch
- FINRA 4511: Financial services books and records format
- EU AI Act: Article 12 record-keeping schema
- InMemory: Development and testing
- FileSystem: Single-node production with write-ahead logging
- S3-Compatible: Distributed production with batched writes
Custom error classes for precise error handling:
LedgerNotInitializedError: Session not startedValidationError: Input validation failuresStorageError: Storage backend issuesChainVerificationError: Hash chain integrity failuresSignatureVerificationError: Signature verification failures
Built-in validation helpers:
validateString,validateNumber,validateBooleanvalidateUUID,validateHash,validateTimestampvalidateArray,validateObject,validateEnum
┌─────────────────────────────────────────────────────────────┐
│ Your AI Agent │
├─────────────────────────────────────────────────────────────┤
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │ OpenAI │ │ Anthropic │ │ LangChain │ │
│ │ Wrapper │ │ Wrapper │ │ Callback │ │
│ └──────┬───────┘ └──────┬───────┘ └──────┬───────┘ │
│ │ │ │ │
│ └─────────────────┼─────────────────┘ │
│ │ │
│ ┌──────▼───────┐ │
│ │ Ledger │ │
│ │ API │ │
│ └──────┬───────┘ │
│ │ │
│ ┌─────────────────┼─────────────────┐ │
│ │ │ │ │
│ ┌──────▼───────┐ ┌──────▼───────┐ ┌──────▼───────┐ │
│ │ Crypto │ │ Schema │ │ Storage │ │
│ │ (chains, │ │ (types, │ │ (memory, │ │
│ │ merkle, │ │ errors, │ │ file, │ │
│ │ ed25519) │ │ validation)│ │ s3) │ │
│ └──────────────┘ └──────────────┘ └──────────────┘ │
└─────────────────────────────────────────────────────────────┘
See ARCHITECTURE.md for detailed architecture documentation.
| Package | Description |
|---|---|
agentledger-core |
Types, crypto, storage, validation, errors, main API |
agentledger-openai |
OpenAI SDK integration |
agentledger-anthropic |
Anthropic SDK integration |
agentledger-langchain |
LangChain callback handler |
agentledger-cli |
Command-line verification and export |
# Verify chain integrity
npx agentledger-cli verify audit.jsonl
# Export to compliance format
npx agentledger-cli export audit.jsonl --format=finra_4511
# Replay session timeline
npx agentledger-cli replay audit.jsonl
# Show statistics and compliance score
npx agentledger-cli summary audit.jsonlAgentLedger Verification
──────────────────────────────────────────────────
ℹ Session: 550e8400-e29b-41d4-a716-446655440000
ℹ Organization: acme-financial
ℹ Agent: financial-advisor-v1
ℹ Entries: 47
Hash Chain Verification
✓ Chain integrity verified (47 entries)
Merkle Tree Verification
✓ Merkle root verified
Signature Verification
✓ Organization signature verified
Signed at: 2024-12-25T10:30:00.000Z
──────────────────────────────────────────────────
✓ All verifications passed
import { createLedger, hashContent } from 'agentledger-core';
const ledger = createLedger({
orgId: 'broker-dealer-xyz',
agentId: 'trading-advisor',
compliance: ['FINRA_4511', 'FINRA_3110'],
retentionDays: 2555, // 7 years
signingKeys: loadFromHSM(), // Production: use HSM
});
// Log trading recommendation with human approval
await ledger.logDecision({
decisionId: 'trade_rec_123',
category: 'routing',
optionsConsidered: [
{ option_id: 'buy_vti', description: 'Buy VTI', score: 0.8 },
{ option_id: 'hold', description: 'Hold position', score: 0.2 },
],
selectedOption: 'buy_vti',
reasoningHash: hashContent(reasoning),
humanReviewRequired: true,
});
await ledger.logApproval({
approverId: 'REP_67890',
approverRole: 'Registered Representative',
decisionRef: 'entry_uuid',
approvalType: 'APPROVE',
});import { createLedger } from 'agentledger-core';
const ledger = createLedger({
orgId: 'mercy-health',
agentId: 'triage-assistant',
compliance: ['HIPAA', 'SOC2'],
retentionDays: 2190, // 6 years
});
// Log with PII flags (content stored as hash only)
await ledger.storeContent({
contentType: 'completion',
parentEntryId: entryId,
content: clinicalNote,
containsPii: true,
piiTypes: ['medical', 'name'],
});import { createLedger } from 'agentledger-core';
const ledger = createLedger({
orgId: 'eu-corp-gmbh',
agentId: 'high-risk-classifier',
compliance: ['EU_AI_ACT', 'GDPR'],
});
// Export for regulatory submission
const euFormat = await ledger.export({ format: 'eu_ai_act' });class Ledger {
// Session management
start(initiatedBy, metadata?): Promise<string>
close(): Promise<AuditLog>
// Logging
logModelCall(params): Promise<ChainedEntry>
logToolInvocation(params): Promise<ChainedEntry>
logDecision(params): Promise<ChainedEntry>
logApproval(params): Promise<ChainedEntry>
snapshot(params): Promise<ChainedEntry>
storeContent(params): Promise<ChainedEntry>
// Verification
verify(): Promise<{ valid: boolean; errors: string[] }>
// Access
getEntries(start?, end?): Promise<ChainedEntry[]>
count(): Promise<number>
getSession(): SessionEnvelope
// Export
export(options): Promise<Buffer>
}type AuditEntry =
| ModelCall // LLM API calls
| ToolInvocation // Tool/function executions
| DecisionPoint // Agent routing decisions
| HumanApproval // Human-in-the-loop approvals
| StateSnapshot // Checkpoint captures
| ContentReference // External content storageimport {
LedgerNotInitializedError,
ValidationError,
isAgentLedgerError
} from 'agentledger-core';
try {
await ledger.logModelCall({...});
} catch (error) {
if (isAgentLedgerError(error)) {
console.error(`AgentLedger error: ${error.code} - ${error.message}`);
}
}- Key Management: Store signing keys in HSM/KMS for production
- PII Handling: Use
containsPiiflag and hash-only storage for sensitive data - Access Control: Implement RBAC for audit log access
- Retention: Configure
retentionDaysper compliance requirements - Tamper Evidence: Regularly verify chain integrity
- Append: O(1) with async batching for S3
- Verify: O(n) full chain, O(log n) with Merkle proofs
- Memory: ~200 bytes per entry (hashes only)
- Latency: <1ms per log entry (in-memory), <5ms (filesystem)
- Python SDK (
pip install agentledger) - AutoGen integration
- CrewAI integration
- Parquet export with DuckDB queries
- Real-time SIEM streaming (Kafka, Kinesis)
- Kubernetes operator for log aggregation
- Web dashboard for audit exploration
See CONTRIBUTING.md for development setup and guidelines.
Alex Galle-From
- Website: alexgallefrom.io
- GitHub: @substrateagnostic
MIT License - see LICENSE for details.
Built for the accountability cliff. When AI agent accountability becomes mandatory, you'll already be compliant.