Skip to content

Routing Engine

The routing engine is the core of Layerr. It is not rule-based configuration. It is a dynamic scoring system that analyses the incoming request, the workspace profile, the workload context, and real-time provider conditions to pick the best model for the job.

FilePurpose
routing/enrichment.tsEnriches routing decisions with workspace, provider, and workload context
routing/prompt-builder.tsBuilds the orchestration prompt for the strategy engine
routing/types.tsCore routing types: RoutingInput, RoutingDecision, EnrichedDecision
orchestration/orchestrator.tsMain orchestrator that coordinates the full routing pipeline
orchestration/fallback.tsBuilds fallback chains when the primary provider fails
providers/resolution.tsResolves provider URLs, credentials, and capability matching

Routing Pipeline

enrichRoutingDecision takes a raw routing input and enriches it with:

  • Workspace configuration (active profile, budgets, preferences)
  • Provider health status (latency, error rate, rate limit headroom)
  • Workload context (language, stack, complexity estimate)
  • Historical performance (previous similar requests, calibration data)

The QualityScoringEngine.scoreTrace method scores each candidate provider on:

  • Capability match: Can this model handle the detected language/stack?
  • Cost efficiency: Cost per token for this workload type
  • Latency: Recent p50/p99 response times
  • Reliability: Error rate and circuit breaker status
  • Confidence grade: Historical accuracy on similar requests

buildFallbackChain constructs an ordered list of providers to try:

  1. Primary: Highest scored provider
  2. Secondary: Next best, different provider family (diversification)
  3. Tertiary: Cheapest viable option
  4. Emergency: Self-hosted / open-source fallback

resolveProviderChain resolves URLs, injects credentials, and verifies rate limit headroom before attempting the request.

Every routing decision produces a RoutingDecision record with:

  • Chosen provider and model
  • Full candidate list with scores
  • Fallback chain
  • Rationale (human-readable explanation)
  • Estimated cost
  • Confidence score

These decisions are stored and replayable. See Replay Platform for details.