LLM ships a set of utility functions designed specifically for plugin authors. These cover the most common needs: looking up API keys stored by the user, finding the right place to persist plugin data, raising user-friendly errors, and generating fake response objects for tests. This page documents each utility with its signature, parameters, and a practical example.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/simonw/LLM/llms.txt
Use this file to discover all available pages before exploring further.
llm.get_key()
Retrieves an API key or other secret from LLM’s key registry. Returns the key as a string, orNone if it cannot be resolved.
Falling back to an environment variable
Passenv= to check an environment variable if the key isn’t in the registry:
Accepting user-supplied input
When your plugin lets users type a key directly (rather than pre-configuring it), pass the raw input asinput=. If the input matches a stored alias it is looked up; otherwise it is used verbatim:
inputvalue, if it matches a key alias inkeys.jsoninputvalue verbatim, if it doesn’t match an alias- Stored key for
alias, if configured - Environment variable named by
env None
An older positional-argument calling convention is still supported for backwards compatibility, but the keyword argument form shown here is strongly preferred.
llm.user_dir()
Returns the path to LLM’s configuration directory as apathlib.Path object, creating the directory if it doesn’t already exist.
On macOS this is ~/Library/Application Support/io.datasette.llm. The exact location varies by operating system.
Plugins should store their own data in a subdirectory of this directory:
llm.ModelError
Raisellm.ModelError from inside execute() to surface a clean error message to the user without a Python traceback. LLM’s CLI layer catches this exception and displays the message directly:
ModelError for expected failure conditions — missing dependencies, invalid configuration, quota exceeded — where a stack trace would be confusing rather than helpful.
AsyncResponse.fake()
Creates a pre-builtAsyncResponse object for use in tests. This is useful when testing code that consumes async responses, such as conversation history builders.
The signature is:
_done set to True and its _chunks pre-populated with the provided response string, so it behaves like a completed async response without any real model invocation.