Skip to content

Persist per-turn token usage + product marker for third-party usage trackers #1420

Description

@instax-dutta

What

Third-party usage trackers (e.g. xiufengsun/TokenTracker#499) cannot measure Freebuff token usage from local files today. Two small persistence additions would unblock passive tracking without changing any runtime behavior.

Why it is blocked today (verified on current main)

  1. ChatMessageMetadata (cli/src/types/chat.ts) has no usage field. chat-messages.json carries credits + metadata.runState only.
  2. Token counts (inputTokens, outputTokens, cachedInputTokens) flow only in-memory via onUsageReceived (sdk/src/impl/llm.ts, packages/agent-runtime/src/run-agent-step.ts) and are never written to chat-messages.json or run-state.json. providerOptions.codebuff.usage in persisted history carries only { cost, costDetails } (sdk/src/impl/model-provider.ts).
  3. IS_FREEBUFF (cli/src/utils/constants.ts) is never stamped to disk, so Freebuff and paid Codebuff chats sharing ~/.config/manicode/ are indistinguishable after the fact.

Request (2 additions, both gated on existing constants)

  1. Per-turn usage receipt: on run completion, write the final ModelUsageData ({ inputTokens, outputTokens, cachedInputTokens, totalTokens, reasoningOutputTokens? }, type in common/src/types/contracts/llm.ts) into the persisted chat state — e.g. ChatMessage.metadata.usage in chat-messages.json and/or a usage summary in run-state.json. Raw provider values only, no estimation. Note inputTokens is inclusive of cache reads (common/src/util/tokens.ts), so consumers need both fields to avoid double-counting.
  2. Product marker: stamp e.g. { product: "freebuff" } (exact key your call) into run-state.json / chat-meta, gated on the existing IS_FREEBUFF flag. Natural home: saveChatState in cli/src/utils/run-state-storage.ts or writeChatMeta in cli/src/utils/chat-meta.ts. This fixes the Codebuff-vs-Freebuff attribution problem permanently; model-ID heuristics rot on every catalog change.

Scope notes

  • No behavior change, no estimation fallback, no limits/quota surface (rateLimitsByModel stays out of scope — it is pool quota, not tokens).
  • Env nit: third-party docs currently say FREEBUFF_DATA_DIR; actual override is FREEBUFF_CONFIG_DIR (cli/src/utils/config-dir.ts). Worth one line in README when this ships.

Happy to test the build on macOS once a snapshot exists.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:cliThe Codebuff/Freebuff terminal clientbot:triagedClassified by the community triage bottype:featureA request for new behavior

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions