# Startup latency

1. **CLI command** builds the parser, checks first run
2. no model catalogue read
3. **TUI first frame** Node draws, the Worker is already up
4. the Worker warmed SDKs and turn modules after start
5. **First turn** reaches the model as fast as later turns

Code: `interfaces/server/prewarm.py`; `operations/worker/runner.py` `_warm_providers`; `interfaces/cli/commands/providers/parser.py`; `interfaces/cli/application.py` `_needs_first_run_setup`; `models/auth/store.py` `stored_providers`; `programs/tools/web/pdf.py` `_tool_check_fn`. Tests: `tests/component/core/test_startup_latency.py`.

## What was measured

A fresh trial Worker per round (`OPENPROGRAM_TRIAL=1`, own data directory, port and fake model), the code before and after the change in alternating rounds. Load average was 50 to 115, so single runs vary twofold; figures are medians of 12 TUI starts and 5 Worker starts per tree.

| Path | Before | After | Cause |
| --- | --- | --- | --- |
| Building the CLI parser | 0.2–0.27 s with a cached catalogue; 2.6 s without one | 0.09 s | `providers/parser.py` imported `providers/keys.py` for its arguments, which imported `models.listing` and read the models.dev catalogue, fetching it when no cache existed. Every command builds the whole parser |
| First-run check | Read the catalogue for every known provider | Reads only the stored key files | `store.get` resolves company aliases through the catalogue; the check now asks `store.stored` for the providers that have a file |
| TUI: CLI to Node spawn, Worker running | 2.2 s | 1.2 s | The two items above |
| TUI first frame, Worker running | 3.2 s | 2.0 s | Node and Ink rendering after the spawn is unchanged |
| First turn after Worker start, to the model request | 3.9 s; 1.14 s in a quieter run | 0.84 s; 0.76 s | One-time imports on the turn: the OpenAI SDK 0.44 s and its resources 0.19 s, AppKit, ApplicationServices and ScreenCaptureKit 0.12 s for the computer-use check, pypdf 0.07 s for the PDF tool's availability check. Sampling the turn: 0.95 s busy before, 0.24 s after |
| Later turns | 0.14–0.4 s | unchanged | The bound for the first turn |

## Background warm-up

After the providers are initialised, `_warm_providers` runs `prewarm.run` on the same background thread. Each step is independent; one that raises is skipped and the Worker log gets one line with each step's time.

| Step | What it loads |
| --- | --- |
| `provider_sdks` | The SDK of each model in use: the default conversation model and every Agent's pinned model, mapped by API (`openai`, `anthropic`, `google.genai`) with their resource modules. SDKs of providers no one uses stay unloaded |
| `turn_modules` | The turn runtime and the computer-use provider registry |
| `computer_use_frameworks` | The macOS accessibility and capture frameworks, only where computer use reports itself available |

The PDF tool's availability check uses `importlib.util.find_spec` instead of importing pypdf, so tool listing no longer loads it at all.

## Why this shape

| Peer | What it does, and what we take |
| --- | --- |
| oh-my-pi | `connection-prewarm.ts` opens a connection to the provider while the user types. It does not carry over: each turn runs on its own asyncio loop (`loop_runner.run_loop_blocking`), so a pooled connection cannot outlive the loop that opened it. What does carry over is warming the work the first request pays once, which here is imports |
| Hermes | `_startup_fast.py` keeps the CLI's import path short. Same rule for our parser: a command's startup imports only what that command needs |
| OpenCode, Grok Build leader | A resident process shared by clients. OpenProgram's Worker already is one; the remaining cost was its first turn |

## Implementation status

Not changed: in a development checkout the Worker's start also runs `git rev-parse` for the revision and scans the docs tree for its newest file; an installed runtime does neither. Node and Ink start-up inside the TUI was not profiled.
