๐ง AI Agent Cost ManagementJune 12, 2026โ
Tests passing
AI Cost Analyzer
A data processing tool for retrospective analysis of AI agent token usage and API costs. It takes API logs as input, calculates usage metrics, identifies trends, and generates visual reports to help developers optimize their workflow and reduce costs.
What It Does
- Load API usage logs in CSV or JSON format.
- Analyze token usage and calculate daily cost metrics.
- Generate visual reports in PDF format.
Installation
Install the required dependencies using pip:
pip install pandas matplotlibUsage
python ai_cost_analyzer.py --input usage_logs.csv --output report.pdfSource Code
import argparse
import pandas as pd
import matplotlib.pyplot as plt
import json
from io import StringIO
def load_data(input_file):
"""Load data from a CSV or JSON file."""
try:
if input_file.endswith('.csv'):
with open(input_file, 'r') as f:
return pd.read_csv(f)
elif input_file.endswith('.json'):
with open(input_file, 'r') as f:
return pd.read_json(f)
else:
raise ValueError("Unsupported file format. Please use CSV or JSON.")
except Exception as e:
raise ValueError(f"Error loading file: {e}")
def analyze_usage(data):
"""Analyze token usage and calculate cost metrics."""
if 'timestamp' not in data.columns or 'tokens' not in data.columns or 'cost' not in data.columns:
raise ValueError("Input data must contain 'timestamp', 'tokens', and 'cost' columns.")
data['timestamp'] = pd.to_datetime(data['timestamp'])
data.set_index('timestamp', inplace=True)
# Calculate daily token usage and cost
daily_stats = data.resample('D').sum()
return daily_stats
def generate_report(daily_stats, output_file):
"""Generate a report with visualizations and save it as a PDF."""
try:
plt.figure(figsize=(10, 6))
# Plot token usage
plt.subplot(2, 1, 1)
plt.plot(daily_stats.index, daily_stats['tokens'], marker='o', label='Token Usage')
plt.title('Daily Token Usage')
plt.xlabel('Date')
plt.ylabel('Tokens')
plt.legend()
# Plot cost
plt.subplot(2, 1, 2)
plt.plot(daily_stats.index, daily_stats['cost'], marker='o', color='red', label='Cost')
plt.title('Daily Cost')
plt.xlabel('Date')
plt.ylabel('Cost (USD)')
plt.legend()
plt.tight_layout()
plt.savefig(output_file)
plt.close()
except Exception as e:
raise ValueError(f"Error generating report: {e}")
def main():
parser = argparse.ArgumentParser(description='AI Cost Analyzer: Analyze token usage and API costs.')
parser.add_argument('--input', required=True, help='Path to the input API usage log file (CSV or JSON).')
parser.add_argument('--output', required=True, help='Path to the output report file (PDF).')
args = parser.parse_args()
try:
# Load data
data = load_data(args.input)
# Analyze usage
daily_stats = analyze_usage(data)
# Generate report
generate_report(daily_stats, args.output)
print(f"Report successfully generated: {args.output}")
except Exception as e:
print(f"Error: {e}")
if __name__ == '__main__':
main()Community
Downloads
ยทยทยท
Rate this tool
No ratings yet โ be the first!
Details
- Tool Name
- ai_cost_analyzer
- Category
- AI Agent Cost Management
- Generated
- June 12, 2026
- Tests
- Passing โ
- Fix Loops
- 2
Quick Install
Clone just this tool:
git clone --depth 1 --filter=blob:none --sparse \ https://github.com/ptulin/autoaiforge.git cd autoaiforge git sparse-checkout set generated_tools/2026-06-12/ai_cost_analyzer cd generated_tools/2026-06-12/ai_cost_analyzer pip install -r requirements.txt 2>/dev/null || true python ai_cost_analyzer.py