Dev.to Security πŸ” Cybersecurity πŸ‘ 0 πŸ“– 5 min read

Auditable AI billing without a blockchain: signed batches and their limits

The difficult part of AI API billing is often the request that almost finished. The router sent the prompt, the provider began responding, and the connection disappeared before final usage arrived. A database can store a

The difficult part of AI API billing is often the request that almost finished. The router sent the prompt, the provider began responding, and the connection disappeared before final usage arrived. A database can store a precise number for that request without making the number well justified.

I am building TeeRouter, and this led to two separate design questions: what evidence supports a charge, and how can someone later detect changes to the published accounting records?

The implementation borrows a useful idea associated with blockchains: publish cryptographically verifiable records that readers can inspect independently. It does not run a blockchain or claim decentralized consensus. This article examines the public source at commit 5c89b551; it is an implementation case study, not an independent certification of the deployed service.

First define what a charge means

A receipt should preserve the pricing decision used for that request. Looking up today's price when displaying last week's usage makes historical charges difficult to reproduce.

TeeRouter freezes the pricing revision, applicable rules, and pool multiplier at admission. Provider-specific calculators interpret native usage fields. The basis is public API list pricing multiplied by the pool's multiplier, not a claim about the merchant's actual subscription expenditure or negotiated invoice.

The billing implementation uses integer arithmetic, retains pre-rounding amounts, and rounds upward once per request to micro-USD. This makes the chosen calculation reproducible without promising to match a provider's monthly invoice rounding, taxes, or discounts.

For a simple illustrative quote, 10,000 ordinary input tokens at $2 per million plus 1,000 output tokens at $8 per million produces a $0.028 base cost. A 1.2 multiplier gives $0.0336. These are made-up rates, not a current model quotation. Real records must also distinguish cached input, applicable pricing dimensions, and how each provider reports usage.

A timeout is an observation, not a usage counter

The interrupted-request policy separates several outcomes:

Observed outcome Accounting treatment
Router rejects the request before forwarding No charge
Upstream rejects it with HTTP 3xx/4xx No charge; any reported counters are retained as waived usage
Sending fails before the complete request is flushed, with no generation evidence No charge
Complete request is flushed, then the connection fails before a reply Estimate input at ordinary input pricing; no unseen output
Generation starts, then delivery fails Use official counters where available and estimates for supported missing portions
A successful response has missing or invalid usage Preserve an explicit pending reason

A generated refusal inside a successful model response is different from an HTTP rejection: generation may have consumed billable tokens.

The controversial row is the timeout after a full flush. A successful local flush does not prove that the remote application received or executed the request. The policy chooses a financial treatment for that ambiguity; it does not eliminate the ambiguity. Customers need to know that an estimated input charge can occur at this boundary.

Estimation is restricted to failed or interrupted model requests and is labelled estimated or mixed, with partial coverage. Unknown hidden output is not invented. A downstream cancellation cancels the upstream operation rather than draining it in the background, although closing a connection cannot guarantee the provider immediately stops computing.

Publishing these distinctions matters more than making every row appear confidently β€œsettled.” A signed estimate remains an estimate.

Sign a batch, retain the exact bytes

The current public receipt format signs batches of records. A manifest includes the boot and key identity, batch number, previous hash, sequence range, record digest, count, and calculated total.

Conceptually, verification follows this chain:

original record bytes -> SHA-256 digest -> manifest
manifest + protocol label -> Ed25519 signature
manifest -> reference to preceding batch

There is one signature per batch. The current public format does not have per-record Merkle inclusion proofs. A reader needs the original complete records_json to verify the records; reserializing JSON may change the bytes being checked.

The verifier distinguishes a valid signature from an independently trusted TEE signing identity. The latter also requires hardware evidence and an approved reference policy. It is not established by a public key or a β€œverified” label included in the downloaded bundle.

Hash links let readers check continuity between supplied batches. They do not create consensus, prove that an operator has disclosed the latest batch, or prove that no other history exists. Independently retaining known batch heads could strengthen monitoring, but that is a separate system property, not something the signature supplies automatically.

Storage acknowledgment is not settlement

Consider a batch that reaches external storage, followed by a lost acknowledgment. Regenerating it with new metadata would complicate duplicate detection. TeeRouter freezes the signed bytes before upload and resends those same bytes on a later cycle.

A valid durable-storage acknowledgment permits releasing the uploaded raw details. It does not reset consumption or update user balances. Per-user, per-batch summaries remain until a separate settlement acknowledgment applies a balance update through an identified uploaded head.

That boundary is necessary because requests may finish while settlement is happening. Retiring consumption only through the acknowledged head preserves newer charges. Revisions and idempotent retries prevent repeating the same settlement from applying it twice.

The default upload cycle is five minutes. That is a delivery schedule, not a guarantee of completeness or immediate publication.

What survives a restart?

The optional accounting journal stores committed events and delivery state in SQLite WAL with authenticated encryption. Frozen batches keep their original signatures after restart; new batches use the new process identity.

It protects successfully committed records. It cannot reconstruct an in-flight request that never produced an event, recover usage that was never observed, or prevent deletion or rollback of an entire journal copy. On a journal write failure or capacity limit, the implementation closes new admission instead of silently discarding accepted events.

This makes durability a concrete, bounded claim rather than β€œnothing can ever be lost.”

Public accounting without publishing conversations

Public batches exclude prompts, answers, credentials, and refusal text. They contain accounting fields and refusal categories. User pseudonyms rotate using a fresh public batch nonce and a private random identity, rather than hashing a short username.

That reduces direct linkage across batches but does not guarantee anonymity. Timing, model choices, merchant identifiers, and distinctive traffic volumes can still correlate activity.

The resulting ledger is useful because readers can check the bytes they received, inspect charge evidence, and distinguish estimates from observed usage. Deposits, withdrawals, externally managed balances, missing history, and provider-side execution remain separate questions. Keeping those boundaries explicit is what makes the evidence meaningful.

Writing disclosure: This article was drafted by an AI assistant from the linked public source code and protocol documentation. The account owner maintains TeeRouter.

πŸ“° Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.