Skip to content

Case study 02 · Public repo · MVP, no production traffic

A cost ledger withone row per request.

An LLM cost gateway. Point an OpenAI or Anthropic client at it and each request is priced twice: what you paid, and what you would have paid without caching.

Role
Design, build, tests
Stack
TypeScript, Cloudflare Workers, Hono, Next.js, Postgres, OpenTelemetry
Runs on
Local Workers runtime in the demo
Status
Public, MIT, 18 tests, MVP

Read the code ↗

npm run demo:all · synthetic traffic, mock upstream
Totals
  paid              $0.018345
  would have paid   $0.033284
  ledger saved      $0.016075  (sum of per-request savedUsd, each floored at 0)
  net saved         $0.014939  (would have paid - paid; includes the cache-write premium)
  net saving rate   44.9% of would-have-paid
  upstream calls    8 for 9 requests

Traces: 9 spans received by the OTLP sink.

Synthetic traffic against a mock upstream: these figures show the accounting works, not what real traffic saves.
The request mix was built so that seven of nine requests benefit from a cache. The 44.9% is a property of that mix, not of Tokenwise.

The problem

The model bill arrives as one number.

A provider invoice says what the month cost. It does not say which feature spent it, whether caching is switched on, or what the same traffic would have cost without it.

Tokenwise sits between the client and the provider. It turns on Anthropic prompt caching for stable prefixes, serves exact duplicates from a response cache, and writes one ledger row per request so any total can be recomputed from rows.

How it works

Follow one requestthrough it.

Drag sideways to follow it →

Proxy · Cloudflare Worker (Hono)POST /v1/…after the responsehitresponseClient appOpenAI or Anthropic SDK1 Authtwk_ key → project config2 Response cache lookupexact duplicates only3 Prompt-cache injectioncache_control on stable prefixes4 Forwardwith the project's own provider key5 Cost mathpaid vs would-have-paidCONFIG_KVkeys + configCACHE_KVresponsesOpenAI / Anthropicthe providerWeb app /api/ingestPostgres ledger_eventsOTLP collector
Cache miss: forwarded to the provider, then priced twice.Response-cache hit: answered from the cache, no provider call.
  1. Fail open

    If the gateway itself breaks, the request is forwarded untouched.

  2. No database on the hot path

    The Worker reads only KV. Ledger and trace export happen after the response.

  3. A conservative cache

    Opt-in per project. Never caches streams, temperature above 0, tool use or errors.

  4. Priced twice

    Paid, and would-have-paid on the same model with no caching. No model routing.

What is measured

Test figures. Not a savings claim.

0
requests of production traffic. No real-world saving exists to quote
18
tests: 9 on the cost math, 9 end to end through the proxy
9 of 9
demo requests produced a ledger event and a span
8
provider calls for those 9 requests: one was a cache hit

Read this before the numbers

  • The line worth reading is the gap between ledger saved and net saved. Writing a cache entry costs more than not caching, and a per-request floor at zero hides that premium. The honest total is would-have-paid minus paid.
  • Every dollar figure is arithmetic on a hand-entered pricing table that was not re-checked against the providers' price pages.
  • Latency added by the proxy, throughput and behaviour under real traffic are not measured.

What it leaves out

The limits,stated plainly.

  • Model routing

    Not built. Savings are only ever counted against the model the client asked for.

  • Budgets and alerts

    Not built.

  • A daily rollup

    The table exists in the schema. Nothing fills it yet.

  • A replay queue

    A ledger event that fails to ingest is dropped.

  • The OpenAI Responses API

    Forwarded, but its usage shape is not parsed, so those ledger rows are not reliable.

  • A deployment with traffic

    It has run under the local Workers runtime only.