Skip to content

an open-source alternative to the dots, bots and muses of the world, run an office of claude code/codex like agents on your laptop, sandboxes or anywhere, uses your existing subscriptions

feat(realtime): rt-9 cost-guard engine — audio pricing, session cost store, real spawn estimate (Realtime Michael P2)

Chaitanya Giri172ffdafd556
diff --git a/src/main/realtimeCost.ts b/src/main/realtimeCost.ts
new file mode 100644
index 00000000..5521c682
--- /dev/null
+++ b/src/main/realtimeCost.ts
@@ -0,0 +1,67 @@
+/**
+ * Realtime Michael — main-process cost module (card rt-9, cost-guard).
+ *
+ * Two jobs, both consumed by Kevin's core files via a clean import (no edits from
+ * me to those files — rt-9 ships net-new only):
+ *
+ * 1. spawnCostSpoken() — the REAL $/hour estimate that replaces rt-5's stub in
+ * src/main/realtimeActions.ts. The spawn-confirm flow speaks it back before
+ * hiring an agent. Reuses the existing per-model price table (./pricing) so
+ * the figure tracks the same rates the cost-ledger uses.
+ * 2. re-exports the shared realtime audio cost helpers so a main-side caller can
+ * price a realtime usage delta without reaching into ../shared directly.
+ *
+ * The LIVE session meter + spend cap live in the renderer (where the voice
+ * session and its usage events are) — see src/renderer/src/realtime/costStore.ts.
+ */
+import { priceFor } from './pricing';
+import { computeRealtimeUsd, formatUsd, type RealtimeUsage } from '../shared/realtimePricing';
+
+export { computeRealtimeUsd, formatUsd, type RealtimeUsage };
+
+/**
+ * Rough token throughput of an actively-working coding agent, per wall-clock hour.
+ * A heuristic — real usage swings hugely with the task — used only to turn the
+ * per-model price into a spoken ballpark for the spawn/hire confirm. Tuned to a
+ * busy agent (lots of file reads → big cached-input share).
+ */
+const AGENT_TOKENS_PER_HOUR = {
+ inputTokens: 200_000,
+ outputTokens: 40_000,
+ cacheReadTokens: 400_000,
+ cacheWriteTokens: 60_000
+};
+
+/** Provider → a representative model id when the caller didn't name one, so
+ * priceFor() lands on a sensible family. Non-Claude providers fall through to the
+ * table's Sonnet default (a rough proxy — the estimate is explicitly a ballpark). */
+const PROVIDER_DEFAULT_MODEL: Record<string, string> = {
+ claude: 'claude-opus-4-8',
+ codex: 'gpt',
+ antigravity: 'gemini',
+ gemini: 'gemini'
+};
+
+/** Estimated USD/hour to RUN a freshly-spawned agent on the given provider/model. */
+export function estimateSpawnCostUsdPerHour(provider: string, model?: string): number {
+ const m = (model && model.trim()) || PROVIDER_DEFAULT_MODEL[provider.toLowerCase()] || '';
+ const p = priceFor(m);
+ const t = AGENT_TOKENS_PER_HOUR;
+ return (
+ (t.inputTokens / 1_000_000) * p.inputPerM +
+ (t.outputTokens / 1_000_000) * p.outputPerM +
+ (t.cacheReadTokens / 1_000_000) * p.cacheReadPerM +
+ (t.cacheWriteTokens / 1_000_000) * p.cacheWritePerM
+ );
+}
+
+/**
+ * A spoken phrase for the spawn/hire echo-back confirm, e.g.
+ * "about $6.50 an hour while it runs". Drop-in replacement for rt-5's stub string
+ * in realtimeActions.ts (the spawn branch): just interpolate this where the stub
+ * said "roughly a few dollars an hour".
+ */
+export function spawnCostSpoken(provider: string, model?: string): string {
+ const perHour = estimateSpawnCostUsdPerHour(provider, model);
+ return `about ${formatUsd(perHour)} an hour while it runs (a rough estimate)`;
+}
diff --git a/src/renderer/src/realtime/costStore.ts b/src/renderer/src/realtime/costStore.ts
new file mode 100644
index 00000000..b44f837e
--- /dev/null
+++ b/src/renderer/src/realtime/costStore.ts
@@ -0,0 +1,119 @@
+/**
+ * Realtime Michael — renderer cost store (card rt-9, cost-guard).
+ *
+ * A tiny external store (same useSyncExternalStore shape as session.ts) that
+ * tracks the LIVE cost of the current voice session: it accumulates usage deltas,
+ * prices them via the shared audio rates, and exposes a running dollar figure +
+ * token counts, an optional spend cap, and an idle signal for mic-off-when-idle.
+ *
+ * Net-new + disjoint by design: I own this file + the HUD that reads it. Kevin's
+ * session (session.ts) feeds it through TWO one-line calls (the integration points
+ * god assigned to him):
+ * • on connect(): resetRealtimeCost()
+ * • on each usage delta: recordRealtimeUsage(usage)
+ * and may read getRealtimeCostSnapshot()/isRealtimeIdle()/the overCap flag to
+ * auto-disconnect on cap or after idle (the mic-off action lives in the session,
+ * which this store does not own).
+ */
+import { useSyncExternalStore } from 'react';
+import { computeRealtimeUsd, normalizeRealtimeUsage, type RealtimeUsage } from '@shared/realtimePricing';
+
+export interface RealtimeCostState {
+ /** Running session cost in USD (conservative upper bound — see realtimePricing). */
+ usd: number;
+ inputTokens: number;
+ outputTokens: number;
+ /** Optional spend cap in USD; null = no cap. */
+ capUsd: number | null;
+ /** True once usd >= capUsd (cap set). The session should warn / auto-stop. */
+ overCap: boolean;
+ /** Epoch ms of the last usage delta (proxy for last voice activity), or null. */
+ lastActivityTs: number | null;
+ /** Epoch ms the current session's metering began, or null when off. */
+ startedTs: number | null;
+}
+
+const initial: RealtimeCostState = {
+ usd: 0,
+ inputTokens: 0,
+ outputTokens: 0,
+ capUsd: null,
+ overCap: false,
+ lastActivityTs: null,
+ startedTs: null
+};
+
+let state: RealtimeCostState = { ...initial };
+const listeners = new Set<() => void>();
+
+function emit(): void {
+ for (const l of listeners) l();
+}
+function setState(patch: Partial<RealtimeCostState>): void {
+ state = { ...state, ...patch };
+ emit();
+}
+function recomputeOverCap(usd: number, capUsd: number | null): boolean {
+ return capUsd != null && capUsd > 0 && usd >= capUsd;
+}
+
+/** Begin metering a fresh session (Kevin: call from session connect()). Preserves
+ * the user's chosen cap across sessions; zeroes the running totals. */
+export function resetRealtimeCost(startedAtMs: number): void {
+ setState({
+ usd: 0,
+ inputTokens: 0,
+ outputTokens: 0,
+ overCap: false,
+ lastActivityTs: null,
+ startedTs: startedAtMs
+ });
+}
+
+/** Stop metering (session off). Keeps the final figure visible until next reset. */
+export function endRealtimeCost(): void {
+ setState({ startedTs: null });
+}
+
+/** Accumulate one usage delta (Kevin: call on each realtime usage event). */
+export function recordRealtimeUsage(usage: RealtimeUsage, nowMs: number): void {
+ const { inputTokens, outputTokens } = normalizeRealtimeUsage(usage);
+ if (inputTokens === 0 && outputTokens === 0) return;
+ const usd = state.usd + computeRealtimeUsd(usage);
+ setState({
+ usd,
+ inputTokens: state.inputTokens + inputTokens,
+ outputTokens: state.outputTokens + outputTokens,
+ overCap: recomputeOverCap(usd, state.capUsd),
+ lastActivityTs: nowMs
+ });
+}
+
+/** Set (or clear, with null) the session spend cap. */
+export function setRealtimeCap(capUsd: number | null): void {
+ const cap = capUsd != null && capUsd > 0 ? capUsd : null;
+ setState({ capUsd: cap, overCap: recomputeOverCap(state.usd, cap) });
+}
+
+/** True when no usage delta has arrived for `thresholdMs` while a session is live —
+ * the cue for mic-off-when-idle (the session decides whether to disconnect). */
+export function isRealtimeIdle(thresholdMs: number, nowMs: number): boolean {
+ if (state.startedTs == null) return false;
+ const since = state.lastActivityTs ?? state.startedTs;
+ return nowMs - since >= thresholdMs;
+}
+
+export function getRealtimeCostSnapshot(): RealtimeCostState {
+ return state;
+}
+
+function subscribe(cb: () => void): () => void {
+ listeners.add(cb);
+ return () => listeners.delete(cb);
+}
+
+/** React binding for the cost HUD + the cap control. */
+export function useRealtimeCost(): RealtimeCostState & { setCap: (capUsd: number | null) => void } {
+ const snap = useSyncExternalStore(subscribe, getRealtimeCostSnapshot);
+ return { ...snap, setCap: setRealtimeCap };
+}
diff --git a/src/shared/realtimePricing.ts b/src/shared/realtimePricing.ts
new file mode 100644
index 00000000..a07a31d1
--- /dev/null
+++ b/src/shared/realtimePricing.ts
@@ -0,0 +1,65 @@
+/**
+ * Realtime Michael — audio token pricing for the OpenAI gpt-realtime-2 voice loop
+ * (card rt-9, cost-guard). Shared so BOTH the main cost module (src/main/
+ * realtimeCost.ts) and the renderer cost store (src/renderer/src/realtime/
+ * costStore.ts) price from one source. Pure + dependency-free (no electron, no
+ * main-only imports) so it's safe to import from either process.
+ *
+ * Pricing (locked plan / board): gpt-realtime-2 AUDIO tokens bill at $32 per 1M
+ * input and $64 per 1M output. A voice turn is dominated by audio; text tokens
+ * (instructions, tool args) and cached tokens are cheaper. We deliberately price
+ * ALL input/output tokens at the audio rate: for a cost GUARD a conservative
+ * UPPER bound is the safe choice (warn early rather than under-count), and it uses
+ * only the authoritative audio numbers rather than guessing text/cache rates. The
+ * raw token counts are surfaced alongside the dollar figure so it stays auditable.
+ */
+
+/** USD per 1,000,000 audio tokens (gpt-realtime-2). */
+export const REALTIME_AUDIO_INPUT_PER_MTOK = 32;
+export const REALTIME_AUDIO_OUTPUT_PER_MTOK = 64;
+
+/**
+ * A usage delta as reported by the realtime session. Accepts both the SDK's
+ * camelCase (`inputTokens`) and the raw realtime API's snake_case
+ * (`input_tokens`) so the caller can forward whatever shape it has.
+ */
+export interface RealtimeUsage {
+ inputTokens?: number;
+ outputTokens?: number;
+ totalTokens?: number;
+ input_tokens?: number;
+ output_tokens?: number;
+ total_tokens?: number;
+}
+
+/** Pull input/output token counts out of either casing; missing ⇒ 0. */
+export function normalizeRealtimeUsage(u: RealtimeUsage | null | undefined): {
+ inputTokens: number;
+ outputTokens: number;
+} {
+ const n = (v: number | undefined): number => (typeof v === 'number' && isFinite(v) && v > 0 ? v : 0);
+ return {
+ inputTokens: n(u?.inputTokens) || n(u?.input_tokens),
+ outputTokens: n(u?.outputTokens) || n(u?.output_tokens)
+ };
+}
+
+/**
+ * USD cost for ONE realtime usage delta. Conservative upper bound: every input
+ * token is priced at the audio-input rate and every output token at the
+ * audio-output rate (see file header).
+ */
+export function computeRealtimeUsd(u: RealtimeUsage | null | undefined): number {
+ const { inputTokens, outputTokens } = normalizeRealtimeUsage(u);
+ return (
+ (inputTokens / 1_000_000) * REALTIME_AUDIO_INPUT_PER_MTOK +
+ (outputTokens / 1_000_000) * REALTIME_AUDIO_OUTPUT_PER_MTOK
+ );
+}
+
+/** Compact USD formatter: <$1 shows cents (e.g. $0.42), else 2dp (e.g. $12.30). */
+export function formatUsd(n: number): string {
+ if (!isFinite(n) || n <= 0) return '$0.00';
+ if (n < 0.01) return '<$0.01';
+ return `$${n.toFixed(2)}`;
+}