How to Monitor AI API Spend in Real Time
Why usage monitoring matters
AI spend rarely explodes because of one giant request. It usually creeps up through repeated retries, long prompts, agent loops, and a few teams using the wrong model for the job. If you are building on Claude or GPT models, you need a simple way to answer three questions every day: how many requests did we send, how many tokens did we burn, and what did that cost?
That is where a relay like 59API helps. It gives developers cheap, pay-as-you-go access to Claude Opus, Sonnet, Haiku, Fable, and GPT models through the same OpenAI-style flow many apps already use. Because it is compatible with Claude Code, Codex, and any OpenAI SDK, you can monitor spend without rewriting your stack. The base URL is https://api.59api.com, so you can point your existing client there and start logging usage immediately.
Step 1: Log usage on every request
The most important habit is to store the usage object returned by the API. In OpenAI-compatible responses, this usually includes prompt tokens, completion tokens, and total tokens. Once you have that, you can calculate estimated cost per request and roll it up by user, project, or model.
Python example: from openai import OpenAI; import os; client = OpenAI(base_url='https://api.59api.com', api_key=os.getenv('AI_API_KEY')); resp = client.responses.create(model='claude-sonnet-4', input='Summarize this support ticket in one sentence.'); usage = getattr(resp, 'usage', None); print('request_id=', getattr(resp, 'id', 'n/a')); print('prompt_tokens=', getattr(usage, 'input_tokens', 0)); print('output_tokens=', getattr(usage, 'output_tokens', 0))
JavaScript example: import OpenAI from 'openai'; const client = new OpenAI({ baseURL: 'https://api.59api.com', apiKey: process.env.AI_API_KEY }); const resp = await client.responses.create({ model: 'gpt-4o-mini', input: 'Draft a short changelog entry.' }); console.log({ id: resp.id, usage: resp.usage });
If your SDK version returns slightly different field names, keep the same pattern: capture the raw response, store the request id, and persist the usage payload in your logs or database.
Step 2: Turn tokens into dollars
Token counts are useful, but cost is what your finance team cares about. Keep a small price table in your app for each model you use, and calculate spend with a formula like this:
Estimated cost = (input_tokens / 1,000,000) x input_rate + (output_tokens / 1,000,000) x output_rate
For example, if one request uses 2,000 input tokens and 500 output tokens, and your chosen model has separate input and output rates, the math is instant. This is especially handy when you compare Claude Opus for hard reasoning versus Haiku or Fable for lighter tasks. On 59API, the low pay-as-you-go pricing makes that model comparison easier because you are not forced into expensive minimums or bloated commitments.
Step 3: Add budget guardrails
- Set a daily cap: stop background jobs when spend crosses a threshold.
- Route by task: use cheaper models for classification, summarization, and extraction.
- Trim prompts: remove duplicated context and giant chat history when it is not needed.
- Batch non-urgent work: one well-structured request is cheaper than many tiny retries.
- Alert on anomalies: if one endpoint suddenly doubles its token usage, investigate immediately.
A simple SQLite or CSV ledger is enough for small teams. For example, write one row per request with timestamp, model, prompt_tokens, output_tokens, estimated_cost, and environment. Then group by day to spot spikes before they become invoices.
Step 4: Watch the expensive patterns
Most teams discover the same three waste sources: agents looping on the same task, long system prompts copied into every call, and large-model overuse for small jobs. Because 59API offers official-quality native models at some of the lowest relay costs, you can keep the high-quality model for the hard part and use cheaper models for everything else. That is a practical way to reduce spend without sacrificing output quality.
If you already use Claude Code or the OpenAI SDK, the switch is straightforward: change the base URL, keep your existing instrumentation, and start comparing spend per feature. If you have not tried a relay before, 59API is a smart place to begin because it combines compatibility, low cost, and a referral rebate that can lower your net spend even further.
A simple weekly review checklist
- Top 10 endpoints by token usage
- Top 10 users or jobs by estimated cost
- Average cost per successful task
- Requests that retried more than once
- Largest prompts and longest completions
Review these every week, and you will catch waste early. If you want a cheaper place to run the same workflow, sign up for 59API, point your OpenAI-compatible client at https://api.59api.com, and start tracking spend from your very first request.
शुरू करने के लिए तैयार?
कुछ ही मिनटों में Claude और GPT जोड़ें, सबसे कम कीमत पर। साइन अप करें और API key पाएं।
मुफ़्त साइन अप