Listen up. If you're building modern applications, understanding Cost Monitoring in AI & Artificial Intelligence is non-negotiable. This is where simple logic turns into intelligent behavior.
1Tokens Are the Unit of Billing
LLM APIs don't charge per request or per word ā they charge per token, a sub-word chunk of text where roughly 1000 tokens equals 750 English words. Every prompt you send and every response you receive gets billed this way, so a chat feature's cost scales directly with how verbose its prompts and responses are.
Understanding tokens isn't optional for anyone shipping a paid AI feature ā it's the actual unit your margin is calculated in.
// Example
console.log("Running AI concept...");AI logic processed successfully.
2Reading the usage Object
Every completion response includes a usage object breaking down prompt_tokens (input) and completion_tokens (output) for that specific call ā this is the authoritative source for what a request actually cost, not an estimate you compute yourself.
Production systems log this object for every call, typically attached to the user id and feature name, so cost can later be attributed to specific users or product surfaces.
const completion = await openai.chat.completions.create({...});
// Extracting the usage data
const usage = completion.usage;
console.log(`Input: ${usage.prompt_tokens}, Output: ${usage.completion_tokens}`);AI logic processed successfully.
3Input and Output Are Priced Differently
Providers typically charge a higher rate per output token than per input token, since generation is more computationally expensive than processing the prompt ā a 2x rate difference between input and output pricing is common across major providers.
This means the actual cost formula is (prompt_tokens Ć inputRate) + (completion_tokens Ć outputRate), not a single flat per-token rate applied uniformly.
const INPUT_PRICE = 0.03 / 1000;
const OUTPUT_PRICE = 0.06 / 1000;
const total = (usage.prompt_tokens * INPUT_PRICE) + (usage.completion_tokens * OUTPUT_PRICE);AI logic processed successfully.
4Hard Limits: Check Before You Spend
A hard limit check runs before the API call, not after: if user.balance <= 0, the middleware returns a 402 Payment Required and never calls the LLM at all, guaranteeing you never spend money you can't recoup from a depleted account.
Checking after the call is too late ā the API cost has already been incurred by the time you'd find out the user couldn't pay for it.
if (user.balance <= 0) {
return res.status(402).json({ error: 'Insufficient credits' });
}
next(); // Proceed to API callAI logic processed successfully.
5Real-Time Monitoring Protects Sustainability
Logging cost per request as it happens (rather than reconciling against a provider invoice at month-end) is what lets you catch a runaway usage spike ā a bug causing infinite retries, or an abusive user ā within minutes instead of discovering a five-figure bill weeks later.
A dashboard or alert on aggregate spend per hour is a cheap safeguard relative to the cost of an undetected billing incident.
Budget: Protected
AI logic processed successfully.
6Counting Tokens Locally with tiktoken
Different model families split text into tokens differently, so you can't reliably estimate cost by counting characters or words ā the tiktoken library implements the exact tokenizer OpenAI's models use, letting you count tokens locally before a request is even sent.
This is essential for pre-flight checks: estimating a prompt's cost, or trimming a message array down to fit a budget, both require knowing the real token count in advance.
import { encoding_for_model } from 'tiktoken';
const enc = encoding_for_model('gpt-4');
const tokens = enc.encode('Hello world').length;AI logic processed successfully.
7Monitoring Speed, Not Just Spend
Tracking tokens-per-second (generation speed) alongside dollar cost catches a different class of problem: a provider having a slow day, or a model swap that's technically cheaper but noticeably slower, both degrade user experience without necessarily costing more money.
A cost dashboard without a latency/throughput dashboard next to it only tells half the production-health story.
const tps = usage.completion_tokens / (endTime - startTime);
console.log(`Generation Speed: ${tps} tokens/sec`);AI logic processed successfully.
8From Feature to Sustainable Business
Combining accurate token accounting, hard spending limits, and real-time monitoring is what separates a demo that works fine with a handful of test users from a product that can be priced, billed, and scaled to thousands of paying customers without an unpredictable cost structure.
Every piece covered here (usage extraction, pricing math, budget checks, local token counting, throughput monitoring) is a required component of that system, not an optional nice-to-have.
Business: Scalable
AI logic processed successfully.
9Next: Managing Conversation Context
Cost monitoring is one half of the operational picture; the next lesson covers the other half ā managing conversation history and context windows so long-running chats stay both affordable and within model limits.
Context Mastery Next
AI logic processed successfully.
10Step-by-Step Breakdown
LLMs like GPT-4 process text in chunks called 'tokens'. 1000 tokens roughly equals 750 words. To build a profitable SaaS, you MUST track these.
Every API call returns a usage object. You must extract this data to deduct costs from your user's balance or track internal spending.
Input tokens (your prompt) are usually cheaper than Output tokens (the response). Let's calculate the cost for a GPT-4 call.
Checkpoint: Which object inside the OpenAI response contains the information about how many tokens were used?
- āresponse.metadata
- āresponse.usage
If a user has 0 credits, do NOT call the API. Implement a 'Hard Limit' check in your middleware before calling OpenAI.
By monitoring costs in real-time, you ensure that your application remains sustainable and your budget is protected.
Checkpoint: Why should you check a user's credit balance BEFORE making the API call instead of after?
- āTo reduce API latency
- āTo prevent incurring costs that the user cannot pay for
Tokenization varies by model. GPT-4 might tokenize differently than Claude or Llama. Always use model-specific libraries like 'tiktoken' for local counts.
Monitoring isn't just for costs; it's for quality. Track tokens-per-second to ensure your user experience doesn't degrade.
Checkpoint: Which JavaScript library is the standard for counting OpenAI-compatible tokens locally?
- āgpt-count
- ātiktoken
Cost management mastered! You're now ready to build a sustainable and scalable AI business.
Next, we'll learn how to manage complex conversation histories and chat context.
Compute a Real API Cost. Finish computing the dollar cost of a request from its input and output token counts.
Level Up š
Advanced cheat sheets, SEO tricks, and interview prep for this topic.
Browser Support
Fully supported.
Fully supported.
Fully supported.
Fully supported.
Accessibility (A11y)
1Surface Remaining Usage/Credits Clearly
If a product exposes a usage-based quota (e.g. '50 AI messages left this month'), display it as real text content near the input, not just a color-coded progress bar ā a screen reader user needs the same numeric information a sighted user gets at a glance.
<p>You have 12 AI credits remaining this month.</p>SEO Implications
- 1
Cost Dashboards Are Internal Tooling, Not Public Pages
Token usage logs and cost dashboards are operational tools for your team, never meant to be public or indexable ā the only SEO-relevant content in this domain is documentation like this page explaining the pattern, which should be genuinely unique rather than duplicated boilerplate.
Best Practices
Set max_tokens Explicitly, Don't Rely on Defaults
An unbounded or overly generous max_tokens setting lets a single unusually long generation blow past your expected cost-per-request; setting a deliberate ceiling caps the worst-case cost of any individual call.
Attribute Cost to a Feature, Not Just a User
Log which feature (chat, summarization, image generation) triggered each API call, not just which user triggered it ā this is what lets you later answer 'which feature is actually driving our AI spend' instead of just 'who is spending the most'.
Frequent Bugs
Calculating cost using a hardcoded per-1000-token price that's gone stale after a provider price change.
Provider pricing changes periodically and silently from the perspective of your codebase. Keep pricing constants in a config value you can update without a code deploy, or fetch current pricing from the provider's pricing API/docs periodically rather than hardcoding it once and forgetting about it.
Real-World Examples
A Per-User Credit System
A SaaS product converts each user's subscription tier into a monthly token budget, deducts the exact usage.prompt_tokens + usage.completion_tokens from that budget after every call, and returns a 402 the moment the budget hits zero ā giving finance a predictable per-tier cost ceiling regardless of how chatty individual users are.
await db.user.update({
where: { id: userId },
data: { tokensUsed: { increment: usage.total_tokens } }
});