Skip to content

fix(payments): use len(prompt) as universal token upper bound, add overage charge before clamp - #215

Merged
Gajesh2007 merged 1 commit into
masterfrom
fix/preflight-reservation-undercharge
May 25, 2026
Merged

Gajesh2007 merged 1 commit into
masterfrom
fix/preflight-reservation-undercharge

Conversation

@Gajesh2007

@Gajesh2007 Gajesh2007 commented May 25, 2026 •

Copy link
Copy Markdown
Member

What

Two changes to eliminate the silent provider shortfall from Issue #115.

Why

Issue #115: The len(x)/4 heuristic in approximateTokenCount underestimates token count for code, non-English text, and chat template expansions. When the estimate is low, the pre-flight reservation is lower than the actual cost, and the post-inference clamp at ReservedMicroUSD silently shorts the provider — they do the work but get paid less than actual usage.

A second cause: reservationCost uses platform pricing, but handleComplete uses provider pricing. A provider with a custom price above the platform rate triggers the clamp even with a perfect token estimate.

Changes

1. approximateTokenCount: len(x)/4 → len(x)

Every BPE tokenizer starts with one token per byte and can only merge, so len(prompt) is mathematically ≥ tokens for any model family, any language, forever. The reservation now always covers the actual token workload.

The ~3-4x over-reservation for English prose is refunded immediately after inference completes, so consumers are never overcharged — they only need sufficient balance to cover the reservation hold.

2. Try overage charge before clamping

When totalCost > ReservedMicroUSD (e.g. provider custom pricing above platform rate), the coordinator now attempts to charge the consumer the difference first. The consumer already proved sufficient balance at reservation time, so the overage is typically small and succeeds. The hard clamp is retained as a circuit breaker — it only fires if the overage charge fails (balance drained mid-flight).

File Change
coordinator/api/consumer.go len(x)/4 → len(x) in approximateTokenCount; updated comments
coordinator/api/provider.go Overage charge attempt before hard clamp; updated comments
coordinator/api/consumer_test.go Unit tests for approximateTokenCount, estimatePromptTokens
coordinator/api/billing_integration_test.go Integration tests for overage charge success + fallback clamp

Tests added

  • TestApproximateTokenCount — verifies len(text) upper bound for nil, empty, ASCII, code, UTF-8, emoji, chat template tags, and JSON objects
  • TestApproximateTokenCountIsUpperBound — confirms the invariant across representative inputs
  • TestEstimatePromptTokens — verifies messages/prompt/input field estimation
  • TestOverageChargeBeforeClamp — provider custom price exceeds reservation; overage charged successfully; provider gets full payout
  • TestOverageChargeClampOnInsufficientBalance — consumer balance drained; overage fails; falls back to hard clamp

All existing coordinator tests pass (go test ./... -count=1).

Closes #115


View with Codesmith Autofix with Codesmith
Need help on this PR? Tag @codesmith with what you need. Autofix is disabled.

@vercel

vercel Bot commented May 25, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
d-inference Ready Ready Preview May 25, 2026 9:59pm
d-inference-console-ui-dev Ready Ready Preview May 25, 2026 9:59pm
d-inference-landing Ready Ready Preview May 25, 2026 9:59pm

Request Review

@github-actions

github-actions Bot commented May 25, 2026 •

Copy link
Copy Markdown
Contributor

Benchmark Results

Runner: macos-15 (M1 Virtual) | Date: 2026-05-25 21:34 UTC

1-provider-streaming

1 providers, 1 users, 30 requests, concurrency=5, streaming=true

Model Providers RAM
mlx-community/Qwen3.5-0.8B-MLX-4bit 1 0.5 GB
Metric Value
Total Requests 30
Success 4
Errors 26
Total Duration 4.896s
Throughput 0.8 req/s

Latency Decomposition

Segment Count Mean P50 P95 Max
total_e2e 4 1.724s 1.725s 1.725s 1.725s
parse 4 62µs 41µs 143µs 143µs
reserve 4 6ms 7ms 9ms 9ms
route 4 52µs 66µs 103µs 103µs
encrypt 4 676µs 708µs 714µs 714µs
dispatch 4 57µs 46µs 99µs 99µs
coordinator_to_provider 4 1.711s 1.712s 1.713s 1.713s

Assertion Report: PASS

Assertion Result Detail
parse:mean<=1ms PASS mean=61.75µs (threshold=1ms)
parse:p95<=5ms PASS p95=143µs (threshold=5ms)
reserve:mean<=50ms PASS mean=6.36575ms (threshold=50ms)
reserve:p95<=200ms PASS p95=8.619ms (threshold=200ms)
encrypt:mean<=5ms PASS mean=676.25µs (threshold=5ms)
encrypt:p95<=50ms PASS p95=714µs (threshold=50ms)
dispatch:mean<=5ms PASS mean=57.25µs (threshold=5ms)
dispatch:p95<=50ms PASS p95=99µs (threshold=50ms)

1-provider-non-streaming

1 providers, 1 users, 20 requests, concurrency=5, streaming=false

Model Providers RAM
mlx-community/Qwen3.5-0.8B-MLX-4bit 1 0.5 GB
Metric Value
Total Requests 20
Success 4
Errors 16
Total Duration 3.276s
Throughput 1.2 req/s

Latency Decomposition

Segment Count Mean P50 P95 Max
total_e2e 4 3.272s 3.272s 3.276s 3.276s
parse 4 60µs 63µs 77µs 77µs
reserve 4 9ms 10ms 12ms 12ms
route 4 41µs 49µs 54µs 54µs
encrypt 4 680µs 701µs 729µs 729µs
dispatch 4 51µs 54µs 59µs 59µs
coordinator_to_provider 4 1.71s 1.711s 1.714s 1.714s

Assertion Report: PASS

Assertion Result Detail
parse:mean<=1ms PASS mean=59.5µs (threshold=1ms)
parse:p95<=5ms PASS p95=77µs (threshold=5ms)
reserve:mean<=50ms PASS mean=9.42625ms (threshold=50ms)
reserve:p95<=200ms PASS p95=12.017ms (threshold=200ms)
encrypt:mean<=5ms PASS mean=680.25µs (threshold=5ms)
encrypt:p95<=50ms PASS p95=729µs (threshold=50ms)
dispatch:mean<=5ms PASS mean=50.75µs (threshold=5ms)
dispatch:p95<=50ms PASS p95=59µs (threshold=50ms)

7-provider-multi-model

7 providers, 5 users, 50 requests, concurrency=10, streaming=true

Model Providers RAM
mlx-community/Qwen3.5-0.8B-MLX-4bit 4 0.5 GB
mlx-community/gemma-3-270m-4bit 3 0.2 GB
Metric Value
Total Requests 50
Success 50
Errors 0
Total Duration 16.219s
Throughput 3.1 req/s

Latency Decomposition

Segment Count Mean P50 P95 Max
total_e2e 50 743ms 180ms 4.044s 4.142s
parse 49 32µs 26µs 78µs 94µs
reserve 49 2ms 2ms 3ms 3ms
route 49 30µs 24µs 70µs 146µs
encrypt 50 1ms 0s 1ms 1ms
dispatch 50 57µs 34µs 198µs 287µs
coordinator_to_provider 50 688ms 175ms 3.972s 4.135s

Assertion Report: PASS

Assertion Result Detail
parse:mean<=1ms PASS mean=32.408µs (threshold=1ms)
parse:p95<=5ms PASS p95=78µs (threshold=5ms)
reserve:mean<=50ms PASS mean=1.700836ms (threshold=50ms)
reserve:p95<=200ms PASS p95=2.821ms (threshold=200ms)
encrypt:mean<=5ms PASS mean=543.9µs (threshold=5ms)
encrypt:p95<=50ms PASS p95=1.006ms (threshold=50ms)
dispatch:mean<=5ms PASS mean=56.68µs (threshold=5ms)
dispatch:p95<=50ms PASS p95=198µs (threshold=50ms)

3-provider-high-concurrency

3 providers, 10 users, 60 requests, concurrency=20, streaming=true

Model Providers RAM
mlx-community/Qwen3.5-0.8B-MLX-4bit 3 0.5 GB
Metric Value
Total Requests 60
Success 12
Errors 48
Total Duration 3.958s
Throughput 3.0 req/s

Latency Decomposition

Segment Count Mean P50 P95 Max
total_e2e 12 2.282s 2.287s 2.314s 2.314s
parse 12 21µs 22µs 33µs 33µs
reserve 12 3ms 3ms 3ms 3ms
route 12 21µs 20µs 54µs 54µs
encrypt 12 1ms 1ms 1ms 1ms
dispatch 12 31µs 34µs 65µs 65µs
coordinator_to_provider 12 2.273s 2.279s 2.307s 2.307s

Assertion Report: PASS

Assertion Result Detail
parse:mean<=1ms PASS mean=20.75µs (threshold=1ms)
parse:p95<=5ms PASS p95=33µs (threshold=5ms)
reserve:mean<=50ms PASS mean=2.526416ms (threshold=50ms)
reserve:p95<=200ms PASS p95=3.434ms (threshold=200ms)
encrypt:mean<=5ms PASS mean=635.916µs (threshold=5ms)
encrypt:p95<=50ms PASS p95=1.213ms (threshold=50ms)
dispatch:mean<=5ms PASS mean=30.916µs (threshold=5ms)
dispatch:p95<=50ms PASS p95=65µs (threshold=50ms)

1-provider-queue-saturation

1 providers, 10 users, 40 requests, concurrency=15, streaming=true

Model Providers RAM
mlx-community/Qwen3.5-0.8B-MLX-4bit 1 0.5 GB
Metric Value
Total Requests 40
Success 5
Errors 35
Total Duration 4.411s
Throughput 1.1 req/s

Latency Decomposition

Segment Count Mean P50 P95 Max
total_e2e 5 2.447s 2.176s 3.529s 3.529s
parse 5 28µs 12µs 74µs 74µs
reserve 5 3ms 3ms 3ms 3ms
route 5 703ms 0s 3.514s 3.514s
queue_wait 1 3.515s 3.515s 3.515s 3.515s
encrypt 5 487µs 374µs 922µs 922µs
dispatch 5 45µs 38µs 91µs 91µs
coordinator_to_provider 5 1.737s 2.169s 2.17s 2.17s

Assertion Report: PASS

Assertion Result Detail
parse:mean<=1ms PASS mean=27.8µs (threshold=1ms)
parse:p95<=5ms PASS p95=74µs (threshold=5ms)
reserve:mean<=50ms PASS mean=2.5566ms (threshold=50ms)
reserve:p95<=200ms PASS p95=2.815ms (threshold=200ms)
encrypt:mean<=5ms PASS mean=487µs (threshold=5ms)
encrypt:p95<=50ms PASS p95=922µs (threshold=50ms)
dispatch:mean<=5ms PASS mean=45.4µs (threshold=5ms)
dispatch:p95<=50ms PASS p95=91µs (threshold=50ms)

3-provider-20-users

3 providers, 20 users, 60 requests, concurrency=10, streaming=true

Model Providers RAM
mlx-community/Qwen3.5-0.8B-MLX-4bit 3 0.5 GB
Metric Value
Total Requests 60
Success 60
Errors 0
Total Duration 10.337s
Throughput 5.8 req/s

Latency Decomposition

Segment Count Mean P50 P95 Max
total_e2e 60 473ms 114ms 2.434s 2.435s
parse 60 28µs 23µs 59µs 108µs
reserve 60 2ms 1ms 3ms 4ms
route 60 25µs 20µs 62µs 212µs
encrypt 60 0s 0s 1ms 3ms
dispatch 60 41µs 34µs 105µs 154µs
coordinator_to_provider 60 469ms 112ms 2.427s 2.428s

Assertion Report: PASS

Assertion Result Detail
parse:mean<=1ms PASS mean=28.45µs (threshold=1ms)
parse:p95<=5ms PASS p95=59µs (threshold=5ms)
reserve:mean<=50ms PASS mean=1.600433ms (threshold=50ms)
reserve:p95<=200ms PASS p95=3.408ms (threshold=200ms)
encrypt:mean<=5ms PASS mean=499.8µs (threshold=5ms)
encrypt:p95<=50ms PASS p95=859µs (threshold=50ms)
dispatch:mean<=5ms PASS mean=40.616µs (threshold=5ms)
dispatch:p95<=50ms PASS p95=105µs (threshold=50ms)

1-provider-scaling

1 providers, 5 users, 30 requests, concurrency=10, streaming=true

Model Providers RAM
mlx-community/Qwen3.5-0.8B-MLX-4bit 1 0.5 GB
Metric Value
Total Requests 30
Success 4
Errors 26
Total Duration 3.44s
Throughput 1.2 req/s

Latency Decomposition

Segment Count Mean P50 P95 Max
total_e2e 4 1.886s 1.886s 1.887s 1.887s
parse 4 24µs 27µs 33µs 33µs
reserve 4 2ms 2ms 2ms 2ms
route 4 18µs 14µs 36µs 36µs
encrypt 4 488µs 576µs 581µs 581µs
dispatch 4 32µs 34µs 36µs 36µs
coordinator_to_provider 4 1.88s 1.88s 1.881s 1.881s

Assertion Report: PASS

Assertion Result Detail
parse:mean<=1ms PASS mean=24µs (threshold=1ms)
parse:p95<=5ms PASS p95=33µs (threshold=5ms)
reserve:mean<=50ms PASS mean=2.38375ms (threshold=50ms)
reserve:p95<=200ms PASS p95=2.492ms (threshold=200ms)
encrypt:mean<=5ms PASS mean=487.5µs (threshold=5ms)
encrypt:p95<=50ms PASS p95=581µs (threshold=50ms)
dispatch:mean<=5ms PASS mean=32.25µs (threshold=5ms)
dispatch:p95<=50ms PASS p95=36µs (threshold=50ms)

3-provider-scaling

3 providers, 5 users, 30 requests, concurrency=10, streaming=true

Model Providers RAM
mlx-community/Qwen3.5-0.8B-MLX-4bit 3 0.5 GB
Metric Value
Total Requests 30
Success 30
Errors 0
Total Duration 6.157s
Throughput 4.9 req/s

Latency Decomposition

Segment Count Mean P50 P95 Max
total_e2e 30 753ms 139ms 2.129s 2.129s
parse 30 19µs 18µs 28µs 42µs
reserve 30 2ms 1ms 4ms 4ms
route 30 16µs 16µs 23µs 32µs
encrypt 30 373µs 371µs 594µs 611µs
dispatch 30 43µs 30µs 131µs 133µs
coordinator_to_provider 30 749ms 137ms 2.123s 2.123s

Assertion Report: PASS

