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

View Source

Simple API key lookup with configurable precedence.

Precedence (first match wins):

  1. :api_key option (per request)
  2. Application.get_env/2
  3. System.get_env/1
  4. JidoKeys.get/1 (includes ReqLLM.put_key)

Happy path: works with ReqLLM.Model structs and provider atoms. Uses each provider's default_env_key/0 callback when available.

Examples

# Recommended: Use ReqLLM.put_key
ReqLLM.put_key(:anthropic_api_key, "sk-ant-...")

# Works with models (extracts provider automatically)
model = ReqLLM.Model.from("anthropic:claude-3-sonnet")
key = ReqLLM.Keys.get!(model)

# Per-request override (highest priority)
key = ReqLLM.Keys.get!(model, api_key: "sk-override-...")

# Debug key source
{:ok, key, source} = ReqLLM.Keys.get(model)
Logger.debug("Using key from #{source}")

Configuration Options

# Recommended: ReqLLM.put_key (secure in-memory)
ReqLLM.put_key(:anthropic_api_key, "sk-ant-...")

# Alternative: Application config
Application.put_env(:req_llm, :anthropic_api_key, "sk-ant-...")

# Alternative: Environment variable
System.put_env("ANTHROPIC_API_KEY", "sk-ant-...")

# .env files are auto-loaded via JidoKeys

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 | :jido

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.