Package com.tnsai.memory.strategy
Class TieredContextLoader
java.lang.Object
com.tnsai.memory.strategy.TieredContextLoader
Loads conversation context in tiers with token budgets.
Instead of loading all history and then pruning, TieredContextLoader assembles context from three tiers with independent token budgets:
- Tier 1 (recent): Last N messages loaded verbatim -- highest priority
- Tier 2 (summary): Older messages summarized by LLM -- medium priority
- Tier 3 (search): Semantically relevant past messages -- lowest priority
Token Budget Allocation
Total Budget (e.g., 4000 tokens) +-- Tier 1 (recent): takes what it needs (up to totalBudget - summary - search) +-- Tier 2 (summary): up to summaryTokenBudget (e.g., 500) +-- Tier 3 (search): up to searchTokenBudget (e.g., 300)
Usage Example
TieredContextLoader loader = TieredContextLoader.builder()
.store(memoryStore)
.summarizer(messages -> llm.summarize(messages))
.config(TieredContextLoader.TierConfig.defaults())
.build();
LoadedContext ctx = loader.load("What did we discuss about caching?");
List<Map<String, Object>> messages = ctx.toMessageList();
- Since:
- 0.1.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classBuilder for TieredContextLoader.static interfaceFunctional interface for summarizing a list of messages.static final recordResult of tiered context loading.static final recordConfiguration for tier token budgets. -
Method Summary
Modifier and TypeMethodDescriptionstatic TieredContextLoader.Builderbuilder()Creates a new builder.Loads tiered context for the given query.
-
Method Details
-
builder
Creates a new builder. -
load
Loads tiered context for the given query.Assembles context by:
- Taking the last
recentTurnCount * 2messages verbatim (user + assistant pairs) - Summarizing older messages (if any exist)
- Searching for semantically relevant past messages matching the query
- Parameters:
currentQuery- the current user query (used for semantic search), may be null- Returns:
- loaded context with token estimates
- Taking the last
-