โ AI App Building (No-Code & Low-Code)
Day 9 of 14
Cost Management for AI APIs
Understanding Your AI Cost Drivers
AI API costs have three drivers: input tokens (what you send to the model โ system prompts, user messages, context), output tokens (what the model generates โ typically 3โ5x the cost per token of input), and frequency (how often you call the API). Reducing any of these reduces cost.
The most common cost optimisation mistakes: (1) Using an expensive model for simple tasks โ Claude Opus for a task Haiku handles equally well. (2) Sending the full system prompt on every request when prompt caching would reduce input token cost by 80โ90%. (3) Not batching requests โ making 100 individual API calls when one call with 100 items would work. (4) No caching of deterministic responses โ if the same question gets the same answer, cache the result at the application layer.
For BathCheck's report generation: if a user regenerates the same report 5 times (common during testing), you should cache the first result and serve it from cache. If a user's assessment data hasn't changed, the report won't change either. This single optimisation can reduce Claude API costs by 30โ40% in a product with a report preview feature.
Monitoring and Budgeting AI Costs
Set up cost monitoring before you have a problem, not after. Anthropic's console shows usage by API key โ check it weekly initially, then monthly once you understand your patterns. Set up a billing alert at your monthly budget threshold.
Cost projection: calculate your cost per user-action (cost per report generated, cost per chat message processed). Then multiply by your projected user volume. If one assessment report costs $0.02 at Claude Sonnet pricing and you process 1,000 reports/month, that's $20/month โ trivial. If your system prompt is 4,000 tokens and you have 10,000 API calls per month with no caching, that's 40M prompt tokens = $120/month at Sonnet input pricing โ significant but manageable.
A practical cost dashboard: add an api_calls table to your Supabase database. Log every Claude API call with: model, input_tokens, output_tokens, cost_calculated, user_id, feature_used. Query this weekly. You'll immediately see which features are most expensive per user and which users are outlier high-consumers (potential abuse or power users who might upgrade to an enterprise plan).
โก Today's Action
Calculate your current (or projected) monthly Claude API cost for one feature. Break it down to cost-per-user-action. Is this sustainable at your planned pricing? If not, identify the one change (different model, caching, reduced prompt size) that would have the biggest cost impact.
๐ก Pro Tip
Implement user-level rate limiting on AI features from Day 1. 'Generate up to 20 reports per month on the Starter plan' protects you from a single heavy user exhausting your AI budget. This also creates a natural upgrade incentive.