LLM Token Economics, Prompt Caching, and Per-Tenant Cost Attribution
AI features have real variable marginal cost on every request. Unlike traditional web CRUD endpoints where compute per call is fractions of a microcent, an unoptimized multi-step LLM loop with long context can burn dollars per active user per day. Disciplined token economics keeps SaaS gross margins healthy.
Where Token Costs Concentrate
Input tokens typically account for 75 to 90 percent of total tokens in RAG and agent workloads, while output tokens usually cost 3x to 5x more per million tokens and dominate wall-clock latency.
| Optimization Lever | Typical Savings | Implementation Effort | Quality Risk |
|---|---|---|---|
| Prefix Prompt Caching | 50% to 85% on repeated system/tool tokens | Low (reorder static prefix) | None |
| Model Tiering (small for routing) | 40% to 70% on classification turns | Low | Low (verify via evals) |
| Context Compression & Top-K Cap | 25% to 50% on RAG inputs | Medium | Often improves quality |
| Concise Output Schemas | 20% to 40% on output tokens + latency | Low | None |
Making Prefix Prompt Caching Actually Hit
Prompt caches match exact byte-identical prefixes from the start of the request. One tiny mistake destroys cache hit rate: injecting Current time: 2026-11-30T10:14:22Z or a per-request UUID at the top of the system prompt invalidates the cache on every single call!
- Keep the static system prompt, tool definitions, and workspace knowledge policy at the top.
- Round timestamps to the current date (or place timestamps in the final user message) rather than embedding second-level clocks in the system prefix.
- Keep tool array ordering deterministic across requests.
Per-Tenant Cost Attribution
Record normalized cost in micro-dollars (integer math avoids floating-point drift) on every LLM completion alongside tenantId, feature, model, inputTokens, cachedInputTokens, and outputTokens:
export function computeCostMicroUsd(usage: {
uncachedInputTokens: number;
cachedInputTokens: number;
outputTokens: number;
}, ratesPerMillionMicroUsd: {
input: number;
cachedInput: number;
output: number;
}): number {
return Math.round(
(usage.uncachedInputTokens * ratesPerMillionMicroUsd.input +
usage.cachedInputTokens * ratesPerMillionMicroUsd.cachedInput +
usage.outputTokens * ratesPerMillionMicroUsd.output) /
1_000_000
);
}
Next: instrument every call for fast root-cause debugging in LLM Observability: Traces, Spans, and Failure Taxonomies.