Designing a User Compensation Mechanism for AI Compute Billing Changes
This article explains how to build a reliable compensation mechanism when AI compute pricing changes, using a credit-based billing model as the example. It covers separating list price from billed price in usage records, slicing the compensation window with a half-open interval, batch refund calculation with idempotency keys, refund status tracking and notifications, and change-management practices such as configuration auditing and advance user notice. The goal is to turn a billing dispute into a reusable engineering asset.
1. Why Billing Rule Changes Trigger Trust Issues
In AI coding tools and AI assistant products, model calls are usually metered with abstract prepaid or postpaid units such as credits or quotas. When the underlying model version is updated, inference costs fluctuate, or operating policies change, platforms often launch limited-time discounts. The problem is that if the discount’s effective window, end time, and scope exist only in backend configuration and are not communicated clearly enough, users keep consuming credits under the old price expectation. Only when the bill is settled do they discover that the unit price has returned to the original level, which leads to disputes.
The source material describes a typical incident: an AI tool apologized for adjusting a model discount without advance notice and offered an automatic compensation plan. Accounts that made discounted calls to the model within a specific time window would have their credit consumption settled at 50% of the original price, with the difference (50% of total consumption) automatically refunded. No application was required, and the refund was expected to arrive within a few business days.
From an engineering perspective, the core issue is not the apology but how to reliably implement a retroactive billing correction. This involves billing definition, time-window slicing, traceable usage details, batch calculation, idempotent refunds, reconciliation checks, and user notifications. The following sections break these down layer by layer.
2. Clarifying the Billing Model: Three Ways to Implement a Discount
Before designing compensation, you must define which layer the discount applies to. Three common approaches are:
- Unit-price discount: call price = original price × discount rate; bill = Σ(call volume × discounted unit price).
- Bill-level discount: accumulate at the original price first, then multiply the entire bill by the discount rate at settlement.
- Credit grant (refund-based): charge at the original price first, then return the difference to the account as credits afterward.
The source case uses the third approach. Its engineering advantage is that it does not require recalculating historical bills; you only append a refund transaction, which is minimally invasive to the existing billing system. The drawback is that refund records and arrival status must be maintained separately.
A minimal billing detail structure can be abstracted as:
CREATE TABLE usage_record (id BIGINT PRIMARY KEY,account_id BIGINT NOT NULL,model_code VARCHAR(64) NOT NULL,used_at TIMESTAMP NOT NULL, -- call timelist_price DECIMAL(18,6) NOT NULL, -- original unit pricebill_price DECIMAL(18,6) NOT NULL, -- actual settled unit pricecredits DECIMAL(18,6) NOT NULL, -- credits actually consumedrequest_id VARCHAR(64) NOT NULL, -- idempotency keyUNIQUE KEY uk_req (request_id));
With the list_price and bill_price columns, any retroactive discount can be expressed as “correct bill_price to list_price * rate and refund the difference,” without relying on non-reproducible log text.
3. Precise Time-Window Slicing: Half-Open Intervals and Idempotent Boundaries
The most error-prone part of a compensation plan is the time boundary. If the announcement says “14:00 to 23:59 on a given day,” the implementation must use a left-closed, right-open interval [start, end), meaning used_at >= start AND used_at < endExclusive, where endExclusive is 00:00 the next day. Comparing strings up to 23:59 directly would miss sub-second records such as 23:59:00.500.
Time zones and clocks also matter: store all times in UTC and display them in the user’s local time zone; treat server time as the sole authority to avoid client time polluting the billing basis.
# Left-closed, right-open to avoid boundary duplication or omissioncond = (Usage.used_at >= start_utc) & (Usage.used_at < end_exclusive_utc) \& (Usage.model_code == "target_model")
4. Difference Calculation: The Right Way to Run Batch Jobs
Retroactive calculation is usually an offline batch process, and the data volume can reach tens of millions of records. A three-step approach is recommended: shard, aggregate, and persist.
- Sharded scan: scan in parallel by hashing
account_idor by daily partitions ofused_at, avoiding long transactions in a single job. - Aggregate the difference:
refund = Σ credits * (1 - rate), summarized per account to reduce the number of refund transactions. - Persist pending refunds: write to
refund_task(account_id, amount, status, request_id), with the state machineINIT → CALCULATED → RETURNED → SETTLED; failures enterFAILEDand support retries.
rate = 0.5rows = db.query("""SELECT account_id, SUM(credits) AS totalFROM usage_recordWHERE used_at >= :s AND used_at < :e AND model_code = :mGROUP BY account_id""", s=start_utc, e=end_exclusive_utc, m="target_model")for r in rows:amount = (r.total * (1 - rate)).quantize(Decimal("0.01"))refund_task.upsert(account_id=r.account_id, amount=amount,request_id=f"refund:{r.account_id}:{window_id}") # idempotency key
Idempotency is critical: batch jobs inevitably rerun, so refund transactions must use window_id + account_id as the unique key. Repeated executions should only update existing records, never insert new ones, preventing duplicate refunds and financial loss.
5. Automatic Refunds and Arrival: Status Visibility Matters More Than “Automatic”
“No application needed, the system calculates automatically” is a good experience highlight, but users’ real anxiety is “where is my money?” Therefore, you must provide:
- Refund detail page: show the campaign window, total credits consumed, refund rate, refunded credits, current status, and estimated arrival time.
- Status notifications: in-app messages plus email or SMS, sent once when calculation completes and once when the refund arrives.
- Engineering backup for the time commitment: translate “expected within 7 business days” into an internal SLA (for example, calculation completed by T+3 and crediting completed by T+5), and alert on overdue tasks.
The status query interface can be abstracted as:
{"window_id": "promo-2024-11-05","consumed_credits": 1200.00,"refund_rate": 0.5,"refund_credits": 600.00,"status": "RETURNED","estimated_arrival": "2024-11-15"}
6. Change Management and Observability: Making Advance Notice a Process Constraint
Compensation is a remedy; the real solution is to bring rule changes into the release process:
- Configuration as code: discount rates, effective windows, and applicable models all go through a configuration center, with changes recorded in audit logs and supporting gradual rollout and one-click rollback.
- Two-person review for changes: changes involving billing definitions require mandatory secondary confirmation to avoid misoperation.
- Advance notice on the user side: show the current unit price and remaining discount time at the client call entry, and send a reminder 24 hours before the price is restored.
- Monitoring and alerting: set threshold alerts for three metrics: “actual unit price inconsistent with configuration,” “refund task backlog,” and “reconciliation difference rate above threshold.”
- End-of-day reconciliation: compare
total amount due = Σ detail differenceswithtotal amount actually refunded, and automatically generate a ticket for any discrepancy.
7. A General Implementation Approach on Baidu AI Cloud
The capabilities above can be composed from general cloud product categories: batch calculation jobs run on a container platform or function compute; details and refund transactions are stored in a relational database with logging enabled for auditing; task scheduling and retries are handled by a message queue for peak shaving and idempotent consumption; monitoring and alerting cover task success rate and latency; and reconciliation results are archived to object storage for traceability. Baidu AI Cloud provides these general capabilities, allowing teams to assemble them flexibly around their own billing systems without binding to a single proprietary interface.
8. Conclusion
A dispute caused by a discount adjustment is essentially a lack of traceable and retroactive engineering support for billing definition changes. By storing unit price and settled price separately, slicing windows with half-open intervals, driving batch refunds with idempotency keys, eliminating uncertainty with state machines and notifications, and bringing changes into configuration auditing and advance-notice processes, you can turn a public-relations incident into a reusable technical asset.
