kilocode icon indicating copy to clipboard operation
kilocode copied to clipboard

Auto-compaction fails for local Ollama models (32K context) with "Compaction exhausted" error

Open vycheslav-iv opened this issue 5 months ago • 5 comments

Description

Problem

Auto-compaction does not work reliably for local Ollama models with 32K context window. After several automatic compactions, the session crashes with: Compaction exhausted: context still exceeds model limits after 3 attempts { "name": "ContextOverflowError", "data": { "message": "Compaction exhausted: context still exceeds model limits after 3 attempts" } }

Manual compaction (/compact) works fine.

Steps to reproduce

  1. Configure Kilo Code to use a local Ollama model with 32K context (e.g., qwen3-coder-fixed:latest).
  2. Set up limit.input: 28000, limit.output: 8192, and compaction settings (reserved: 4000, preserve_recent_tokens: 4000).
  3. Start a new session and work on a project (e.g., a Tetris game with ~700 lines of code).
  4. Let the conversation grow near the 32K limit. Auto-compaction triggers automatically.
  5. After 2-3 successful compactions, the error appears and the session stops.

Expected behavior

Auto-compaction should work continuously without crashing, allowing very long sessions (1M+ tokens) as claimed in Kilo Code documentation.

Actual behavior

Auto-compaction works 1-3 times, then fails with the above error.

Root cause analysis (my findings)

  • Kilo Code's built-in system prompt consumes ~15K tokens.
  • Each compaction adds a summary message that accumulates over time.
  • Even with limit.input: 28000, the combined size of system prompt + compaction prompt + accumulated summaries + conversation eventually exceeds the model's context window.
  • Using a 64K model for compaction helps but doesn't fully solve the problem (summaries still accumulate).

Environment

  • OS: Windows 11
  • VS Code: version 1.99.0 (or your version)
  • Kilo Code extension: version 0.19.x (or your version)
  • Ollama: 0.6.2 (or your version)
  • Model: qwen3-coder-fixed:latest (32K context) / qwen3-coder:30b-64k (64K context for compaction)

Configuration

{
  "provider": {
    "ollama-local": {
      "options": { "baseURL": "http://127.0.0.1:11434/v1/" },
      "models": {
        "qwen3-coder-fixed:latest": {
          "limit": { "context": 32768, "input": 28000, "output": 8192 }
        }
      }
    }
  },
  "compaction": {
    "auto": true,
    "threshold_percent": 90,
    "prune": true,
    "tail_turns": 2,
    "preserve_recent_tokens": 4000,
    "reserved": 4000
  }
}


Related GitHub issues
#9774

#9494

#9887 (partial fix)

Additional context
I have attached a detailed investigation report. The issue is reproducible on Windows 11 with multiple local Ollama models.

### Plugins

none

### Kilo version

[email protected]

### Steps to reproduce

_No response_

### Screenshot and/or share link

_No response_

### Operating System

_No response_

### Terminal

_No response_

vycheslav-iv avatar Jun 10 '26 13:06 vycheslav-iv

Has my message been accepted to resolve the issue?

vycheslav-iv avatar Jul 07 '26 11:07 vycheslav-iv

I also raised the same issue #12124, #12196 but it has not been resolved yet.

hi-one-gg avatar Jul 14 '26 05:07 hi-one-gg

I have "old" 5.16.1 JetBrains plugin of KiloCode (for PyCharm) and context compaction works fine. Now I've installed 7.0.9 JetBrains plugin for Android Studio and context compaction here doesn't work. Neither it works for kilocode cli 7.4.13

(as a backend I'm using ollama and owui)

kaomoneus avatar Jul 24 '26 12:07 kaomoneus

Exact same problem on Pi coder. Compaction seems to be additive indeed, making the session unusable after some time.

FlorianCassayre avatar Sep 12 '26 16:09 FlorianCassayre

This is the same root cause as #15068: compactionAttempts was not reset after successful compaction rounds, so long turns on 32K models with large system prompts eventually hit the cap after 2-3 normal compactions.

Fixed in commit 9be7edfe62 on branch fix/compaction-loop-bugs: compactionAttempts resets to 0 after any step that completes without error, so consecutive compactions separated by completed steps do not count toward the MAX_COMPACTION_ATTEMPTS cap.

HenricHuntsman avatar Oct 27 '26 12:10 HenricHuntsman