What This Workflow Does
This n8n workflow solves the critical challenge of monitoring AI API costs by automatically tracking token usage across OpenAI and Gemini models. As businesses increasingly rely on multiple AI services, managing and predicting costs becomes complex due to varying token pricing across models and providers.
The workflow extracts and summarizes key metrics including prompt tokens, completion tokens, and total tokens used per API call. It provides visibility into your AI consumption patterns, helping you identify optimization opportunities, forecast budgets accurately, and compare cost-effectiveness between different AI models.
How It Works
1. API Call Monitoring
The workflow connects to both OpenAI and Gemini APIs to capture usage data from each request. It logs the timestamp, model used, and token counts for every API interaction.
2. Token Classification
Each API response is analyzed to separate prompt tokens (input) from completion tokens (output). This distinction helps identify whether costs are driven by lengthy inputs or expansive outputs.
3. Cost Calculation
Using the latest pricing for each model, the workflow calculates the actual cost per request. Prices are automatically updated to reflect current rates from both providers.
4. Data Aggregation
The system compiles daily and weekly summaries showing usage trends, cost comparisons between models, and efficiency metrics like tokens per dollar spent.
Who This Is For
This workflow is essential for any business or team regularly using AI APIs. It's particularly valuable for:
- Development teams building AI-powered applications
- Product managers overseeing AI feature budgets
- Finance teams needing to forecast and allocate AI costs
- Startups optimizing limited resources across multiple AI providers
What You'll Need
- Active API keys for OpenAI and/or Gemini
- n8n instance (cloud or self-hosted)
- Storage solution for historical data (Google Sheets, Airtable, or database)
- Optional: Visualization tool (Tableau, Looker, or similar) for dashboards
Quick Setup Guide
- Download the JSON template file
- Import into your n8n instance
- Configure your API keys in the credential manager
- Set your preferred data storage destination
- Activate the workflow and test with sample API calls
- Schedule automatic execution (daily recommended)
Key Benefits
Cost visibility: Eliminate billing surprises with real-time tracking of token consumption across all your AI services.
Usage optimization: Identify inefficient patterns where simpler models could suffice or where prompts could be refined.
Budget forecasting: Predict future costs based on historical usage patterns and growth trends.
Vendor comparison: Make data-driven decisions about when to use OpenAI versus Gemini based on actual cost-performance metrics.
Automated reporting: Save hours of manual tracking with automatically generated usage reports.
Pro tip: Combine this workflow with alerting rules to notify your team when token usage exceeds expected thresholds, helping prevent budget overruns.