perf(cli): eager litellm import dominates CLI cold-start
Problem
Every raven command has a slow cold start (~2-3s), even trivial ones like
raven --version that print one line and exit. The time is almost entirely
import cost, not command work.
Root cause
Importing the CLI entry module (raven.cli.commands) eagerly imports litellm
at module top level, and that single dependency accounts for ~85% of startup.
litellm transitively pulls in the OpenAI SDK, httpx/httpcore, aiohttp,
pydantic, and tiktoken.
Measured (warm cache, direct .venv/bin/raven):
| command | cold real |
|---|---|
raven --version |
~2.4s |
raven --help |
~2.4s |
raven status --help |
~2.5s |
raven <group> --help |
~2.2-2.5s |
import raven.cli.commands: ~2.6s cumulative;litellm~2.3s of that (~85%).- Import floor with litellm excluded: ~0.4s.
- Raven's own code contributes ~0.1s; the remainder is dependencies.
- Ruled out: the everos memory substrate is NOT on the startup path. Its imports
are correctly deferred to call time (
import everosin isolation is ~40ms and only runs when a memory call executes).
Where litellm is imported eagerly
Two sites load litellm on every invocation:
raven/cli/commands.py:50- a top-levelimport litellm, used only to setlitellm.suppress_debug_info = True.raven/cli/deep_research_commands.py:19imports fromraven.agent.tools.deep_research, which makesraven/agent/__init__.pyeagerly constructAgentLoop-> litellm. This fires because Typer registers all subcommand modules at app-construction time. Removing site 1 alone is not enough; this chain still loads litellm.
Among the subcommand modules, only deep_research_commands pulls litellm; the
others (agent, provider, status, doctor, cron, ...) are litellm-free and load
providers lazily at runtime.
Proposed work
Phase 1 (biggest win, low risk):
- Defer the top-level litellm import in
commands.pyto the code path that actually calls the model (mirroring the existing lazy pattern atprovider_commands.py:109). - Make
raven/agent/__init__.pyexpose its re-exports lazily (PEP 562__getattr__) so importing a submodule no longer constructsAgentLoop. - Expected: ~2.3s -> ~0.4s for every non-agent command.
Phase 2 (polish):
- Lazy subcommand-group loading so
raven Ximports only module X, andraven --version/--helpimport almost nothing. - Expected:
--version/--help-> ~0.1s.
Relation to #128
#128 (TUI render delay) shares this litellm root cause for part of its delay, but the TUI render path has additional, TUI-specific costs tracked there. This issue covers general CLI-command cold start; #128 covers the TUI render.
Acceptance criteria
raven --versionand other non-agent commands start in well under 1s.- litellm is not imported unless a command actually needs a model.
- Existing CLI behavior and tests are unchanged.