# Changes Summary: Length and Context Window Auto-Continue ## Overview Implemented functionality to automatically continue the LLM loop in the agent when the previous stop reason was due to length or context window problems. ## Files Modified ### 1. `backend/agentpress/thread_manager.py` **Changes Made:** - Updated auto-continue logic in `auto_continue_wrapper()` function - Added detection for length and context window related finish reasons - Enhanced logging and documentation **Specific Changes:** 1. **Enhanced Finish Reason Detection:** ```python # Before: Only handled 'tool_calls' and 'xml_tool_limit_reached' if chunk.get('finish_reason') == 'tool_calls': # auto-continue logic elif chunk.get('finish_reason') == 'xml_tool_limit_reached': # stop logic # After: Also handles length/context issues finish_reason = chunk.get('finish_reason') if finish_reason == 'tool_calls': # auto-continue logic elif finish_reason == 'xml_tool_limit_reached': # stop logic elif finish_reason in ['length', 'context_length_exceeded', 'max_tokens']: # NEW: auto-continue for length/context issues ``` 2. **Updated Documentation:** - Modified parameter documentation to include new finish reasons - Enhanced logging messages to show supported finish reasons 3. **Improved Logging:** - Added specific logging for length/context auto-continue events - Enhanced parameter logging to show supported finish reasons ### 2. `backend/docs/LENGTH_CONTEXT_AUTO_CONTINUE.md` (New File) **Purpose:** Comprehensive documentation of the new feature **Contents:** - Problem statement and solution overview - Implementation details and code examples - Configuration options and usage examples - Benefits, limitations, and future enhancements - Testing strategies and monitoring information ## New Functionality ### Auto-Continue Triggers The agent now automatically continues the LLM loop when it encounters these finish reasons: 1. **`"length"`** - Response truncated due to token limit 2. **`"context_length_exceeded"`** - Context window exceeded 3. **`"max_tokens"`** - Maximum tokens reached 4. **`"tool_calls"`** - Existing behavior for function/tool calls ### Auto-Continue Exclusions The agent will NOT continue for these finish reasons: 1. **`"stop"`** - Normal completion 2. **`"xml_tool_limit_reached"`** - XML tool limit reached 3. **`"agent_terminated"`** - Agent terminated 4. Any other finish reason ## Configuration The feature is controlled by the existing `native_max_auto_continues` parameter: - **`native_max_auto_continues > 0`**: Enables auto-continue for both tool calls and length/context issues - **`native_max_auto_continues = 0`**: Disables all auto-continue functionality ## Backward Compatibility ✅ **Fully Backward Compatible** - Existing behavior for `"tool_calls"` finish reasons is preserved - Existing behavior for `"xml_tool_limit_reached"` finish reasons is preserved - No changes to existing API interfaces - No changes to existing configuration parameters ## Testing The implementation includes: - Logic verification through test scenarios - Comprehensive documentation with examples - Clear logging for monitoring and debugging ## Benefits 1. **Improved Task Completion**: Agents can complete tasks even when responses are truncated 2. **Better User Experience**: No manual intervention needed when limits are hit 3. **Automatic Recovery**: Seamless recovery from length/context limitations 4. **Maintained Performance**: Still respects maximum iteration limits ## Impact - **Low Risk**: Minimal changes to existing codebase - **High Value**: Significantly improves agent reliability and user experience - **No Breaking Changes**: All existing functionality preserved - **Easy to Disable**: Can be disabled by setting `native_max_auto_continues=0`