ReqLLM.Keys (ReqLLM v1.0.0-rc.8)

View Source

Handles API key lookup with the following precedence:

  1. :api_key option (per-request override)
  2. Application.get_env(:req_llm, config_key)
  3. 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.

Retrieves API key for a provider/model with source information.

Retrieves API key for a provider/model, raising on failure.

Types

key_source()

@type key_source() :: :option | :application | :system

Functions

config_key(provider)

@spec config_key(atom()) :: atom()

Returns the application config key for a provider.

Examples

iex> ReqLLM.Keys.config_key(:anthropic)
:anthropic_api_key

env_var_name(provider)

@spec env_var_name(atom()) :: String.t()

Returns 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"

fetch(provider_id, opts \\ [])

This function is deprecated. Use ReqLLM.Keys.get/2 instead.

See ReqLLM.Keys.get/2.

fetch!(provider_id, opts \\ [])

This function is deprecated. Use ReqLLM.Keys.get!/2 instead.

See ReqLLM.Keys.get!/2.

get(provider, opts)

@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.

get!(provider_or_model, opts \\ [])

@spec get!(
  ReqLLM.Model.t() | atom(),
  keyword()
) :: String.t() | no_return()

Retrieves API key for a provider/model, raising on failure.