shenyu icon indicating copy to clipboard operation
shenyu copied to clipboard

[Task] Extend token reservations to identity-scoped token and cost budgets

Open BobSong-dev opened this issue 1 month ago • 1 comments

Background

Parent #7949. This task owns the remaining multidimensional token and cost budget policy. Identity-scoped budgets must build on trustworthy per-attempt metering and existing reservations, not a competing settlement implementation.

Existing ownership and reuse

#7702 and open PR #7848 own opt-in estimated completion-token reservation and reconciliation, Redis-time fixed windows, idempotent settlement, cancellation and unknown-usage handling and validation and metrics. Do not duplicate that implementation or open another overshoot bug. As of 2026-10-21, #7848 was open, not a merged baseline.

#7934 owns the usage ledger and versioned estimated cost; #7911 and PR #7937 own Responses usage repair. #7955 completes shared metering coverage. Preserve the existing token-limiter compatibility policy unless explicitly configured otherwise.

Implementation scope: gaps beyond #7848

  • Extend the accepted reservation foundation to trusted tenant, subject, internal API-key ID and model dimensions and token or cost policy based on accepted #7955 measurements and #7934 price provenance.
  • Define atomic reserve, settle and compensate across applicable dimensions: all-or-nothing admission or reliable compensation, one authoritative settlement chain, and idempotent attempt and logical correlation.
  • Cover retry and fallback attempts without creating a new retry policy or double charging. Specify failure, cancellation, missing usage, process crash, expired reservations and price or policy changes.
  • Ship #7956 shadow and enforce integration with isolated shadow accounting, audited decisions and bounded metrics. Reuse #7957 atomic-admission lessons.
  • State soft-budget exposure whenever maximum consumption or trustworthy final usage cannot be bounded. Reserved estimates and estimated monetary costs are not provider invoices or hard actual-spend guarantees.

Non-goals

Rewriting #7848, a second ledger, parser or settlement pipeline, provider invoicing, new model fallback, or unconditional hard-budget claims. Coordinate with the existing contributors without changing their issue scope or assignment.

Acceptance criteria

  • [ ] An explicit gap matrix against the final #7848 implementation confirms no duplicate accounting or reservation ownership.
  • [ ] Concurrent multi-gateway and multidimensional admission cannot partially consume budgets without compensation.
  • [ ] Retry, fallback and duplicate settlement reconcile exactly once per accounting identity; late settlement cannot charge unrelated windows or policies.
  • [ ] Unknown usage, crash and expiry policies expose outstanding risk and do not silently become zero usage or cost.
  • [ ] Price version, currency and effective time remain traceable; shadow cannot debit enforce state.
  • [ ] Existing non-reservation rules remain compatible; documentation distinguishes completed-usage thresholds, estimated exposure and supported hard or soft bounds.

Tests

Extend the #7848 real Redis suite with multi-identity and dimension races, retries and fallback, duplicate and late settlement, partial failure compensation, outage, crash and expiry, zero and unknown usage and price changes; add an actual JSON and SSE metering-to-budget fixture and shadow, enforce and trusted-identity isolation tests.

Dependencies

Completed identity and request/terminal-context contracts, accepted #7955 and #7934, #7956 and the reusable #7702 and #7848 foundation; coordinate #7911 and #7937. Reassess this gap list when #7848 merges before implementation.

Staged delivery and integration boundary (2026-10-30)

#7848 remains OPEN. Build on its final accepted reserve/settle/compensate contract rather than copying an unmerged branch into another budgeting subsystem; #7702 remains independently deliverable.

  1. Reconcile the final reservation and #7955 usage contracts, then add trusted-identity token dimensions with atomic admission/compensation and one accounting identity.
  2. Add monetary policies only for categories supported by #7934, retaining price revision, currency and effective time. Unpriced/unknown attempts cannot become zero-cost debits.
  3. Demonstrate #7956 shadow/enforce isolation and multidimensional retry/fallback/duplicate/late-settlement scenarios against independent ledger records.

Routing, fallback or opt-in structured-output repair can create additional billable attempts. Recheck target policy/capabilities and available exposure before each call; preserve one #8121 deadline and reconcile actual work without double debiting. Resource permits belong to #8254, not token reservations. Retain defined outcomes for partial reserve failure, crash/expiry, missing usage and policy/price changes; do not add a second ledger or retry engine.

BobSong-dev avatar Oct 21 '26 11:10 BobSong-dev

/assign

BobSong-dev avatar Nov 07 '26 03:11 BobSong-dev