Skip to content

Routing Engine

The Routing Engine is the heart of Layerr. It takes an incoming request, enriches it with context, scores providers, builds a fallback chain, and produces an execution plan, all within milliseconds.

[1] Classify Intent -- workflow/classifier.ts
|
v
[2] Analyze Workload -- workload/analyzer.ts
|
v
[3] Resolve Strategy -- strategy/engine.ts
|
v
[4] Enrich Routing Decision -- routing/enrichment.ts (THE CORE)
|
v
[5] Score Candidates -- scoring/scorer.ts
|
v
[6] Build Fallback Chain -- orchestration/fallback.ts
|
v
[7] Resolve Provider Chain -- providers/resolution.ts
|
v
[8] Execute -- gateway/handler.ts

The enrichment function (routing/enrichment.ts) is where the magic happens:

function enrichRoutingDecision(
intent: IntentClassification,
workload: WorkloadProfile,
strategy: Strategy,
providers: ProviderMetadata[],
telemetry: ProviderTelemetry
): RoutingDecision {
// 1. Filter ineligible providers (capability mismatch)
const eligible = resolveCapabilities(workload, providers);
// 2. Score each eligible provider
const scored = eligible.map(p => scoreTrace(p, workload, strategy, telemetry));
// 3. Apply strategy weights
const weighted = applyStrategyWeights(scored, strategy.weights);
// 4. Build fallback chain
const fallbackChain = buildFallbackChain(weighted, strategy.constraints);
return {
primary: weighted[0],
fallbackChain,
explanation: buildRoutingExplanation(weighted, strategy)
};
}

The routing prompt builder (routing/prompt-builder.ts) assembles prompts that help the routing system understand the request better. It includes:

  1. Request summary: What the user is asking for
  2. File context: Active files, language, stack
  3. Historical patterns: What worked for similar requests
  4. Workspace preferences: Strategy, budget, restrictions

The fallback builder (orchestration/fallback.ts) ensures resilience:

RuleDescription
Provider diversityNo two consecutive providers from the same company
Model family diversityMix of model architectures for resilience
Geographic diversityDifferent regions for availability
Tier diversityMix of premium and standard models
function buildFallbackChain(
scoredProviders: ScoredCandidate[],
constraints: StrategyConstraints
): ProviderCandidate[] {
// 1. Sort by composite score
const ordered = scoredOrder(scoredProviders);
// 2. Apply diversity rules
const diverse = applyDiversityRules(ordered);
// 3. Respect constraints (max cost, min quality)
return filterByConstraints(diverse, constraints);
}

Key type definitions (routing/types.ts):

interface RoutingDecision {
primary: ScoredCandidate;
fallbackChain: ProviderCandidate[];
scores: ScoreBreakdown;
explanation: Explanation;
estimatedCost: number;
estimatedLatency: number;
}
interface ScoreBreakdown {
quality: number;
speed: number;
cost: number;
health: number;
workspaceFit: number;
weightedTotal: number;
}

The routing engine is designed for sub-100ms decision latency:

OptimizationImplementation
Cached intelligenceProvider intelligence cached in memory
Pre-computed scoresQuality scores updated asynchronously
Lazy scoringOnly score top-N candidates, not all
Fast pathDirect routing for high-confidence simple requests
FilePurpose
routing/enrichment.tsCore routing enrichment logic
routing/prompt-builder.tsRouting prompt assembly
routing/types.tsRouting type definitions
orchestration/fallback.tsFallback chain construction
src/workspace/control/RoutingPreview.tsxFrontend routing preview
src/features/replay/RoutingExplanationTimeline.tsxRouting explanation timeline
src/features/replay/DecisionBreakdownPanel.tsxScore breakdown panel
FailureCauseMitigation
All providers exhaustedSystem-wide outageReturn 502 with full trace
No eligible providersCapability mismatchReturn 400 with suggested alternatives
Budget exceededWorkspace over limitReturn 402 with budget status
Timeout during routingSlow health checksUse cached health scores
Ambiguous intentLow classification confidenceRoute to most capable model