Skip to content

feat(routing): add cost-aware policy scoring and limits - #1015

Draft
Wibias wants to merge 9 commits into
lidge-jun:devfrom
Wibias:feat/ri-08-cost-aware-routing
Draft

feat(routing): add cost-aware policy scoring and limits#1015
Wibias wants to merge 9 commits into
lidge-jun:devfrom
Wibias:feat/ri-08-cost-aware-routing

Conversation

@Wibias

@Wibias Wibias commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator

Summary

RI-08 of the Router Intelligence / Routing Control Plane programme. Adds
cost-aware policy scoring and hard cost limits, reusing the canonical
price/cost normalization (src/usage/cost.ts).

Estimated cost, price-source provenance, estimated-vs-authoritative
distinction, and cost-limit exclusions are all recorded in the route-decision
trace. Unknown prices stay unknown (never free).

Scope

  • src/routing/cost.ts:
    • costEvidenceForCandidate() - estimateRequestCost-backed evidence:
      estimatedUsd, priceSource (jawcode / expected / unmatched), incomplete
      (estimated usage OR expected-price overlay), limitUsd, excludedByLimit.
    • costScore() - deterministic relative score (cheaper is better, reference
      = COST_SCORE_REFERENCE_USD 1.0 or the profile limit when set); unknown
      returns null.
  • src/routing/evaluator.ts:
    • hard limit: estimatedUsd > limits.maxEstimatedCostUsd -> cost-limit
      exclusion, ineligible;
    • unknown cost policy (unknownEvidence.cost): exclude -> unknown-price
      exclusion, penalize -> deterministic 0.3 floor, allow -> priority only;
    • cost weight folds into the composite score and components.cost.
  • src/router.ts - execution assembles cost evidence per candidate (usage is
    unknown pre-dispatch, so execution-time cost is honestly unknown unless
    evidence is supplied via dry-run/evaluate).
  • tests/cost-scoring.test.ts - 6 tests.

Estimated vs authoritative

  • incomplete is set when usage was estimated OR the price came from the
    expected-price overlay; authoritative jawcode prices with reported usage are
    complete.
  • No monthly billing, invoicing, or hidden automatic budgets.

Privacy / security

  • Cost evidence is a derived number (USD) + source code; no prices leak into
    per-request payloads beyond the bounded trace field.
  • bun run privacy:scan passes.

Compatibility

  • Additive; existing configs load unchanged (limits already validated in
    RI-04). RI-05..07 behavior preserved when optimize.cost: 0 and
    unknownEvidence.cost: allow.

Dependency

Non-goals

  • No billing/invoicing/budget automation.
  • No request-side token estimation at execution time (documented: cost limits
    are enforced on evidence supplied via dry-run/evaluate; execution-time cost
    stays unknown-until-proven).

Local verification (exact)

  • bun x tsc --noEmit -> PASSED (0 errors)
  • bun run test tests/cost-scoring.test.ts -> 6/6 pass
  • Focused regression suites -> 113/113 pass across 9 files
  • bun run privacy:scan -> passed

@coderabbitai

coderabbitai Bot commented Aug 4, 2026

Copy link
Copy Markdown

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 92fe8457-0c7a-4a8d-904b-55a5d2d0fddc

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Comment @coderabbitai help to get the list of available commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant