ยง 10 ยท Performance Deep-Dive

Cold Start Optimization

When a user's device loads a model for the first time, we don't make them wait. Speculative cloud dispatch fires at t=0 โ€” before the gate-check even finishes.

10.1

The Cold Start Problem

First local inference on a device incurs a "cold start" penalty โ€” the model must be decompressed and loaded into VRAM. On an M-series Mac with fast storage, that's 2โ€“3 seconds. On mid-range Android with slower memory, it can be 5+ seconds. Pure local-first strategies fail here: the user waits in silence.

The original V1 approach dispatched cloud only after the gate-check confirmed the model was cold. That added 50โ€“80ms of unnecessary silence before the first cloud token. The refactored router eliminates this entirely with speculative dispatch.

10.2

Speculative Dispatch (current implementation)

flow
User Request arrives at Router
    โ†“
[t=0ms] Step 0: Synchronous privacy check  โ† runs BEFORE any I/O, zero latency
    policy == "local_only"  โ†’ BLOCK speculative entirely โ€” data never leaves device
    policy == "no_cloud"    โ†’ BLOCK speculative โ€” local/mesh only
    otherwise               โ†’ speculative_ok = true
    โ†“
[t=0ms] Step 1: Fire speculative cloud stream (AbortController created)
        + Run gate-check concurrently (50โ€“80ms)
    โ†“
    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
    โ”‚  Cloud stream (speculative)    โ”‚  Gate-check (concurrent)     โ”‚
    โ”‚  AbortController signal live   โ”‚  registry: models_loaded?    โ”‚
    โ”‚  token 0 โ† t+180ms            โ”‚  capability: VRAM โ‰ฅ model?   โ”‚
    โ”‚  token 1 โ† t+210ms            โ”‚  history: used in last 2h?   โ”‚
    โ”‚  ...                           โ”‚  โ†’ resolves at t+50โ€“80ms     โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
    โ†“
[t+50-80ms] Step 2: Gate-check resolves
    โ†“
    gate_result.warm == true?  (63% of requests โ€” pilot data)
    โ†’ AbortController.abort()        โ† cloud stream cancelled immediately
    โ†’ local model takes over         โ† TTFT = ~12ms from warm model
    โ†’ 0โ€“3 tokens billed to cloud     โ† cost: ~$0.000001

    gate_result.warm == false?  (37% of requests)
    โ†’ keep cloud stream              โ† already delivering tokens since t+180ms
    โ†’ background: warm local model   โ† 2โ€“5s loading
    โ†’ seamless handoff when ready    โ† local takes over mid-stream

Key improvement: TTFT drops from gate_check_ms + cloud_RTT (~230โ€“260ms) to just cloud_RTT (~180ms). On warm-model hits (63%), the cloud stream is aborted before meaningful tokens are billed โ€” saving ~$0.000001 per aborted request.

10.3

Privacy Gate (synchronous, pre-dispatch)

The privacy check is the first operation executed โ€” synchronous, before any network call is issued. This is a structural guarantee, not a policy flag checked later.

The SDK router is the entry point for every inference request. Its first operation is the privacy gate โ€” a synchronous check that runs before any network I/O is initiated. This is a structural guarantee, not a policy flag evaluated later in the request lifecycle.

Step 0 ยท Privacy gateEvaluated synchronously before any await. If the privacy policy forbids cloud, speculative dispatch is structurally impossible โ€” no cloud request is created, no network call is issued, and no data leaves the device.
Step 1 ยท Speculative dispatchOnly reached if the privacy gate passed AND the device is a local candidate. A cloud stream is started concurrently with a local warm-up check. If the local model is already warm, the cloud stream is cancelled before any tokens are billed. If the model is cold, the cloud stream is kept and local warms in the background for future requests.
Step 2 ยท Standard scoringReached when the device is not a local candidate or the privacy policy blocked speculative dispatch. The request follows the standard routing path without any concurrent cloud stream.
Structural privacy guarantee
For local_only andno_cloud policies, speculative dispatch is not merely discouraged โ€” it is structurally impossible. The privacy gate resolves before the code path that creates a cloud request is ever entered.
PolicySpeculative fires?Reason
local_onlyNeverStructural block โ€” privacy gate runs before any I/O
no_cloudNeverPolicy explicitly forbids cloud calls
cheapYes (if local candidate)AbortController cancels if warm โ€” cost saved
auto / balancedYes (if local candidate)Default path โ€” most users
low-latencyYes (if local candidate)Speculative gives lowest possible TTFT
10.4

