Monitor AI API Usage and Spend in 15 Minutes
AI API bills grow through ordinary development decisions: longer prompts, agent loops, retries, and higher traffic. Monitoring should answer three questions: what did each request consume, which workload generated it, and how much budget remains? Start with one instrumentation layer around your API client, then add cost estimates and spending controls.
1. Put measurement around every API call
Create a shared wrapper around your SDK calls instead of adding logging separately to every feature. Record each attempt, including failed requests and retries. Otherwise, a successful user interaction can hide several potentially billable calls.
For each attempt, capture these fields:
- Identity: timestamp, internal request ID, provider request ID when available, environment, project, and feature.
- Routing: requested model, returned model identifier when available, and API endpoint.
- Performance: status, latency, retry count, and streaming or non-streaming mode.
- Consumption: reported input tokens, output tokens, and any separate cache or reasoning usage fields.
Store metadata rather than raw prompts, responses, API keys, or personal information. Use pseudonymous tenant identifiers when allocating costs across customers.
2. Capture usage correctly, especially with streaming
For OpenAI-compatible responses, look for usage fields such as prompt_tokens, completion_tokens, and total_tokens. Other interfaces may expose different names. Normalize these into your own schema while retaining the original usage object for troubleshooting.
Streaming needs extra attention: usage may arrive only in a final event, require an option, or be absent. Check the endpoint documentation and test your actual client. If a stream disconnects before usage arrives, label consumption as unknown or estimated, not zero. Track missing-usage requests as a separate metric.
3. Turn tokens into estimated spend
Maintain a versioned rate table keyed by model and applicable billing category. Include the rate's effective date so a pricing change does not silently rewrite historical estimates.
Basic formula: estimated cost = input tokens / 1,000,000 × input price per million + output tokens / 1,000,000 × output price per million.
For example, 250,000 input tokens and 50,000 output tokens cost 0.25 times the input rate plus 0.05 times the output rate. Use the current published rates, not remembered prices. Apply separate cache or other billing rules where documented; do not add reasoning tokens again if they are already included in billable output.
Keep estimates separate from billed charges. Reconcile daily against the usage or billing records available from your service, using matching time zones and reporting windows.
4. Build a small, useful dashboard
You do not need a dedicated observability platform immediately. Structured application logs feeding a database or your existing metrics stack are enough to start. Display:
- Estimated spend today and month to date, grouped by project and model.
- Requests, input tokens, and output tokens over time.
- Cost per successful task, including its retries and agent subcalls.
- Error rate, retry rate, latency, and missing-usage percentage.
Compare workloads rather than token prices alone. A cheaper model that needs repeated corrections may cost more per completed task.
5. Add alerts and enforceable controls
Set daily and monthly budgets per project. Alert at practical thresholds such as 50%, 80%, and 100%, and flag sudden increases in hourly spend or retry rate. Add application-side output limits, retry caps with backoff, and maximum agent steps.
Alerts do not stop spending. Before admitting work, check the remaining budget and reserve a conservative estimated amount for in-flight requests. Reconcile afterward. Allow headroom for concurrency and delayed usage reporting; an application-side check is not an exact billing cutoff.
6. Keep model comparisons and routing simple
59API provides low-cost, pay-as-you-go access to native, official-quality models without downgrades. Its service base URL is https://api.59api.com, with compatibility for OpenAI SDKs, Claude Code, Codex, and Gemini CLI. Follow the relevant integration instructions for endpoint paths and authentication.
Use your measurements to compare models such as DeepSeek V4 Flash and Claude Sonnet 5.5 on the same representative tasks. Track quality, latency, and cost per successful result before changing routing. If you want to test this workflow across models through one relay, consider signing up for 59API and starting with a small, monitored budget.
Ready to get started?
Connect Claude & GPT in minutes at the lowest prices — full-power, never downgraded. Sign up to get your API key.
Sign up free