#21242: fix(memory): add token budget limits for memory tools (#21187)
agents
size: S
Cluster:
Memory Management Enhancements
Fixes openclaw/openclaw#21187.
Adds token-budget clamping for memory tool outputs to prevent context overflow:
- memory.limits.maxSearchInjectedTokens (default 800)
- memory.limits.maxGetInjectedTokens (default 1600)
AI-assisted: yes.
Testing:
- pnpm vitest run --config vitest.e2e.config.ts src/agents/tools/memory-tool.e2e.test.ts
- pnpm check
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
Adds token budget limits (`memory.limits.maxSearchInjectedTokens` and `memory.limits.maxGetInjectedTokens`) to prevent memory tool outputs from overflowing context windows. The implementation includes:
- New `MemoryLimitsConfig` type with two configurable token budgets (defaults: 800 for search, 1600 for get)
- `clampTextByInjectedTokens()` function that estimates tokens and truncates text with a safety margin
- `clampResultsByInjectedTokens()` function that processes search results within the budget
- Comprehensive test coverage for both search and get clamping behavior
- Proper integration with existing QMD character-based clamping
The token estimation uses a mocked `estimateTokens()` function in tests with a simple character-to-token ratio (1:10), ensuring deterministic test behavior. The implementation correctly handles edge cases like zero/negative budgets and applies a 1.2x safety margin to prevent overshoot.
<h3>Confidence Score: 4/5</h3>
- This PR is safe to merge with minor documentation improvements recommended
- The implementation is well-tested with comprehensive e2e tests covering edge cases, follows existing patterns, and includes proper type definitions. The token budget logic is sound with appropriate safety margins. Only minor documentation gaps in schema help/labels files prevent a perfect score.
- Consider updating `src/config/schema.help.ts` and `src/config/schema.labels.ts` to document the new config options
<sub>Last reviewed commit: ff3e400</sub>
<!-- greptile_other_comments_section -->
<sub>(2/5) Greptile learns from your feedback when you react with thumbs up/down!</sub>
<!-- /greptile_comment -->
Most Similar PRs
#21217: fix: memory prune command to prevent unbounded MEMORY.md growth
by theognis1002 · 2026-02-19
75.7%
#4999: fix(memory-flush): use contextTokens instead of totalTokens for thr...
by Farfadium · 2026-01-30
75.3%
#17345: feat: Memory kernel rebuild with token budgeting, summary sidecar, ...
by markmusson · 2026-02-15
75.1%
#12425: feat(core): Native Token Optimization Defaults (Pruning, Compaction...
by oneles · 2026-02-09
75.0%
#12254: fix: add minimum token guard to prevent double compaction after flush
by Yida-Dev · 2026-02-09
74.6%
#5808: fix(memory): truncate oversized chunks before embedding
by douvy · 2026-02-01
74.0%
#9012: fix(memory): resilient flush for large sessions [AI-assisted]
by cheenu1092-oss · 2026-02-04
73.8%
#11364: fix(memory/qmd): prevent cascading failure when query fails or retu...
by blazerui · 2026-02-07
73.5%
#17041: fix(memory-flush): add softThresholdPercent for context-relative th...
by Limitless2023 · 2026-02-15
73.3%
#15307: fix(memory): handle mixed/no-results QMD query output
by MohammadErfan-Jabbari · 2026-02-13
73.1%