Skip to content

Feat: Add Context Level Selection and Configurable Token Limits #342

Closed

Description

@integry

Description
We need to restore the ability for users to select the context size used for planning (Minimal, Balanced, Max).
Currently, token limits are hardcoded constants (e.g., 100k or 150k), but models have varying capacities (200k for standard Anthropic, up to 1M for some tiers).

Requirements

  1. Model Limits Configuration: Create a central configuration to define context limits for different model IDs.
  2. Context Level Option: Add a 'contextLevel' field to the 'TaskDraftConfig'.
    • 'minimal': Uses a small portion of the context (e.g., 10%).
    • 'balanced': Uses a moderate portion (e.g., 30-40% or fixed ~50k).
    • 'max': Uses the full model capacity minus a safety buffer (e.g., 95% of limit).
  3. Dynamic Limit Calculation: Update 'taskPlanningService.ts' to calculate the 'tokenLimit' passed to 'generateContext' based on the selected level and the specific model's limit.
  4. Backend Integration: Ensure 'generatePlan' and 'generateContextPreview' use these dynamic values instead of hardcoded constants.

Note: Do not confuse this with 'Granularity' (which controls how tasks are split). This controls how much code is read into the prompt.

Activity

  1. integry commented on Dec 10, 2025

    @integry
    OwnerAuthor

    Here is the suggested implementation plan.

    First, create a new config file for model limits:

    packages/core/src/config/modelLimits.ts

    export type ContextLevel = 'minimal' | 'balanced' | 'max';
    
    export const MODEL_LIMITS: Record<string, number> = {
      'default': 200000,
      // Map specific model IDs to their context window
      'claude-3-5-sonnet-20240620': 200000,
      'claude-3-opus-20240229': 200000,
      // Add other models as needed
    };
    
    export const CONTEXT_RATIOS: Record<ContextLevel, number> = {
      minimal: 0.1,    // 10% of context
      balanced: 0.50,  // 50% of context
      max: 0.95        // 95% (leaving 5% breathing room)
    };
    
    export function getEffectiveTokenLimit(modelId: string | undefined, level: ContextLevel): number {
      const limit = MODEL_LIMITS[modelId || 'default'] || MODEL_LIMITS['default'];
      return Math.floor(limit * CONTEXT_RATIOS[level]);
    }

    Then, update the planning service to use these configurations:

    packages/core/src/services/taskPlanningService.ts

    import { getEffectiveTokenLimit, ContextLevel } from '../config/modelLimits.js';
    // ... imports
    
    // Update interface
    export interface TaskDraftConfig {
      baseBranch: string;
      granularity: 'single' | 'balanced' | 'granular';
      contextLevel: ContextLevel; // New field
      manualFiles: string[];
      autoFiles: string[];
    }
    
    // In generatePlan function
    export async function generatePlan(options: GeneratePlanOptions): Promise<Plan> {
      // ... existing setup
      
      const contextConfig = draft.context_config as TaskDraftConfig | null;
      const contextLevel = contextConfig?.contextLevel || 'balanced';
      
      // TODO: Get actual model ID from config or draft if available, currently defaulting
      const tokenLimit = getEffectiveTokenLimit('claude-3-5-sonnet-20240620', contextLevel);
    
      // ... checkout branch logic
    
      const contextResult = await generateContext({
        repoPath: worktreePath, 
        filesToInclude: relevantFilePaths.length > 0 ? relevantFilePaths : undefined, 
        tokenLimit, // Use dynamic limit
        correlationId 
      });
      
      // ... rest of function
    }
    
    // Update generateContextPreview similarly
    export async function generateContextPreview(options: GenerateContextPreviewOptions): Promise<PreviewResult> {
      // ... setup
      
      // Extract contextLevel if it is passed in options (you might need to update GenerateContextPreviewOptions interface too)
      const contextLevel = 'balanced'; // Default or from options
      const tokenLimit = getEffectiveTokenLimit('claude-3-5-sonnet-20240620', contextLevel);
    
      // ... 
      
      const contextResult = await generateContext({ 
        repoPath: worktreePath, 
        filesToInclude: combinedFiles.length > 0 ? combinedFiles : undefined, 
        tokenLimit, 
        correlationId 
      });
      
      // ... rest of function
    }
  2. propr-dev commented on Dec 10, 2025

    @propr-dev
    Contributor

    🤖 AI processing has started for this issue using claude-opus-4-5 model.

    I'll analyze the problem and work on a solution. This may take a few minutes.

    Processing Details:

    • Model: claude-opus-4-5
    • Branch: ai-fix/342-feat-add-context-level-se-20251210-1919-claude-opus-4-5-e0u
    • Base Branch: main
    • Worktree: issue-342-20251210-1919-claude-opus-4-5-e0u
  3. added a commit that references this issue on Dec 10, 2025
  4. added a commit that references this issue on Dec 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions