ReqLLM.Keys (ReqLLM v1.0.0-rc.8)
View SourceHandles API key lookup with the following precedence:
- :api_keyoption (per-request override)
- Application.get_env(:req_llm, config_key)
- System.get_env(env_var)(dotenvy loads .env at startup)
Happy path: works with ReqLLM.Model structs and provider atoms.
Uses each provider's default_env_key/0 callback when available.
Examples
# From .env file (recommended)
# ANTHROPIC_API_KEY=sk-ant-...
ReqLLM.Keys.get(:anthropic, [])
# From application config
Application.put_env(:req_llm, :anthropic_api_key, "sk-ant-...")
ReqLLM.Keys.get(:anthropic, [])
# Per-request override
ReqLLM.Keys.get(:anthropic, api_key: "sk-ant-...")
# Works with models (extracts provider automatically)
model = ReqLLM.Model.from("anthropic:claude-3-sonnet")
key = ReqLLM.Keys.get!(model)
# Debug key source
{:ok, key, source} = ReqLLM.Keys.get(model)
Logger.debug("Using key from #{source}")Summary
Functions
Returns the application config key for a provider.
Returns the expected environment variable name for a provider.
See ReqLLM.Keys.get/2.
See ReqLLM.Keys.get!/2.
Retrieves API key for a provider/model with source information.
Retrieves API key for a provider/model, raising on failure.
Types
Functions
Returns the application config key for a provider.
Examples
iex> ReqLLM.Keys.config_key(:anthropic)
:anthropic_api_keyReturns the expected environment variable name for a provider.
Uses the provider's default_env_key callback if available, otherwise uses the provider registry.
Examples
iex> ReqLLM.Keys.env_var_name(:anthropic)
"ANTHROPIC_API_KEY"See ReqLLM.Keys.get/2.
See ReqLLM.Keys.get!/2.
@spec get( ReqLLM.Model.t() | atom(), keyword() ) :: {:ok, String.t(), key_source()} | {:error, String.t()}
Retrieves API key for a provider/model with source information.
@spec get!( ReqLLM.Model.t() | atom(), keyword() ) :: String.t() | no_return()
Retrieves API key for a provider/model, raising on failure.