# Context Standard: Context Analytics

## Overview
Context analytics tracks token usage, identifies optimization opportunities, and monitors session efficiency.

## Metrics Collected

### Session Metrics
- Session ID, start time, feature, agent
- Token usage at session start vs end
- Optimization actions taken
- Files read (and token cost per file)

### Context Efficiency Score
```
Efficiency = 1 - (contextTokens / totalContextCapacity)
Target: > 0.6 (60% of context available for work)
Warning: < 0.4 (less than 40% available)
Critical: < 0.2 (less than 20% available)
```

## Viewing Analytics

### Feature Analytics
```bash
morph-spec analytics feature morph-next-gen

# Context Usage — morph-next-gen
# ─────────────────────────────────────
# Sessions: 12
# Avg session tokens: 45,200
# Total tokens used: 542,400
#
# Efficiency distribution:
# Excellent (>0.8)  ████████░░  8/12 (67%)
# Good (0.6-0.8)    ██░░░░░░░░  2/12 (17%)
# Poor (<0.6)       ██░░░░░░░░  2/12 (17%)
#
# Top token consumers:
#   CLAUDE.md              23,000 tokens (42%)
#   standards/coding.md     2,100 tokens (4%)
#   spec.md                 1,800 tokens (3%)
#
# Optimizations recommended:
#   → Replace CLAUDE.md with priming file (save ~22,500 tokens)
#   → Use context bundle for resume sessions
```

### Context Dashboard
```bash
morph-spec analytics context

# Core Four Status
# ─────────────────────────────────────
# Context:  [████████░░░░░░░░░░░░] 41% (82,000/200,000 tokens)
# Model:    claude-sonnet-4-6
# Prompt:   3,200 tokens (current turn)
# Tools:    12 tools active
```

## Optimization Triggers

| Condition | Trigger | Action |
|-----------|---------|--------|
| Context > 75% | Warning | Suggest bundle creation |
| Context > 90% | Critical | Auto-suggest micro-agent split |
| CLAUDE.md loaded | Info | Suggest priming file |
| > 5 standards loaded | Info | Suggest standards subset |
| Unused MCP servers | Warning | Suggest MCP hygiene |

## Analytics Storage
Events stored in JSONL format at `.morph/analytics/`:
- `threads-log.jsonl` — thread lifecycle events
- `context-log.jsonl` — context optimization events
- `trust-log.jsonl` — trust/approval events

Each event:
```json
{
  "timestamp": "2026-02-19T00:00:00Z",
  "type": "context_warning",
  "feature": "morph-next-gen",
  "data": {
    "contextPercent": 82,
    "tokensUsed": 164000,
    "totalCapacity": 200000,
    "recommendation": "CREATE_BUNDLE"
  }
}
```

## Pruning
Analytics auto-prune after 90 days. Manual prune:
```bash
morph-spec analytics project --prune 30
```
