Skip to content

Add automatic context compaction and retry on context errors #603

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. 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.

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.
  • 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