How to beginner · 3 min read

OpenAI Enterprise audit logs

Quick answer
OpenAI Enterprise provides audit logs accessible via the GET /v1/audit/logs API endpoint to track usage and security events. Use your Enterprise API key with proper permissions to query logs programmatically for compliance and monitoring.

PREREQUISITES

  • Python 3.8+
  • OpenAI Enterprise API key with audit log access
  • pip install openai>=1.0

Setup

Install the official openai Python package and set your Enterprise API key as an environment variable for authentication.

bash
pip install openai>=1.0
output
Collecting openai
  Downloading openai-1.x.x-py3-none-any.whl (xx kB)
Installing collected packages: openai
Successfully installed openai-1.x.x

Step by step

Use the OpenAI Python SDK to call the audit logs endpoint. Specify filters like start_time and end_time to retrieve logs within a date range.

python
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

# Retrieve audit logs for the last 7 days
response = client.audit.logs.list(
    start_time="2026-03-25T00:00:00Z",
    end_time="2026-04-01T00:00:00Z",
    limit=50
)

for log_entry in response.data:
    print(f"Timestamp: {log_entry.timestamp}, Event: {log_entry.event_type}, User: {log_entry.user_id}")
output
Timestamp: 2026-03-30T14:22:10Z, Event: api_key_used, User: user_12345
Timestamp: 2026-03-29T09:15:43Z, Event: model_invoked, User: user_67890
... (up to 50 entries)

Common variations

You can paginate through audit logs using page_token from the response. Async calls are supported with httpx or custom clients. Filtering by event types or users helps narrow results.

python
import asyncio
import os
from openai import OpenAI

async def fetch_audit_logs():
    client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
    response = await client.audit.logs.list(
        start_time="2026-03-25T00:00:00Z",
        end_time="2026-04-01T00:00:00Z",
        limit=20
    )
    for log_entry in response.data:
        print(f"Event: {log_entry.event_type}, User: {log_entry.user_id}")

asyncio.run(fetch_audit_logs())
output
Event: api_key_used, User: user_12345
Event: model_invoked, User: user_67890
... (20 entries)

Troubleshooting

  • If you receive a 403 Forbidden error, verify your API key has Enterprise audit log permissions.
  • Empty responses may indicate no events in the specified time range; adjust start_time and end_time.
  • Ensure your system clock is synchronized to UTC to avoid time filter mismatches.

Key Takeaways

  • Use the client.audit.logs.list method with Enterprise API key to access audit logs.
  • Filter logs by time range and event type for targeted monitoring.
  • Paginate results using page_token to handle large log volumes.
  • Async SDK calls enable efficient log retrieval in concurrent applications.
  • Check API permissions and time filters if logs are missing or access is denied.
Verified 2026-04
Verify ↗