Skip to content

Cost reconciliation

The cost-reconcile extension records the real billed cost that providers report in their own response payloads, so session cost totals match the actual bill instead of rate-card estimates.

Bundled with Selesai

Loaded automatically with Selesai; no per-extension npm install is required.

Manifest entry
./cost-reconcile.ts
Runtime surface
Automatic

Setup and prerequisites

  • Bundled with Selesai and loaded automatically; no separate install is required.
  • Works with gateways that report a cost in the response: LiteLLM (x-litellm-response-cost header), OpenRouter (usage.cost / total_cost), and object-shaped cost.total.
  • Providers whose payloads carry no cost (first-party OpenAI/Anthropic) retain their rate-card estimate, which matches the bill when catalog prices are configured.

What it sets up

  • Installs a stream-wrapping fetch once per process that scans LLM API responses for provider-reported cost and response ids. Streamed bodies repeat the response id across chunks and carry tool-call ids (call_*), so the wrapper keys the billed cost by every id found. Capture finishes before the provider SDK finalizes its assistant message.
  • On message_end, replaces the finalized assistant usage.cost.total with the provider-reported billed amount before the message is persisted, consuming only the captured entry matching the finalized message’s responseId, and writes a cost-reconcile custom session entry per response id (bounded per session) for zentui and existing-session compatibility. A duplicate capture of the same id (retry) is marked ambiguous rather than assigning either bill.
  • The zentui footer prefers reconciled entries over the rate-card total when both exist for the same response id, and refreshes once after settle so late reconciliation is picked up.
  • Cost labels format adaptively so sub-cent per-call costs stay visible: 3 decimals at $1+, 4 decimals at $0.01+, and 6 decimals below (e.g. $0.000336 instead of $0.000).

What you can configure

Cost reconciliation is automatic: it captures provider-reported costs from LLM responses and records them as session entries; the zentui footer prefers them over rate-card estimates.

No user configuration

This capability does not expose user-editable settings. Its behavior is controlled by the commands and runtime rules documented below.

Source evidence

There are no user-facing settings. Capture limits are constants in the extension source.

What you can do

  • See session cost totals that match what the gateway actually billed.
  • Keep the zentui footer cost label accurate for OpenRouter-style gateways and custom catalogs with dated pricing.

Commands, tools, and shortcuts

  • None. The extension is fully automatic.

Limits and safety

  • Capture is best-effort: the response body is scanned in the background while the response stream passes through unchanged; the main response is never altered.
  • Bodies are scanned up to a size cap; the last cost match in a stream wins (usage lands in the final chunk).
  • The extension never reads or stores message content — only response ids, provider/model names, and reported cost numbers.

Source evidence

Selesai version: 0.10.0