The Economics Layer makes every provider decision financially transparent. It tracks real costs, simulates alternative scenarios, computes savings attribution, and protects budgets before they break.
Function Purpose File Cost Tracking Records actual spend per provider, model, and workspace economics/attribution/engine.tsSavings Attribution Computes how much Layerr saved vs. baseline economics/attribution/engine.tsBudget Protection Enforces budget caps and triggers alerts budget/evaluator.tsSimulation What-if analysis: “What would this cost on another provider?” economics/simulation/engine.tsInsights High-level economic insights and recommendations economics/insights/engine.ts
The attribution engine (economics/attribution/engine.ts) computes:
Metric Description Monthly Spend Total cost this calendar month Provider Breakdown Cost per provider Category Breakdown Cost per model category (fast, balanced, reasoning) Fallback Cost Additional cost incurred by fallback attempts Premium Avoidance Amount saved by routing to cheaper capable models Category Efficiency Quality per dollar by category
tokensSaved: baselineTrace . tokensOut - actualTrace . tokensOut ,
costSavedUsd: baselineTrace . costUsd - actualTrace . costUsd ,
latencySavedMs: baselineTrace . latencyMs - actualTrace . latencyMs ,
efficiencyGain: actualTrace . qualityScore / actualTrace . costUsd
The budget evaluator (budget/evaluator.ts) enforces:
Check Action Pre-request budget check Block if workspace is over budget Soft threshold alert Notify when 80% of budget consumed Category tier limiting Block premium models when budget is tight Batch job quota Limit batch job size based on remaining budget
The simulation engine (economics/simulation/engine.ts) answers “what if” questions:
Simulation Description simulateBaselineExecution()Cost if routed to default baseline model simulateAllProfiles()Cost if each profile (cost/speed/quality/balanced) was used buildSimulationInputFromReplayTrace()Simulate an alternative for an existing trace computeBalancedBaseline()Baseline cost for balanced strategy
The insights engine (economics/insights/heuristics.ts) detects economic patterns:
Heuristic What It Detects detectHighFallbackProviders()Providers that frequently trigger costly fallbacks detectSlowProviders()Providers whose latency suggests poor value detectFallbackCostEscalation()Situations where fallback costs spiral detectHighFleetFallbackRate()Workspace-wide fallback rate trends detectSimulationSavings()Potential savings from simulation results detectPremiumAvoidance()Successful routing away from premium models
Component File Purpose ProviderEconomicsOverlaysrc/features/providers/economics/Provider-level cost dashboard EconomicComparisonInspectorsrc/features/evaluation/Compare costs across providers TraceEconomicPanelsrc/traces/TraceEconomicPanel.tsxCost analysis per trace BudgetProtectionPanelBudget UI with alerts and controls
File What It Does economics/attribution/engine.tsCost tracking and savings computation economics/attribution/queries.tsSQL queries for cost aggregation economics/simulation/engine.tsWhat-if cost simulation economics/insights/engine.tsEconomic insight generation economics/insights/heuristics.tsPattern detection heuristics budget/evaluator.tsBudget enforcement and impact evaluation src/features/providers/economics/Frontend economics dashboards
Execution Engine → provides token counts and actual costs
Replay → provides traces for savings computation
Simulation → simulates alternatives using provider pricing metadata
Workspace Profiles → reads budget constraints
Quality Evaluation → correlates cost with quality for efficiency metrics