Skip to content

[Audit & Plan] Billing Architecture Alignment: CEP-8 Payment Waiving, Top-Up & Capping Fixes #1

Description

@1amKhush

@ContextVM-org

Key Findings

1. CEP-8 Payment Waiving vs. Top-Up Alignment

Currently, chat.complete hardcodes a balance check inside the tool handler and throws a raw Error("insufficient balance"). Per design/billing.md:

  • chat.complete is a waived capability: resolvePrice handles authorization. If funded (balanceUsd >= floor), it returns waivePrice(). Output is capped by balance and usage is debited post-generation. If unfunded, resolvePrice returns rejectPrice("insufficient balance") (-32000 RPC rejection).
  • balance.topup is a priced capability: resolvePrice returns quotePrice(sats) emitting a CEP-8 -32042 Payment Required Lightning invoice. Paid invoices credit the USD ledger.

2. Free Model Output Capping Bug

In pricing.ts (computeOutputCap), when rates.outputPerToken <= 0 (free model), omitting explicit user/model max tokens causes cap to evaluate to Infinity. Because Number.isFinite(Infinity) is false, it falls back to { maxTokens: 1 }, abruptly terminating free streams after a single token.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions