Skip to content

feat(billing): single-source usage via main-process relay#2358

Draft
k11kirky wants to merge 1 commit into
posthog-code/context-breakdown-fill-categoriesfrom
posthog-code/usage-monitor-relay
Draft

feat(billing): single-source usage via main-process relay#2358
k11kirky wants to merge 1 commit into
posthog-code/context-breakdown-fill-categoriesfrom
posthog-code/usage-monitor-relay

Conversation

@k11kirky
Copy link
Copy Markdown
Contributor

@k11kirky k11kirky commented May 25, 2026

Problem

The renderer was polling the LLM gateway directly every 30 seconds to fetch usage data, coupling the renderer to the gateway and duplicating polling logic that the UsageMonitorService already handles.

Changes

The UsageMonitorService now caches the most recent usage snapshot (latestUsage) and emits a UsageUpdated event after every successful poll. Two new methods are exposed: getLatest() to retrieve the cached snapshot synchronously, and refreshNow() to trigger an immediate poll.

The usageMonitorRouter exposes these via three new tRPC endpoints: onUsageUpdated (a subscription that streams snapshots as they arrive), getLatest (a query to bootstrap the renderer before the first event), and refresh (a mutation to force an immediate poll). The llmGateway.usage query endpoint has been removed since it is no longer needed.

The useUsage hook in the renderer now subscribes to onUsageUpdated instead of polling on an interval. It bootstraps from getLatest on mount and exposes a refetch that calls the refresh mutation. The window-focus-based refetch interval logic has been removed entirely.

How did you test this?

Three new unit tests were added to service.test.ts:

  • Verifies that UsageUpdated is emitted and the snapshot is cached on every successful poll.
  • Verifies that UsageUpdated is not emitted and the snapshot remains null when the gateway throws.
  • Verifies that refreshNow triggers a fresh poll and returns the resulting snapshot.

Publish to changelog?

No

Copy link
Copy Markdown
Contributor Author

k11kirky commented May 25, 2026

@k11kirky k11kirky force-pushed the posthog-code/usage-monitor-relay branch from 3562601 to 4055770 Compare May 25, 2026 16:58
@k11kirky k11kirky force-pushed the posthog-code/context-breakdown-fill-categories branch from 923dacd to 08d0f9b Compare May 25, 2026 16:58
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