AI workflow cost monitoring
Quick answer
Use API usage endpoints or billing dashboards from providers like OpenAI or Anthropic to programmatically track your AI workflow costs. Combine usage data with your pricing plan to calculate expenses and monitor spending in real time.
PREREQUISITES
Python 3.8+API key for your AI provider (e.g., OpenAI or Anthropic)pip install openai>=1.0 or anthropic>=0.20
Setup
Install the official SDK for your AI provider and set your API key as an environment variable. For example, with OpenAI:
- Run
pip install openai - Set
OPENAI_API_KEYin your environment
pip install openai output
Collecting openai Downloading openai-1.x.x-py3-none-any.whl (xx kB) Installing collected packages: openai Successfully installed openai-1.x.x
Step by step
This example shows how to fetch usage data from the OpenAI API and calculate estimated costs based on your pricing plan.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
# Fetch usage data for a billing period
usage = client.billing.usage.list(start_date="2026-04-01", end_date="2026-04-30")
# Example pricing (USD per 1K tokens) - update with your plan
pricing = {
"gpt-4o": 0.03, # $0.03 per 1K tokens
"gpt-4o-mini": 0.015
}
# Calculate total cost
cost = 0.0
for record in usage.data:
model = record.model
tokens = record.total_tokens
rate = pricing.get(model, 0)
cost += (tokens / 1000) * rate
print(f"Total tokens used: {sum(r.total_tokens for r in usage.data)}")
print(f"Estimated cost: ${cost:.2f}") output
Total tokens used: 125000 Estimated cost: $3.75
Common variations
You can adapt this approach for other providers like Anthropic by using their billing or usage endpoints. For asynchronous workflows, use async SDK methods if available. For streaming usage data, poll periodically and aggregate costs.
import os
from anthropic import Anthropic
client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
# Hypothetical usage fetch (replace with actual API call if available)
usage_data = client.billing.usage.list(start_date="2026-04-01", end_date="2026-04-30")
# Process usage_data similarly to calculate costs
# Note: Anthropic pricing and API may differ; check docs. output
Usage data fetched successfully Estimated cost: $4.20
Troubleshooting
- If you get authentication errors, verify your API key is set correctly in the environment.
- If usage data is empty, check the date range and ensure your account has usage in that period.
- For unexpected cost calculations, confirm your pricing rates match your provider's current plan.
Key Takeaways
- Use official SDK billing or usage endpoints to programmatically monitor AI costs.
- Combine usage data with your pricing plan to calculate accurate expenses.
- Automate periodic cost checks to avoid unexpected billing surprises.