Repository navigation
Description
Describe the bug
I'm using a bring-your-own-key reasoning model (chat completions, thinking on) through the Copilot CLI agent host in VS Code. During tool-heavy work my sessions compact every few minutes: 19 compactions in 107 minutes. Each one fires when the runtime thinks the context is about 80% full, but by then the model server has only seen 30 to 40% of the window. So I keep losing context while there's plenty of room left.
From what I can tell, the local context estimate counts each earlier assistant turn at its full output token count, and for a thinking model nearly all of that is reasoning. Most reasoning-model servers drop old reasoning when they build the prompt (Qwen3- and DeepSeek-R1-style chat templates do this), so it never takes up space in the real prompt. The estimate also doesn't seem to use the provider's reported inputTokens, so nothing pulls it back down.
The model has a real window of 262,144 tokens, and I set maxPromptTokens to 223,232. In my sessions:
session.compaction_startfires withtrigger: "threshold"andcurrentTokensbetween about 180k and 205k.session.compaction_completesaystokenAccountingBasis: "local_full_context_estimate".- For the request right before each compaction, the provider reports about 65k to 110k input tokens. Across 513 compactions, the estimate is a median 2.2 times the provider's number.
- The provider's number is the right one. I sent an over-limit request on purpose, and the server's "request resolved to N input tokens" matched a linear fit of the reported usage.
- Usage does reach the runtime. 105,258 of 105,265
session.usage_recordevents haveusage.inputTokens, and 105,247 of those are above 0. My gateway includes cached tokens ininputTokens(only 481 records havecacheReadTokensabove 0), so the provider totals aren't understated. - Between two compactions, the estimate grows by about 100k tokens while the provider's prompt grows by 10k to 50k.
- I regressed that growth on content type over 23 cycles. Reasoning text adds about 0.21 tokens per character to the estimate and roughly nothing to the provider's count. Tool results add about 0.16 to the estimate and 0.09 to the provider's count.
Affected version
The runtime that ships with VS Code 1.148 (Copilot CLI agent host). I also reproduced it outside VS Code with runtime 1.0.117 from @microsoft/copilot-sdk 2.0.4.
Steps to reproduce the behavior
I wrote a small OpenAI-compatible mock server and an SDK script for this, and I'm happy to share them.
- Point a BYOK model at the mock with
maxPromptTokens: 64000, which puts the threshold at 51,200. - Have every model step return about 12,000 characters of reasoning and one tool call, with a 2,000-character tool result. Report the reasoning as about 3,020 output tokens, as a thinking model would. Report the prompt size as chars/4 of the request without the old reasoning, which is what a server with one of those templates actually tokenizes.
- Run 30 steps and watch
tool.execution_complete.toolTelemetry.metrics.context_current_tokens, which carries the runtime's running estimate.
What happens:
- Each step adds 3,283 to the estimate and about 570 to the provider's count. Compaction fires at an estimate of 52.6k while the provider reports 21.3k, a ratio of 2.46. Chat completions and the Responses API behave the same way.
- If the mock reports 50 output tokens instead, each step adds 313 and nothing compacts. The difference of 2,970 per step is exactly the extra output tokens.
- If the mock reports
input_tokens: 0instead of the real number, the estimate hardly moves (13,260, 13,573, 13,886 against 13,263, 13,576, 13,889). That's why I think the estimate doesn't use the provider's input count at all. - The runtime does resend the old reasoning (
reasoning_contenton chat completions,reasoningitems on Responses). It's the server that leaves it out.
Expected behavior
Compaction should start when the real prompt gets close to the limit. Some ideas for getting there:
- After each response, take the provider's prompt count (
prompt_tokensorinput_tokens, plus cached tokens when the provider reports them separately) as the base, and only estimate locally what was added since. - When there's no provider count, don't charge old reasoning at its full output token count. At most, count what the request actually contains.
- Say which basis triggered the compaction in
tokenAccountingBasis, and put both numbers oncompaction_start, so a mismatch like this shows up in the logs.
Additional context
This looks related to #5517, where a pasted PDF was counted at its encoded size (620k estimated against 31k real). In both cases the local estimate drifts far from the provider's count and nothing corrects it.
I also looked at the VS Code side. The agent host's BYOK bridge passes provider usage through to the runtime, so the estimate and the decision to compact seem to live entirely in the runtime.
The repro ran on Windows 11, x64.
Activity