Skip to content

feat(cost): native budget + cost on the SDK transports; consolidate pricing#24

Merged
stxkxs merged 1 commit into
mainfrom
feat/native-cost-budget
Jun 4, 2026
Merged

feat(cost): native budget + cost on the SDK transports; consolidate pricing#24
stxkxs merged 1 commit into
mainfrom
feat/native-cost-budget

Conversation

@stxkxs

@stxkxs stxkxs commented Jun 3, 2026

Copy link
Copy Markdown
Member

The one genuine catch-up from the native-vs-hand-rolled audit. See the commit message for full detail.

Stacked on #21 (base = feat/claude-mid-2026-currency) — it needs the SDK 0.3.x bump (maxBudgetUsd exists only there) and the model-aware usage.ts from #21. Retarget to main once #21 merges.

Summary

  • Closes a real gap: budget enforcement + cost tracking only worked on managed-agents. Now the sdk/sdk-k8s/claude-cli transports get native budget (maxBudgetUsderror_max_budget_usd) and native cost (total_cost_usd from the result, surfaced on session.status_idle) — previously neither existed there.
  • Consolidates pricing into one src/pricing.ts (model + cache aware), replacing 4 divergent copies (2 of them Sonnet-flat). The live budget tracker and the perf report are now model-aware, so Opus roles aren't under-priced.

Theory preserved

The budget policy (per-run limit from getBudgetLimit) stays fab's — only enforcement + cost now use native primitives where the transport exposes them.

Verification

npm run lint / build / format:check clean · npm test 274/274 (+pricing + sdk-events cost tests).

…ricing

The native-vs-hand-rolled audit found one genuine catch-up: budget enforcement
and cost tracking only worked on the managed-agents transport. This closes that
gap with native SDK primitives and unifies the pricing tables. The budget
POLICY stays fab's; only enforcement + cost now ride native knobs where the
transport exposes them.

─── Native budget + cost on sdk / sdk-k8s / claude-cli (Rank 1) ───

These transports had NO budget enforcement and NO cost tracking: the tracker in
streamSessionWithAdvisor only accumulates `span.model_request_end` events, which
only managed-agents emits.

- sdk.ts passes `maxBudgetUsd` (from getBudgetLimit) into the Agent SDK
  query(); the SDK stops the run with an `error_max_budget_usd` result when the
  USD cap is exceeded, which sdk-events already maps to session.error. Native
  per-run budget enforcement on the SDK transports.
- sdk-events attaches the result's native `total_cost_usd` onto session.status_idle;
  streamSessionWithAdvisor reads it and reports the actual run cost on those
  transports (previously $0 / unknown). claude-cli rides the same translator, so
  it's covered too.

─── Pricing consolidation (Rank 2) ───

- New src/pricing.ts is the single source: MODEL_RATES (Opus $5/$25, Sonnet
  $3/$15, Haiku $1/$5) + cache multipliers (read 0.1x, write 1.25x) + rateFor +
  estimateCost. Replaces four divergent copies — usage.ts's (good), workflows.ts
  MODEL_PRICING (speed-keyed Sonnet-flat), and perf.ts's per-role + totals
  (Sonnet-flat inline).
- workflows.ts budget tracker now prices spans model+cache-aware via the role's
  model instead of a flat Sonnet rate.
- usage.ts + perf.ts consume pricing.ts; perf per-role and totals are now
  model-aware, so Opus roles are no longer under-priced.

─── Tests ───

- pricing.test.ts — rateFor tiers + sonnet fallback; estimateCost input/output +
  cache-read (0.1x) / cache-write (1.25x) + null cache fields.
- sdk-events.test.ts — total_cost_usd attaches to status_idle; omitted on the
  managed-agents result shape.

Depends on the SDK 0.3.x bump + model-aware usage.ts from #21 (this branch is
stacked on it). Verification: npm run lint / build / format:check clean; 274 tests.

Co-authored-by: stxkxsbot <275011021+stxkxsbot@users.noreply.github.com>
@stxkxs stxkxs force-pushed the feat/native-cost-budget branch from 0251c7b to c2d7389 Compare June 4, 2026 02:07
@stxkxs stxkxs changed the base branch from feat/claude-mid-2026-currency to main June 4, 2026 02:07
@stxkxs stxkxs marked this pull request as ready for review June 4, 2026 02:07
@stxkxs stxkxs merged commit ac7b1b3 into main Jun 4, 2026
@stxkxs stxkxs deleted the feat/native-cost-budget branch June 4, 2026 02:13
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.

1 participant