simonw / simonw/llm

Avoid top-level imports in `cli.py`

Open
#848 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
12.5k
Forks
998
Avg merge
3d 13h
Merged PRs (30d)
10

Description

Outputing the help takes about one second on my system:

$ time llm --help > /dev/null
llm --help > /dev/null  0.82s user 0.10s system 99% cpu 0.920 total

That's because cli.py is importing a lot of libraries at the top-level. I propose moving the imports in that file to the functions where they are needed. It may not look asthetic, but it will speed up the call to --help by many orders of magnitudes, and probably somewhat speed up the warm up time when calling subcommands.

Let me know if I can help out with a PR. (It's probably also something that an LLM would be good at.)

Edit: I'm seeing that __init__.py is also affected, and my suggestions probably doesn't apply there ...

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading cli.py and measuring the existing llm --help timing shown in the issue. Move imports that are only needed by specific functions into those functions, while checking the note about init.py separately. Done means help loads substantially faster without changing command behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
cli, performance
Issue type
Refactor
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.