Skip to content

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

Closed

Description

@shantur

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

Activity

  1. added
    opentuiThis relates to changes in v1.0, now that opencode uses opentui
    on Nov 17, 2025
  2. github-actions commented on Nov 17, 2025

    @github-actions
    Contributor

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

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

  3. removed
    opentuiThis relates to changes in v1.0, now that opencode uses opentui
    on Nov 18, 2025
  4. self-assigned this
    on Nov 18, 2025
  5. shantur commented on Nov 18, 2025

    @shantur
    ContributorAuthor

    Fixed with #4256

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions