Introduction
In the rapidly evolving world of AI, token usage has become a critical consideration for developers and businesses alike. Microsoft CEO Satya Nadella's recent admission about being a 'token-maxer' highlights a common challenge: the temptation to use the most powerful AI models for every task, even when it's not necessary or cost-effective. This tutorial will teach you how to monitor and manage token usage when working with OpenAI's API, helping you avoid wasteful practices while maintaining productivity.
Prerequisites
- Python 3.7 or higher installed on your system
- OpenAI Python library installed (via pip)
- An OpenAI API key
- Basic understanding of Python programming and API interactions
Step-by-step Instructions
1. Setting Up Your Environment
1.1 Install Required Libraries
First, you'll need to install the OpenAI Python library. Open your terminal or command prompt and run:
pip install openai
This library provides a convenient interface to interact with OpenAI's API, including built-in token counting capabilities.
1.2 Configure Your API Key
Create a Python script and set up your environment variables:
import os
from openai import OpenAI
# Set your API key
client = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))
Store your API key in an environment variable for security. Never hardcode it in your scripts.
2. Understanding Token Usage
2.1 Basic Token Counting
OpenAI provides token counting utilities. Let's create a function to count tokens in your prompts:
import tiktoken
def count_tokens(text):
encoding = tiktoken.encoding_for_model('gpt-4')
return len(encoding.encode(text))
# Example usage
prompt = "Explain quantum computing in simple terms"
token_count = count_tokens(prompt)
print(f"Tokens in prompt: {token_count}")
Understanding token usage helps you make informed decisions about when to use different models. GPT-4 typically costs more per token than GPT-3.5, so knowing your token usage is crucial for cost management.
2.2 Monitoring Token Usage in API Calls
Let's create a function that tracks token usage during API calls:
def analyze_prompt_cost(prompt, model='gpt-4'):
# Count tokens
token_count = count_tokens(prompt)
# Estimate cost based on model
if model == 'gpt-4':
cost_per_token = 0.03 / 1000 # $0.03 per 1000 tokens
elif model == 'gpt-3.5-turbo':
cost_per_token = 0.0015 / 1000 # $0.0015 per 1000 tokens
total_cost = token_count * cost_per_token
return {
'tokens': token_count,
'estimated_cost': total_cost,
'model': model
}
This function helps you make informed decisions about which model to use based on cost and token usage.
3. Implementing Smart Model Selection
3.1 Create a Decision-Making Framework
Now, let's build a system that intelligently selects the appropriate model based on task complexity:
def select_appropriate_model(prompt):
token_count = count_tokens(prompt)
# Simple heuristic: use GPT-3.5 for simple tasks
if token_count < 500:
return 'gpt-3.5-turbo'
else:
return 'gpt-4'
# Example usage
simple_prompt = "What's the weather today?"
model = select_appropriate_model(simple_prompt)
print(f"Recommended model: {model}")
This approach aligns with Nadella's advice - don't waste powerful models on simple tasks. It's more cost-effective and environmentally responsible.
3.2 Comprehensive Analysis Function
Let's create a comprehensive function that analyzes both token usage and cost:
def analyze_and_optimize(prompt):
print(f"Prompt: {prompt}")
# Analyze token usage
token_count = count_tokens(prompt)
print(f"Token count: {token_count}")
# Analyze costs for different models
models = ['gpt-3.5-turbo', 'gpt-4']
for model in models:
cost_info = analyze_prompt_cost(prompt, model)
print(f"{model}: {cost_info['tokens']} tokens, ${cost_info['estimated_cost']:.6f}")
# Recommend best model
recommended_model = select_appropriate_model(prompt)
print(f"Recommended model: {recommended_model}")
This function provides a complete picture of your prompt's resource requirements, helping you make better decisions.
4. Practical Implementation
4.1 Full Working Example
Here's a complete example that demonstrates token monitoring and smart model selection:
import os
from openai import OpenAI
import tiktoken
# Initialize client
client = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))
def count_tokens(text):
encoding = tiktoken.encoding_for_model('gpt-4')
return len(encoding.encode(text))
def analyze_prompt_cost(prompt, model='gpt-4'):
token_count = count_tokens(prompt)
if model == 'gpt-4':
cost_per_token = 0.03 / 1000
elif model == 'gpt-3.5-turbo':
cost_per_token = 0.0015 / 1000
total_cost = token_count * cost_per_token
return {
'tokens': token_count,
'estimated_cost': total_cost,
'model': model
}
def select_appropriate_model(prompt):
token_count = count_tokens(prompt)
if token_count < 500:
return 'gpt-3.5-turbo'
else:
return 'gpt-4'
def analyze_and_optimize(prompt):
print(f"Prompt: {prompt}")
token_count = count_tokens(prompt)
print(f"Token count: {token_count}")
models = ['gpt-3.5-turbo', 'gpt-4']
for model in models:
cost_info = analyze_prompt_cost(prompt, model)
print(f"{model}: {cost_info['tokens']} tokens, ${cost_info['estimated_cost']:.6f}")
recommended_model = select_appropriate_model(prompt)
print(f"Recommended model: {recommended_model}")
return recommended_model
# Test with different prompts
simple_prompt = "What's the weather today?"
complex_prompt = "Explain the implications of quantum computing on modern cryptography and provide specific examples of algorithms that might be affected."
print("=== Simple Prompt Analysis ===")
model1 = analyze_and_optimize(simple_prompt)
print("\n=== Complex Prompt Analysis ===")
model2 = analyze_and_optimize(complex_prompt)
This complete implementation shows how to apply token monitoring and smart model selection in practice.
5. Advanced Considerations
5.1 Cost Monitoring
For larger projects, consider implementing a cost tracking system:
class CostTracker:
def __init__(self):
self.total_cost = 0
self.cost_history = []
def add_cost(self, cost):
self.total_cost += cost
self.cost_history.append(cost)
def get_total_cost(self):
return self.total_cost
def get_average_cost(self):
if not self.cost_history:
return 0
return sum(self.cost_history) / len(self.cost_history)
# Usage
tracker = CostTracker()
# After each API call, add the cost
# tracker.add_cost(cost)
This helps you monitor and control costs as your AI applications scale.
5.2 Logging and Reporting
Implement logging to track your token usage patterns:
import logging
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def log_token_usage(prompt, model, cost):
logger.info(f"Prompt: {prompt[:50]}... | Model: {model} | Cost: ${cost:.6f} | Tokens: {count_tokens(prompt)}")
Logging helps you identify patterns and optimize your AI usage over time.
Summary
This tutorial has taught you how to monitor and manage token usage when working with OpenAI's API. By understanding token counting, analyzing costs, and implementing smart model selection, you can avoid the 'token-maxing' trap that even industry leaders like Satya Nadella admit to falling into. The key is to match your computational resources to your actual needs - using powerful models only when necessary, which saves costs and promotes more sustainable AI practices.



