Skip to content

Observational Memory capability (observe/reflect with token thresholds) #68

Description

@adtyavrdhn

Summary

A memory system that passively observes conversation tokens and, when thresholds are reached, triggers observation (summarize recent messages) and reflection (compress observations) cycles — maintaining long-term context without explicit user intervention.

Motivation

For long-running coding sessions, conversation context grows until it exceeds the model's window. Observational Memory (OM) is a more sophisticated approach than simple truncation (#21 Compaction): it uses separate observer and reflector models to extract important information from expiring messages before they're removed. Mastra implements a full OM system with 12+ event types and configurable thresholds.

Code References

Mastra — OM configuration

// packages/core/src/harness/types.ts:246-255
interface HarnessOMConfig {
  defaultObserverModelId?: string;   // Model for observations
  defaultReflectorModelId?: string;  // Model for reflections
  defaultObservationThreshold?: number;  // Token threshold (default: 30K)
  defaultReflectionThreshold?: number;   // Token threshold (default: 40K)
}

Mastra — OM progress tracking

// packages/core/src/harness/types.ts:401-431
interface OMProgressState {
  status: 'idle' | 'observing' | 'reflecting';
  pendingTokens: number;
  threshold: number;
  thresholdPercent: number;
  observationTokens: number;
  reflectionThreshold: number;
  buffered: {
    observations: { status, chunks, messageTokens, projectedMessageRemoval, observationTokens };
    reflection: { status, inputObservationTokens, observationTokens };
  };
  generationCount: number;
  stepNumber: number;
  preReflectionTokens: number;
}

Mastra — OM events (12+ types)

// packages/core/src/harness/types.ts:641-725
| { type: 'om_status'; windows: { active, buffered }; recordId, threadId, stepNumber, generationCount }
| { type: 'om_observation_start'; cycleId, operationType, tokensToObserve }
| { type: 'om_observation_end'; cycleId, durationMs, tokensObserved, observationTokens, observations?, currentTask?, suggestedResponse? }
| { type: 'om_observation_failed'; cycleId, error, durationMs }
| { type: 'om_reflection_start'; cycleId, tokensToReflect }
| { type: 'om_reflection_end'; cycleId, durationMs, compressedTokens, observations? }
| { type: 'om_reflection_failed'; cycleId, error, durationMs }
| { type: 'om_buffering_start'; cycleId, operationType, tokensToBuffer }
| { type: 'om_buffering_end'; cycleId, operationType, tokensBuffered, bufferedTokens, observations? }
| { type: 'om_activation'; cycleId, operationType, chunksActivated, tokensActivated }
| { type: 'om_thread_title_updated'; cycleId, threadId, oldTitle?, newTitle }

Mastra — OM model switching

// packages/core/src/harness/harness.ts:966-1159
async switchObserverModel({ modelId }): Promise<void>;
async switchReflectorModel({ modelId }): Promise<void>;
getObservationThreshold(): number;
getReflectionThreshold(): number;

pydantic-ai design sketch

class ObservationalMemory(AbstractCapability[AgentDepsT]):
    """Passive observation + reflection memory system."""
    
    observer_model: Model | KnownModelName = 'gpt-4o-mini'
    reflector_model: Model | KnownModelName = 'gpt-4o-mini'
    observation_threshold: int = 30_000  # tokens
    reflection_threshold: int = 40_000   # tokens
    
    async def before_model_request(self, ctx, messages):
        token_count = count_tokens(messages)
        if token_count > self.observation_threshold:
            observations = await self._observe(messages)
            # Replace old messages with observation summary
        if self._observation_tokens > self.reflection_threshold:
            reflections = await self._reflect(self._observations)
            # Compress observations into reflections

Who has this

Framework Feature Details
Mastra Full OM system 2-phase (observe+reflect), configurable models/thresholds, 12+ events
Claude Code Auto memory Saves important info to persistent memory files
OpenAI Agents SDK Not present Manual memory management
MemGPT/Letta Tiered memory Working/archival/recall memory with auto-management

Relationship

Metadata

Metadata

Assignees

No one assigned

    Labels

    inspiration: mastra-harnessInspired by Mastra Harness researchnew-capabilityProposes or adds a standalone capability (AbstractCapability subclass)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions