Skip to content

feat: CodeBuddy IDE support + fix CLI cached-token double-counting - #39

Merged
juliantanx merged 2 commits into
mainfrom
feat/codebuddy-ide-support
Jul 1, 2026
Merged

feat: CodeBuddy IDE support + fix CLI cached-token double-counting#39
juliantanx merged 2 commits into
mainfrom
feat/codebuddy-ide-support

Conversation

@juliantanx

Copy link
Copy Markdown
Owner

Summary

Two related CodeBuddy fixes, discovered while investigating why CodeBuddy usage wasn't showing up in the dashboard.

1. CodeBuddy IDE support (new source)

The existing codebuddy parser only covered the CLI variant (~/.codebuddy/projects/*.jsonl). The Tencent CodeBuddy IDE (incl. the CN build) stores usage completely differently — per-message JSON files under:

~/Library/Application Support/CodeBuddyExtension/Data/**/CodeBuddyIDE/**/history/<session>/<conversation>/messages/*.json

So IDE usage was never detected. This adds a new codebuddy-ide source (reported under the existing CodeBuddy tool, so it shows as one tool in the UI):

  • Cross-platform probe (macOS / Windows / Linux), overridable via AIUSAGE_CODEBUDDY_IDE_PATH.
  • Dedicated parser that emits one record per conversation from the cumulative statsSnapshot (cache-miss input, cached input, output), with a stable id so re-parsing is idempotent (no double-counting across turns).
  • Workspace cwd resolved from the prompt's Workspace Folder block.
  • A codebuddyIde watermark cursor to skip already-imported conversations.

2. Fix: CodeBuddy CLI double-counted cached tokens

The CLI logs usage under both message.usage (Anthropic-shaped field names but OpenAI-style semanticsinput_tokens already includes cached tokens) and providerData.rawUsage. The generic parser assumed Anthropic semantics and added cache_read_input_tokens on top of the cache-inclusive input_tokens, inflating input by the cached amount (~100× on cache-heavy turns).

Now reads the clean prompt_cache_hit/miss decomposition from rawUsage (falling back to message.usage with cache reads subtracted).

Verified on real glm-5.1 CLI logs:

input (before) input (after)
turn 1 27537 273
turn 2 28923 251

Cache reads preserved; cost drops accordingly (was overstated ~4–5×).

Test plan

  • New core regression test (codebuddy-cli.test.ts) — rawUsage decomposition + message.usage fallback.
  • New CLI parser test (parse-codebuddy.test.ts) — one record per conversation, skips no-usage conversations, idempotent re-parse.
  • Full suites green: core 98, cli 398.
  • Verified against real local data (IDE deepseek-v4-flash; CLI glm-5.1).

Notes

  • Models like deepseek-v4-flash / glm-5.1 are CodeBuddy's own names; they resolve to prices via the existing LiteLLM-synced registry aliases. CodeBuddy bills in its own credit unit, so token-based cost is an estimate.
  • packages/web/src/lib/constants.js needs no change — it's keyed by tool enum, and this reuses codebuddy.

The existing codebuddy parser only covered ~/.codebuddy/projects JSONL,
so the Tencent CodeBuddy CN IDE went undetected — its per-message JSON
logs live under CodeBuddyExtension/Data/**/CodeBuddyIDE/**/history/
<session>/<conversation>/messages/.

Add a new codebuddy-ide source (reported under the existing CodeBuddy
tool) with a cross-platform probe, a dedicated parser that emits one
record per conversation from the cumulative statsSnapshot, and a
watermark cursor to skip already-imported conversations. Override the
path with AIUSAGE_CODEBUDDY_IDE_PATH.
CodeBuddy CLI logs usage under both message.usage (Anthropic-shaped
field names but OpenAI-style semantics, where input_tokens already
includes cached tokens) and providerData.rawUsage. The generic parser
assumed Anthropic semantics and added cache_read_input_tokens on top of
the cache-inclusive input_tokens, inflating input by the cached amount
(~100x on cache-heavy turns).

Read the clean prompt_cache_hit/miss decomposition from rawUsage,
falling back to message.usage with cache reads subtracted from input.
Verified against real glm-5.1 CLI logs (input 27537 -> 273, 28923 ->
251) and covered by a core regression test.
@juliantanx
juliantanx merged commit 330ae3d into main Jul 1, 2026
7 checks passed
@juliantanx
juliantanx deleted the feat/codebuddy-ide-support branch July 16, 2026 03:30
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