← Back to PRs

#21242: fix(memory): add token budget limits for memory tools (#21187)

by Asm3r96 open 2026-02-19 20:30 View on GitHub →
agents size: S
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