Lesson 6 of 9 · 25m
Token Economics and Cost Control
Model cost as a product requirement, instrument token spend by feature and tenant, and use architecture instead of wishful prompts to control it.
LLM cost surprises rarely come from one gigantic prompt. They come from ordinary product success: more users, longer transcripts, bigger retrieval contexts, retries, agents looping, eval suites, and background jobs nobody remembered. If cost is not part of the design, your feature margin becomes whatever the model decides to consume.
Know the unit economics
Start with a simple calculation:
cost per request =
(input_tokens x input_price_per_token)
+ (cached_input_tokens x cached_input_price_per_token)
+ (output_tokens x output_price_per_token) + tool_or_reranker_costThen multiply by real usage: requests per session, retries, agent steps, background summarization, and evaluation runs. Output tokens are often the expensive surprise because verbose answers feel free during a demo. Instrument by feature, model, tenant, and endpoint:
usage_events.emit({
"feature": "support_copilot", "tenant_id": tenant.id, "model": response.model,
"input_tokens": response.usage.input_tokens, "output_tokens": response.usage.output_tokens,
"cache_read_tokens": response.usage.input_tokens_details.cached_tokens,
"request_id": request_id,
})Aggregated monthly cost tells you finance is unhappy. Per-feature usage tells you what to fix.
Reduce input before upgrading intelligence
The cheapest token is the one never sent. Common reductions are architectural:
transcript trimming keep only turns relevant to current workflow
retrieval narrowing filter by product, locale, ACL, date, and version
deduplication remove repeated chunks and near-duplicates
structured state replace raw history with typed workflow state
prompt pruning delete examples that no longer change decisions
compression summarize stable background once, not every turnDo not compress blindly. A vague summary can cause a wrong answer that costs more than the tokens you saved. Preserve exact identifiers, dates, amounts, and policy constraints as structured fields.
Choose models by job, not by prestige
Use expensive models where ambiguity, judgment, or difficult synthesis dominates. Use smaller models for classification, extraction, routing, query rewriting, and first-pass drafts:
router/extractor small, deterministic-ish, structured output
retrieval rerank specialized reranker or small model
customer answer stronger model, constrained by evidence
escalation summary mid-tier model, low variance
nightly eval judge stronger judge on sampled failuresThis is not just cost optimization. Smaller models often have lower latency, which improves the product.
Bound generation explicitly
Set maximum output tokens, maximum tool steps, maximum retrieval chunks, per-user budgets, and per-tenant budgets. Enforce them server-side:
response = client.responses.create(
model=model,
input=messages,
max_output_tokens=900,
timeout=30,
)Timeouts and token caps are not the same. A request can be fast and expensive, or slow because the provider is overloaded. Use both.
Cache the right layer
Provider prompt caching can reduce repeated prefix cost. Application-level semantic caching can avoid a call entirely, but it is risky when answers depend on tenant state, document freshness, or user entitlements. Cache stable reference answers, not account-specific advice. Key caches with corpus revision, prompt version, model, locale, and authorization context.
Field notes
- Retry logic can multiply cost during provider incidents; use backoff, jitter, and circuit breakers.
- Streaming does not reduce token cost; it improves perceived latency.
- Long outputs may be a product defect. If users only need three bullets, do not pay for twelve paragraphs.
- Evaluation suites can become a major hidden expense; sample large suites in CI and run full suites on release candidates.
- Cost controls need failure behavior: degrade to cached answers, smaller context, or human escalation rather than silently stopping.
Was this lesson helpful?