A prepaid wallet should reserve funds before generation begins, increase that hold from deduplicated cumulative usage while the response is streaming, and reconcile the final charge when the stream ends. Each update should be an atomic, idempotent ledger operation tied to a stable stream ID. If the wallet cannot fund an increase, the system should follow a defined policy—such as stopping generation at a safe boundary—rather than allowing unbounded usage.
The Short Answer: Reserve Before Generation, Increase Against Cumulative Usage, Then Reconcile
A robust streaming lifecycle has six stages:
- Authorize: Calculate an initial hold from the request limit, estimated usage, or a policy-defined ceiling.
- Start: Create the reservation and admit the request as one concurrency-safe decision.
- Measure: Receive provisional usage observations from the inference serving layer.
- Extend: Increase the reservation from a monotonic usage high-water mark, using idempotent ledger operations.
- Stop or continue: If the next increase succeeds, continue streaming. If it fails, apply the documented insufficient-funds policy.
- Finalize: Reconcile against authoritative final usage, post the charge once, and release any unused reservation.
This design limits the gap between funded usage and generated usage without requiring a separate wallet write for every token. The exact reservation size, update cadence, pricing calculation, and stop policy remain implementation choices because workloads and commercial rules differ.
Keep usage measurement separate from monetary accounting
Several related values should not be treated as interchangeable:
- Generated usage is work completed by the model or serving system.
- Delivered usage is output successfully transmitted to the client.
- Authoritative metered usage is the usage record selected for charging under the service’s rules.
- Reserved funds are temporarily held against expected charges but have not yet been posted.
- Posted charges are finalized ledger entries for consumed service.
- Settlement is the process that converts the final metered amount into posted charges and releases the remaining hold.
A token event can inform the expected charge, but it should not directly mutate a balance without ledger controls. For example, generated output may differ from delivered output after a client disconnect, and the applicable charging policy must determine which measurement is billable.
Pricing can also involve more than one token counter. Input tokens, output tokens, cached tokens, tool calls, media units, or other billable events may have different rates. Model-specific tokenization can affect the final count. The reservation service should therefore consume a normalized usage-and-price calculation rather than assume that every streamed event represents one identically priced unit.
Treat the wallet ledger as authoritative for posted, reserved, and available funds
The wallet ledger—or an equivalent auditable accounting record—should remain the source of truth for monetary state. Serving telemetry reports usage; it does not decide whether funds have been reserved or posted.
Under a simple accounting convention, funded value is allocated among:
- Available value that can fund new work
- Active reservations for admitted requests
- Posted charges for completed usage
Top-ups, refunds, credits, and adjustments introduce additional entries, but every transition should remain traceable. A single mutable balance field without durable reservation and posting records makes concurrency, retries, and recovery difficult to reason about.
The ledger should also preserve the relationship between a stream and its financial transitions. A reservation record will commonly need a stable stream identifier, current reserved amount, covered usage high-water mark, status, expiration or lease information, and identifiers for idempotent updates. These are architectural examples rather than a prescribed schema.
Create an Initial Reservation Before the First Output Token
The system should consider affordability before admitting open-ended generation. Waiting until completion to check the balance can leave the wallet exposed to the entire charge, especially when multiple requests use the same prepaid balance concurrently.
Estimate the initial hold from an explicit request limit or policy ceiling
The initial reservation can be derived from one or more known inputs:
- The charge for metered input already accepted
- A client-supplied maximum output limit
- A server-enforced generation ceiling
- A policy-based estimate for the workload class
- Expected charges for tool calls or other enabled billable units
- Applicable treatment of cached input or reused context
The estimate should use the pricing and tokenizer rules applicable to the selected model. It should not silently assume that input and output have the same price or that every provider counts cached and uncached usage identically.
A reservation does not have to cover the theoretical maximum request charge in every design. A smaller initial hold followed by incremental extensions may reduce unnecessarily locked funds, but it increases ledger activity and the possibility that a stream must be interrupted later. Reserving the maximum simplifies some failure decisions but can reduce available purchasing capacity for other concurrent requests.
This is a policy trade-off rather than a universally correct formula. Latency-sensitive chat, batch enrichment, and agentic workflows can warrant different serving and reservation policies because they differ in duration, concurrency, interruption cost, and predictability.
Make admission and reservation one atomic, conditional decision
Admission should succeed only if the reservation is successfully created. Conceptually, the ledger operation is:
> If sufficient available value exists, move the requested amount from available to reserved for this stream; otherwise, reject admission.
The check and transition must be atomic or protected by an equivalent concurrency-control mechanism. Reading a balance and then writing a reservation in separate, unguarded operations allows two streams to observe the same available funds and both proceed.
The appropriate implementation might use a transactional database update, compare-and-swap operation, serialized wallet actor, or another conditional-write design. The specific mechanism depends on the wallet’s storage and consistency architecture. Regardless of mechanism, admission should receive a definitive outcome before output is released under the prepaid policy.
Every initial request should also carry a stable idempotency key. If an API gateway or client retries after a timeout, the wallet should return the existing reservation result rather than create a second hold for the same logical stream.
Increase the Hold from a Monotonic Usage High-Water Mark
Once streaming begins, reservation updates should be based on cumulative covered usage or cumulative charge—not by repeatedly debiting raw token deltas with no deduplication.
Suppose a stream reports cumulative output usage of 100 units and later 160 units. If the reservation already covers usage through 100, the second update should reserve only the additional charge associated with the move from 100 to 160. If the 160-unit event is delivered twice, the second copy should produce no additional reservation.
For pricing with multiple dimensions, the high-water mark may be a normalized cumulative charge or a structured usage vector covering input, output, cached input, tool calls, and other units. The important property is that the amount already covered by the reservation is durable and cannot move backward during the active stream.
Use stable identities, sequencing, and idempotency
Each reservation-extension request should include enough information to identify and order the operation. Common controls include:
- A stable request or stream ID
- An idempotency key for the extension operation
- A monotonic usage sequence number or version
- The cumulative usage or cumulative calculated charge
- The previously covered high-water mark or expected reservation version
Sequence numbers help reject stale events, while idempotency keys prevent the same event from being applied twice. Cumulative values make recovery safer because a consumer can compare the latest observation with durable state instead of reconstructing the total from every intermediate delta.
Out-of-order events should never reduce the reserved high-water mark. Duplicate events should become no-ops. Conflicting updates should be retried against the latest ledger version rather than blindly appended as additional holds.
Apply each increase as an atomic ledger transition
A reservation extension should conditionally move only the required additional amount from available to reserved. The operation should verify that the stream is still active, the incoming high-water mark is newer, and sufficient available funds exist under the selected policy.
The following pseudocode is a general design pattern, not Token Forge Cloud product behavior:
function extend_reservation(stream_id, event_id, sequence, cumulative_usage):
begin transaction
if operation_exists(event_id):
return prior_result(event_id)
reservation = lock_active_reservation(stream_id)
if sequence <= reservation.last_sequence:
record_noop(event_id)
commit
return reservation
target_charge = price(cumulative_usage, reservation.pricing_snapshot)
additional_hold = max(0, target_charge - reservation.reserved_value)
if wallet.available_value < additional_hold:
record_extension_failure(event_id, stream_id, sequence)
commit
return INSUFFICIENT_FUNDS
wallet.available_value -= additional_hold
wallet.reserved_value += additional_hold
reservation.reserved_value += additional_hold
reservation.covered_usage = cumulative_usage
reservation.last_sequence = sequence
record_success(event_id)
commit
return reservation
A production design must define how prices are versioned, how multi-dimensional usage is compared, and whether provisional usage can ever be corrected downward. Those decisions should be explicit rather than embedded in an assumed token counter.
Choose an update cadence that balances exposure and overhead
Updating after every streamed token minimizes the distance between observed usage and reserved value, but it can create excessive ledger traffic and additional coordination in the generation path. Updating only at completion reduces write volume but leaves the largest unfunded interval.
A practical cadence can combine triggers such as:
- A material increase in estimated charge
- A time-based checkpoint
- Remaining reserved headroom falling below a policy threshold
- A workflow boundary, such as completion of a tool call
- A transition into a higher-cost generation stage
Smaller increments can reduce exposure between updates but increase wallet-write volume and the chance that ledger latency affects streaming. Larger increments reduce update frequency but hold more funds and can temporarily limit concurrent requests. The right policy depends on request duration, concurrency, expected output distribution, wallet-write capacity, and the desired user experience.
Define what happens when an increase cannot be funded
Insufficient funds during a stream must produce a deliberate serving decision. Possible policies include:
- Stop generation at the next defined safe boundary
- Stop releasing additional output while cancellation propagates
- Permit a limited, documented grace amount
- Continue only if another approved funding source becomes available
- Reject further tool calls or costly workflow stages
The implementation should account for usage produced between the last successful hold and the point at which generation actually stops. Cancellation is rarely instantaneous across every component, so a reservation buffer or controlled grace policy may be appropriate. Any such buffer should be bounded and visible in the wallet policy.
The client should receive a clear terminal reason where the API contract permits it. The reservation should then enter reconciliation rather than being left active indefinitely.
Reconcile final usage and release unused funds
When the stream completes, the settlement worker should obtain the authoritative final usage selected by the metering policy. It should then:
- Calculate the final charge using the applicable pricing snapshot.
- Post that charge exactly once using a finalization idempotency key.
- Consume the required value from the stream’s reservation.
- Release any unused reserved value back to available funds.
- Mark the reservation finalized so later duplicate completion events become no-ops.
If the final charge is greater than the active reservation, the system needs a defined exception policy. It might attempt one final conditional extension, consume an allowed bounded grace amount, or create a recoverable outstanding state. The correct choice depends on the commercial and operational model; it should not be hidden as an accidental negative balance.
Recover from disconnects, crashes, and missing final events
A stream may end without a clean completion event because the client disconnects, a worker crashes, cancellation races with generation, or an event is lost. Active reservations therefore need lifecycle controls beyond the synchronous request path.
A lease or expiration time can identify reservations that require investigation, but expiration should not simply release funds without checking usage. A recovery worker can inspect the latest authoritative metering state, determine whether generation is still active, finalize completed work, or renew the reservation when processing legitimately continues.
Useful recovery mechanisms include:
- Durable stream and reservation state
- Renewable leases for active generation
- An outbox or equivalent reliable event-publication pattern
- Retry-safe finalization jobs
- Dead-letter handling for malformed or repeatedly failing events
- Periodic reconciliation between serving records and wallet entries
These controls should address cancellation, timeout, duplicate retry, worker replacement, and missing-final-event scenarios. Asynchronous reconciliation is particularly important because not every failure can be resolved before the client connection closes.
Enforce explicit invariants
Tests, monitoring, and reconciliation should verify several core invariants:
- Available value does not become negative under the chosen prepaid policy.
- Covered usage and reserved value do not move backward while a stream is active, except through an explicit correction workflow.
- A retry cannot apply the same reservation increase twice.
- A final charge cannot be posted twice.
- A finalized reservation cannot be extended by a late streaming event.
- Under the selected accounting convention, every transition conserves value across available, reserved, posted, and explicitly recorded adjustment entries.
These invariants should be tested under concurrency and failure injection, not only in a successful single-stream path.
Relating wallet design to Token Forge Cloud serving telemetry
Token Forge Cloud provides API-first model access and usage data through Managed Model APIs. Teams designing downstream metering should still define which observations are provisional, which record is authoritative at finalization, and how those records map to their own pricing and ledger rules.
For private deployments, Token Forge Cloud Private LLM Inference focuses on serving-layer control and optimization through caching, routing, batching, quantization, and GPU scheduling. These serving decisions can affect the operational context around usage measurement and inference economics, so metering architects should align wallet policies with the actual serving path.
The prepaid wallet, reservation ledger, posting process, and settlement workflow described in this guide are external architectural responsibilities; they are not presented as native Token Forge Cloud wallet or billing functionality.
Next Step
Contact Token Forge Cloud to discuss API access, private deployment, and LLM inference cost control.