Braintrust.Config (Braintrust v0.3.0)

View Source

Configuration management for the Braintrust SDK.

Configuration Sources (in order of precedence)

  1. Runtime options passed to functions
  2. Process dictionary (via Braintrust.configure/1)
  3. Application config (via config :braintrust, ...)
  4. Environment variables

Configuration Options

OptionTypeDefaultDescription
:api_keystringBRAINTRUST_API_KEY envAPI key for authentication
:base_urlstringBRAINTRUST_API_URL env or https://api.braintrust.devBase URL for API
:timeoutinteger60_000Request timeout in ms
:max_retriesinteger2Maximum retry attempts

Examples

Environment Variable

export BRAINTRUST_API_KEY="sk-your-api-key"

Application Config

# config/config.exs
config :braintrust,
  api_key: System.get_env("BRAINTRUST_API_KEY"),
  timeout: 30_000

Runtime Config

# Configure for current process
Braintrust.configure(api_key: "sk-...")

# Or pass options directly to functions
Braintrust.Project.list(api_key: "sk-...")

Summary

Functions

Gets the API key, raising if not configured.

Clears runtime configuration for the current process.

Sets runtime configuration for the current process.

Gets a configuration value.

Validates an API key format.

Types

config_key()

@type config_key() :: :api_key | :base_url | :timeout | :max_retries

config_value()

@type config_value() :: String.t() | pos_integer()

Functions

api_key!(opts \\ [])

@spec api_key!(keyword()) :: String.t()

Gets the API key, raising if not configured.

Examples

iex> Braintrust.Config.api_key!(api_key: "sk-test123")
"sk-test123"

Raises

clear()

@spec clear() :: :ok

Clears runtime configuration for the current process.

Examples

iex> Braintrust.Config.configure(api_key: "sk-test")
:ok
iex> Braintrust.Config.clear()
:ok
iex> Braintrust.Config.get(:api_key)
nil

configure(opts)

@spec configure(keyword()) :: :ok

Sets runtime configuration for the current process.

Configuration set this way takes precedence over application config and environment variables, but is overridden by options passed directly to functions.

Examples

iex> Braintrust.Config.configure(api_key: "sk-test", timeout: 30_000)
:ok
iex> Braintrust.Config.get(:api_key)
"sk-test"
iex> Braintrust.Config.get(:timeout)
30000

get(key, opts \\ [])

@spec get(
  config_key(),
  keyword()
) :: config_value() | nil

Gets a configuration value.

Looks up configuration in order:

  1. Runtime options (if provided)
  2. Process dictionary
  3. Application config
  4. Environment variable (for :api_key and :base_url)
  5. Default value

Examples

iex> Braintrust.Config.get(:base_url)
"https://api.braintrust.dev"

iex> Braintrust.Config.get(:timeout)
60000

iex> Braintrust.Config.get(:base_url, base_url: "https://custom.api.com")
"https://custom.api.com"

valid_api_key?(arg1)

@spec valid_api_key?(String.t() | nil) :: boolean()

Validates an API key format.

Braintrust API keys use two prefixes:

  • sk- for user API keys
  • bt-st- for service tokens

Examples

iex> Braintrust.Config.valid_api_key?("sk-abc123")
true

iex> Braintrust.Config.valid_api_key?("bt-st-xyz789")
true

iex> Braintrust.Config.valid_api_key?("invalid-key")
false

iex> Braintrust.Config.valid_api_key?(nil)
false