fix(copilot): stop billing cached tokens twice on OTel spans (#1830)
Some checks failed
CI / semgrep (push) Waiting to run
Tests / test-platforms (macos-latest) (push) Waiting to run
Tests / test (22) (push) Waiting to run
Tests / test (22.13.0) (push) Waiting to run
Tests / test (24) (push) Waiting to run
Tests / test (26) (push) Waiting to run
Tests / test-platforms (windows-latest) (push) Waiting to run
Tests / desktop (push) Waiting to run
Tests / vscode (push) Waiting to run
macOS Menubar CI / test (push) Has been cancelled
Windows Menubar CI / check (ubuntu-latest) (push) Has been cancelled
Windows Menubar CI / check (windows-latest) (push) Has been cancelled

* fix(copilot): stop billing cached tokens twice on OTel spans

VS Code Copilot Chat writes gen_ai.usage.input_tokens on agent-traces.db
spans as the full prompt, cache reads and writes included. The OTel parser
passed it to calculateCost as uncached input, so every cached token was
billed at the input rate on top of its cache rate. The parser now subtracts
the cache counts the way the chatSessions path does, and keeps an input
below them as written.

Bumps the copilot parse version so cached OTel calls re-parse. Calls whose
spans the extension already pruned keep the old input.

* test(upgrade-path): expect the copilot OTel cache subtraction across the bump

The parse-version bump re-parses the surviving agent-traces.db spans with
the cache counts taken out of input, so input and cost drop by design while
the pruned calls keep the 0.9.20 figure. Calls, output and cache reads stay
exact; input must drop by exactly the cache counts of the surviving
cache-inclusive spans, and cost must drop.
This commit is contained in:
Resham Joshi 2026-11-02 20:26:36 +01:00 • committed by GitHub
parent f043008f50
commit f846b63097
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 70 additions and 6 deletions

View file

@ -5,6 +5,7 @@
### Fixed
- **Copilot agent requests in VS Code chat count every model call.** Copilot Chat 0.69 keeps only the last call of an agent request in its metadata, so CodeBurn showed a fraction of the output and cost. It now reads the request's totals (all calls, with the cache split when VS Code records it), and a request that records GitHub's credits costs exactly that bill (1 credit = $0.01) in reports and plan credits. A chat session that also ran on the agent host is counted once, from the agent host. In Copilot plan credits each chat request is rated on its own. Past Copilot days re-derive once after the update.
- Copilot showed `claude-sonnet-4-6` as "Sonnet 4" instead of "Sonnet 4.6".
- **Copilot OTel spans billed cached tokens twice.** VS Code Copilot Chat writes a span's input tokens as the full prompt, cache included, and CodeBurn priced that input and the cache again on top. The cache is now subtracted before pricing. Only installs with `github.copilot.chat.otel.enabled` are affected; cached calls re-parse once after the update, and calls whose spans the extension already pruned keep their old figure.
## 0.9.27 - 2026-10-11

View file

@ -496,6 +496,14 @@ else {
// double direction is asserted but not demonstrated: a faithful re-keying
// simulation needs the parser to mint genuinely different keys, and a cheap
// stand-in is collapsed by the serve-time dedup before it can be counted.)
//
// 2 Nov 2026, #1830: OTel spans billed cached tokens twice. input_tokens on a
// span is cache-inclusive, and the bump re-parses the surviving spans with the
// cache counts subtracted, so their input and cost drop by design while the
// pruned calls keep the 0.9.20 figure. Calls, output and cache reads stay
// exact; input must drop by exactly the cache counts of the surviving spans
// that were cache-inclusive, and cost must drop. Drop this once a published
// CLI carries the fix.
step('durable history across a bump on an extant, pruned source')
const prunedCache = join(CACHES, 'pruned')
@ -540,6 +548,7 @@ if (beforeRecords.calls === 0) {
// populated: the surviving rows are what the bump re-parses, the pruned ones
// exist only in the cache from here on.
let pruned = 0
let reparsedCache = 0
try {
const { DatabaseSync } = await import('node:sqlite')
const db = new DatabaseSync(copilotDb)
@ -549,6 +558,12 @@ if (beforeRecords.calls === 0) {
const delAttr = db.prepare('DELETE FROM span_attributes WHERE span_id = ?')
for (const id of doomed) { delAttr.run(id); delSpan.run(id) }
pruned = doomed.length
const attrsOf = db.prepare('SELECT key, value FROM span_attributes WHERE span_id = ?')
for (const id of ids.filter((_, i) => i % 2 === 1)) {
const a = Object.fromEntries(attrsOf.all(id).map(r => [r.key, Number(r.value)]))
const cached = (a['gen_ai.usage.cache_read.input_tokens'] ?? 0) + (a['gen_ai.usage.cache_creation.input_tokens'] ?? 0)
if ((a['gen_ai.usage.input_tokens'] ?? 0) >= cached) reparsedCache += cached
}
const left = db.prepare('SELECT COUNT(*) AS n FROM spans').get().n
db.close()
ok(`pruned ${pruned} of ${ids.length} spans; ${left} remain in a DB that still exists`)
@ -567,14 +582,18 @@ if (beforeRecords.calls === 0) {
const verdict = afterRecords.calls < beforeRecords.calls ? ' — LOST history'
: afterRecords.calls > beforeRecords.calls ? ' — DOUBLED'
: ''
check(sameRecords(beforeRecords, afterRecords),
const expectedRecords = { ...beforeRecords, inputTokens: beforeRecords.inputTokens - reparsedCache }
const repriced = r => sameRecords({ ...r, cost: 0 }, { ...expectedRecords, cost: 0 })
&& (reparsedCache > 0 ? r.cost < beforeRecords.cost : sameRecords(r, beforeRecords))
console.log(` note copilot input ${beforeRecords.inputTokens} -> ${expectedRecords.inputTokens} expected (#1830, cached tokens were billed twice on OTel spans; tokens and cost now exclude the cache counts)`)
check(repriced(afterRecords),
`copilot across the bump: ${showRecords(beforeRecords)} -> ${showRecords(afterRecords)}${verdict}`)
// And persisted, not merely served: the second run reads the cache the
// bump rewrote, so a carry-forward that only survived in memory fails.
capture(newBin, prunedCache, join(PAYLOADS, 'pruned-warm'))
const warmRecords = copilotRecords(join(PAYLOADS, 'pruned-warm'))
check(sameRecords(beforeRecords, warmRecords),
check(sameRecords(afterRecords, warmRecords) && repriced(warmRecords),
`and again from the rewritten cache: ${showRecords(warmRecords)}`)
}
}

View file

@ -2739,10 +2739,15 @@ function createOtelParser(
const subagentTypes = subagentsByTrace.get(spanMetadata.trace_id)
const timestamp = epochToISO(spanMetadata.start_time_ms)
// input_tokens is cache-INCLUSIVE. An input below the cached count
// can only come from an emitter that excludes it, so it stays as is.
const cached = cacheReadTokens + cacheCreationTokens
const uncached = inputTokens >= cached ? inputTokens - cached : inputTokens
// calculateCost with FULL token data — this is the key improvement.
const costUSD = calculateCost(
model,
inputTokens,
uncached,
outputTokens,
cacheCreationTokens,
cacheReadTokens,
@ -2754,7 +2759,7 @@ function createOtelParser(
sessionId: conversationId,
project,
model,
inputTokens,
inputTokens: uncached,
outputTokens,
cacheCreationInputTokens: cacheCreationTokens,
cacheReadInputTokens: cacheReadTokens,

View file

@ -501,7 +501,10 @@ export const PROVIDER_PARSE_VERSIONS: Record<string, string> = {
// anthropic-auto fallback; cached entries must re-parse once.
// chatsession-agent-totals-v1: chat-session requests read every agent round
// (modelTotals, root completionTokens) and copilotCredits as nanoAiu.
copilot: 'cli-shutdown-cost-v1-skills-source-provenance-v1-session-store-v3-chatsession-otel-skills-v1-otel-trace-metadata-once-v1-transcript-unknown-usage-v1-otel-workspace-project-v1-journal-request-input-v1-tokenless-turns-v1-store-row-output-v1-jetbrains-opus-4-7-v1-chatsession-agent-totals-v1',
// otel-cache-inclusive-input-v1: cached OTel calls hold inputTokens with
// cache reads and writes still inside it, so they must re-parse. Calls whose
// spans agent-traces.db already pruned keep the old input.
copilot: 'cli-shutdown-cost-v1-skills-source-provenance-v1-session-store-v3-chatsession-otel-skills-v1-otel-trace-metadata-once-v1-transcript-unknown-usage-v1-otel-workspace-project-v1-journal-request-input-v1-tokenless-turns-v1-store-row-output-v1-jetbrains-opus-4-7-v1-chatsession-agent-totals-v1-otel-cache-inclusive-input-v1',
// authoritative-usage-v4: persist one Grok session call from top-level
// authoritative totals, use modelUsage only for priced attribution, clamp
// reasoning per record, and label mixed sessions estimated.

View file

@ -1715,7 +1715,7 @@ describe('copilot provider - OTel cache token parsing', () => {
attrs: {
'gen_ai.conversation.id': 'conv-001',
'gen_ai.response.model': 'gpt-4.1',
'gen_ai.usage.input_tokens': 1000,
'gen_ai.usage.input_tokens': 51500,
'gen_ai.usage.output_tokens': 200,
'gen_ai.usage.cache_read.input_tokens': 50000,
'gen_ai.usage.cache_creation.input_tokens': 500,
@ -1742,6 +1742,42 @@ describe('copilot provider - OTel cache token parsing', () => {
expect(call.cacheReadInputTokens).toBe(50000)
expect(call.cacheCreationInputTokens).toBe(500)
expect(call.costUSD).toBeGreaterThan(0)
expect(call.costUSD).toBeCloseTo(calculateCost('gpt-4.1', 1000, 200, 500, 50000, 0), 12)
})
it('keeps an input below the cached count as written', async () => {
if (!isSqliteAvailable()) return
createOtelDb(dbPath)
insertSpan(dbPath, {
spanId: 'span-excl',
traceId: 'trace-excl',
operationName: 'chat',
startTimeMs: 1000,
attrs: {
'gen_ai.conversation.id': 'conv-excl',
'gen_ai.response.model': 'gpt-4.1',
'gen_ai.usage.input_tokens': 1000,
'gen_ai.usage.output_tokens': 200,
'gen_ai.usage.cache_read.input_tokens': 50000,
'gen_ai.usage.cache_creation.input_tokens': 500,
},
})
const provider = createCopilotProvider('/nonexistent/jsonl', '/nonexistent/ws')
const src = (await provider.discoverSessions()).find(s => s.path === dbPath)
expect(src).toBeDefined()
const calls: ParsedProviderCall[] = []
for await (const call of provider.createSessionParser(src!, new Set()).parse()) {
calls.push(call)
}
expect(calls).toHaveLength(1)
expect(calls[0]!.inputTokens).toBe(1000)
expect(calls[0]!.cacheReadInputTokens).toBe(50000)
expect(calls[0]!.cacheCreationInputTokens).toBe(500)
expect(calls[0]!.costUSD).toBeCloseTo(calculateCost('gpt-4.1', 1000, 200, 500, 50000, 0), 12)
})
it('discovers one source per OTel DB file (not per conversation)', async () => {