nanocoder icon indicating copy to clipboard operation
nanocoder copied to clipboard

[Bug] Token counts and costs are lost when loading chat history

Open akramcodez opened this issue 3 months ago • 2 comments

Description

When continuing a previous conversation from history, the token counts and estimated costs (e.g., Tokens: 7k | <$0.01) are missing from the old messages. This suggests that the token usage metadata is either not being saved to disk when the session is persisted, or it's not being successfully parsed and restored when the chat history is loaded back into the webview.

Steps to Reproduce

  1. Start a new chat session and send a prompt.
  2. Wait for the AI to respond. Notice that the token usage and cost are displayed at the bottom of the response.
  3. Close the chat or switch to a different session.
  4. Reopen the previous chat session from history.
  5. Look at the previous AI response - the token count and cost indicator are now missing.

Expected Behavior

The token usage and cost for every message should be permanently saved with the chat history. When reloading a previous session, the token count should be accurately restored and displayed exactly as it was when the message was originally generated.

Actual Behavior

The token/cost UI disappears when loading historical messages, meaning this data is currently ephemeral and lost upon closing the session.

Proposed Solution

Ensure that token usage and cost metadata are properly serialized into the session history JSON files on disk. When loadSession (or the equivalent history parser) rebuilds the chat context, it must map this metadata back onto the message objects sent to the VS Code webview.

Additional Context

  • [x] I have searched existing issues to ensure this is not a duplicate
  • [x] This feature aligns with the project's goals (local-first AI assistance)

Before

Image

After

Image

Implementation Notes (optional)

  • Check plugins/vscode/src/prompt-history.ts (or wherever session JSON is saved/loaded) to verify if usage or tokens metadata is being intentionally stripped or accidentally omitted during serialization.
  • Ensure the AcpAgent.loadSession method in the CLI backend is correctly reconstructing and forwarding the usage data via the Agent Client Protocol to the VS Code extension.

akramcodez avatar Aug 31 '26 11:08 akramcodez

I would like to pick this up. I traced the gap to the ACP path: usage and cost are returned only with the live prompt response, while persisted session messages and history replay carry no per-turn usage metadata. I propose to store optional usage/cost on the final assistant message for each completed turn, replay that metadata after the matching response, and add regression coverage for save/load/replay plus backward compatibility with existing sessions that have no usage fields. Could you assign this to me if that scope looks right?

RRXXZZYY avatar Aug 31 '26 11:08 RRXXZZYY

sure @RRXXZZYY 👍

akramcodez avatar Aug 31 '26 11:08 akramcodez