basic-memory icon indicating copy to clipboard operation
basic-memory copied to clipboard

Progressive note loading — compact index with on-demand full content

Open bm-clawd opened this issue 8 months ago • 10 comments

Idea

Inspired by OpenViking's L0/L1/L2 progressive loading and Hermes Agent's "hot memory vs cold recall" architecture.

Problem

When agents work with BM notes, they currently load full note content. For large knowledge graphs, this means either:

  • Stuffing too many tokens into context (expensive, slow)
  • Loading too few notes and missing relevant context

Proposal

Implement tiered note loading through the MCP tools:

L0 — Index (always cheap to inject)

  • Title, type, tags, status
  • Relation summary (connected entity names)
  • ~50-100 tokens per note

L1 — Observations (medium cost)

  • All observations with categories
  • Key relations with types
  • ~200-500 tokens per note

L2 — Full content (on demand)

  • Complete markdown body
  • All relations with context
  • Attachments/references

The search_notes and build_context tools could return L0 by default, with a depth or detail parameter to request L1/L2. This keeps agent prompts compact while making the full graph accessible on demand.

Benefits

  • Dramatically fewer tokens for knowledge graph navigation
  • Agents can scan more notes before deciding which to read fully
  • Better prompt cache stability (compact index stays stable)
  • Aligns with how humans browse — scan titles, then dive in

References

  • OpenViking L0/L1/L2: github.com/volcengine/OpenViking
  • Hermes Agent memory layers (hot prompt memory + cold FTS recall)
  • Relates to Momentum UI progressive rendering

bm-clawd avatar Mar 21 '26 14:03 bm-clawd

Claude finished @bm-clawd's task in 1m 47s —— View job


Issue Triage Complete

  • [x] Read issue and understand request
  • [x] Explore relevant codebase sections
  • [x] Check for duplicate issues
  • [x] Apply labels and complete triage

Classification

Field Assessment
Type Feature Request
Priority Medium
Component MCP Tools (search_notes, build_context) + Schemas
Complexity Complex

Labels applied: enhancement


Analysis

No duplicates found. Issue #689 (memory budget indicators) is related but different.

Current state of relevant tools:

  • search_notes — returns full search results with matched_chunk excerpts and complete entity data. No detail parameter exists. Already returns somewhat compact data (matched chunks, not full content).
  • build_context — already has a depth parameter (graph traversal hops, not content detail level) and timeframe. Returns hierarchical context with observations, relations, and related entities — always full content.

What the proposal asks for: A new detail (or similar) parameter on search_notes and build_context that controls how much of each note's content is returned:

  • L0: Title, tags, type, relation summary (~50-100 tokens)
  • L1: Observations + key relations (~200-500 tokens)
  • L2: Full content (current behavior)

Implementation Considerations

This is a complex feature requiring changes across multiple layers:

  1. New schema types — EntitySummaryL0, EntitySummaryL1 response variants, or a unified schema with optional fields controlled by detail level
  2. MCP tool parameters — add detail: Literal["L0", "L1", "L2"] = "L2" (or similar) to search_notes and build_context
  3. Service/repository layer — queries need to optionally suppress content fields or the formatting needs to truncate/omit based on level
  4. Prompt/text formatting — the output_format="text" serializers need to respect the detail level
  5. Tests — unit + integration tests for each detail level
  6. Note: build_context already has a depth parameter (hops), so the new parameter must be named distinctly (e.g., detail or content_level) to avoid confusion

Key design question: Should L0/L1/L2 be an enum or should it be a boolean flag like compact=True? The L0/L1/L2 naming is clear for agents but could be confusing. An alternative would be content_detail: Literal["index", "summary", "full"].


Recommended Next Steps

  1. Design the schema changes and parameter naming
  2. Prototype L0 output for search_notes (easiest entry point — already returns partial data)
  3. Add content_detail parameter to build_context (careful not to conflict with existing depth)
  4. Update text formatters accordingly
  5. Add tests

claude[bot] avatar Mar 21 '26 14:03 claude[bot]

I'm hitting MCP response size limits at 57 pages. search_notes with broad queries returns 300K-1.3M characters, exceeding the max allowed tokens. build_context across all pages for graph-wide analysis costs ~50K tokens and won't scale past ~200 pages. A compact response mode that returns only titles and relation targets (without full page content) would make graph-wide operations practical. Current workaround: sequential build_context per page + Obsidian CLI for structural queries.

digitalities avatar Apr 12 '26 22:04 digitalities

In our experience, progressive loading is useful not only for reducing token usage, but also for separating evidence types.

A compact search or index response should be treated as discovery. A full exact-address read should be treated as content verification. Neither should silently imply current operational authority.

A useful implementation detail would be making the requested detail level explicit, for example:

  • index or discovery;
  • observations and relations;
  • full note content.

It would also help if the response made clear whether the result came from the current indexed state or from a direct addressed read.

This feedback was prepared collaboratively by Semyon Poklad and the AI agent Lad. It is based on a small multi-agent family-research MVP and is offered as a user-side observation, not a claim about Basic Memory's internal architecture. The final text was reviewed and manually approved by Semyon before posting.

TeaShaman-cyber avatar Aug 02 '26 20:08 TeaShaman-cyber

Some measurements in support of this, from a deliberately tiny knowledge base. The report above gives aggregate numbers at scale, and it's hard to see the per-note cost in those.

Corpus is 9 notes on Basic Memory 0.23.2. Same query, same project, only the scope changed:

Query scope JSON response
search_notes, default, page_size 10 31,706 chars
same query, entity_types: ["observation"] 11,728 chars

A 63% reduction, on nine notes.

The mechanism is visible through bm inspect chunks, which 0.23.0 added. Each note is indexed as one entity row holding the whole body, plus one row per observation and relation. For one 998-word note that's 15 rows totalling 7,921 characters, of which the single entity row is 6,233. So 79% of that note's indexed bytes are the body as a single blob, and when that row is what matched, the body is what comes back.

Both fields are returned per result, not one or the other. One hit in the run above carried content of 2,517 characters alongside a matched_chunk of 1,505, so the caller pays for the full body and the excerpt.

That's worth flagging against the triage analysis, which reads "Already returns somewhat compact data (matched chunks, not full content)". It returns both, so the current baseline is more expensive than that suggests. #1455 and #1470 cap content at 4,000 characters, which bounds the damage, but a default page of 10 still has a 40,000 character ceiling regardless of how well the match localises.

For anyone hitting this before the feature lands, entity_types: ["observation"] is a usable approximation of the proposed L1 today. It returns observation rows instead of the entity row, so facts come back at their own granularity rather than wrapped in the note that contains them. It doesn't help build_context, which still returns everything.

llucax avatar Sep 07 '26 12:09 llucax

Thanks for the concrete measurements. For agents with shell access, the POSIX CLI offers a useful interim workflow: bm grep "topic" --plain renders compact results with snippets capped at 200 characters, and bm cat "note-permalink" --lines 120-180 --plain lets the agent inspect a selected range. Explicit --plain matters here: JSON output retains the search response payload. This reduces agent-visible output, not the underlying search payload, and does not solve compact graph-wide retrieval for MCP clients.

I've opened #1501 for line scanning in note reads and literal grep: matching line numbers, bounded context, and targeted reads that can expand or continue. That complements this issue; the broader compact/progressive response request remains valid.

phernandez avatar Sep 07 '26 20:09 phernandez

The first compact-discovery slice is in #1515: search_notes(compact=True) omits note bodies and matched excerpts while retaining identifiers, metadata, relation fields, scores and pagination. It works for project and all-project searches. A real MCP long-note scenario verifies the discovery-then-read workflow and reduces JSON size to less than one-third on that fixture. This is opt-in and reduces MCP output; API/database work and compact build_context remain separate follow-ups. CI and review are running.

phernandez avatar Sep 08 '26 04:09 phernandez

The companion slices are now open: #1516 adds compact build_context output and #1518 exposes --compact for CLI search-notes/build-context, with a discovery/read guide. #1518 is stacked on the MCP changes. Review caught observation prose in generated labels and loss of workspace-qualified read paths; both have been addressed, and fresh CI/Codex gates are running. These are opt-in responses, not a hard token budget or a reduction in underlying retrieval work.

phernandez avatar Sep 08 '26 05:09 phernandez

Thanks for the answer and recommendations!

The options you mention seem unreleased, is there an ETA for a new release that includes them?

llucax avatar Sep 08 '26 12:09 llucax

@llucax working on the v0.24.0 release asap

phernandez avatar Sep 08 '26 12:09 phernandez

Closing as superseded by the smaller delivered design. We are not implementing the proposed L0/L1/L2 progressive-loading system. Compact search/context responses and bounded note reads by section and line range now provide the practical token-control behavior without adding another loading abstraction.

phernandez avatar Sep 14 '26 03:09 phernandez