FastAPI service: semantic caching in one line
Byte-identical repeats are served from the exact cache at $0 and near-duplicates from the opt-in semantic cache. Here is the change for FastAPI service, the switch that turns it on, and the arithmetic on gpt-6.1-sol.
FastAPI service reaches TokenZero with one base-URL argument and keeps your openai key. Every byte-identical repeat is served at $0: at a 20% repeat share, 100,000 reference calls on gpt-6.1-sol cost $560 instead of $700 a month; the semantic cache extends that to near-duplicates when you opt in.
· Source: pricing table 2026-10-26; overhead from the published benchmark on /benchmarks
At a glance
- Framework
- FastAPI service (Python)
- Capability
- semantic caching
- Change
- app/llm.py: base URL argument
- Request shape
- OpenAI chat / Responses
- Turn it on
- X-TokenZero-Semantic-Cache: true
- Workspace setting
- semantic_cache: true, cache_ttl: 3600
- Reference model
- gpt-6.1-sol ($2.00 in / $10.00 out per 1M, table 2026-10-26)
- Exact-cache saving at a 20% repeat share
- $140/month (computed)
- Gateway overhead (measured)
- 1.52 ms p50, 3.42 ms p95
- Parity floor
- 99.8%
- Free tier
- $500/month routed spend
Reference workload on gpt-6.1-sol: 2,000 input (1,500 static) / 300 output tokens, 100,000 calls per month, table 2026-10-26
| Scenario | Per call | Per month | Delta |
|---|---|---|---|
| List price, no gateway | $0.00700 | $700 | — |
| Static prefix cached (prompt compression) | $0.00415 | $415 | $285 (41%) |
| 20% exact-cache repeats | $0.00560 | $560 | $140 (20%) |
| All calls cascaded to gpt-6-luna | $0.00035 | $35 | $665 (95%) |
| Budget guardrail at $500/month | — | capped at $500.00 | 402 after 71,428 calls |
The change
1. Point FastAPI service at the gateway (app/llm.py):
-client = AsyncOpenAI()
+client = AsyncOpenAI(base_url="https://tokenzero.sh/v1")
@app.post("/answer")
async def answer(q: Query):
r = await client.chat.completions.create(model="gpt-6.1-sol", messages=[{"role": "system", "content": SYSTEM}, {"role": "user", "content": q.text}])Read X-TokenZero-Request-Id from the response to attach the signed receipt to your own logs.
2. Turn on semantic caching. Per request: X-TokenZero-Semantic-Cache: true (exact cache is automatic for temperature-0 or seeded requests; X-TokenZero-Cache: always forces it). Per workspace (applies to every call without touching code): PATCH /v1/workspaces/settings {semantic_cache: true, cache_ttl: 3600} or the dashboard toggle.
3. Prove it. npx -y @tokenzero/cli doctor checks the gateway, the signer, the pricing table and your key; then watch cache_hit_rate_pct and savings from engine cache on the scorecard.
Questions people ask
How do I point FastAPI service at TokenZero?
Pass base_url / baseURL = https://tokenzero.sh/v1 where FastAPI service builds its client (app/llm.py) and keep your openai key; nothing else changes.
What does semantic caching save on gpt-6.1-sol?
Every byte-identical repeat is served at $0: at a 20% repeat share, 100,000 reference calls on gpt-6.1-sol cost $560 instead of $700 a month; the semantic cache extends that to near-duplicates when you opt in.
Does semantic caching change the model's output?
The exact cache returns the provider's own earlier response for a byte-identical request; the semantic cache is opt-in and only matches within the distance you accept. Non-deterministic requests are never cached unless you force it.
What does TokenZero cost?
Free up to $500 per month of routed spend; then 15% of verified savings with a 3x-ROI-or-$0 rule, capped plans available. Gateway overhead measured at 1.52 ms p50.