# `ReqLLM.Keys`
[🔗](https://github.com/agentjido/req_llm/blob/main/lib/req_llm/keys.ex#L1)

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)`

Under Mix, `.env` is loaded at startup; releases skip it unless
`config :req_llm, load_dotenv: true` is set.

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)
    {:ok, model} = ReqLLM.model("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}")

# `key_source`

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

# `config_key`

```elixir
@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`

```elixir
@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`

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

# `fetch!`

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

# `get`

```elixir
@spec get(LLMDB.Model.t() | atom(), keyword()) ::
  {:ok, String.t(), key_source()} | {:error, String.t()}
```

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

# `get!`

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

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

---

*Consult [api-reference.md](api-reference.md) for complete listing*
