Skip to main content

AI Audit Trail SDK - Python

Drop-in SDK for logging AI/ML decisions with compliance tracking. Get GDPR and EU AI Act compliant audit trails in under 15 minutes.

Installation

pip install ai-audit-sdk

Quick Start

from ai_audit_sdk import AuditLogger
import openai

```python
# Initialize the logger with your API key
logger = AuditLogger(api_key="your_api_key")

For development and testing, use your sandbox API key:

# Development/Testing with sandbox tenant
logger = AuditLogger(api_key="your_sandbox_api_key")

Your existing OpenAI code

client = openai.OpenAI() response = client.chat.completions.create( model="gpt-4", messages=[{"role": "user", "content": "Hello, world!"}] )

Log the decision for compliance

logger.log_decision( input_text="Hello, world!", output_text=response.choices[0].message.content, model_name="gpt-4", metadata={ "user_id": "user123", "session_id": "session456" } )


## Features

- **Asynchronous Logging**: Won't slow down your application
- **Compliance Ready**: GDPR Article 22 and EU AI Act support
- **Multi-Model Support**: Works with any AI/ML model
- **Secure**: API key authentication with encrypted transmission
- **Reliable**: Built-in error handling and timeout management

## API Reference

### AuditLogger

Main class for logging AI decisions.

```python
logger = AuditLogger(api_key="your_key")

Production:

logger = AuditLogger(api_key="prod_api_key_xxx")

Development/Testing:

logger = AuditLogger(api_key="sandbox_api_key_xxx")

Note: All traffic goes through the production infrastructure at https://explainableai.azurewebsites.net. Tenant isolation is handled via API keys, not URLs.

Methods

log_decision(input_text, output_text, ...)

Log an AI decision asynchronously (recommended).

logger.log_decision(
    input_text="The input prompt",
    output_text="The AI response",
    model_name="gpt-4",  # optional, defaults to 'unknown'
    metadata={  # optional
        "user_id": "user123",
        "session_id": "session456",
        "tags": ["production", "chat"]
    },
    confidence=0.95,  # optional, 0.0 to 1.0
    response_time=1200,  # optional, in milliseconds
    provider="openai",  # optional
    model_version="2024-02-01",  # optional
    risk_level="low",  # optional: 'low', 'medium', 'high'
    prompt_tokens=100,  # optional
    completion_tokens=50,  # optional
    total_tokens=150,  # optional
    cost_micros=1000,  # optional, cost in millionths of currency unit
    external_ref="req_123",  # optional, your internal reference
    data_subject_id="user_123",  # optional, for GDPR compliance
    lawful_basis="consent",  # optional, GDPR lawful basis
    automated_decision=True,  # optional, GDPR Article 22
    redact_pii=False,  # optional, redact PII from stored data
    priority="normal"  # optional: 'low', 'normal', 'high'
)

Log an AI decision asynchronously (recommended).

Parameters:

  • input_text (str): The input prompt or data
  • output_text (str): The AI model's output
  • model_name (str): Name of the AI model used
  • metadata (dict): Additional context (user_id, session_id, etc.)
  • confidence (float): Model confidence score (0.0 to 1.0)
  • response_time (int): Response time in milliseconds
log_decision_sync(...)

Same as log_decision() but blocks until complete. Returns True if successful.

Simple Function

For one-off logging:

from ai_audit_sdk import log_ai_decision

log_ai_decision(
    api_key="your_key",
    input_text="prompt",
    output_text="response",
    model_name="gpt-4"
)

Configuration

Set your API key as an environment variable:

export AI_AUDIT_API_KEY="your_api_key"

Then use it in your code:

import os
from ai_audit_sdk import AuditLogger

logger = AuditLogger(api_key=os.getenv("AI_AUDIT_API_KEY"))

Error Handling

The SDK uses fire-and-forget async logging by default. Errors are logged to stdout but won't crash your application.

For critical applications, use synchronous logging:

success = logger.log_decision_sync(input_text, output_text)
if not success:
    # Handle logging failure
    print("Failed to log decision")

Examples

OpenAI Integration

import openai
from ai_audit_sdk import AuditLogger

logger = AuditLogger(api_key="your_audit_key")
client = openai.OpenAI(api_key="your_openai_key")

def get_ai_response(prompt, user_id):
    start_time = time.time()
    
    response = client.chat.completions.create(
        model="gpt-4",
        messages=[{"role": "user", "content": prompt}]
    )
    
    response_time = int((time.time() - start_time) * 1000)
    
    # Log for compliance
    logger.log_decision(
        input_text=prompt,
        output_text=response.choices[0].message.content,
        model_name="gpt-4",
        metadata={"user_id": user_id},
        response_time=response_time
    )
    
    return response.choices[0].message.content

Context Manager

with AuditLogger(api_key="your_key") as logger:
    logger.log_decision("input", "output", "model")
    # Logger automatically closes when exiting the context

License

MIT License

Metadata

Release files for ai-audit-sdk 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ai-audit-sdk 1.0.1
File Size Uploaded Provenance Actions
ai_audit_sdk-1.0.1.tar.gz 16.7 kB Without provenance Details for ai_audit_sdk-1.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ai-audit-sdk 1.0.1
File Interpreter ABI Platform Actions
ai_audit_sdk-1.0.1-py3-none-any.whl Python 3 none any Details for ai_audit_sdk-1.0.1-py3-none-any.whl

Total release size: 31.0 kB

Release files / ai_audit_sdk-1.0.1.tar.gz

Download URL ai_audit_sdk-1.0.1.tar.gz
Size 16.7 kB
Tags Source
SHA-256 checksum
How to use checksums
68003a76153a9364ec99c8024b28015e40a726e88e95d1bd3d8b41d9a34fe8a8
BLAKE2b-256 checksum
How to use checksums
92b0d701606eba92a35378f97cae84f8e8716f46bf7397c8a97fd79ac4cf282a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.6

Release files / ai_audit_sdk-1.0.1-py3-none-any.whl

Download URL ai_audit_sdk-1.0.1-py3-none-any.whl
Size 14.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2928cfe4cc8f8698358427b21aeb724c8318dfcea77ce2e49e2a3a5b03ac01cb
BLAKE2b-256 checksum
How to use checksums
8fa5ab921e7da2848e5796ed4d507c5c8a650011c5da8832da6a2237ccc7f5b5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.6

Security for ai-audit-sdk 1.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page