mirror of https://github.com/kortix-ai/suna.git
3.8 KiB
3.8 KiB
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:
-
Enhanced Finish Reason Detection:
# 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
-
Updated Documentation:
- Modified parameter documentation to include new finish reasons
- Enhanced logging messages to show supported finish reasons
-
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:
"length"
- Response truncated due to token limit"context_length_exceeded"
- Context window exceeded"max_tokens"
- Maximum tokens reached"tool_calls"
- Existing behavior for function/tool calls
Auto-Continue Exclusions
The agent will NOT continue for these finish reasons:
"stop"
- Normal completion"xml_tool_limit_reached"
- XML tool limit reached"agent_terminated"
- Agent terminated- 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 issuesnative_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
- Improved Task Completion: Agents can complete tasks even when responses are truncated
- Better User Experience: No manual intervention needed when limits are hit
- Automatic Recovery: Seamless recovery from length/context limitations
- 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