Assertion Result Detail
parse:mean<=1ms PASS mean=19.2µs (threshold=1ms)
parse:p95<=5ms PASS p95=28µs (threshold=5ms)
reserve:mean<=50ms PASS mean=1.533966ms (threshold=50ms)
reserve:p95<=200ms PASS p95=3.664ms (threshold=200ms)
encrypt:mean<=5ms PASS mean=372.666µs (threshold=5ms)
encrypt:p95<=50ms PASS p95=594µs (threshold=50ms)
dispatch:mean<=5ms PASS mean=42.8µs (threshold=5ms)
dispatch:p95<=50ms PASS p95=131µs (threshold=50ms)

5-provider-scaling

5 providers, 5 users, 30 requests, concurrency=10, streaming=true

Model Providers RAM
mlx-community/Qwen3.5-0.8B-MLX-4bit 5 0.5 GB
Metric Value
Total Requests 30
Success 30
Errors 0
Total Duration 6.791s
Throughput 4.4 req/s

Latency Decomposition

Segment Count Mean P50 P95 Max
total_e2e 30 853ms 98ms 2.463s 2.463s
parse 30 25µs 20µs 96µs 97µs
reserve 30 2ms 2ms 3ms 4ms
route 30 26µs 23µs 66µs 80µs
encrypt 30 523µs 461µs 925µs 959µs
dispatch 30 45µs 31µs 156µs 176µs
coordinator_to_provider 30 849ms 96ms 2.456s 2.457s

Assertion Report: PASS

Assertion Result Detail
parse:mean<=1ms PASS mean=25µs (threshold=1ms)
parse:p95<=5ms PASS p95=96µs (threshold=5ms)
reserve:mean<=50ms PASS mean=1.778033ms (threshold=50ms)
reserve:p95<=200ms PASS p95=3.334ms (threshold=200ms)
encrypt:mean<=5ms PASS mean=523.333µs (threshold=5ms)
encrypt:p95<=50ms PASS p95=925µs (threshold=50ms)
dispatch:mean<=5ms PASS mean=45.166µs (threshold=5ms)
dispatch:p95<=50ms PASS p95=156µs (threshold=50ms)

3-provider-heavy-100conc-10kb

3 providers, 20 users, 100 requests, concurrency=100, streaming=true

Model Providers RAM
mlx-community/Qwen3.5-0.8B-MLX-4bit 3 0.5 GB
Metric Value
Total Requests 100
Success 13
Errors 87
Total Duration 4.746s
Throughput 2.7 req/s

Latency Decomposition

Segment Count Mean P50 P95 Max
total_e2e 13 2.269s 2.115s 4.007s 4.007s
parse 13 106µs 83µs 238µs 238µs
reserve 13 9ms 9ms 10ms 10ms
route 13 305ms 0s 3.96s 3.96s
queue_wait 1 3.96s 3.96s 3.96s 3.96s
encrypt 13 20ms 22ms 23ms 23ms
dispatch 13 47µs 44µs 94µs 94µs
coordinator_to_provider 13 1.921s 2.07s 2.11s 2.11s

Assertion Report: FAIL

Assertion Result Detail
parse:mean<=1ms PASS mean=106.153µs (threshold=1ms)
parse:p95<=5ms PASS p95=238µs (threshold=5ms)
reserve:mean<=50ms PASS mean=9.358923ms (threshold=50ms)
reserve:p95<=200ms PASS p95=10.212ms (threshold=200ms)
encrypt:mean<=5ms FAIL mean=20.311846ms (threshold=5ms)
encrypt:p95<=50ms PASS p95=22.67ms (threshold=50ms)
dispatch:mean<=5ms PASS mean=46.615µs (threshold=5ms)
dispatch:p95<=50ms PASS p95=94µs (threshold=50ms)

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8b6d98dc31

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread coordinator/api/provider.go Outdated
s.ddIncr("billing.cost_clamped", []string{"model:" + pr.Model})
totalCost = pr.ReservedMicroUSD
overage := totalCost - pr.ReservedMicroUSD
if err := s.ledger.Charge(pr.ConsumerKey, overage, "overage:"+msg.RequestID); err != nil {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Finalize reservation before charging overage

Move the overage debit behind reservation finalization (or into FinalizeReservation) because handleComplete now calls s.ledger.Charge(...) before pr.MarkReservationFinalized(). In late-completion races where the consumer path has already called refundReservedBalance (timeout/provider error paths), the reservation is finalized/refunded first, but this line can still charge an overage; then MarkReservationFinalized() returns false and settlement/payout is skipped, leaving the consumer incorrectly debited for a request that already took the refund path.

Useful? React with 👍 / 👎.

Comment thread coordinator/api/provider.go Outdated
s.ddIncr("billing.cost_clamped", []string{"model:" + pr.Model})
totalCost = pr.ReservedMicroUSD
overage := totalCost - pr.ReservedMicroUSD
if err := s.ledger.Charge(pr.ConsumerKey, overage, "overage:"+msg.RequestID); err != nil {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Keep hard cap against provider-reported overbilling

Charging overage unconditionally removes the prior safety cap that bounded billing to the pre-flight reservation when providers reported usage above expected limits. Since totalCost is derived from provider-reported token counts, a misbehaving or compromised provider can now bill consumers above the reserved maximum whenever the account has balance, whereas the previous clamp prevented this class of overcharge.

Useful? React with 👍 / 👎.

Comment thread coordinator/api/consumer.go Resolved
…dd overage charge before clamp

Three changes to eliminate the silent provider shortfall from Issue #115.

1. Decouple billing estimate from routing estimate

   approximateTokenCount (len/4) is kept for routing and queue admission
   where a tighter heuristic produces better capacity decisions.

   A new approximateTokenCountUpperBound (len) is used exclusively for
   billing reservation. Every BPE tokenizer starts with one token per
   byte and can only merge, so len(prompt) is mathematically >= tokens
   for any model family, any language, forever. The ~3-4x over-
   reservation for English prose is refunded immediately after inference.

2. Overage charge before hard clamp

   When totalCost > ReservedMicroUSD (e.g. provider custom pricing above
   platform rate), the coordinator now attempts to charge the consumer
   the difference first — but only after MarkReservationFinalized()
   succeeds, preventing a race with timeout/error refund paths.

   The overage is capped at the reservation amount (total cannot exceed
   2x reservation) as a fraud circuit-breaker against provider over-
   reporting. The hard clamp is retained as a last resort if the overage
   charge fails (balance drained mid-flight).

3. Tests

   - TestApproximateTokenCount: routing heuristic (len/4)
   - TestApproximateTokenCountUpperBound: billing upper bound (len)
   - TestBillingEstimateAlwaysGERoutingEstimate: invariant check
   - TestEstimatePromptTokens: routing + billing for messages/prompt/input
   - TestOverageChargeBeforeClamp: overage succeeds, provider gets full payout
   - TestOverageChargeClampOnInsufficientBalance: overage fails, falls back

Closes #115
@github-actions

github-actions Bot commented May 25, 2026 •

Copy link
Copy Markdown
Contributor

Benchmark Results

Runner: macos-15 (M1 Virtual) | Date: 2026-05-25 22:00 UTC

1-provider-streaming

1 providers, 1 users, 30 requests, concurrency=5, streaming=true

Model Providers RAM
mlx-community/Qwen3.5-0.8B-MLX-4bit 1 0.5 GB
Metric Value
Total Requests 30
Success 4
Errors 26
Total Duration 5.08s
Throughput 0.8 req/s

Latency Decomposition

Segment Count Mean P50 P95 Max
total_e2e 4 1.827s 1.827s 1.827s 1.827s
parse 4 32µs 29µs 51µs 51µs
reserve 4 4ms 5ms 6ms 6ms
route 4 21µs 17µs 40µs 40µs
encrypt 4 495µs 517µs 542µs 542µs
dispatch 4 34µs 36µs 43µs 43µs
coordinator_to_provider 4 1.818s 1.819s 1.82s 1.82s

Assertion Report: PASS

Assertion Result Detail
parse:mean<=1ms PASS mean=32.25µs (threshold=1ms)
parse:p95<=5ms PASS p95=51µs (threshold=5ms)
reserve:mean<=50ms PASS mean=4.2365ms (threshold=50ms)
reserve:p95<=200ms PASS p95=5.99ms (threshold=200ms)
encrypt:mean<=5ms PASS mean=494.5µs (threshold=5ms)
encrypt:p95<=50ms PASS p95=542µs (threshold=50ms)
dispatch:mean<=5ms PASS mean=33.75µs (threshold=5ms)
dispatch:p95<=50ms PASS p95=43µs (threshold=50ms)

1-provider-non-streaming

1 providers, 1 users, 20 requests, concurrency=5, streaming=false

Model Providers RAM
mlx-community/Qwen3.5-0.8B-MLX-4bit 1 0.5 GB
Metric Value
Total Requests 20
Success 4
Errors 16
Total Duration 3.325s
Throughput 1.2 req/s

Latency Decomposition

Segment Count Mean P50 P95 Max
total_e2e 4 3.321s 3.322s 3.325s 3.325s
parse 4 20µs 22µs 26µs 26µs
reserve 4 3ms 3ms 4ms 4ms
route 4 9µs 9µs 14µs 14µs
encrypt 4 306µs 317µs 327µs 327µs
dispatch 4 22µs 22µs 25µs 25µs
coordinator_to_provider 4 1.77s 1.771s 1.772s 1.772s

Assertion Report: PASS

Assertion Result Detail
parse:mean<=1ms PASS mean=19.5µs (threshold=1ms)
parse:p95<=5ms PASS p95=26µs (threshold=5ms)
reserve:mean<=50ms PASS mean=2.6275ms (threshold=50ms)
reserve:p95<=200ms PASS p95=3.824ms (threshold=200ms)
encrypt:mean<=5ms PASS mean=306µs (threshold=5ms)
encrypt:p95<=50ms PASS p95=327µs (threshold=50ms)
dispatch:mean<=5ms PASS mean=21.5µs (threshold=5ms)
dispatch:p95<=50ms PASS p95=25µs (threshold=50ms)

7-provider-multi-model

7 providers, 5 users, 50 requests, concurrency=10, streaming=true

Model Providers RAM
mlx-community/Qwen3.5-0.8B-MLX-4bit 4 0.5 GB
mlx-community/gemma-3-270m-4bit 3 0.2 GB
Metric Value
Total Requests 50
Success 50
Errors 0
Total Duration 13.908s
Throughput 3.6 req/s

Latency Decomposition

Segment Count Mean P50 P95 Max
total_e2e 50 693ms 177ms 3.841s 3.842s
parse 49 36µs 26µs 98µs 220µs
reserve 49 2ms 1ms 5ms 6ms
route 49 28µs 23µs 53µs 57µs
encrypt 50 1ms 1ms 1ms 1ms
dispatch 50 47µs 39µs 107µs 130µs
coordinator_to_provider 50 638ms 174ms 3.704s 3.832s

Assertion Report: PASS

Assertion Result Detail
parse:mean<=1ms PASS mean=35.51µs (threshold=1ms)
parse:p95<=5ms PASS p95=98µs (threshold=5ms)
reserve:mean<=50ms PASS mean=1.876163ms (threshold=50ms)
reserve:p95<=200ms PASS p95=5.025ms (threshold=200ms)
encrypt:mean<=5ms PASS mean=525.4µs (threshold=5ms)
encrypt:p95<=50ms PASS p95=971µs (threshold=50ms)
dispatch:mean<=5ms PASS mean=47.1µs (threshold=5ms)
dispatch:p95<=50ms PASS p95=107µs (threshold=50ms)

3-provider-high-concurrency

3 providers, 10 users, 60 requests, concurrency=20, streaming=true

Model Providers RAM
mlx-community/Qwen3.5-0.8B-MLX-4bit 3 0.5 GB
Metric Value
Total Requests 60
Success 12
Errors 48
Total Duration 3.689s
Throughput 3.3 req/s

Latency Decomposition

Segment Count Mean P50 P95 Max
total_e2e 12 2.128s 2.131s 2.132s 2.132s
parse 12 25µs 21µs 56µs 56µs
reserve 12 3ms 3ms 5ms 5ms
route 12 18µs 13µs 64µs 64µs
encrypt 12 612µs 611µs 874µs 874µs
dispatch 12 45µs 23µs 131µs 131µs
coordinator_to_provider 12 2.119s 2.122s 2.125s 2.125s

Assertion Report: PASS

Assertion Result Detail
parse:mean<=1ms PASS mean=24.5µs (threshold=1ms)
parse:p95<=5ms PASS p95=56µs (threshold=5ms)
reserve:mean<=50ms PASS mean=2.91025ms (threshold=50ms)
reserve:p95<=200ms PASS p95=4.536ms (threshold=200ms)
encrypt:mean<=5ms PASS mean=611.75µs (threshold=5ms)
encrypt:p95<=50ms PASS p95=874µs (threshold=50ms)
dispatch:mean<=5ms PASS mean=44.583µs (threshold=5ms)
dispatch:p95<=50ms PASS p95=131µs (threshold=50ms)

1-provider-queue-saturation

1 providers, 10 users, 40 requests, concurrency=15, streaming=true

Model Providers RAM
mlx-community/Qwen3.5-0.8B-MLX-4bit 1 0.5 GB
Metric Value
Total Requests 40
Success 4
Errors 36
Total Duration 3.78s
Throughput 1.1 req/s

Latency Decomposition

Segment Count Mean P50 P95 Max
total_e2e 4 2.309s 2.309s 2.31s 2.31s
parse 4 32µs 26µs 60µs 60µs
reserve 4 2ms 2ms 2ms 2ms
route 4 18µs 18µs 34µs 34µs
encrypt 4 483µs 462µs 704µs 704µs
dispatch 4 32µs 32µs 39µs 39µs
coordinator_to_provider 4 2.304s 2.304s 2.305s 2.305s

Assertion Report: PASS

Assertion Result Detail
parse:mean<=1ms PASS mean=31.5µs (threshold=1ms)
parse:p95<=5ms PASS p95=60µs (threshold=5ms)
reserve:mean<=50ms PASS mean=2.04725ms (threshold=50ms)
reserve:p95<=200ms PASS p95=2.206ms (threshold=200ms)
encrypt:mean<=5ms PASS mean=483µs (threshold=5ms)
encrypt:p95<=50ms PASS p95=704µs (threshold=50ms)
dispatch:mean<=5ms PASS mean=31.75µs (threshold=5ms)
dispatch:p95<=50ms PASS p95=39µs (threshold=50ms)

3-provider-20-users

3 providers, 20 users, 60 requests, concurrency=10, streaming=true

Model Providers RAM
mlx-community/Qwen3.5-0.8B-MLX-4bit 3 0.5 GB
Metric Value
Total Requests 60
Success 38
Errors 22
Total Duration 8.128s
Throughput 4.7 req/s

Latency Decomposition

Segment Count Mean P50 P95 Max
total_e2e 38 735ms 135ms 2.486s 2.486s
parse 38 27µs 24µs 60µs 66µs
reserve 38 2ms 2ms 3ms 5ms
route 38 21µs 19µs 58µs 62µs
encrypt 38 0s 0s 1ms 2ms
dispatch 38 36µs 35µs 65µs 89µs
coordinator_to_provider 38 731ms 130ms 2.481s 2.481s

Assertion Report: PASS

Assertion Result Detail
parse:mean<=1ms PASS mean=26.71µs (threshold=1ms)
parse:p95<=5ms PASS p95=60µs (threshold=5ms)
reserve:mean<=50ms PASS mean=1.690763ms (threshold=50ms)
reserve:p95<=200ms PASS p95=3.092ms (threshold=200ms)
encrypt:mean<=5ms PASS mean=468.447µs (threshold=5ms)
encrypt:p95<=50ms PASS p95=644µs (threshold=50ms)
dispatch:mean<=5ms PASS mean=35.842µs (threshold=5ms)
dispatch:p95<=50ms PASS p95=65µs (threshold=50ms)

1-provider-scaling

1 providers, 5 users, 30 requests, concurrency=10, streaming=true

Model Providers RAM
mlx-community/Qwen3.5-0.8B-MLX-4bit 1 0.5 GB
Metric Value
Total Requests 30
Success 4
Errors 26
Total Duration 3.63s
Throughput 1.1 req/s

Latency Decomposition

Segment Count Mean P50 P95 Max
total_e2e 4 2.097s 2.097s 2.097s 2.097s
parse 4 15µs 15µs 21µs 21µs
reserve 4 2ms 2ms 2ms 2ms
route 4 12µs 14µs 15µs 15µs
encrypt 4 1ms 1ms 1ms 1ms
dispatch 4 19µs 20µs 29µs 29µs
coordinator_to_provider 4 2.092s 2.092s 2.092s 2.092s

Assertion Report: PASS

Assertion Result Detail
parse:mean<=1ms PASS mean=15µs (threshold=1ms)
parse:p95<=5ms PASS p95=21µs (threshold=5ms)
reserve:mean<=50ms PASS mean=2.18175ms (threshold=50ms)
reserve:p95<=200ms PASS p95=2.36ms (threshold=200ms)
encrypt:mean<=5ms PASS mean=737.25µs (threshold=5ms)
encrypt:p95<=50ms PASS p95=1.036ms (threshold=50ms)
dispatch:mean<=5ms PASS mean=19.25µs (threshold=5ms)
dispatch:p95<=50ms PASS p95=29µs (threshold=50ms)

3-provider-scaling

3 providers, 5 users, 30 requests, concurrency=10, streaming=true

Model Providers RAM
mlx-community/Qwen3.5-0.8B-MLX-4bit 3 0.5 GB
Metric Value
Total Requests 30
Success 30
Errors 0
Total Duration 5.808s
Throughput 5.2 req/s

Latency Decomposition

Segment Count Mean P50 P95 Max
total_e2e 30 809ms 146ms 2.335s 2.335s
parse 30 24µs 22µs 43µs 59µs
reserve 30 2ms 1ms 3ms 3ms
route 30 19µs 16µs 35µs 73µs
encrypt 30 0s 0s 1ms 1ms
dispatch 30 31µs 26µs 77µs 96µs
coordinator_to_provider 30 805ms 144ms 2.329s 2.33s

Assertion Report: PASS

Assertion Result Detail
parse:mean<=1ms PASS mean=23.633µs (threshold=1ms)
parse:p95<=5ms PASS p95=43µs (threshold=5ms)
reserve:mean<=50ms PASS mean=1.567833ms (threshold=50ms)
reserve:p95<=200ms PASS p95=2.777ms (threshold=200ms)
encrypt:mean<=5ms PASS mean=432.866µs (threshold=5ms)
encrypt:p95<=50ms PASS p95=742µs (threshold=50ms)
dispatch:mean<=5ms PASS mean=31.4µs (threshold=5ms)
dispatch:p95<=50ms PASS p95=77µs (threshold=50ms)

5-provider-scaling

5 providers, 5 users, 30 requests, concurrency=10, streaming=true

Model Providers RAM
mlx-community/Qwen3.5-0.8B-MLX-4bit 5 0.5 GB
Metric Value
Total Requests 30
Success 30
Errors 0
Total Duration 6.869s
Throughput 4.4 req/s

Latency Decomposition

Segment Count Mean P50 P95 Max
total_e2e 30 816ms 13ms 2.411s 2.412s
parse 30 33µs 28µs 66µs 114µs
reserve 30 2ms 2ms 5ms 6ms
route 30 26µs 24µs 42µs 46µs
encrypt 30 1ms 1ms 1ms 5ms
dispatch 30 77µs 30µs 268µs 925µs
coordinator_to_provider 30 810ms 5ms 2.401s 2.404s

Assertion Report: PASS

Assertion Result Detail
parse:mean<=1ms PASS mean=32.666µs (threshold=1ms)
parse:p95<=5ms PASS p95=66µs (threshold=5ms)
reserve:mean<=50ms PASS mean=2.398866ms (threshold=50ms)
reserve:p95<=200ms PASS p95=5.348ms (threshold=200ms)
encrypt:mean<=5ms PASS mean=682.4µs (threshold=5ms)
encrypt:p95<=50ms PASS p95=936µs (threshold=50ms)
dispatch:mean<=5ms PASS mean=77.233µs (threshold=5ms)
dispatch:p95<=50ms PASS p95=268µs (threshold=50ms)

3-provider-heavy-100conc-10kb

3 providers, 20 users, 100 requests, concurrency=100, streaming=true

Model Providers RAM
mlx-community/Qwen3.5-0.8B-MLX-4bit 3 0.5 GB
Metric Value
Total Requests 100
Success 12
Errors 88
Total Duration 3.954s
Throughput 3.0 req/s

Latency Decomposition

Segment Count Mean P50 P95 Max
total_e2e 12 2.157s 2.147s 2.179s 2.179s
parse 12 0s 0s 1ms 1ms
reserve 12 9ms 9ms 10ms 10ms
route 12 41µs 38µs 82µs 82µs
encrypt 12 18ms 18ms 19ms 19ms
dispatch 12 52µs 44µs 103µs 103µs
coordinator_to_provider 12 2.117s 2.107s 2.139s 2.139s

Assertion Report: FAIL

Assertion Result Detail
parse:mean<=1ms PASS mean=486.416µs (threshold=1ms)
parse:p95<=5ms PASS p95=1.084ms (threshold=5ms)
reserve:mean<=50ms PASS mean=9.060416ms (threshold=50ms)
reserve:p95<=200ms PASS p95=10.198ms (threshold=200ms)
encrypt:mean<=5ms FAIL mean=18.08425ms (threshold=5ms)
encrypt:p95<=50ms PASS p95=18.635ms (threshold=50ms)
dispatch:mean<=5ms PASS mean=51.583µs (threshold=5ms)
dispatch:p95<=50ms PASS p95=103µs (threshold=50ms)

@Gajesh2007
Gajesh2007 merged commit 968cc4d into master May 25, 2026
17 checks passed
@Gajesh2007
Gajesh2007 deleted the fix/preflight-reservation-undercharge branch May 25, 2026 22:05

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c01263a883

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +1070 to +1073
} else if totalCost > pr.ReservedMicroUSD {
// Actual cost exceeds reservation (e.g. provider custom
// pricing above platform rate). Attempt to charge the
// consumer the difference. Cap overage at the reservation

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Restrict overage debits to trusted overage cases

Fresh evidence in this revision: even after adding the 2x clamp, the new totalCost > pr.ReservedMicroUSD branch still attempts ledger.Charge for any overage without requiring a custom-price scenario or validating usage against the request’s bounded token budget. In practice, a buggy/malicious provider can report inflated usage and now debit consumers above their pre-flight reservation (up to the new cap), whereas the previous behavior hard-clamped at reservation. Gate overage charging to verifiable/expected cases (e.g., known custom pricing deltas) before debiting.

Useful? React with 👍 / 👎.

if s.billing != nil {
consumerKey := consumerKeyFromContext(r.Context())
reservedMicroUSD = s.reservationCost(model, estimatedPromptTokens, requestedMaxTokens)
reservedMicroUSD = s.reservationCost(model, billingPromptTokens, requestedMaxTokens)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Carry billing token estimate into provider top-up logic

This patch reserves base cost with billingPromptTokens, but provider-specific reservation top-ups still derive from PendingRequest.EstimatedPromptTokens (the routing len/4 heuristic). That mismatch means dense-token prompts can still be under-reserved for custom-priced providers, pushing settlement into the overage/clamp path and reintroducing provider shortfall the change is trying to eliminate. Propagate the billing upper-bound estimate to the provider top-up path instead of reusing the routing heuristic.

Useful? React with 👍 / 👎.


stream, _ := parsed["stream"].(bool)
estimatedPromptTokens := estimatePromptTokens(parsed)
billingPromptTokens := estimateBillingPromptTokens(parsed)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Compute reservation after Responses input normalization

For /v1/responses requests, billing reservation is computed from the raw parsed payload before conversion, but the conversion path strips binary parts (input_image/input_file) down to short placeholders before sending to providers. That means large image/file URLs or blobs can inflate billingPromptTokens far beyond the actual prompt tokens consumed, triggering avoidable insufficient_funds rejections despite low real inference cost. Derive billing prompt tokens from the normalized provider payload for Responses requests to avoid this false over-reservation.

Useful? React with 👍 / 👎.

This branch was successfully deployed

3 active deployments
Preview – d-inference — c01263a8 Deployed May 25, 2026 by vercel[bot]
Preview – d-inference-console-ui-dev — c01263a8 Deployed May 25, 2026 by vercel[bot]
Preview – d-inference-landing — c01263a8 Deployed May 25, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Pre-flight reservation heuristic can undercharge, leaving provider unpaid for work

1 participant