Executive Summary

A practical blueprint for logging raw usage and generating invoices from a versioned pricing table—no vibes, no disputes.

Billing becomes a nightmare the moment you mix ‘estimated usage’ with ‘real money’. The fix is boring and powerful: store raw usage facts, then compute invoices from a versioned pricing table.

If someone disputes an invoice, you don’t argue. You replay the ledger.

“Production is where good ideas meet boring reality. The winners instrument the boring part.”
AI & Dev Dispatch

The Core Idea

Most “AI failures” are system failures: missing contracts, missing logs, missing ownership lines. Fix the system, and the model suddenly looks smarter.

Contract

Define the stable input/output boundary first.

Logs

Capture raw facts, not just summaries.

Policy

Centralize allow/deny decisions and expose reason codes.

UX

Make failure legible and recoverable.

// Store raw usage facts, not estimates.
INSERT INTO usage_ledger (
  request_id, org_id, user_id,
  provider, model,
  input_tokens, output_tokens,
  created_at
) VALUES ($1,$2,$3,$4,$5,$6,$7, NOW());

That snippet is not a complete app. It’s a reminder: your system should prefer verifiable facts over narrative.

Failure Modes You’ll Actually See

  • Aggregating too early

    Storing only totals removes the ability to audit and replay.

  • No pricing version

    If pricing changes, old invoices become ambiguous without a historical table.

  • Missing provider facts

    You need model, provider, raw input/output token counts, and timestamps.

  • No dispute workflow

    A ledger is useless if you can’t generate a human-readable explanation quickly.

Implementation Notes

Persist raw usage per request: provider, model, input_tokens, output_tokens, request_id, org_id, user_id, timestamp.

Compute invoices from a pricing table with effective_start/effective_end and a version string.

Expose downloadable CSVs built from the ledger, not from cached totals.

For architecture and rollout planning, use the Contact Hub.

Ship‑Ready Checklist

Use this as a pre‑deploy gate. If you can’t check these boxes, don’t pretend you’re “done.”

- A single source of truth for versions (prompt/policy/schema) and a way to display them in-app.
- Request correlation ID visible in UI, logged server-side, and searchable.
- Explicit failure UX: what happened, why, and a safe next step.
- An audit trail you can replay: inputs, decisions, outputs, and cost facts.
- A small test harness (even 20 cases) that runs before deployment.

Want this turned into a working product?

Use the Contact Hub to scope features, security, billing, and the deployment plan.

Open Contact Hub