Skip to content

Replay & Trace Comparison

The Replay Layer captures every orchestration decision as an immutable Trace and provides tools to compare, replay, and analyse those traces. This is the foundation of Layerr’s self-improving nature.

A trace is a complete record of one request’s journey through the Layerr system:

interface Trace {
traceId: string;
workspaceId: string;
createdAt: Date;
intent: IntentClassification;
workload: WorkloadProfile;
strategy: string;
routingDecision: {
primaryProvider: string;
primaryModel: string;
fallbackChain: string[];
scores: ScoreBreakdown;
};
execution: {
attempts: Attempt[];
finalProvider: string;
finalModel: string;
latencyMs: number;
tokensIn: number;
tokensOut: number;
costUsd: number;
};
explanation: Explanation;
}

Traces are persisted in SQLite (storage/traces.ts and storage/replay.ts):

FunctionPurpose
listRecentTraces()Retrieves recent traces for a workspace
compareTraceIds()Compares two traces side-by-side
analyzeRecentTraces()Aggregates trends across recent traces
buildReplayTrace()Reconstructs a full trace from raw data

The Replay Studio (src/features/replay/OrchestrationReplayStudio.tsx) allows users to:

  1. Replay a request: Send the same request again and compare outcomes
  2. Test alternative strategies: “What if I had used Speed instead of Quality?”
  3. Compare providers: See how Claude vs GPT-4o handled the same prompt
  4. View economic impact: Compare costs across replays (src/replay/ReplayEconomicOverlay.tsx)

The comparator (replay/comparator.ts) produces diff-style comparisons:

DimensionComparison
LatencyDelta in milliseconds
CostDelta in USD
QualitySide-by-side quality scores
ProviderWhich provider/model was used
ExplanationWhy different decisions were made

The trend engine (replay/analysis.ts) detects patterns across traces:

AnalysisDescription
analyzeTrend()Detects latency/cost/quality trends over time
Provider degradationIdentifies providers whose quality is declining
Strategy driftDetects when a strategy is no longer optimal
Workload evolutionTracks how workspace workloads change over time

Every replay includes economic analysis (src/replay/ReplayEconomicOverlay.tsx):

MetricDescription
Base CostCost of the original execution
Comparison CostCost of the replayed/alternative execution
SavingsAmount saved (or overspent) by the chosen route
Cost EfficiencyQuality per dollar
ComponentFilePurpose
ReplayComparisonViewsrc/replay/ReplayComparisonView.tsxSide-by-side trace comparison
ReplayEconomicOverlaysrc/replay/ReplayEconomicOverlay.tsxCost analysis overlay
TrendAnalysisViewsrc/replay/TrendAnalysisView.tsxTrend detection UI
OrchestrationReplayStudiosrc/features/replay/OrchestrationReplayStudio.tsxFull replay studio
FileWhat It Does
replay/comparator.tsTrace comparison logic. Produces diff-style outputs
replay/analysis.tsTrend analysis and pattern detection across traces
storage/replay.tsReplay storage and retrieval layer
storage/traces.tsCore trace persistence
src/replay/ReplayComparisonView.tsxFrontend comparison UI
src/replay/ReplayEconomicOverlay.tsxCost analysis UI
src/replay/TrendAnalysisView.tsxTrend detection UI
src/features/replay/Complete replay feature module (studio, models, hooks)
  1. Execution Engine → writes traces after every request
  2. Explainability → stores explanations as part of each trace
  3. Economics → reads traces for cost attribution and savings analysis
  4. Adaptive Learning → reads trace histories to learn from outcomes
  5. Quality Evaluation → uses replay for A/B testing and calibration
  6. Benchmarks → replays traces against new providers for benchmarking