Skip to content

Feat/conversation compaction#7558

Open
yeyan00 wants to merge 3 commits intoagno-agi:mainfrom
yeyan00:feat/conversation-compaction
Open

Feat/conversation compaction#7558
yeyan00 wants to merge 3 commits intoagno-agi:mainfrom
yeyan00:feat/conversation-compaction

Conversation

@yeyan00
Copy link
Copy Markdown

@yeyan00 yeyan00 commented Apr 17, 2026

Summary

Add conversation compaction support for both Agent and Team. When conversation history grows large (exceeding a
configurable token threshold), the system automatically compacts older messages into an LLM-generated summary, reducing
context window usage while preserving conversation continuity.

Behavior:

  • enable_compaction=False (default): No changes to existing Agent/Team logic — fully backward compatible
  • enable_compaction=True: Compaction logic is activated, history is compacted when exceeding threshold

Key changes:

  • New compaction module (libs/agno/agno/compaction/) with a CompactionManager that handles threshold detection,
    summary generation via LLM, and compaction boundaries
  • Agent compaction: triggers after each run when history exceeds the threshold, stores compacted_run_ids in the run
    record
  • Team compaction: triggers at the team level only (member agents are not affected), correctly scoped so member runs
    are not compacted independently
  • Compacted summaries are loaded as priority history in subsequent runs, replacing older messages

Resolves #7557

Type of change

  • New feature

Checklist

  • Code complies with style guidelines
  • Ran format/validation scripts (./scripts/format.sh and ./scripts/validate.sh)
  • Self-review completed
  • Documentation updated (comments, docstrings)
  • Examples and guides: Relevant cookbook examples have been included or updated (if applicable)
  • Tested in clean environment
  • Tests added/updated (if applicable)

Duplicate and AI-Generated PR Check

  • I have searched existing open pull requests and confirmed that no other PR already addresses this
    issue
  • If a similar PR exists, I have explained below why this PR is a better approach
  • Check if this PR was entirely AI-generated (by Copilot, Claude Code, Cursor, etc.)

Test Results

All 10 integration tests passed:

Test Result
test_compaction_triggers_on_large_history ✅ PASSED
test_compaction_async ✅ PASSED
test_compaction_default_off (agent) ✅ PASSED
test_team_compaction_triggers ✅ PASSED
test_team_compaction_persists_in_session ✅ PASSED
test_team_compaction_not_on_member_runs ✅ PASSED
test_team_compaction_stats ✅ PASSED
test_team_compaction_async ✅ PASSED
test_team_compaction_default_off ✅ PASSED
test_team_no_compaction_when_disabled ✅ PASSED

Files Changed

New files:

  • libs/agno/agno/compaction/__init__.py
  • libs/agno/agno/compaction/manager.py
  • cookbook/02_agents/03_context_management/conversation_compaction.py
  • cookbook/03_teams/09_context_management/team_conversation_compaction.py
  • libs/agno/tests/integration/agent/test_compaction.py
  • libs/agno/tests/integration/teams/test_compaction.py

Modified files:

  • libs/agno/agno/models/base.py — default context_window: Optional[int] = 128000
  • libs/agno/agno/agent/agent.pyenable_compaction, compaction_manager params
  • libs/agno/agno/agent/_init.py — compaction manager initialization
  • libs/agno/agno/agent/_messages.py — compaction priority history loading
  • libs/agno/agno/agent/_run.py — 4 compaction trigger points
  • libs/agno/agno/run/agent.pycompacted_run_ids field
  • libs/agno/agno/session/agent.py — compaction boundary detection
  • libs/agno/agno/team/team.pyenable_compaction, compaction_manager params
  • libs/agno/agno/team/_init.py_set_compaction_manager()
  • libs/agno/agno/team/_messages.py — compaction priority history loading
  • libs/agno/agno/team/_run.py — 6 compaction trigger points
  • libs/agno/agno/run/team.pycompacted_run_ids field
  • libs/agno/agno/session/team.py — compaction boundary detection

mzm\mzm added 3 commits April 17, 2026 15:37
- Fix transitive compaction chain detection: _find_compacted_run_ids() and
  session get_messages() now expand the full chain of compacted runs,
  preventing incorrectly loading runs that were indirectly compacted
- Add missing ashould_compact() async method to CompactionManager
- Fix 4 f-string violations per .cursorrules (no f-string without variables)
@yeyan00 yeyan00 requested a review from a team as a code owner April 17, 2026 08:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request] Support Context-Length-Based Auto Compaction Mechanism

1 participant