Introduction
In the world of AI, we're seeing a shift from 'tokenmaxxing' to 'token rationing' - where companies are now carefully monitoring how much AI compute their employees use. This tutorial will teach you how to build a simple AI budget monitoring system using Python that can track token usage and alert you when limits are approaching. This is a practical skill for anyone working with AI APIs like OpenAI's GPT models, where each API call consumes tokens that cost money.
Prerequisites
- Basic understanding of Python programming
- Python 3.7 or higher installed on your computer
- OpenAI API key (free to get from OpenAI's website)
- Basic knowledge of how AI APIs work (what tokens are and how they're used)
Step-by-step Instructions
Step 1: Set Up Your Python Environment
First, we need to create a new Python project and install the required dependencies. Open your terminal or command prompt and run:
mkdir ai-budget-monitor
cd ai-budget-monitor
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
pip install openai python-dotenv
Why this step? We're creating a clean project environment and installing the necessary libraries to interact with the OpenAI API and manage environment variables.
Step 2: Create Your API Key Configuration
Create a file called .env in your project directory and add your OpenAI API key:
OPENAI_API_KEY=your_actual_api_key_here
Why this step? Storing your API key in a separate file prevents accidentally sharing it in code repositories or exposing it publicly.
Step 3: Create the Main Monitoring Script
Create a file called budget_monitor.py and add this code:
import os
import openai
from dotenv import load_dotenv
import time
# Load environment variables
load_dotenv()
# Initialize OpenAI client
openai.api_key = os.getenv('OPENAI_API_KEY')
# Track usage
usage_log = []
max_tokens_per_day = 10000 # Set your daily limit
# Function to simulate AI API call
def make_ai_call(prompt):
try:
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": prompt}],
max_tokens=500
)
# Get token usage from response
usage = response['usage']['total_tokens']
usage_log.append(usage)
print(f"AI call completed. Tokens used: {usage}")
print(f"Current daily total: {sum(usage_log)} tokens")
# Check if we're approaching the limit
check_budget_limit()
return response
except Exception as e:
print(f"Error making AI call: {e}")
return None
# Function to check budget limit
def check_budget_limit():
total_used = sum(usage_log)
percentage_used = (total_used / max_tokens_per_day) * 100
if percentage_used >= 90:
print("⚠️ WARNING: You've used 90% of your daily token limit!")
elif percentage_used >= 75:
print("⚠️ Alert: You've used 75% of your daily token limit.")
elif percentage_used >= 50:
print("✅ You've used 50% of your daily token limit.")
else:
print("✅ Token usage is within normal limits.")
# Function to reset daily usage
def reset_daily_usage():
global usage_log
usage_log = []
print("Daily usage has been reset.")
# Main execution
if __name__ == "__main__":
print("AI Budget Monitor Started!")
print(f"Daily limit: {max_tokens_per_day} tokens")
# Example usage
for i in range(3):
print(f"\nMaking AI call #{i+1}:")
make_ai_call("Explain what tokens are in AI language.")
time.sleep(1) # Small delay to simulate real usage
Why this step? This creates the core functionality that will track how many tokens you're using, alert you when you're approaching limits, and simulate making AI API calls.
Step 4: Test Your Budget Monitor
Run your script to see it in action:
python budget_monitor.py
Why this step? Testing ensures your monitoring system works correctly and shows you how it tracks token usage and provides alerts.
Step 5: Enhance Your Monitor with Logging
Let's improve our monitor by adding proper logging to track usage over time. Update your budget_monitor.py with this enhanced version:
import os
import openai
from dotenv import load_dotenv
import time
import json
from datetime import datetime
# Load environment variables
load_dotenv()
# Initialize OpenAI client
openai.api_key = os.getenv('OPENAI_API_KEY')
# Track usage
usage_log = []
max_tokens_per_day = 10000 # Set your daily limit
log_file = 'usage_log.json'
# Function to save usage to file
def save_usage_log():
log_data = {
'date': datetime.now().strftime('%Y-%m-%d'),
'usage': usage_log,
'total': sum(usage_log),
'timestamp': datetime.now().isoformat()
}
try:
with open(log_file, 'r') as f:
logs = json.load(f)
except FileNotFoundError:
logs = []
logs.append(log_data)
with open(log_file, 'w') as f:
json.dump(logs, f, indent=2)
# Function to load previous usage
def load_usage_log():
try:
with open(log_file, 'r') as f:
logs = json.load(f)
return logs
except FileNotFoundError:
return []
# Function to simulate AI API call
def make_ai_call(prompt):
try:
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": prompt}],
max_tokens=500
)
# Get token usage from response
usage = response['usage']['total_tokens']
usage_log.append(usage)
print(f"AI call completed. Tokens used: {usage}")
print(f"Current daily total: {sum(usage_log)} tokens")
# Check if we're approaching the limit
check_budget_limit()
return response
except Exception as e:
print(f"Error making AI call: {e}")
return None
# Function to check budget limit
def check_budget_limit():
total_used = sum(usage_log)
percentage_used = (total_used / max_tokens_per_day) * 100
if percentage_used >= 90:
print("⚠️ WARNING: You've used 90% of your daily token limit!")
elif percentage_used >= 75:
print("⚠️ Alert: You've used 75% of your daily token limit.")
elif percentage_used >= 50:
print("✅ You've used 50% of your daily token limit.")
else:
print("✅ Token usage is within normal limits.")
# Function to reset daily usage
def reset_daily_usage():
global usage_log
usage_log = []
print("Daily usage has been reset.")
# Function to display usage history
def show_usage_history():
logs = load_usage_log()
if logs:
print("\nUsage History:")
for log in logs[-5:]: # Show last 5 entries
print(f"Date: {log['date']}, Total: {log['total']} tokens")
else:
print("No usage history found.")
# Main execution
if __name__ == "__main__":
print("AI Budget Monitor Started!")
print(f"Daily limit: {max_tokens_per_day} tokens")
# Show previous usage
show_usage_history()
# Example usage
for i in range(3):
print(f"\nMaking AI call #{i+1}:")
make_ai_call("Explain what tokens are in AI language.")
time.sleep(1) # Small delay to simulate real usage
# Save current usage
save_usage_log()
print("\nUsage saved to log file.")
Why this step? Adding logging allows you to track your usage over multiple days and maintain a history of your AI spending, which is crucial for budget management.
Step 6: Set Up Automated Alerts
For a more advanced version, let's add email notifications when limits are approached. First, install the email library:
pip install yagmail
Then add this function to your script:
# Function to send email alerts (optional)
def send_alert_email(message):
try:
# You would need to set up email credentials
# This is a placeholder - you'd need to configure your email settings
print(f"Would send email alert: {message}")
# yag = yagmail.SMTP('[email protected]', 'your_password')
# yag.send(to='[email protected]', subject='AI Budget Alert', contents=message)
except Exception as e:
print(f"Failed to send email: {e}")
Why this step? Email alerts help ensure you don't exceed your budget without realizing it, especially when working on long projects.
Summary
In this tutorial, you've learned how to build a simple but effective AI budget monitoring system. You've created a tool that tracks token usage, provides alerts when approaching limits, and maintains a history of your usage. This is essential for anyone working with AI APIs who wants to avoid unexpected costs and manage their compute resources effectively.
The system you've built demonstrates key concepts in AI resource management, showing how to monitor usage, set limits, and provide actionable feedback. As companies move from tokenmaxxing to token rationing, tools like this become increasingly important for responsible AI usage.



