Skip to content

perf(memory): one recall budget per turn, shared by every section - #425

Merged
mrnasil merged 1 commit into
mainfrom
perf/recall-shared-budget
Nov 7, 2026
Merged

mrnasil merged 1 commit into
mainfrom
perf/recall-shared-budget

Conversation

@mrnasil

@mrnasil mrnasil commented Nov 7, 2026

Copy link
Copy Markdown
Member

Part of the token-efficiency series (#418–#424).

Problem

recall_token_budget (default 800 tokens; documented as "token budget for recalled facts") is meant to bound each turn's <recalled-context>. In inject_parts, two things inflated it:

  1. The budget was resized around the index. It was raised by min(max_index_bytes, 2 × budget) and then lowered by the MEMORY.md index length. But the recall block does not carry the index; only the system prompt path does. With a small or empty index the budget roughly tripled.
  2. Each section had its own budget. Recalled memories, code context and past sessions each started counting from zero against that raised budget.

As a result one turn's recall could reach about nine times the configured size, roughly 7k tokens instead of 800. With turn context now pinned to its prompt (#419), that text stays in the history, so the bound matters more.

Change

  • One shared budget. The three sections draw from one RecallBudget of exactly recall_token_budget, in priority order: memories, then code, then past sessions. A section that finds the budget spent adds nothing. A section's first item still always goes in, so a section is never a bare header.
  • Index unchanged. The index keeps its own max_index_bytes cap and no longer affects which hits are recalled, so build_inject_block and build_recall_block still recall the same hits.
  • Line clipping. Code and session snippets now cut each line at 200 chars, so a minified line or a pasted log cannot fill the budget on its own.
  • Refactor. The three copies of the fill loop are now RecallBudget::section.
  • Docs. docs/config.md now describes the shared budget.

Checks

  • New tests: recall_sections_share_one_budget and clipped_lines_keeps_snippets_short.
  • whycodes-memory --lib 89 passed. The agent lib passes, and so does the TUI recall test (recall_rides_on_turn_context…).
  • clippy is ok. Local llvm-cov on memory: every changed line is covered.

🤖 Generated with Claude Code

`recall_token_budget` (800 tokens by default) was meant to bound each
turn's recall. In practice:

- the budget was raised by `min(max_index_bytes, 2 x budget)` and lowered
  by the MEMORY.md index length, although the recall block does not carry
  the index; with a small index that tripled it;
- recalled memories, code context and past sessions each started from
  zero against that raised budget.

So one turn's `<recalled-context>` could reach about nine times the
configured size (~7k tokens), and with turn context now kept on its prompt
it stays in the history.

The three sections now draw from one `RecallBudget` of exactly
`recall_token_budget`, in priority order; a section that finds it spent
adds nothing. The index keeps its own `max_index_bytes` cap. Code and
session snippets cut each line at 200 chars, so a minified line or pasted
log cannot fill the budget alone. The three copies of the fill loop became
`RecallBudget::section`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@mrnasil
mrnasil force-pushed the perf/recall-shared-budget branch from 39f425d to cab2f6d Compare November 7, 2026 14:08
@mrnasil
mrnasil merged commit 8636457 into main Nov 7, 2026
7 checks passed
@mrnasil
mrnasil deleted the perf/recall-shared-budget branch November 7, 2026 14:20
@mrnasil mrnasil mentioned this pull request Nov 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant