Skip to content

fix: persist accumulated cost in session DB to survive reload - #9191

Merged
DOsinga merged 2 commits into
mainfrom
fix/persist-accumulated-cost
May 14, 2026
Merged

fix: persist accumulated cost in session DB to survive reload#9191
DOsinga merged 2 commits into
mainfrom
fix/persist-accumulated-cost

Conversation

@DOsinga

@DOsinga DOsinga commented May 13, 2026

Copy link
Copy Markdown
Collaborator

Summary

Fixes #6141

When users switch models mid-session, the cost tracking was computed client-side in an ephemeral React hook (useCostTracking). On session reload (page refresh), this in-memory state was lost and cost was incorrectly recalculated using the current model's pricing against all accumulated tokens.

Fix

Compute cost at the time tokens are generated (when we know which model produced them), persist it in a new accumulated_cost column on the sessions table, and expose it via TokenState to the frontend.

Backend:

  • Added accumulated_cost (f64) column to sessions table (schema v13)
  • In update_session_metrics, compute the cost for each usage chunk using the canonical model pricing for the model that generated those tokens, and accumulate it
  • Exposed accumulated_cost in TokenState SSE events

Frontend:

  • CostTracker now uses the backend-persisted accumulatedCost directly
  • Removed the ephemeral useCostTracking hook that lost state on reload
  • Falls back to client-side calculation when backend cost is not yet available

Type of Change

  • Bug fix

Testing

  • All Rust tests pass (59 session tests)
  • All desktop UI tests pass (346 tests)
  • TypeScript compiles cleanly
  • Clippy clean

Compute cost at the time tokens are generated (when we know which model
produced them), accumulate it in a new accumulated_cost column on the
sessions table, and expose it via TokenState to the frontend.

The frontend CostTracker now uses the backend-persisted cost directly,
which correctly accounts for model switches. The ephemeral client-side
useCostTracking hook (which lost state on reload) is removed.

Fixes #6141

Signed-off-by: Douwe Osinga <douwe@squareup.com>
const total = inputCost + outputCost;

return total;
return inputCost + outputCost;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't we move this part to goosed too? and get rid of all calculation on the client?

@DOsinga
DOsinga force-pushed the fix/persist-accumulated-cost branch from d658151 to 7bb8e38 Compare May 14, 2026 16:06
Address review feedback: the client-side fallback calculation was
dead code since the backend and frontend use the same pricing data.
Simplify calculateCost to just return the backend accumulated cost.

Signed-off-by: Douwe Osinga <douwe@squareup.com>
@DOsinga
DOsinga force-pushed the fix/persist-accumulated-cost branch from 7bb8e38 to c212334 Compare May 14, 2026 16:24
@DOsinga
DOsinga added this pull request to the merge queue May 14, 2026
Merged via the queue into main with commit 4671053 May 14, 2026
22 checks passed
@DOsinga
DOsinga deleted the fix/persist-accumulated-cost branch May 14, 2026 17:11
lifeizhou-ap added a commit that referenced this pull request May 15, 2026
* main: (102 commits)
  Dynamically refresh skill instructions each turn (#9217)
  Build non-vulkan linux variants using ubuntu 22.04 (#9211)
  fix(ui): show tool name in approval prompt (#9216)
  feat: add Atomic Chat as declarative OpenAI-compatible provider (#9210)
  chore: bump package.json versions from 0.19.1 to 0.20.0 (#9218)
  feat: support GOOSE_OAUTH_CALLBACK_PORT for stable OAuth redirect_uri (#9209)
  [RFC] feat(oauth): proactive token refresh to avoid re-auth on every session (#8386)
  fix: resolve Azure CLI on Windows by using az.cmd (#9215)
  fix: handle non-interactive terminal in goose configure on Windows (#9214)
  Better parsing of pasted html as markdown so agents understand (#9190)
  fix: persist accumulated cost in session DB to survive reload (#9191)
  fix(publish-npm): build binary from current SHA + add compat check (#9212)
  feat(desktop): add goose://new-session deep link to open fresh chat (#9196)
  Add PR previews using cloudflare pages (#9208)
  fix: prevent tool-use marker leakage in toolshim output (#8310)
  Prompt injection mitigation: update pattern-based detection (#9198)
  remove goose2 related skills (#9189)
  Switch GH pages deploy to actions/artifact workflow (#9025)
  fix(summon): re-apply canonical limits when delegate overrides model (#9183)
  Split code signing from build (#8587)
  ...
@DOsinga
DOsinga restored the fix/persist-accumulated-cost branch June 2, 2026 20:18
shafqatevo pushed a commit to shafqatevo/goose that referenced this pull request Aug 7, 2026
…oose#9191)

Signed-off-by: Douwe Osinga <douwe@squareup.com>
Co-authored-by: Douwe Osinga <douwe@squareup.com>
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.

Accumulated pricing after model changes does not persist across session reload

2 participants