Implementation:777genius Agent teams ai analyzeSession
| Knowledge Sources | |
|---|---|
| Domains | Session_Analysis, Analytics |
| Last Updated | 2026-10-22 00:00 GMT |
Overview
Concrete tool for computing session-level analytics including token usage by model, cost calculations, and context consumption assessment, provided by the Agent Teams application.
Description
The analyzeSession() function takes a `SessionDetail` (session metadata, parsed messages) and computes a comprehensive `SessionReport` in a single pass over the messages. It accumulates per-model token statistics, computes costs from pricing data, and assesses context consumption health.
Key behaviors:
- Session overview: Computes duration from first and last message timestamps, message count.
- Context assessment: Maps context consumption (0-1) to levels: critical (>0.8), high (>0.6), moderate (>0.4), healthy (else).
- Single-pass accumulation: Iterates messages once, accumulating per-model statistics in a `Map<string, ModelTokenStats>`.
- Per-model stats: Tracks API calls, input tokens, output tokens, cache creation, cache read, and cost per model.
- Cost computation: Uses model pricing data from `resources/pricing.json` to compute per-model and total costs.
- Model identification: Each message's `model` field identifies which model was used; messages without a model are attributed to a default.
Usage
- Session report UI: Call `analyzeSession()` to produce the data for the session analytics panel.
- **Cost breakdown:** Use the per-model stats to show which models consumed the most tokens and cost.
Code Reference
Source Location
- Repository: agent-teams
- File: src/renderer/utils/sessionAnalyzer.ts
Signature
export function analyzeSession(detail: SessionDetail): SessionReport;
export interface SessionDetail {
session: SessionMetadata;
messages: ParsedMessage[];
chunks?: EnhancedChunk[];
metrics?: SessionMetrics;
}
export interface SessionReport {
sessionOverview: SessionOverview;
contextConsumptionPct: number | null;
contextAssessment: 'critical' | 'high' | 'moderate' | 'healthy' | null;
modelStats: Map<string, ModelTokenStats>;
totalCostUsd: number;
}
Import
import { analyzeSession } from '@renderer/utils/sessionAnalyzer';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| detail.session | SessionMetadata | Yes | Session metadata (contextConsumption, etc.) |
| detail.messages | ParsedMessage[] | Yes | Parsed messages from JSONL |
Outputs
| Name | Type | Description |
|---|---|---|
| return | SessionReport | Comprehensive session analytics |
| SessionReport.sessionOverview | SessionOverview | Duration, message count, timestamps |
| SessionReport.contextAssessment | null | 'critical', 'high', 'moderate', 'healthy', or null |
| SessionReport.modelStats | Map<string, ModelTokenStats> | Per-model token and cost breakdown |
| SessionReport.totalCostUsd | number | Total cost across all models |
Usage Examples
Analyzing a Session
import { analyzeSession } from '@renderer/utils/sessionAnalyzer';
const report = analyzeSession({
session: sessionMetadata,
messages: parsedMessages,
chunks: enhancedChunks,
});
console.log(`Duration: ${report.sessionOverview.durationSeconds}s`);
console.log(`Context: ${report.contextAssessment}`);
console.log(`Total cost: $${report.totalCostUsd.toFixed(4)}`);
for (const [model, stats] of report.modelStats) {
console.log(` ${model}: ${stats.apiCalls} calls, ${stats.inputTokens} in, ${stats.outputTokens} out, $${stats.costUsd.toFixed(4)}`);
}
Context Health Check
const report = analyzeSession(detail);
if (report.contextAssessment === 'critical') {
console.warn('Context window is nearly full — consider starting a new session');
}