Skip to content

Copilot CLI provider under-reports cost — ignores  session.shutdown  token/cost data in  ~/.copilot/session-state/*/events.jsonl #676

Closed

Description

@fgapito

Summary

The Copilot provider ( src/providers/copilot.ts ) under-reports cost and token usage for Copilot CLI sessions ( ~/.copilot/session-state/*/events.jsonl ). It only parses  assistant.message  events, which carry  outputTokens  only. It never reads the session's  session.shutdown  event, which already contains the full token breakdown (input, cache-read, cache-write, output) and the actual computed cost per model — data the CLI itself writes to disk.

The code even acknowledges the gap in a comment:

// Copilot JSONL only logs outputTokens; inputTokens are NOT available.
// Cost will be lower than actual API cost. This is the original
// behaviour — OTel data (below) replaces it when available.
const costUSD = calculateCost(currentModel, 0, outputTokens, 0, 0, 0)

The OTel/ agent-traces.db  fallback mentioned in the file only applies to VS Code Copilot Chat — it does not exist for standalone Copilot CLI usage, so CLI-only users get no compensating richer source at all. For CLI users, cost is silently and significantly underreported (in my session, cache-read tokens alone were 35k+, entirely excluded).

Evidence

Actual  session.shutdown  event from  ~/.copilot/session-state//events.jsonl  (produced by Copilot CLI 1.0.68):

{"type":"session.shutdown","data":{"shutdownType":"routine","totalPremiumRequests":2,"totalNanoAiu":10001450000,"tokenDetails":{"input":{"tokenCount":4},"cache_read":{"tokenCount":35495},"cache_write":{"tokenCount":35783},"output":{"tokenCount":345}},"totalApiDurationMs":7495,"sessionStartTime":1784102040274,"eventsFileSizeBytes":98693,"codeChanges":{"linesAdded":0,"linesRemoved":0,"filesModified":[]},"modelMetrics":{"claude-sonnet-5":{"requests":{"count":2,"cost":2},"usage":{"inputTokens":71282,"outputTokens":345,"cacheReadTokens":35495,"cacheWriteTokens":35783,"reasoningTokens":31},"totalNanoAiu":10001450000,"tokenDetails":{"input":{"tokenCount":4},"cache_read":{"tokenCount":35495},"cache_write":{"tokenCount":35783},"output":{"tokenCount":345}}}},"currentModel":"claude-sonnet-5","currentTokens":27176,"systemTokens":11899,"conversationTokens":605,"toolDefinitionsTokens":14668}}

Note  modelMetrics..usage  already contains  inputTokens ,  outputTokens ,  cacheReadTokens ,  cacheWriteTokens ,  reasoningTokens , plus  totalNanoAiu  (looks like a pre-computed cost/AIU unit) and  requests.cost . This is a per-session rollup written once at shutdown, in addition to the per-turn  assistant.message  events currently parsed.

Expected behavior

The JSONL parser ( createJsonlParser  in  src/providers/copilot.ts ) should also handle a  session.shutdown  event type and either:

  1. Use  modelMetrics.<model>.usage  (inputTokens/outputTokens/cacheReadTokens/cacheWriteTokens) to emit a corrected/supplementary  ParsedProviderCall  per model with the real token breakdown, replacing the output-tokens-only estimate from  assistant.message  events, or
  2. At minimum, use it to scale/correct the aggregate cost computed from  assistant.message  events for that session.

This event only appears once per session file (at shutdown) and is present even without any VS Code/OTel/JetBrains integration, so it's the most reliable source of accurate cost for CLI-only users.

Environment

• GitHub Copilot CLI version: 1.0.68 (and later — same schema observed across recent versions)
• Session files:  ~/.copilot/session-state//events.jsonl 
• OS: Windows (but the event schema itself is OS-independent)

Activity

  1. avs-io commented on Jul 16, 2026

    @avs-io
    Collaborator

    Confirmed on main at 5d366fb: the JSONL parse loop handles only session.start / session.model_change / subagent.selected / user.message / assistant.message — session.shutdown falls through, and for CLI-only sessions no other source supplies input/cache/reasoning tokens, so cost is computed from output tokens alone. Working on a fix: consume the shutdown rollup's modelMetrics as a supplementary per-model yield (output excluded to avoid double-counting the per-turn events), with a regression test built from the redacted schema in your report. Thanks for the detailed sample — it made the cache-inclusive inputTokens subtlety visible.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions