anthropic/config

Configuration management for the Anthropic client

This module defines the configuration structure and helpers for loading settings from explicit options or environment variables.

Types

A validated API key for Anthropic API authentication.

API keys must be non-empty after trimming whitespace. Use api_key() to create a validated ApiKey from user input, or api_key_unchecked() for trusted sources like environment variables.

pub opaque type ApiKey

Error when creating an ApiKey

pub type ApiKeyError {
  EmptyApiKey
}

Constructors

  • EmptyApiKey

    API key is empty or whitespace-only

Configuration for Anthropic client requests

pub type Config {
  Config(
    api_key: ApiKey,
    base_url: String,
    default_model: option.Option(String),
    timeout_ms: Int,
    max_retries: Int,
  )
}

Constructors

  • Config(
      api_key: ApiKey,
      base_url: String,
      default_model: option.Option(String),
      timeout_ms: Int,
      max_retries: Int,
    )

    Arguments

    api_key

    Validated API key used for authentication

    base_url

    Base URL for Anthropic API requests

    default_model

    Optional default model name

    timeout_ms

    Request timeout in milliseconds

    max_retries

    Number of retries for retryable errors

Optional configuration inputs used when loading configuration

pub type ConfigOptions {
  ConfigOptions(
    api_key: option.Option(String),
    base_url: option.Option(String),
    default_model: option.Option(String),
    timeout_ms: option.Option(Int),
    max_retries: option.Option(Int),
  )
}

Constructors

  • ConfigOptions(
      api_key: option.Option(String),
      base_url: option.Option(String),
      default_model: option.Option(String),
      timeout_ms: option.Option(Int),
      max_retries: option.Option(Int),
    )

    Arguments

    api_key

    Explicit API key (takes precedence over environment variables)

    base_url

    Custom API base URL

    default_model

    Default model to use for requests

    timeout_ms

    Request timeout in milliseconds

    max_retries

    Retry count for transient errors

Values

pub fn api_key(raw: String) -> Result(ApiKey, ApiKeyError)

Create a validated API key.

Returns Ok(ApiKey) if the key is non-empty after trimming whitespace.

Examples

api_key("sk-ant-...")  // Ok(ApiKey)
api_key("")            // Error(EmptyApiKey)
api_key("   ")         // Error(EmptyApiKey)
pub fn api_key_error_to_string(error: ApiKeyError) -> String

Convert an ApiKeyError to a human-readable string.

pub fn api_key_to_string(key: ApiKey) -> String

Get the raw string value from an ApiKey.

Use this when you need to include the key in HTTP headers.

pub fn api_key_unchecked(raw: String) -> ApiKey

Create an ApiKey without validation.

Use this only when you trust the input, such as:

  • Values from environment variables (already validated during load)
  • Values from secure configuration systems

For user input or untrusted sources, use api_key() instead.

pub fn config_options() -> ConfigOptions

Create empty configuration options

pub const default_base_url: String

Default Anthropic API base URL

pub const default_max_retries: Int

Default retry count for transient failures

pub const default_timeout_ms: Int

Default request timeout in milliseconds

pub fn load_config(
  options: ConfigOptions,
) -> Result(Config, error.AnthropicError)

Load configuration using explicit options first, then environment variables.

Sources of configuration (in order of precedence):

  1. Explicit options passed to the client
  2. Environment variables (ANTHROPIC_API_KEY)
pub fn with_api_key(
  options: ConfigOptions,
  api_key: String,
) -> ConfigOptions

Set an explicit API key on configuration options

pub fn with_base_url(
  options: ConfigOptions,
  base_url: String,
) -> ConfigOptions

Set a custom base URL on configuration options

pub fn with_default_model(
  options: ConfigOptions,
  default_model: String,
) -> ConfigOptions

Set a default model on configuration options

pub fn with_max_retries(
  options: ConfigOptions,
  max_retries: Int,
) -> ConfigOptions

Set a retry override on configuration options

pub fn with_timeout_ms(
  options: ConfigOptions,
  timeout_ms: Int,
) -> ConfigOptions

Set a timeout override on configuration options

Search Document