Skip to content

Add automatic context compaction and retry on context errors #605

Description

@szmania

Feature Request

Add the ability to automatically run context compaction when a context error is returned on a tool call, and then retry the tool call. This should be controlled by a configuration option:

enable-context-compaction: true

Problem

Currently, when a tool call fails due to a context error (e.g., token limit exceeded), the user must manually compact the context and retry the tool call. This is cumbersome and disrupts workflows.

Example Error

Your estimated chat context of 267,173 tokens exceeds the 230,000 token limit for openai/agentic!
Response parsing error. Model openai/agentic has hit a token limit!
Input tokens: ~267,173 of 230,000 -- possibly exhausted context window!
Output tokens: ~0 of 128,000
Total tokens: ~267,173 of 230,000 -- possibly exhausted context window!

Proposed Solution

  1. Detect Context Errors: Identify when a tool call fails due to context-related errors (e.g., token limit exceeded).
  2. Automatic Compaction: If enable-context-compaction: true is set, automatically trigger context compaction.
  3. Retry Tool Call: After compaction, retry the failed tool call.
  4. Response Chunking and Summarization: When responses are too large to fit in the context window, implement logic to split the response into chunks and progressively summarize chunks as they fit in the context window. This ensures that important information is retained while reducing context size.
  5. Configuration: Add a new configuration option enable-context-compaction to control this behavior.

Expected Behavior

If enable-context-compaction: true, the system should:

  1. Detect the context error.
  2. Run context compaction automatically.
  3. Retry the tool call with the compacted context.
  4. For large responses, split into chunks and progressively summarize them to fit within context limits.

Acceptance Criteria

  • Detect context errors (e.g., token limit exceeded) during tool calls.
  • Add enable-context-compaction configuration option.
  • Automatically compact context and retry the tool call if the option is enabled.
  • Implement response chunking and progressive summarization for large responses.
  • Ensure the feature is backward-compatible (disabled by default).
  • Add tests for the new functionality.
  • Update documentation to describe the new feature.

Jira Task

This issue is aligned with Jira task CLI-56.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions