Memory Summarization
Compresses multiple memories into structured summaries. Reduces context size while preserving critical information.
Execution Path
code
┌─────────────────────────────────────────────────────────┐ │ EXECUTION DECISION │ ├─────────────────────────────────────────────────────────┤ │ │ │ ┌──────────┐ ┌───────────────────┐ │ │ │ START │────▶│ SUBAGENT AVAILABLE?│ │ │ └──────────┘ └─────────┬─────────┘ │ │ │ │ │ ┌──────────────┴──────────────┐ │ │ ▼ ▼ │ │ ┌────────────┐ ┌────────────┐ │ │ │ YES │ │ NO │ │ │ └─────┬──────┘ └─────┬──────┘ │ │ │ │ │ │ ▼ ▼ │ │ ┌─────────────────────┐ ┌─────────────────────┐ │ │ │ Task(summarizer, │ │ Bash(bun scripts/ │ │ │ │ prompt: "...") │ │ create-summary.ts)│ │ │ └─────────────────────┘ └─────────────────────┘ │ │ │ └─────────────────────────────────────────────────────────┘
Path A: Subagent Available (Preferred)
Use the summarizer subagent for lower token cost:
code
Task( subagent_type: "summarizer", description: "Summarize memories", prompt: "Check stats and create coding_session summary for project 3afb861a-b783-45a6-bba2-4b6d96468aeb" )
Path B: Script Fallback
If subagent unavailable, execute scripts directly:
bash
bun .opencode/skills/memory-summarize/scripts/check-memory-stats.ts PROJECT_ID bun .opencode/skills/memory-summarize/scripts/create-summary.ts PROJECT_ID TYPE
State Machine
code
┌─────────────────────────────────────────────────────────────────────┐ │ SUMMARIZATION WORKFLOW │ ├─────────────────────────────────────────────────────────────────────┤ │ │ │ ┌──────────┐ ┌──────────────┐ ┌─────────────────────────┐ │ │ │ START │────▶│ CHECK_STATS │────▶│ EVALUATE_THRESHOLDS │ │ │ └──────────┘ └──────────────┘ └─────────────────────────┘ │ │ │ │ │ ┌────────────────────────┼────────────┐ │ │ ▼ ▼ │ │ │ ┌────────────┐ ┌────────────┐ │ │ │ │ SKIP │ │ SUMMARIZE │ │ │ │ │ (below │ │ (above │ │ │ │ │ threshold) │ │ threshold) │ │ │ │ └────────────┘ └─────┬──────┘ │ │ │ │ │ │ │ │ │ ┌──────▼──────┐ │ │ │ │ │ SELECT_TYPE │ │ │ │ │ └──────┬──────┘ │ │ │ │ │ │ │ │ │ ┌─────────────────┼─────────────┐│ │ │ │ ▼ ▼ ▼│ │ │ │ ┌────────┐ ┌────────────┐ ┌──────┴┐ │ │ │ │coding_ │ │conversation│ │custom │ │ │ │ │session │ │ │ │ │ │ │ │ └───┬────┘ └─────┬──────┘ └───┬───┘ │ │ │ └───────────────┼────────────┘ │ │ │ ▼ │ │ │ ┌─────────────┐ │ │ │ │ CREATE_ │ │ │ │ │ SUMMARY │ │ │ │ └──────┬──────┘ │ │ │ │ │ │ │ ┌──────▼──────┐ │ │ │ │ VERIFY_ │ │ │ │ │ COMPRESSION │ │ │ │ └──────┬──────┘ │ │ │ │ │ │ │ ┌───────────┴───────────┐ │ │ │ ▼ ▼ │ │ │ ┌────────────┐ ┌────────────┐ │ │ │ │ SUCCESS │ │ FAILURE │ │ │ │ │ ratio > 2.0│ │ ratio < 2.0│ │ │ │ └─────┬──────┘ └─────┬──────┘ │ │ │ │ │ │ │ │ ▼ ▼ │ │ │ ┌──────────┐ ┌──────────┐ │ │ └─▶│ END │◀─────────│ RETRY │ │ │ └──────────┘ └──────────┘ │ │ │ └─────────────────────────────────────────────────────────────────────┘
States
| State | Entry Condition | Action | Exit Condition |
|---|---|---|---|
| CHECK_STATS | Start | Run check-memory-stats.ts | Stats retrieved |
| EVALUATE_THRESHOLDS | Stats available | Compare against thresholds | Decision made |
| SKIP | count < 50 AND tokens < 40000 | Log "below threshold" | End |
| SELECT_TYPE | Above threshold | Determine summary type | Type selected |
| CREATE_SUMMARY | Type selected | Run create-summary.ts | API response |
| VERIFY_COMPRESSION | Summary created | Check ratio > 2.0 | Verified |
| SUCCESS | ratio >= 2.0 | Output summary ID | End |
| FAILURE | ratio < 2.0 | Log warning | Retry or End |
Thresholds
| Metric | Threshold | Action |
|---|---|---|
| Memory count | >= 50 | Summarize |
| Token estimate | >= 40000 | Summarize |
| Compression ratio | < 2.0 | Warning |
Summary Types
coding_session
Input: Development memories (file changes, errors, debugging)
Output sections:
- •
objective: string (one sentence) - •
progress: string[] (action → result) - •
filesTouched: {path, operation, changes}[] - •
currentState: string - •
criticalDetails: string[]
conversation
Input: Dialogue memories (discussions, decisions)
Output sections:
- •
topics: string[] - •
decisions: string[] - •
actionItems: string[] - •
context: string[] - •
openQuestions: string[]
custom
Input: Any memories with custom prompt
Output sections:
- •
prompt: string - •
content: string - •
keyPoints: string[]
Commands
Check Statistics
bash
bun .opencode/skills/memory-summarize/scripts/check-memory-stats.ts PROJECT_ID [--show-compression]
Output:
code
Memory Count: 150 Estimated Tokens: 60000 ⚠️ Memory count exceeds threshold (50) Recommendation: Run summarization
Create Summary
bash
bun .opencode/skills/memory-summarize/scripts/create-summary.ts PROJECT_ID TYPE [CUSTOM_PROMPT]
Parameters:
- •PROJECT_ID: UUID (required)
- •TYPE: coding_session | conversation | custom (default: coding_session)
- •CUSTOM_PROMPT: string (required if TYPE=custom)
Output:
code
Summary ID: abc-123 Compression Ratio: 6.25x Token Reduction: 84% Original: 5000 tokens Compressed: 800 tokens Memories: Summarized: 44 Retained: 6
Get Hybrid Context
bash
bun .opencode/skills/memory-summarize/scripts/get-context.ts PROJECT_ID [QUERY]
Output:
json
{
"summaries": [...],
"recentMemories": [...],
"relevantMemories": [...]
}
Configuration
| Variable | Default | Description |
|---|---|---|
| BRAINY_API_URL | http://localhost:3000 | API endpoint |
| RETAIN_LAST_N | 6 | Memories to keep unsummarized |
| SUMMARIZATION_AUTO_TRIGGER_COUNT | 50 | Memory count threshold |
| SUMMARIZATION_AUTO_TRIGGER_TOKENS | 40000 | Token threshold |
Error Codes
| Error | Cause | Resolution |
|---|---|---|
| "No memories found" | Empty project | Create memories first |
| "All memories in retain window" | count <= RETAIN_LAST_N | Reduce RETAIN_LAST_N or add memories |
| "Project not found" | Invalid PROJECT_ID | Verify UUID |
| "API connection refused" | Server not running | Start BrAIny API |
Decision Tree
code
Is memory count >= 50?
├── YES → Summarize
└── NO → Is token estimate >= 40000?
├── YES → Summarize
└── NO → Skip (no action needed)
If Summarize:
├── Contains file changes, errors, code? → coding_session
├── Contains discussion, decisions? → conversation
└── User specified prompt? → custom