Token Economics and Caching — Cost Engineering
← Back to Dashboard
1. Token Economics and Caching2. Routing for Cost/Quality Balance
Token Economics and Caching
📚 Cost Engineering⏱ 10 min⭐ 95 XP⌨ Hands-on lab
Cost Comes from Repetition and Over-Context
Large context payloads and repeated prompts dominate LLM cost curves. Because input tokens are billed on every request, a bloated system prompt is not a one-time cost - it is a tax on every single call, forever.
Where the Money Actually Goes
# A "small" inefficiency at scale
system prompt: 2,800 tokens (could be 900)
history sent: 6,000 tokens (relevant: ~1,500)
per-request waste: ~6,400 input tokens
× 500k requests/month = 3.2B wasted tokens/month
→ at typical mid-tier input pricing, thousands of dollars for zero quality gain
The Optimization Ladder (Cheapest First)
| Lever | Action | Typical impact |
|---|---|---|
| Trim context | Compact system prompts; window/summarise history; send top-3 chunks not top-10 | 20-60% input reduction |
| Prompt caching | Insert cachePoint checkpoints after stable prefixes (system prompt, tool defs, reference docs) - cached reads get up to ~90% discount | Up to ~90% off cached prefix + >80% TTFT win |
| Cap outputs | Set maxTokens to the real need; instruct terse formats (JSON, bullets) | Output tokens cost several × input tokens |
| Right-size the model | Route easy tasks to Nova Micro / Nova 2 Lite / Claude Haiku 5.5 tiers (next lesson) | Order-of-magnitude per-request difference |
| Batch inference | Move non-interactive S3 JSONL workloads to discounted async batch | 50% discount vs on-demand |
Prompt Caching Mechanics on Bedrock (cachePoint)
In the Converse API, place up to 4 cachePoint blocks right after static content in system, tools, or messages (default 5-minute sliding TTL, or optional "ttl": "1h" on supported Claude 5.x and Amazon Nova models):
response = client.converse(
modelId="us.anthropic.claude-sonnet-5-5",
system=[
{"text": large_policy_handbook}, # must meet model min token threshold (e.g. 1,024+ tokens)
{"cachePoint": {"type": "default"}}, # optional: {"type": "default", "ttl": "1h"}
],
messages=[{"role": "user", "content": [{"text": user_question}]}],
)
usage = response["usage"] # inspect cacheWriteInputTokens (miss) vs cacheReadInputTokens (hit)
- Use compact system prompts - every sentence should earn its tokens.
- Trim irrelevant history - summarise old turns instead of resending them.
- Reuse cached context for repetitive workflows where supported.
Instrument first: log inputTokens/outputTokens per route before optimizing. The biggest saving is usually one chatty endpoint nobody suspected - measurement finds it in an afternoon.
⌨ HANDS-ON LABMeasure Prompt Caching Latency and Cost Savings
⭐ +150 XPAnalyze Converse API invocation telemetry to quantify cache read vs write tokens and price reductions.
1Invoke Converse API with a cachePoint block and inspect cache read/write usage metrics.
OBJECTIVE 1 / 1 — type "hint" if stuck
🧪 Knowledge Check
Press 1-4 to select1 of 2
Most avoidable inference cost usually comes from:
TLS handshakes
Oversized repeated context payloads
CloudWatch metrics
S3 object tags