Advanced

Audit Logging

Build comprehensive audit trails for ML systems that satisfy regulatory requirements, enable incident investigation, and provide accountability for AI-driven decisions.

What to Log in ML Systems

ML audit logging extends beyond traditional application logging. You need to capture the full lifecycle of data, models, and predictions:

CategoryWhat to LogWhy
Data EventsData access, modifications, deletions, exportsCompliance, breach investigation, data lineage
Training EventsTraining runs, hyperparameters, data splits, metricsReproducibility, model provenance
Model EventsRegistration, approval, deployment, rollbackChange management, accountability
Prediction EventsInputs, outputs, confidence scores, latencyFairness auditing, debugging, compliance
Access EventsAuthentication, authorization, role changesSecurity monitoring, insider threat detection

Prediction Auditing

For high-stakes AI decisions (credit, healthcare, hiring), you must be able to explain why a specific prediction was made:

Python - Prediction Audit Logger
import json, uuid, time

class PredictionAuditLogger:
    def log_prediction(self, request, response, model_info):
        audit_record = {
            "prediction_id": str(uuid.uuid4()),
            "timestamp": time.time(),
            "model_id": model_info["id"],
            "model_version": model_info["version"],
            "model_hash": model_info["sha256"],
            "input_hash": hash_input(request),
            "output": response["prediction"],
            "confidence": response["confidence"],
            "feature_importance": response.get("explanations"),
            "requester": request.authenticated_user,
            "latency_ms": response["latency"],
        }

        # Write to append-only, tamper-evident log
        self.audit_store.append(audit_record)

        return audit_record["prediction_id"]

Data Lineage Tracking

Data lineage traces how data flows from raw sources through transformations to model training and predictions:

  • Source tracking: Record where every piece of training data originated and when it was collected.
  • Transformation logging: Log every data transformation, cleaning step, and feature engineering operation.
  • Version control: Track dataset versions alongside model versions. Know exactly which data trained which model.
  • Deletion compliance: When data subjects exercise deletion rights (GDPR), lineage tracking identifies all models trained on that data.

Regulatory Requirements

EU AI Act

High-risk AI systems require detailed logging of inputs, outputs, and system behavior. Logs must be retained for the duration the system is in use and accessible to regulatory authorities.

GDPR

Requires logging of all personal data processing, the ability to explain automated decisions (Article 22), and the capability to delete data and retrain models on request.

SOC 2 / ISO 27001

Require comprehensive audit trails for all system access and changes. ML-specific controls must be mapped to these frameworks for certification.

Privacy in logging: Be careful not to log sensitive data (PII, health information) in prediction audit trails. Hash or tokenize sensitive inputs. Implement retention policies that automatically purge logs after the required period.

Ready to Go Deeper?

Live instructor-led courses from our partners. Affiliate disclosure.