Principle:777genius Agent teams ai Session Analytics
| Knowledge Sources | |
|---|---|
| Domains | Session_Analysis, Analytics |
| Last Updated | 2026-10-22 00:00 GMT |
Overview
Pattern for computing session-level analytics including token usage by model, cost calculations, context consumption assessment, and session duration from parsed session data.
Description
Session Analytics is the process of taking a `SessionDetail` (session metadata, parsed messages, enhanced chunks) and computing a comprehensive `SessionReport` with:
- Session overview: Duration, first/last timestamps, message count.
- Context consumption: Percentage of context window used, with assessment levels (healthy, moderate, high, critical).
- Token usage by model: Per-model API calls, input tokens, output tokens, cache creation, cache read, and cost.
- Cost calculations: Total cost in USD, computed from per-model token counts and pricing data.
- Single-pass accumulation: All metrics are computed in a single pass over the messages, accumulating per-model statistics.
Key mechanisms:
- Model stats map: A `Map<string, ModelTokenStats>` accumulates per-model metrics during the single pass.
- Pricing data: Model pricing is loaded from `resources/pricing.json` for cost computation.
- Context assessment: Context consumption > 0.8 = critical, > 0.6 = high, > 0.4 = moderate, else healthy.
Usage
Use this principle when computing aggregated analytics from a session's raw message data, especially when per-model token and cost breakdowns are needed.
Theoretical Basis
# Abstract algorithm (NOT real implementation)
def analyze_session(detail):
session, messages = detail.session, detail.messages
# Session overview
timestamps = [m.timestamp for m in messages if m.timestamp]
duration = timestamps[-1] - timestamps[0] if timestamps else 0
# Context assessment
ctx_pct = session.context_consumption
if ctx_pct > 0.8: assessment = 'critical'
elif ctx_pct > 0.6: assessment = 'high'
elif ctx_pct > 0.4: assessment = 'moderate'
else: assessment = 'healthy'
# Single-pass per-model accumulation
model_stats = {}
for message in messages:
model = message.model
stats = model_stats.setdefault(model, new_stats())
stats.api_calls += 1
stats.input_tokens += message.input_tokens
stats.output_tokens += message.output_tokens
stats.cost_usd += compute_cost(model, message)
return SessionReport(
duration_ms=duration,
context_assessment=assessment,
model_stats=model_stats
)