Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:23blocks OS Ai maestro Memory Context Building

From Leeroopedia
Knowledge Sources
Domains Memory, RAG
Last Updated 2026-10-26 10:00 GMT

Overview

A context-building pattern that assembles relevant memories, preferences, and patterns into a formatted string for an LLM prompt, truncated to a token budget.

Description

`buildMemoryContext` takes a natural language query and an agent's memory database, searches for relevant memories (via `searchMemories`), fetches top preferences and patterns, and assembles them into a structured context string with sections (Relevant Memories, User Preferences, Known Patterns). The result is truncated to a token budget (rough estimate: 4 chars per token).

`entityNeighbourhood` queries the entity relationship graph for related entities. `aboutEntity` builds a complete "everything known about this entity" card — its relations and the memories that mention it.

Usage

Use this principle when designing an LLM context assembly system where:

  • Relevant memories must be retrieved by semantic similarity to the query
  • Preferences and patterns provide stable context across queries
  • The context must fit within a token budget

Theoretical Basis

The context is a structured assembly with truncation:

buildMemoryContext(agentDb, agentId, query, {maxTokens}):
  memories = searchMemories(agentDb, agentId, query, {limit: 10})
  preferences = getPreferences(agentDb, agentId, 5)
  patterns = getPatterns(agentDb, agentId, 5)
  context = formatSections(memories, preferences, patterns)
  if estimateTokens(context) > maxTokens:
    truncate(context, maxTokens * 4 chars)
  return context

The 4-chars-per-token estimate is a rough heuristic. The sections are ordered by relevance: memories (query-dependent) first, then stable preferences and patterns.

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment