credit-metering Skill
实现或审计使用量积分功能,包含原子化预留、消费、赠送、购买、过期、退款、限额、UI 状态和不可变账本。当应用需要对可消耗用量计费时使用。
安装方式:把技能目录放入 ~/.claude/skills/(Claude Code)或在 claude.ai 设置中启用;也可复制右侧安装命令一键添加。
技能指令原文(SKILL.md)
Credit metering
Build credits as a financial state machine. Preserve existing denominations, billing semantics, and provider integrations.
Workflow
- Inspect backend/runtime, database/ORM, auth and tenant keys, current balance/ledger fields, payment provider, usage paths, retries, queues, and frontend state.
- Infer existing costs, grants, and packs from code. Ask only for unresolved commercial policy: denomination, per-action or measured cost, free/recurring grants, expiration/rollover, refund/clawback behavior, and overspend/debt policy. Never default to a new payment provider when one exists.
- Read database schemas, then implement only the matching database variant. Do not treat PostgreSQL, MySQL, Prisma providers, and MongoDB as interchangeable. PlanetScale runs both Vitess (MySQL) and Postgres: detect the engine before choosing. Write migrations that can run twice.
- Use an append-only ledger with a unique idempotency key/external event ID. Update cached balance and ledger in one database transaction, or derive balance from the ledger when scale permits. Enforce positive input amounts and the chosen non-negative/debt rule in the database.
- Implement explicit operations:
grantfor signup, subscription, promotion, or admin credit;reservebefore costly/concurrent work;captureafter measured success;releaseon cancellation/failure;refundorreverselinked to the original transaction;expire/rolloverwhen credits have lifecycle rules.
- Authenticate and authorize from server context. Never accept balance, price, pack value, or user identity from the browser. Map a server-owned product/price ID to the credit amount.
- Read payment providers only for the detected provider, or for provider choice on a new build. Its facts carry a check date; re-check any older than 90 days before relying on it. On Lemon Squeezy, check its status and flag the Stripe Managed Payments migration path; do not start a new build on it by default. If the provider offers native credits (Dodo credit entitlements, Stripe credit grants), name one balance source of truth and keep gating usage in the app. Verify raw-body signatures, durably deduplicate events, and retry transient failures. Acknowledge before processing only after a durable queue/inbox write.
- Make promotions atomic. Use a redemption table with a unique
(promo_id, user_id)constraint when one redemption per user is intended; update global usage and grant credit in the same transaction. - Treat the UI balance as a projection. Reconcile after responses, account switches, multi-tab updates, refunds, and webhook grants. Optimistic display must not authorize work.
Verification
Test signup/recurring grant, reserve-capture, reserve-release, insufficient balance, variable final cost, duplicate request, concurrent spend, payment replay, out-of-order event, refund/clawback, promo race, account switching, and reconciliation after network failure. Assert ledger/balance invariants and run database concurrency tests plus repository lint/type/test/build commands.
Output
Report the denomination and policies, the balance source of truth, schema/migrations and their re-run result, ledger operations and invariants, protected usage paths, provider/product mapping, UI reconciliation, verification evidence, and remaining dashboard or production setup.