Skip to main content

Track and Optimize API Spending

Build a complete cost monitoring and optimization system to track your Eden AI API usage, identify expensive operations, and stay within budget.

What You’ll Build

By the end of this tutorial, you’ll have:
  • Cost Monitoring Dashboard - Real-time view of your API spending
  • Automated Budget Alerts - Email/Slack notifications when approaching limits
  • Provider Comparison Analysis - Identify the most cost-effective AI providers
  • Usage Optimization - Spot and reduce expensive API patterns

Prerequisites

  • Python 3.8 or higher
  • Eden AI API key
  • Basic understanding of REST APIs
  • Optional: PostgreSQL or SQLite for persistent storage

Problem Statement

As your Eden AI usage grows, you need to:
  1. Track spending across multiple providers and features
  2. Set budgets and receive alerts before overspending
  3. Optimize costs by identifying expensive operations
  4. Compare providers to find the best value for your use case
  5. Generate reports for stakeholders
This tutorial shows you how to build a Python-based system that solves all these challenges.

Architecture Overview

┌─────────────────────────────────────────────────────────────┐
│                     Your Application                         │
│  ┌────────────┐    ┌────────────┐    ┌──────────────┐      │
│  │  Cost      │───▶│  Budget    │───▶│  Alert       │      │
│  │  Monitor   │    │  Tracker   │    │  System      │      │
│  └────────────┘    └────────────┘    └──────────────┘      │
│         │                  │                   │             │
│         ▼                  ▼                   ▼             │
│  ┌────────────────────────────────────────────────────┐    │
│  │           Local Database / Cache                    │    │
│  └────────────────────────────────────────────────────┘    │
└─────────────────────────────────────────────────────────────┘


                ┌───────────────────────┐
                │  Eden AI Cost API     │
                └───────────────────────┘

Step 1: Set Up Cost Monitoring Client

Create a Python client to interact with the Eden AI Cost Monitoring API:

Step 2: Fetch and Cache Daily Usage Data

Implement caching to reduce API calls and improve performance:

Step 3: Analyze Costs by Provider and Feature

Create analysis tools to understand your spending patterns:

Step 4: Generate Cost Reports and Visualizations

Create visual reports to understand trends:

Step 5: Implement Budget Alerts

Add automated alerts when approaching budget limits:

Step 6: Optimize - Identify Most Expensive Operations

Create an optimizer to find cost-saving opportunities:

Step 7: Put It All Together

Create a main script that ties everything together:

Testing

Test your cost monitoring system: Run the test:
export EDENAI_API_KEY="your_api_key_here"
python test_system.py

Production Considerations

1. Scheduling

Run the monitoring system on a schedule using cron:
# Run daily at 9 AM
0 9 * * * /usr/bin/python3 /path/to/main.py >> /var/log/edenai_monitor.log 2>&1
Or use Python’s schedule library:
import schedule
import time

def job():
    # Run your monitoring
    main()

# Schedule daily at 9 AM
schedule.every().day.at("09:00").do(job)

# Or every hour
schedule.every().hour.do(job)

while True:
    schedule.run_pending()
    time.sleep(60)

2. Error Handling

Add robust error handling:
import logging
from requests.exceptions import RequestException

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

try:
    data = monitor.get_usage(begin, end)
except RequestException as e:
    logger.error(f"API request failed: {e}")
    # Send alert to admin
except Exception as e:
    logger.exception("Unexpected error")
    # Send alert to admin

3. Security

  • Store API keys in environment variables or secret managers
  • Use encrypted database for caching sensitive data
  • Implement access controls for reports
  • Use HTTPS for all API communications

4. Scaling

For high-volume monitoring:
  • Use Redis instead of SQLite for caching
  • Implement asynchronous API calls with aiohttp
  • Use job queues (Celery) for background processing
  • Store historical data in a time-series database

Next Steps

Now that you have a complete cost monitoring system:

Complete Example Output

When you run main.py, you’ll see:
🔍 Eden AI Cost Monitoring System

📊 Budget Status
============================================================
Monthly Budget Status
============================================================
Budget:              $500.00
Spent:               $234.56 (46.9%)
Remaining:           $265.44
Days:                 15/31
Projected Total:     $485.63

✓ On track
============================================================

📈 Cost Report
============================================================
Cost Report: 2024-01-01 to 2024-01-15
============================================================

Total Cost: $234.56

Cost by Provider:
------------------------------------------------------------
openai                   $198.45 (84.6%)
anthropic                 $28.12 (12.0%)
google                     $7.99 ( 3.4%)

Top 5 Most Expensive Features:
------------------------------------------------------------
text__chat                     $198.23 (2,341 calls, $0.0847/call)
image__generation               $24.56 (   89 calls, $0.2760/call)
text__embeddings                 $8.45 (1,234 calls, $0.0068/call)
ocr__ocr                         $2.45 (   45 calls, $0.0544/call)
audio__text_to_speech            $0.87 (    3 calls, $0.2900/call)

Current Credits: $156.78
============================================================

💡 Optimization Suggestions
============================================================
Cost Optimization Suggestions
============================================================

Provider Optimization Opportunities:
------------------------------------------------------------
1. Provider 'openai' accounts for 84.6% of costs. Consider comparing with alternative providers.
   Current Cost: $198.45 (84.6%)

============================================================

💳 Current Account Balance: $156.78