Skip to content

feat(copilot): parse VS Code Copilot Chat session token usage - #875

Merged
junhoyeo merged 1 commit into
junhoyeo:mainfrom
leecoder:feat/copilot-vscode-chat-parser
Jul 13, 2026
Merged

feat(copilot): parse VS Code Copilot Chat session token usage#875
junhoyeo merged 1 commit into
junhoyeo:mainfrom
leecoder:feat/copilot-vscode-chat-parser

Conversation

@leecoder

@leecoder leecoder commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Summary

Add support for parsing VS Code Copilot Chat token usage from local chatSessions/*.jsonl files. Previously, tokscale only tracked Copilot usage when OTEL export was explicitly enabled. Now VS Code panel chat usage is automatically discovered and tracked.

What's Changed

  • New file: sessions/copilot_vscode.rs — JSONL parser for VS Code chat session files
  • sessions/mod.rs — module registration
  • scanner.rscopilot_vscode_sessions: Vec<PathBuf> + auto-discovery across macOS/Linux/Windows
  • lib.rs — wired into both parse paths with dedup against OTEL sessions

Data Source

VS Code stores chat sessions in:

  • macOS: ~/Library/Application Support/Code/User/workspaceStorage/*/chatSessions/*.jsonl
  • Linux: ~/.config/Code/User/workspaceStorage/*/chatSessions/*.jsonl
  • Windows: %APPDATA%/Code/User/workspaceStorage/*/chatSessions/*.jsonl

Each JSONL file contains:

  • kind=0 lines with initial requests[] array
  • kind=2 lines with appended request items

From each request item we extract:

Field Source
input tokens promptTokens or result.metadata.promptTokens
output tokens completionTokens or result.metadata.outputTokens
reasoning tokens sum of toolCallRounds[*].thinking.tokens
model result.metadata.resolvedModel (fallback: modelId with copilot/ prefix stripped)
timestamp timestamp (epoch ms)

Dedup Strategy

  • Existing OTEL and desktop app messages take priority
  • VS Code parser only emits messages whose dedup_key is not already present
  • Dedup key format: copilot-vscode:{session_id}:{timestamp_ms}

Tested

  • cargo test -p tokscale-core --lib — 1177 tests pass
  • cargo clippy -p tokscale-core -- -D warnings — clean
  • cargo fmt -p tokscale-core --check — clean
  • Manual verification: today's VS Code Copilot Chat session correctly shows gpt-5.3-codex, 22K input, 154 output, 88 reasoning tokens, $0.042 cost

Summary by cubic

Adds support for parsing VS Code Copilot Chat token usage from local workspaceStorage/*/chatSessions/*.jsonl files. Automatically tracks panel chat usage without OTEL and deduplicates against existing Copilot sources.

  • New Features
    • Added sessions/copilot_vscode.rs to parse kind=0 and kind=2 entries, extract input/output/reasoning tokens, resolve model via resolvedModel (fallback strips copilot/ from modelId), and ignore non‑Copilot requests.
    • Scanner discovers session files on macOS, Linux, and Windows and exposes them via ScanResult.copilot_vscode_sessions.
    • Integrated into both parse paths with pricing; dedup by dedup_key and (session_id, timestamp) with priority to OTEL and desktop app messages.
    • Associates messages with the VS Code workspace via workspace.json when available.

Written for commit 290ad7a. Summary will update on new commits.

Review in cubic

@vercel

vercel Bot commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
tokscale Ignored Ignored Preview Jul 13, 2026 7:00am

Request Review

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

1 issue found across 4 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="crates/tokscale-core/src/sessions/copilot_vscode.rs">

<violation number="1" location="crates/tokscale-core/src/sessions/copilot_vscode.rs:122">
P2: Model/provider attribution can be wrong for IDs that merely contain a family name, because this parser delegates to substring-based inference. Use the repository's delimited-token model matching so non-family Copilot IDs remain `github-copilot`.

(Based on your team's feedback about model-family token matching.) [FEEDBACK_USED]</violation>
</file>

Tip: cubic used a learning from your PR history. Let your coding agent read cubic learnings directly with the cubic MCP.

Re-trigger cubic

Comment thread crates/tokscale-core/src/sessions/copilot_vscode.rs
return None;
}

let provider_id = inferred_provider_from_model(&model_id)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2: Model/provider attribution can be wrong for IDs that merely contain a family name, because this parser delegates to substring-based inference. Use the repository's delimited-token model matching so non-family Copilot IDs remain github-copilot.

(Based on your team's feedback about model-family token matching.)

View Feedback

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At crates/tokscale-core/src/sessions/copilot_vscode.rs, line 122:

<comment>Model/provider attribution can be wrong for IDs that merely contain a family name, because this parser delegates to substring-based inference. Use the repository's delimited-token model matching so non-family Copilot IDs remain `github-copilot`.

(Based on your team's feedback about model-family token matching.) </comment>

<file context>
@@ -0,0 +1,334 @@
+        return None;
+    }
+
+    let provider_id = inferred_provider_from_model(&model_id)
+        .unwrap_or("github-copilot")
+        .to_string();
</file context>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not a VS Code parser issue — inferred_provider_from_model is the project-wide standard used by all client parsers. Copilot resolvedModel values are canonical model IDs (gpt-5.3-codex, claude-sonnet-4) that match correctly. Unknown models fall back to github-copilot which is the correct attribution for Copilot-routed requests where the underlying provider is opaque.

@leecoder
leecoder force-pushed the feat/copilot-vscode-chat-parser branch from e336e51 to bb9a550 Compare July 13, 2026 07:00
VS Code stores per-request token counts in workspaceStorage chatSessions
JSONL files. Parse kind=0 (initial state) and kind=2 (array appends) to
extract promptTokens, completionTokens, resolvedModel, and reasoning
tokens from toolCallRounds.

Scans ~/Library/Application Support/Code/User/workspaceStorage/*/chatSessions/
on macOS and equivalent paths on Linux/Windows. Deduplicates against
OTEL and desktop app sessions by dedup_key and session_id+timestamp.
@leecoder
leecoder force-pushed the feat/copilot-vscode-chat-parser branch from bb9a550 to 290ad7a Compare July 13, 2026 07:00
@junhoyeo
junhoyeo merged commit 074619f into junhoyeo:main Jul 13, 2026
14 checks passed
@leecoder
leecoder deleted the feat/copilot-vscode-chat-parser branch July 13, 2026 08: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.

2 participants