TokenGuard

System shape

Single binary. Three stores. Fail closed.

TokenGuard is a Go HTTP service. The entry point wires config, optional guard dependencies, and an http.ServeMux. Ops UI stays embedded at /dashboard — this page is the map.

Client app
TokenGuard
LLM provider
402 / 409 / 400
Turso
Redis

Guard on → budget + loop preflight → upstream or block → settle usage into Turso

Data stores

  • Turso (libSQL)Billing ledgerusers, api_keys, user_budgets, usage_events, model_prices
  • Upstash Redis RESTLoop stateShort-TTL counters keyed by session + payload hash
  • Pricing catalogCost estimateTurso model_prices + OpenRouter sync; optional pricing.json bootstrap

Packages

  • cmd/tokenguard — bootstrap, routes, shutdown
  • internal/proxy — reverse proxy, guard, providers, mgmt
  • internal/billing — Turso store, budgets, keys, usage
  • internal/cache — Upstash + loop circuit breaker
  • internal/models — pricing engine + OpenRouter sync
  • web/ — product portal, operator console, and integrator docs (Next.js)

Full write-up: docs/ARCHITECTURE.md · How it works