Auto compaction happens much earlier than expected when caching is enabled #2919

Closed
opened 2026-02-16 17:37:48 -05:00 by yindo · 2 comments
Owner

Originally created by @shantur on GitHub (Nov 17, 2025).

Originally assigned to: @rekram1-node on GitHub.

Description

It seems like OpenCode a lot earlier when caching is enabled -
As per
https://github.com/sst/opencode/blob/dev/packages/opencode/src/session/compaction.ts#L35

    const count = input.tokens.input + input.tokens.cache.read + input.tokens.output
    const output = Math.min(input.model.limit.output, SessionPrompt.OUTPUT_TOKEN_MAX) || SessionPrompt.OUTPUT_TOKEN_MAX
    const usable = context - output
    return count > usable

As per this code the total tokens count = input + cache.read + output. As per OpenAI the input tokens is the total size of the prompt and cache.read is the tokens that had cache hit from input. With this logic we are double counting the cache.read tokens and when in long running sessions when the cache is bigger it ends up compaction even though we have a lot of room left.
Below is when I was logging the usage object from OpenAI responses. Opencode ended up compacting even though input tokens were less than 200K ( out of 400K size )

[2025-11-17 16:51:35] [debug] [codex_executor.go:229] codex executor: usage summary provider=codex model=gpt-5.1-codex-medium input_tokens=184704 output_tokens=49 reasoning_tokens=0 cached_tokens=183936 total_tokens=184753
[2025-11-17 16:51:56] [debug] [codex_executor.go:229] codex executor: usage summary provider=codex model=gpt-5.1-codex-medium input_tokens=162951 output_tokens=478 reasoning_tokens=0 cached_tokens=0 total_tokens=163429
[2025-11-17 16:52:02] [debug] [codex_executor.go:229] codex executor: usage summary provider=codex model=gpt-5.1-codex-medium input_tokens=18170 output_tokens=302 reasoning_tokens=256 cached_tokens=17152 total_tokens=18472

Ref - https://platform.openai.com/docs/api-reference/responses/object#responses-object-usage

OpenCode version

1.0.61

Steps to reproduce

  1. Use a provider that supports caching - Using OpenAI Codex in this case via CLIProxyAPI
  2. Have a long running session
  3. Check that session compacts much earlier than expected. The input tokens reported in logs is much less than the context window

Screenshot and/or share link

No response

Operating System

No response

Terminal

No response

Originally created by @shantur on GitHub (Nov 17, 2025). Originally assigned to: @rekram1-node on GitHub. ### Description It seems like OpenCode a lot earlier when caching is enabled - As per https://github.com/sst/opencode/blob/dev/packages/opencode/src/session/compaction.ts#L35 ``` const count = input.tokens.input + input.tokens.cache.read + input.tokens.output const output = Math.min(input.model.limit.output, SessionPrompt.OUTPUT_TOKEN_MAX) || SessionPrompt.OUTPUT_TOKEN_MAX const usable = context - output return count > usable ``` As per this code the total tokens count = input + cache.read + output. As per OpenAI the input tokens is the total size of the prompt and cache.read is the tokens that had cache hit from input. With this logic we are double counting the cache.read tokens and when in long running sessions when the cache is bigger it ends up compaction even though we have a lot of room left. Below is when I was logging the usage object from OpenAI responses. Opencode ended up compacting even though input tokens were less than 200K ( out of 400K size ) ``` [2025-11-17 16:51:35] [debug] [codex_executor.go:229] codex executor: usage summary provider=codex model=gpt-5.1-codex-medium input_tokens=184704 output_tokens=49 reasoning_tokens=0 cached_tokens=183936 total_tokens=184753 [2025-11-17 16:51:56] [debug] [codex_executor.go:229] codex executor: usage summary provider=codex model=gpt-5.1-codex-medium input_tokens=162951 output_tokens=478 reasoning_tokens=0 cached_tokens=0 total_tokens=163429 [2025-11-17 16:52:02] [debug] [codex_executor.go:229] codex executor: usage summary provider=codex model=gpt-5.1-codex-medium input_tokens=18170 output_tokens=302 reasoning_tokens=256 cached_tokens=17152 total_tokens=18472 ``` Ref - https://platform.openai.com/docs/api-reference/responses/object#responses-object-usage ### OpenCode version 1.0.61 ### Steps to reproduce 1. Use a provider that supports caching - Using OpenAI Codex in this case via CLIProxyAPI 2. Have a long running session 3. Check that session compacts much earlier than expected. The input tokens reported in logs is much less than the context window ### Screenshot and/or share link _No response_ ### Operating System _No response_ ### Terminal _No response_
yindo added the bug label 2026-02-16 17:37:48 -05:00
yindo closed this issue 2026-02-16 17:37:48 -05:00
Author
Owner

@github-actions[bot] commented on GitHub (Nov 17, 2025):

This issue might be a duplicate of existing issues. Please check:

  • #3314: Reports "2x token (counts total=total+cached)" and compaction happening at 64k instead of 128k - very similar to the double counting problem described here
  • #4113: User experiencing compaction running "every second message" with similar premature compaction symptoms
  • #3322: Documents token counting inconsistencies between web and TUI, indicating broader token calculation issues

Feel free to ignore if none of these address your specific case.

@github-actions[bot] commented on GitHub (Nov 17, 2025): This issue might be a duplicate of existing issues. Please check: - #3314: Reports "2x token (counts total=total+cached)" and compaction happening at 64k instead of 128k - very similar to the double counting problem described here - #4113: User experiencing compaction running "every second message" with similar premature compaction symptoms - #3322: Documents token counting inconsistencies between web and TUI, indicating broader token calculation issues Feel free to ignore if none of these address your specific case.
Author
Owner

@shantur commented on GitHub (Nov 18, 2025):

Fixed with #4256

@shantur commented on GitHub (Nov 18, 2025): Fixed with #4256
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#2919