llm CLI in 30 minutes
TL;DR: Simon Willison's llm CLI turns any large language model into a
composable Unix tool you can pipe, script, and template from the terminal.
Summary#
-
llmCLI brings large language models to the terminal, eliminating context switches and enabling integration with Unix tools. -
Official resources:
- Project: github.com/simonw/llm
- Documentation: llm.datasette.io
Installation#
llmis a Python package typically installed in a virtual environment- Install with
uvin the current virtual environment: - Benefits of using
uv tool install:- Isolated environments for each tool
- No pollution of global Python packages
- Clean uninstall and updates
- Verify installation:
-
Alternative: Run without installing using
uvxto fetch and runllmon demand without managing an installation:- Convenient for testing, but trades cold-start time for zero setup overhead
-
Quick test:
Getting Help#
-
Every subcommand has a built-in help page:
# Show main help. > llm --help Usage: llm [OPTIONS] COMMAND [ARGS]... Access Large Language Models from the command-line Options: --version Show the version and exit. -h, --help Show this message and exit. Commands: prompt* Execute a prompt aliases Manage model aliases chat Hold an ongoing chat with a model. collections View and manage collections of embeddings embed Embed text and store or return the result embed-models Manage available embedding models embed-multi Store embeddings for multiple strings at once in the... fragments Manage fragments that are stored in the database install Install packages from PyPI into the same environment as LLM keys Manage stored API keys for different models logs Tools for exploring logged prompts and responses models Manage available models ollama Commands for working with models hosted on Ollama server. openai Commands for working directly with the OpenAI API openrouter Commands relating to the llm-openrouter plugin plugins List installed plugins schemas Manage stored schemas similar Return top N similar IDs from a collection using cosine... templates Manage stored prompt templates tools Manage tools that can be made available to LLMs uninstall Uninstall Python packages from the LLM environment -
Subcommands support
--helpfor detailed information:
Managing API Keys#
Using environment variables
- Pass API keys through environment variables such as:
OPENAI_API_KEYANTHROPIC_KEYOPENROUTER_KEY
- Note that
llmusesOPENROUTER_KEY, while usually this is variable is calledOPENROUTER_API_KEY
Using the keys command
- API keys are stored securely and required for cloud-based models (OpenAI, Anthropic, etc.)
- The
keyscommand manages them: - Set keys for any provider:
- Keys are stored in the LLM database (with location varying by OS) and are never logged or cached insecurely
- Once set, models from that provider work automatically:
Working with Models#
- List available models grouped by provider:
- Key features:
- Aliases: Model names like
gpt-4ocan be called as4ofor shorter commands - Default model: The last line shows which model runs when not specified
- Plugin-provided models: Anthropic, Gemini, Ollama available after
installing plugins (e.g.,
llm install llm-anthropic)
- Aliases: Model names like
- Change the default model:
- Common flags:
-m <model>: Select the model for this invocation-s <system>: Provide a system prompt-c: Continue the previous conversation--no-stream: Disable token streaming, print all at once-o <key>=<value>: Pass model-specific options (temperature, max_tokens, etc.)--json: Output response as JSON for programmatic parsing
Chat Mode: Multi-turn Conversations#
- Use
llm chatfor back-and-forth dialogue instead of one-off prompts: - Chat maintains conversation history across turns, so context carries forward automatically
- Pick a model or set a system prompt for the conversation:
- Chat sessions are logged to the database for later review:
Plugins to Unlock More Models#
- Plugins expand
llmwith support for additional model providers - View installed plugins:
-
Install plugins for other providers:
-
After installation, new models appear in
llm models: -
Use plugin models like any other:
Local Models with Ollama#
- Run open-source models locally without API keys or costs using Ollama:
- In another terminal, use the model via
llm: - Benefits of local models:
- No API costs: Run as many times as you want for free
- No rate limits: Process large batches without hitting API ceilings
- Privacy: Your prompts never leave your machine
- Offline: Works without internet connectivity
- Drawbacks of local models:
- Generally less capable than GPT-4o or Claude (but improving rapidly)
- Slower than models served by providers
Fragments: Reusable Prompt Pieces#
- Fragments are named chunks of text loaded from files, useful for giving the model reference context:
- Fragments support multiple sources:
- Local files: Load text directly from the filesystem
- URLs: Reference remote content without copying locally:
- Multiple fragments: Stack
-fflags to compare documents or provide multiple context pieces at once
Piping, Files, and Templates#
-
Because
llmreads from stdin, you can Unix-style pipe anything into it: -
The
teestep keeps the response on the terminal and also writes it to disk, preventing long output from being lost to scrollback
Templates#
-
Templates bundle a system prompt, default model, and parameters into a single reusable YAML file:
-
Use the template in any command, shell script, or Makefile:
-
Benefits:
- Standardize configurations across projects
- Reduce repetition in scripts and automation
- Share template definitions with team members
Logging and History#
- Every prompt and response is logged automatically to a local database for reproducibility and auditing:
- Retrieve a specific logged response:
- View responses in JSON format for parsing:
- Logging is valuable for:
- Auditing: Track what models were asked and when
- Cost analysis: See which models consume tokens
- Reproducibility: Retrieve exact prompts later
- Learning: Review patterns in what works well
Practical Recipes#
- Explain a diff before committing:
- Summarize a long log file:
- Run the same prompt against several models and compare:
- Start a continuing conversation:
- Batch process multiple items:
Advanced Piping Patterns#
- Combine
llmwithjqto extract structured data: - Parse CSV and enrich it with LLM analysis:
- Chain multiple models and compare outputs:
- Extract structured output and feed downstream:
- Monitor logs in real-time and trigger actions:
Cost Awareness#
- Cloud models charge per token; monitor your usage:
- Cheaper alternatives for common tasks:
- Summarization: Use
gpt-4o-minior Haiku instead of GPT-4o - Simple questions: Local Ollama models cost $0
- Batch processing: Use cheaper models, save GPT-4o for complex reasoning
- Prototyping: Test with OpenRouter's cheaper provider options
- Summarization: Use
- Track spending by monitoring regularly:
Why This Matters#
- The power of
llmlies in its Unix philosophy: it behaves like every other command-line tool- Rather than a chat interface,
llmbecomes infrastructure that composes withgrep,sed,jq, and custom scripts
- Rather than a chat interface,
- This shift unlocks:
- Automated workflows: Code review, log triage, document summarization
- Personal tools: Custom scripts that would be too tedious to build against an HTTP API
- Faster iteration: Go from "I wish a model could do this" to a working one-liner in minutes
- Integration ease: Pipe model output into your existing Unix toolchain
- For developers living in the terminal,
llmis the shortest path from idea to implementation