Raven icon indicating copy to clipboard operation
Raven copied to clipboard

perf(cli): eager litellm import dominates CLI cold-start

Open 0xKT opened this issue 4 months ago • 0 comments

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 everos in isolation is ~40ms and only runs when a memory call executes).

Where litellm is imported eagerly

Two sites load litellm on every invocation:

  1. raven/cli/commands.py:50 - a top-level import litellm, used only to set litellm.suppress_debug_info = True.
  2. raven/cli/deep_research_commands.py:19 imports from raven.agent.tools.deep_research, which makes raven/agent/__init__.py eagerly construct AgentLoop -> 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.py to the code path that actually calls the model (mirroring the existing lazy pattern at provider_commands.py:109).
  • Make raven/agent/__init__.py expose its re-exports lazily (PEP 562 __getattr__) so importing a submodule no longer constructs AgentLoop.
  • Expected: ~2.3s -> ~0.4s for every non-agent command.

Phase 2 (polish):

  • Lazy subcommand-group loading so raven X imports only module X, and raven --version/--help import 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 --version and 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.

0xKT avatar Jul 17 '26 03:07 0xKT