Token usage is logged per request for cost monitoring
Why it matters
Token counts are the primary cost driver for every AI API integration, but they are invisible by default if not explicitly captured. Without logging prompt_tokens, completion_tokens, and model name per request, you cannot answer basic operational questions: which feature costs the most, which users are consuming disproportionate tokens, whether a prompt change improved efficiency, or whether usage is trending toward a budget ceiling. NIST AI RMF MEASURE 2.5 requires monitoring AI system resource consumption. Unlogged token usage means you discover cost problems only on the monthly bill.
Severity rationale
High because unlogged token usage makes cost monitoring, quota enforcement, and abuse detection operationally impossible — problems only surface on billing statements.
Remediation
Capture the usage object from every AI response and persist it alongside the request. Include model name so you can break down costs by model tier.
// src/app/api/chat/route.ts
const result = await generateText({
model: openai("gpt-4o"),
messages,
maxTokens: 1000,
});
await db.insert("ai_request_logs").values({
session_id: sessionId,
user_id: userId ?? null,
model: "gpt-4o",
prompt_tokens: result.usage.promptTokens,
completion_tokens: result.usage.completionTokens,
total_tokens: result.usage.totalTokens,
created_at: new Date(),
});
If a database table is premature, use structured console logging: console.log(JSON.stringify({ event: "ai_request", model, ...result.usage })). Verify by making an AI request and checking the log output for a record with token counts.
DetectionPro
The detection method for this pattern is part of AuditBuffet Pro.
The full method (what to look for, pass and fail conditions, and how results get reported) ships inside the AI Token Optimization Audit. The pattern definition, severity rationale, and remediation remain openly licensed under CC-BY-4.0.
External references
- nist-ai-rmf:1.0 · MEASURE 2.5 — AI risk metrics are monitored
- iso-25010:2011 · reliability.maturity — Maturity — token usage logged for operational monitoring
Taxons
History
- 2026-04-18·v1.0.0·Initial import from ai-token-optimization·automated