Rename to kilo code in chats#3
Merged
Merged
Conversation
drakonen
approved these changes
Mar 10, 2025
EamonNerbonne
added a commit
that referenced
this pull request
Oct 23, 2025
cleanup continuedev commit: b2d756c
Xyrces
pushed a commit
to Xyrces/kilocode_costOptimization
that referenced
this pull request
Nov 16, 2025
…rectional switching This commit adds intelligent cost optimization to context-based provider switching, enabling users to stay on free/cheap tiers as long as possible through automatic compaction and bidirectional model switching. ## New Features ### 1. Compaction-First Strategy - Automatically attempts conversation compaction before escalating to expensive models - Tracks compaction attempts per model (default: 3 attempts before switching) - Each successful compaction keeps the user on the cheaper tier - Configurable via `preferCompactionOverSwitch` and `maxCompactionAttemptsBeforeSwitch` ### 2. Bidirectional Switching (Downgrades) - Automatically downgrades to cheaper models after successful compaction - Checks if token usage fits in previous (cheaper) model's context window - Triggers downgrade when usage < downgradeThreshold * previousContextWindow (default 50%) - Configurable via `enableContextBasedDowngrade` and `contextDowngradeThreshold` ### 3. Per-Model Compaction Tracking - Maintains separate compaction attempt counters for each model - Resets counter when switching to a different model - Prevents infinite compaction loops with configurable max attempts ## Configuration Schema Updates ### packages/types/src/provider-settings.ts Added 5 new configuration parameters: - `preferCompactionOverSwitch` (boolean, default: true) - `maxCompactionAttemptsBeforeSwitch` (number, default: 3) - `autoCompactOnThreshold` (boolean, default: true) - `contextDowngradeThreshold` (number, 0.0-1.0, default: 0.5) - `enableContextBasedDowngrade` (boolean, default: true) ## Implementation ### src/api/providers/virtual-quota-fallback.ts Added bidirectional switching methods: - `getPreviousProfileInfo()`: Get info about previous (cheaper) profile - `switchToPreviousProfile()`: Downgrade to previous profile in chain ### src/core/task/Task.ts Enhanced context switching logic with: - `getCompactionAttempts(modelId)`: Track compaction attempts per model - `incrementCompactionAttempts(modelId)`: Increment counter - `resetCompactionAttempts(modelId)`: Reset counter on model switch - `attemptAutoCompaction()`: Automatically compact conversation using existing condenseContext() - `checkContextDowngrade()`: Check and perform downgrade after compaction - Modified `checkContextThresholdAndSwitch()`: Implement compaction-first flow ### Flow: 1. Hit context threshold (e.g., 80% of 200k) 2. Try compaction (up to 3 attempts on current model) 3. If compaction succeeds: stay on cheap model, increment counter 4. If compaction limit reached: offer expensive model switch 5. After switch: reset compaction counter for new model 6. After successful compaction: check if downgrade to cheaper model is possible 7. If usage fits in cheaper model: automatic downgrade, reset counter ## Testing ### src/core/task/__tests__/Task.compaction-first.spec.ts (NEW) Comprehensive test coverage (60+ test cases): - Compaction attempt tracking per model - Compaction-first strategy flow - Bidirectional switching (downgrades) - Integration scenarios (full cost optimization flow) - Telemetry and logging - Edge cases (zero context, missing info, errors) ### src/api/providers/__tests__/virtual-quota-fallback-bidirectional.spec.ts (NEW) Bidirectional switching tests (40+ test cases): - getPreviousProfileInfo() functionality - switchToPreviousProfile() functionality - Integration tests (upgrade then downgrade flows) - Cost optimization scenarios - Edge cases ## Documentation ### cli/docs/CONTEXT_BASED_SWITCHING.md Major documentation update: - Updated overview with compaction-first and bidirectional switching explanation - Added cost optimization configuration example - Documented all 5 new configuration parameters - Added detailed "How It Works" section with flow diagram - Added example cost-optimized workflow showing savings - Added "Cost Optimization Best Practices" section - Added "Tuning for Different Use Cases" with 3 configuration presets - Updated benefits to emphasize cost savings - Marked implemented future enhancements ## Cost Impact Example **Without this feature:** - Hit 160k/200k threshold → Switch to paid tier ($$$) - Stay on paid tier for entire conversation **With this feature:** - Hit 160k/200k → Compact Kilo-Org#1 → Stay on free tier ✓ - Hit 160k again → Compact Kilo-Org#2 → Stay on free tier ✓ - Hit 160k again → Compact Kilo-Org#3 → Stay on free tier ✓ - Hit 160k again → Switch to paid tier ($$$) - Compact conversation → Drop to 80k → Automatic downgrade to free tier ✓ - Continue on free tier! **Result:** Massive cost savings, especially for long-running conversations ## Breaking Changes None. All new features are opt-in with sensible defaults: - `preferCompactionOverSwitch`: true (compaction enabled by default) - `enableContextBasedDowngrade`: true (downgrade enabled by default) - Existing behavior preserved when disabled ## User Impact **Positive:** - Automatic cost optimization with zero configuration required - Stays on free/cheap tiers longer - Transparent notifications when compactions/switches occur - Better cost predictability **Neutral:** - Slightly more frequent compactions (but saves money) - Automatic downgrades (can be disabled if not desired) ## Files Modified - packages/types/src/provider-settings.ts (5 new config params) - src/api/providers/virtual-quota-fallback.ts (2 new methods) - src/core/task/Task.ts (5 new methods, enhanced main logic) - cli/docs/CONTEXT_BASED_SWITCHING.md (comprehensive update) ## Files Added - src/core/task/__tests__/Task.compaction-first.spec.ts - src/api/providers/__tests__/virtual-quota-fallback-bidirectional.spec.ts ## Related Issues This implements the user's feature request for cost optimization through: 1. Compaction before switching to expensive models 2. Configurable compaction attempt limits 3. Automatic downgrade after compaction success ## Technical Notes - Compaction attempts are tracked in-memory per Task instance - Counter keys use format: `compaction_attempts_${modelId}` - Downgrade threshold is percentage of *previous* model's context window - All operations preserve full conversation history - Error handling ensures feature never crashes tasks - Abort checks prevent switches during task cancellation
j-Guru
referenced
this pull request
in j-Guru/kilocode
Feb 23, 2026
…-rendering-by-tool-presence Codex-generated pull request
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.