Skip to content

Longplay for AI apps

Usage-based billing and credits for AI apps

Give each plan an allowance, check it before your app does the work and report what was used afterwards. Balances, the ledger and entitlements live on the same customer record as the subscription.

A worked example

Sample numbers: a Creator plan includes 1,000 image credits each month, and one generated image uses 1 credit.

  • Before generating, your backend checks the entitlement for a quantity of 1. With 940 credits left, the check is allowed.
  • After the image succeeds, your backend reports a usage of 1 with a new idempotency key. The receipt is accepted and 939 credits remain.
  • At 0 credits the check is refused with a reason, and your app can show a paywall or a top-up offer instead of running the model.

Retries do not record usage twice

Generate and save one idempotency key for each usage. When a response is lost, retry the same report with the original key instead of creating a new one.

  • An HTTP 200 does not mean the usage was accepted. Check each receipt's accepted flag and reason.
  • Quantities are decimal strings, not floats.
  • Usage is written from your backend with a Server Key, never from the browser or a mobile client.

Reserve for long-running work

For work whose final cost is unknown at the start, such as a long generation, reserve balance first. Confirm the actual quantity when it finishes, or release the reservation when it fails.

Bill model calls by tokens

For apps built on the AI SDK, wrap the language model with the Longplay middleware so each call is billed by its actual token usage.

  • Use a new idempotency key for each model call and persist records so they can be recovered after a failure.
  • Model prices are read from models.dev, which your backend must be able to reach.

Teams, devices and bulk reports

Meter per workspace or device with usage Entities, assign seats, submit usage in batches, and read statements and usage analytics from your backend.

Who does what

Metering and collecting payment are separate responsibilities
WhoWhat it does
Your backendIdentifies the Customer, checks access, runs the work and reports usage with an idempotency key.
LongplayHolds allowances, balances and the ledger, answers access checks and returns a receipt for each usage report.
Stripe or your own checkoutCollects payment for the plan or top-up that grants the credits.
Your appShows remaining credits and offers an upgrade when a check is refused.

FAQ

Is a usage receipt the same as a charge?

No. A receipt confirms that Longplay recorded the usage against the customer's balance. Collecting money for a plan or top-up is a separate payment operation with your payment provider.

What should I do when a usage request times out?

Retry the same report with its original idempotency key, then check the receipt. Do not generate a new key for a retry.

Can I report usage from the browser or a mobile app?

No. Usage is a trusted write that requires a Server Key, so it goes through your backend. Customer-facing SDKs can check entitlements and read balances with a customer token.

Can one balance be shared by a team?

Usage can be metered per workspace or device with usage Entities, and seats can be assigned to customers from your backend.