AbortController Mechanics

Why abort works here

  • AbortController.abort() mid-stream: Closes the fetch ReadableStream and sends a connection reset to the provider. Billing stops at the last token the provider had already sent. If abort fires within the first RTT (~30ms), typically 0 tokens are billed.
  • Gate-check target: <80ms: The gate-check must resolve fast enough that abort fires before the cloud provider bills a meaningful number of tokens. Target is sub-80ms; current measurement is 50โ€“80ms, well inside the first cloud token window (~180ms RTT).
  • Provider compatibility: All four primary cloud adapters (OpenAI, Anthropic, Groq, Together) honor the AbortController signal and stop streaming. Billing granularity varies โ€” OpenAI bills per-token, Groq bills on completion.
  • No partial output to client: Cloud tokens received before abort are buffered in the SDK, not streamed to the user. On abort, the buffer is discarded. Client sees a clean local stream starting from token 0.

Edge case: gate-check slower than first cloud token

If gate-check takes > 180ms (e.g., cold registry miss, slow device), the cloud stream has already delivered token 0 to the buffer before abort can fire. In this case, the SDK keeps cloud as primary and does not switch to local โ€” aborting after token delivery would cause a visible stream discontinuity. The timeout heuristic:

pseudo
if gate_check_ms > cloud_first_token_ms:
  // Too late to abort cleanly โ€” accept cloud as primary
  // Local warmup continues in background for future requests
  keep cloud_stream as primary
  mark device as "warming" in registry (next request will be warm)
10.5

Cold Start Detection Signals

SignalCheckAction
Node registryDevice heartbeat includes `models_loaded` list โ€” target model present?Warm โ†’ local dispatch, abort speculative
Capability probeDevice VRAM โ‰ฅ model_size + 500MB headroom?Eligible for speculative
Request historyDevice requested same model in last 2 hours?Likely in OS page cache โ†’ warm
PolicyUser policy is local_only or no_cloud?Speculative blocked synchronously before any I/O
10.6

Economics of Speculative Dispatch

ScenarioTTFTCloud tokens billedTotal costSavings vs. pure cloud
Warm hit (63%) โ€” abort fires~12ms (local)0โ€“3 tokens (~$0.000001)$0.000001 + $0.00001 local~99.6%
Cold start (37%) โ€” cloud kept~180ms (cloud)All cloud tokens for first segment$0.0001 + $0.00001 local~92%
Pure cloud (baseline)~180msFull response$0.00250โ€”
local-only (policy blocked)~12ms if warm, 2โ€“5s if coldZero (speculative blocked)$0.00000100% (but stall risk if cold)

Weighted average across warm/cold split: TTFT โ‰ˆ 75ms ยท cost โ‰ˆ $0.0000037 per request ยท savings โ‰ˆ 99.9% vs. pure cloud for warm-heavy workloads.

10.7

Metrics & SLOs

MetricTargetMeasured (pilot)
Time to first token (warm path)โ‰ค 20ms~12ms (local model already in VRAM)
Time to first token (cold path)โ‰ค 200ms180โ€“210ms (cloud source, speculative)
Gate-check latency (p95)โ‰ค 80ms50โ€“80ms
Abort-before-billing rateโ‰ฅ 90%~93% (gate-check fast enough)
User-visible delay0ms0ms (continuous stream, buffer hides transition)
Handoff hiccup (p95)< 10ms2โ€“5ms
Design intent
A user with local-only privacy policy should have a structural guarantee โ€” not a promise โ€” that no data leaves their device. The synchronous privacy gate enforces this before the first line of async code runs. Speculative dispatch is an opt-in performance feature, not a default that can accidentally violate privacy.