Skip to content

feat(compaction): reconcile token estimator with provider-reported usage #3437

Description

@aheritier

Summary

Improve token-estimate accuracy so the /context view and compaction triggers agree with reality. The current estimator is a len/4 chars-per-token heuristic (+5 overhead) in compaction.EstimateMessageTokens.

Proposed behavior

  • Reconcile the heuristic against provider-reported usage per message (already available via MessageUsage, pkg/runtime/event.go ~line 372).
  • Keep a conservative bias so we compact slightly early rather than overflow.

Implementation notes (starting points)

  • pkg/compaction/compaction.go
  • pkg/runtime/compactor/compactor.go
  • Extend estimator tests

Complexity: S–M.

Background: shared plan context-management-gap-analysis (Phase 2, item 6).

Dependencies / Relationships

Metadata

Metadata

Assignees

Labels

area/runtimeRuntime engine, agent loop execution, tool dispatch, loop detectionkind/featPR adds a new feature (maps to feat:). Use on PRs only.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions