Imagine you are running a business that uses artificial intelligence to automate customer support, generate content, or analyze data. Every time your application sends a request to an AI model, you want to keep a detailed record of what happened, why it happened, and what the result was. This record-keeping system is called an "audit trail," and it is one of the most important practices for anyone working with AI APIs in production environments.
In this comprehensive guide, I will walk you through everything you need to know about implementing audit trails for AI API usage, starting from absolute zero knowledge. Whether you are a small business owner, a junior developer, or a technical writer who needs to understand the concepts, this tutorial will give you hands-on experience with real code examples that you can copy, paste, and run immediately. By the end of this guide, you will have a fully functional audit trail system running on the HolySheep AI platform, which offers remarkable cost savings with rates of just $1 for every ¥1 spent, representing an 85% reduction compared to competitors charging ¥7.3, plus support for WeChat and Alipay payments, sub-50ms latency performance, and generous free credits upon registration.
What is an Audit Trail and Why Do You Need One?
Let me explain the concept in simple terms. Think of an audit trail as a detailed diary that records every conversation your application has with an AI service. When you send a message to an AI model asking it to help with a customer inquiry, the audit trail will automatically capture the exact time the message was sent, what the message contained, which AI model processed it, how long the processing took, what the AI responded with, and whether everything worked correctly or if there were any errors.
This becomes critically important for several reasons that directly impact your business operations and compliance requirements.
Compliance and Regulatory Requirements
If your business operates in healthcare, finance, legal services, or any industry with strict regulatory oversight, you likely need to demonstrate that AI decisions were made fairly, transparently, and without bias. An audit trail provides the documentary evidence required by auditors and regulators. For example, if an AI system denies a loan application or recommends a medical treatment, regulators may require you to show exactly what information the AI considered and how it reached that conclusion.
Debugging and Troubleshooting
When something goes wrong with your AI-powered application, the audit trail becomes your best friend. Instead of guessing what happened, you can look through the detailed logs to see exactly what request was sent, what response was received, and where the problem occurred. This can reduce debugging time from hours to minutes, saving your development team countless hours of frustration.
Cost Optimization
AI API calls can add up quickly, and understanding your usage patterns helps you optimize costs. The HolySheep AI platform offers transparent pricing with 2026 rates as follows: GPT-4.1 at $8 per million tokens, Claude Sonnet 4.5 at $15 per million tokens, Gemini 2.5 Flash at $2.50 per million tokens, and DeepSeek V3.2 at just $0.42 per million tokens. By tracking which models are being used, how frequently they are called, and what volume of tokens is consumed, you can identify opportunities to switch to more cost-effective models or optimize your prompts to reduce token usage.
Security Monitoring
Audit trails help you detect unusual patterns that might indicate security threats, such as someone trying to manipulate your AI system, excessive API usage that might indicate credential theft, or requests containing potentially malicious content designed to trick your AI into revealing sensitive information.
Prerequisites: What You Need Before Getting Started
Before we dive into the code, let me ensure you have everything necessary to follow along with this tutorial. I will assume you have no prior experience with APIs, so we will start from the very basics.
Tools You Will Need
- A HolySheep AI Account: If you do not already have one, sign up here to create your free account and receive complimentary credits to start experimenting.
- Python Installation: Python is a popular programming language that we will use throughout this tutorial. If you are on Windows, download Python from python.org. If you are on Mac, Python usually comes pre-installed, but you can verify by opening Terminal and typing "python3 --version". Linux users can install Python through their package manager.
- A Text Editor: Any text editor will work. Visual Studio Code is free and excellent for beginners, offering syntax highlighting and helpful error messages that make coding easier.
- Basic Computer Skills: You should know how to navigate folders, create files, and use the command line or terminal on your computer.
Once you have these tools ready, you are all set to begin building your audit trail system.
Understanding the HolySheep AI API Structure
Before writing any code, let us understand how the HolySheep AI API works. Think of an API (Application Programming Interface) as a messenger that takes your request, delivers it to the AI service, and brings back the response. It is like ordering food at a restaurant: you give your order to the waiter (the API), the kitchen prepares your food (the AI processes your request), and the waiter brings back your meal (the API returns the response).
The API Endpoint
The HolySheep AI API is accessible at the base URL: https://api.holysheep.ai/v1. Every API call you make will start with this address, similar to how every web page you visit starts with "https://". The "/v1" indicates that you are using version 1 of the API, which ensures compatibility even as the service evolves over time.
Authentication: How the API Knows Who You Are
When you create an account on HolySheep AI, you receive an API key that acts like a digital password. Every time your application makes a request to the API, it includes this key to prove that the request is coming from your account. This key looks like a long string of random letters and numbers, and you should treat it like a password, never sharing it publicly or committing it to public code repositories.
The Request and Response Cycle
When you want the AI to generate a response, you send a request containing your message, specify which AI model you want to use, and configure various settings. The API receives this request, forwards it to the appropriate AI model, waits for the response, and delivers it back to your application. Throughout this entire process, you want to capture details that will form your audit trail.
Step-by-Step Implementation: Building Your First Audit Trail
Now comes the exciting part. I will guide you through creating a complete audit trail system step by step. Each step builds upon the previous one, so follow along carefully and take your time to understand each concept before moving forward.
Step 1: Setting Up Your Python Environment
First, create a new folder on your computer where you will store your project files. Name it something descriptive like "ai-audit-trail". Inside this folder, create a new file called "audit_trail.py". This will be the main file where we write our audit trail code.
At the top of your file, we need to import the necessary libraries. A library is a collection of pre-written code that performs common tasks, saving you from having to write everything from scratch. For our audit trail, we will use the requests library to make API calls and the json library to handle data in a format that both humans and computers can read easily.
# Import the libraries we need for making API calls and handling data
import requests
import json
import time
from datetime import datetime
Your HolySheep AI API key - replace this with your actual key
Get your free API key by signing up at https://www.holysheep.ai/register
API_KEY = "YOUR_HOLYSHEEP_API_KEY"
The base URL for the HolySheep AI API
BASE_URL = "https://api.holysheep.ai/v1"
After adding this code, you will want to install the requests library if you have not already done so. Open your terminal or command prompt and type: pip install requests
Step 2: Creating the Audit Log Data Structure
An audit log entry needs to capture all the relevant information about an API call. Think of it like filling out a form with all the important details. We will create a structured format that ensures we never miss capturing any critical information.
# Create a class to manage our audit trail
class AuditTrail:
def __init__(self, log_file="audit_log.json"):
"""
Initialize the audit trail system.
The log_file parameter specifies where we will store our audit records.
"""
self.log_file = log_file
self.logs = []
def create_log_entry(self, request_data, response_data, status_code,
duration_ms, error_message=None):
"""
Create a detailed log entry capturing all aspects of an API call.
Parameters:
- request_data: What we sent to the API
- response_data: What we received back
- status_code: HTTP status code indicating success or failure
- duration_ms: How long the API call took in milliseconds
- error_message: Any error message if something went wrong
"""
entry = {
"timestamp": datetime.now().isoformat(),
"request": {
"url": f"{BASE_URL}/chat/completions",
"model": request_data.get("model", "unknown"),
"token_count_estimate": self._estimate_tokens(request_data),
"messages": request_data.get("messages", [])
},
"response": {
"status_code": status_code,
"model_used": response_data.get("model", "unknown") if response_data else None,
"completion_tokens": response_data.get("usage", {}).get("completion_tokens", 0) if response_data else 0,
"prompt_tokens": response_data.get("usage", {}).get("prompt_tokens", 0) if response_data else 0,
"total_tokens": response_data.get("usage", {}).get("total_tokens", 0) if response_data else 0,
"response_content": self._extract_content(response_data) if response_data else None
},
"performance": {
"duration_ms": duration_ms,
"latency_category": self._categorize_latency(duration_ms)
}
}
# Add error information if present
if error_message:
entry["error"] = {
"message": error_message,
"severity": "high" if status_code >= 500 else "medium" if status_code >= 400 else "low"
}
self.logs.append(entry)
return entry
def _estimate_tokens(self, request_data):
"""
Estimate the number of tokens in our request.
Tokens are the basic units of text that AI models process.
"""
messages = request_data.get("messages", [])
total_chars = sum(len(msg.get("content", "")) for msg in messages)
# Rough estimate: 1 token ≈ 4 characters for English text
return max(1, total_chars // 4)
def _extract_content(self, response_data):
"""
Extract the actual text content from the AI's response.
"""
try:
choices = response_data.get("choices", [])
if choices and len(choices) > 0:
return choices[0].get("message", {}).get("content", "")
except Exception:
pass
return None
def _categorize_latency(self, duration_ms):
"""
Categorize the API call latency for monitoring purposes.
HolySheep AI typically delivers sub-50ms latency for optimal performance.
"""
if duration_ms < 50:
return "excellent"
elif duration_ms < 200:
return "good"
elif duration_ms < 500:
return "acceptable"
else:
return "slow"
def save_logs(self):
"""
Save all audit log entries to a JSON file for persistence.
"""
with open(self.log_file, 'w', encoding='utf-8') as f:
json.dump(self.logs, f, indent=2, ensure_ascii=False)
print(f"✅ Audit logs saved to {self.log_file}")
def print_summary(self):
"""
Print a summary of all logged API calls.
"""
print("\n" + "="*70)
print("AUDIT TRAIL SUMMARY")
print("="*70)
print(f"Total API calls logged: {len(self.logs)}")
if self.logs:
# Calculate statistics
total_tokens = sum(entry["response"]["total_tokens"] for entry in self.logs)
avg_latency = sum(entry["performance"]["duration_ms"] for entry in self.logs) / len(self.logs)
error_count = sum(1 for entry in self.logs if "error" in entry)
print(f"Total tokens processed: {total_tokens}")
print(f"Average latency: {avg_latency:.2f}ms")
print(f"API calls with errors: {error_count}")
print("="*70)
Step 3: Creating the AI API Client with Integrated Audit Trail
Now we need to create the function that actually makes API calls to the AI service while automatically logging everything to our audit trail. This is the core of our system.
def call_ai_with_audit(audit_trail, model, messages, temperature=0.7, max_tokens=1000):
"""
Make an API call to HolySheep AI and automatically create an audit log entry.
Parameters:
- audit_trail: The AuditTrail instance to log to
- model: Which AI model to use (e.g., "gpt-4.1", "claude-sonnet-4.5")
- messages: List of message objects with 'role' and 'content'
- temperature: How creative/random the AI should be (0.0 to 1.0)
- max_tokens: Maximum length of the response
Returns:
- The AI's response content
"""
# Prepare the request payload
request_data = {
"model": model,
"messages": messages,
"temperature": temperature,
"max_tokens": max_tokens
}
# Set up headers with authentication
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
}
# Record the start time for measuring latency
start_time = time.time()
try:
# Make the actual API call
response = requests.post(
f"{BASE_URL}/chat/completions",
headers=headers,
json=request_data,
timeout=30 # Maximum wait time of 30 seconds
)
# Calculate how long the call took
end_time = time.time()
duration_ms = (end_time - start_time) * 1000
# Parse the response
response_data = response.json()
status_code = response.status_code
# Create the audit log entry
audit_trail.create_log_entry(
request_data=request_data,
response_data=response_data if status_code == 200 else None,
status_code=status_code,
duration_ms=duration_ms,
error_message=None if status_code == 200 else response_data.get("error", {}).get("message", "Unknown error")
)
# Return the response content if successful
if status_code == 200:
choices = response_data.get("choices", [])
if choices and len(choices) > 0:
return choices[0]["message"]["content"]
else:
print(f"❌ API Error ({status_code}): {response_data.get('error', {}).get('message', 'Unknown error')}")
return None
except requests.exceptions.Timeout:
# Handle timeout errors
duration_ms = (time.time() - start_time) * 1000
audit_trail.create_log_entry(
request_data=request_data,
response_data=None,
status_code=408,
duration_ms=duration_ms,
error_message="Request timed out after 30 seconds"
)
print("❌ Request timed out. The API took too long to respond.")
return None
except requests.exceptions.RequestException as e:
# Handle network errors
duration_ms = (time.time() - start_time) * 1000
audit_trail.create_log_entry(
request_data=request_data,
response_data=None,
status_code=0,
duration_ms=duration_ms,
error_message=f"Network error: {str(e)}"
)
print(f"❌ Network error: {str(e)}")
return None
Step 4: Putting It All Together with a Complete Example
Now let us create a complete working example that demonstrates all the concepts we have learned. This example will make several API calls to different AI models, automatically logging everything to our audit trail.
# Initialize the audit trail system
audit = AuditTrail(log_file="ai_usage_audit.json")
print("🚀 HolySheep AI Audit Trail Demo")
print("="*50)
Example 1: Using GPT-4.1 for a creative writing task
print("\n📝 Example 1: Creative Writing with GPT-4.1")
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Write a short haiku about artificial intelligence."}
]
response = call_ai_with_audit(
audit_trail=audit,
model="gpt-4.1",
messages=messages,
temperature=0.8,
max_tokens=100
)
if response:
print("🤖 AI Response:")
print(response)
Example 2: Using DeepSeek V3.2 for a cost-effective analysis task
print("\n📊 Example 2: Analysis with DeepSeek V3.2 (Cost-effective option)")
messages = [
{"role": "system", "content": "You are a data analysis assistant."},
{"role": "user", "content": "Explain the benefits of using audit trails in three bullet points."}
]
response = call_ai_with_audit(
audit_trail=audit,
model="deepseek-v3.2",
messages=messages,
temperature=0.5,
max_tokens=150
)
if response:
print("🤖 AI Response:")
print(response)
Example 3: Using Gemini 2.5 Flash for a quick information request
print("\n⚡ Example 3: Quick Query with Gemini 2.5 Flash")
messages = [
{"role": "system", "content": "You provide concise, accurate information."},
{"role": "user", "content": "What is the capital of France?"}
]
response = call_ai_with_audit(
audit_trail=audit,
model="gemini-2.5-flash",
messages=messages,
temperature=0.3,
max_tokens=50
)
if response:
print("🤖 AI Response:")
print(response)
Save all logs and print summary
audit.save_logs()
audit.print_summary()
print("\n✨ Demo complete! Check 'ai_usage_audit.json' for your detailed audit trail.")
Understanding Your Audit Log Output
After running the complete example, you will have a file called "ai_usage_audit.json" containing all your audit records. Let me explain what each field means so you can make sense of your data.
The Timestamp Field
The timestamp uses ISO format (e.g., "2026-01-15T10:30:45.123456") which includes the date, time, and microseconds. This precise timing is crucial for investigating issues, as it allows you to correlate problems with specific events in your application or external systems.
Request Information
The request section captures everything you sent to the AI service. The URL shows which endpoint was called, the model field indicates which AI model processed your request, the token count estimate gives you a rough idea of how much text you sent, and the messages array contains the actual conversation history.
Response Information
The response section captures the AI's reply. The status code tells you whether the request succeeded (200) or failed (various other codes). The token counts are particularly important for cost tracking, as HolySheep AI charges based on the number of tokens processed. The response content is the actual text generated by the AI.
Performance Metrics
The performance section records how long each API call took. HolySheep AI is optimized for speed, typically delivering sub-50ms latency for most requests. If you see calls taking much longer than this, it might indicate network issues or that you should consider different optimization strategies.
Advanced Audit Trail Features for Production Use
While the basic audit trail we built works well for learning and small projects, production applications need additional features to handle real-world requirements. Let me share some advanced patterns that I have implemented in my own projects.
Adding User Context to Your Audit Logs
In production systems, you often need to know not just what happened, but who triggered the action. You can extend the audit trail to include user identifiers, session IDs, and other context information that helps you understand the full picture.
def call_ai_with_context_audit(audit_trail, model, messages, user_id=None,
session_id=None, request_id=None, metadata=None):
"""
Enhanced API call function that includes user and session context.
Additional Parameters:
- user_id: Identifier for the user making the request
- session_id: Current session identifier
- request_id: Unique identifier for this specific request
- metadata: Any additional context information as a dictionary
"""
import uuid
# Generate request ID if not provided
if not request_id:
request_id = str(uuid.uuid4())
# Prepare extended request data
request_data = {
"model": model,
"messages": messages,
"temperature": 0.7,
"max_tokens": 1000,
"user_context": {
"user_id": user_id,
"session_id": session_id,
"request_id": request_id,
"metadata": metadata or {}
}
}
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json",
"X-Request-ID": request_id # Custom header for request tracking
}
start_time = time.time()
try:
response = requests.post(
f"{BASE_URL}/chat/completions",
headers=headers,
json=request_data,
timeout=30
)
duration_ms = (time.time() - start_time) * 1000
response_data = response.json()
# Create enhanced log entry with context
entry = audit_trail.create_log_entry(
request_data=request_data,
response_data=response_data if response.status_code == 200 else None,
status_code=response.status_code,
duration_ms=duration_ms,
error_message=None if response.status_code == 200 else
response_data.get("error", {}).get("message", "Unknown error")
)
# Add context to the log entry
entry["context"] = {
"user_id": user_id,
"session_id": session_id,
"request_id": request_id,
"metadata": metadata
}
if response.status_code == 200:
return response_data["choices"][0]["message"]["content"]
else:
return None
except Exception as e:
duration_ms = (time.time() - start_time) * 1000
audit_trail.create_log_entry(
request_data=request_data,
response_data=None,
status_code=500,
duration_ms=duration_ms,
error_message=f"Unexpected error: {str(e)}"
)
return None
Implementing Log Rotation and Retention Policies
For long-running applications, you will accumulate thousands or millions of log entries. Implementing log rotation ensures that old logs are archived or deleted according to your retention policies, preventing your storage from filling up while still maintaining compliance with any data retention requirements.
import os
from pathlib import Path
class RotatingAuditTrail(AuditTrail):
"""
Enhanced audit trail with automatic log rotation.
"""
def __init__(self, log_directory="audit_logs", max_entries_per_file=1000):
"""
Initialize with rotation settings.
Parameters:
- log_directory: Where to store log files
- max_entries_per_file: How many entries before creating a new file
"""
self.log_directory = Path(log_directory)
self.log_directory.mkdir(exist_ok=True)
self.max_entries_per_file = max_entries_per_file
self.current_file_index = 0
super().__init__(log_file=str(self._get_current_log_file()))
def _get_current_log_file(self):
"""
Get the filename for the current log file.
"""
return self.log_directory / f"audit_log_{self.current_file_index:04d}.json"
def _get_next_log_file(self):
"""
Move to the next log file.
"""
self.current_file_index += 1
self.logs = []
self.log_file = str(self._get_current_log_file())
return self.log_file
def create_log_entry(self, request_data, response_data, status_code,
duration_ms, error_message=None):
"""
Override to implement automatic rotation.
"""
# Check if we need to rotate
if len(self.logs) >= self.max_entries_per_file:
self.save_logs()
self._get_next_log_file()
print(f"🔄 Rotated to new log file: {self.log_file}")
return super().create_log_entry(
request_data, response_data, status_code, duration_ms, error_message
)
def get_all_logs(self):
"""
Retrieve all audit logs from all rotated files.
"""
all_logs = []
for log_file in sorted(self.log_directory.glob("audit_log_*.json")):
try:
with open(log_file, 'r', encoding='utf-8') as f:
logs = json.load(f)
all_logs.extend(logs)
except (json.JSONDecodeError, FileNotFoundError):
continue
return all_logs
Common Errors and Fixes
Throughout my experience implementing audit trails for AI API usage, I have encountered numerous issues that can trip up beginners and experienced developers alike. Let me share the most common problems and their solutions so you can avoid the frustration I went through learning these lessons.
Error 1: Authentication Failed - Invalid API Key
Symptom: When you run your code, you see an error message like "Authentication failed" or "Invalid API key" with a 401 status code. The API refuses to process your requests, and your audit trail shows this as an authentication failure.
Cause: This error occurs when the API key you provided is incorrect, expired, revoked, or not properly included in the request headers. Common mistakes include accidentally including spaces before or after the key, using an old key after generating a new one, or copying only part of the key.
Solution: First, verify that you have copied your API key correctly from the HolySheep AI dashboard. The key should look like a long string starting with "hs-" or similar prefix. Make sure there are no leading or trailing spaces. Also check that you are using the key for the correct environment (production vs. development if applicable). Here is a debugging function to help identify authentication issues:
def verify_api_connection(api_key):
"""
Test your API key and diagnose connection issues.
"""
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
try:
# Try a minimal API call to verify authentication
test_payload = {
"model": "deepseek-v3.2",
"messages": [{"role": "user", "content": "test"}],
"max_tokens": 5
}
response = requests.post(
f"{BASE_URL}/chat/completions",
headers=headers,
json=test_payload,
timeout=10
)
if response.status_code == 200:
print("✅ API key is valid and authentication successful!")
return True
elif response.status_code == 401:
print("❌ Authentication failed. Please check:")
print(" - Is your API key correct?")
print(" - Has your API key expired?")
print(" - Do you have permission to access this API?")
return False
elif response.status_code == 429:
print("⚠️ Rate limit exceeded. You are making too many requests.")
print(" Wait a moment and try again, or upgrade your plan.")
return False
else:
print(f"❌ Unexpected error ({response.status_code}): {response.text}")
return False
except requests.exceptions.SSLError:
print("❌ SSL Certificate error. Your system may have outdated certificates.")
print(" Try updating your operating system or the requests library.")
return False
except requests.exceptions.ConnectionError:
print("❌ Connection error. Cannot reach the API server.")
print(" Check your internet connection and firewall settings.")
return False
Error 2: Rate Limiting and Quota Exceeded
Symptom: Your API calls start failing with 429 status codes, and your audit trail shows rate limit errors. This might happen suddenly even if your code has been working for days or weeks without issues.
Cause: Every API service limits how many requests you can make within a certain time period. HolySheep AI implements rate limiting to ensure fair usage across all customers. You might be exceeding these limits because of sudden traffic spikes, inefficient code that makes redundant API calls, or simply outgrowing your current plan's quota.
Solution: Implement exponential backoff with jitter to handle rate limits gracefully. This technique automatically waits longer between retries, increasing the wait time exponentially while adding randomness to prevent synchronized retries from multiple clients:
import random
import time
def call_with_rate_limit_handling(audit_trail, model, messages,
max_retries=5, base_delay=1):
"""
Make API calls with automatic rate limit handling and retries.
"""
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
}
request_data = {
"model": model,
"messages": messages,
"max_tokens": 1000
}
for attempt in range(max_retries):
start_time = time.time()
try:
response = requests.post(
f"{BASE_URL}/chat/completions",
headers=headers,
json=request_data,
timeout=30
)
duration_ms = (time.time() - start_time) * 1000
# Create audit entry for all attempts
audit_trail.create_log_entry(
request_data=request_data,
response_data=response.json() if response.status_code == 200 else None,
status_code=response.status_code,
duration_ms=duration_ms,
error_message=None if response.status_code == 200 else
response.json().get("error", {}).get("message", "Rate limited")
)
if response.status_code == 200:
return response.json()["choices"][0]["message"]["content"]
elif response.status_code == 429:
# Rate limited - implement exponential backoff
wait_time = base_delay * (2 ** attempt) + random.uniform(0, 1)
print(f"⚠️ Rate limited. Waiting {wait_time:.2f} seconds before retry...")
time.sleep(wait_time)
continue
else:
# Other errors - return None
print(f"❌ API error: {response.status_code}")
return None
except requests.exceptions.Timeout:
duration_ms = (time.time() - start_time) * 1000
audit_trail.create_log_entry(
request_data=request_data,
response_data=None,
status_code=0,
duration_ms=duration_ms,
error_message="Request timed out"
)
print("❌ Request timed out.")
return None
print("❌ Max retries exceeded.")
return None
Usage example with rate limit handling
print("🔄 Testing rate limit handling...")
messages = [
{"role": "user", "content": "Hello, this is a test message."}
]
result = call_with_rate_limit_handling(audit, "deepseek-v3.2", messages)
if result:
print("✅ Request successful!")
print(f"Response: {result}")
Error 3: Response Parsing Failures
Symptom: Your code runs without errors, but you see None values in your audit logs or the response content is missing. Sometimes the error message mentions "KeyError" or "IndexError" when trying to access response fields.
Cause: AI APIs can return responses in different formats depending on the model, the specific request, or error conditions. Your code might be trying to access fields that do not exist in the actual response. For example, streaming responses have a completely different structure than standard responses, and error responses often lack the fields that successful responses include.
Solution: Implement defensive parsing with proper error handling and fallback values. Always validate that expected fields exist before accessing them, and provide sensible defaults when data is missing:
def safe_extract_response_content(response_data):
"""
Safely extract content from API response with multiple fallback strategies.
"""
# Check if response_data is valid
if not response_data:
return None, "No response data provided"
# Check for error responses
if "error" in response_data:
error_msg = response_data["error"].get("message", "Unknown API error")
return None, f"API error: {error_msg}"
# Try standard chat completions format
try:
choices = response_data.get("choices", [])
if choices and len(choices) > 0:
message = choices[0].get("message", {})
content = message.get("content", "")
if content:
return content, "success"
except (KeyError, TypeError, IndexError):
pass
# Try alternative formats (