Config Module

Configuration management utilities for LLM applications.

This module provides comprehensive tools for managing LLM-specific configuration across different providers and models.

Usage:

# Common imports - top-level
from kerb.config import (
    Config,              # Alias for ConfigManager
    ConfigManager,       # Main configuration manager
    load_config,         # Load configuration from file
    save_config,         # Save configuration to file
)

# Providers - specialized
from kerb.config.providers import (
    get_openai_config,
    get_anthropic_config,
    get_google_config,
    validate_credentials,
)

Features:

  • Centralized LLM configuration management

  • Multi-provider support (OpenAI, Anthropic, Google, Azure, HuggingFace)

  • Secure API key management with environment variables

  • Encrypted secrets storage for prototyping/development

  • Model configuration with full parameter control (temperature, max_tokens, etc.)

  • Provider switching utilities

  • Configuration validation

  • File-based and environment variable loading

  • Change listeners for reactive updates

  • Configuration history and rollback

kerb.config.Config

alias of ConfigManager

class kerb.config.ConfigManager(app_name='llm_app', config_file=None, auto_load_env=True, encryption_key=None, encryption_salt=None)[source]

Bases: object

Centralized configuration manager for LLM applications.

Manages model configs, provider settings, and API keys in a unified way. Focuses on LLM-specific configuration without general application settings.

__init__(app_name='llm_app', config_file=None, auto_load_env=True, encryption_key=None, encryption_salt=None)[source]

Initialize configuration manager.

Parameters:
  • app_name (str) – Application name

  • config_file (Optional[str]) – Path to configuration file

  • auto_load_env (bool) – Automatically load from environment variables

  • encryption_key (Optional[str]) – Optional encryption key for secrets (auto-generated if None)

  • encryption_salt (Optional[bytes]) – Optional salt for key derivation (auto-generated if None)

get_config()[source]

Get current application configuration.

Return type:

AppConfig

set_config(config)[source]

Set application configuration and notify listeners.

Return type:

None

add_change_listener(listener)[source]

Add a listener for configuration changes.

Return type:

None

add_model(config)[source]

Add or update a model configuration.

Return type:

None

get_model(name)[source]

Get model configuration by name.

Return type:

Optional[ModelConfig]

remove_model(name)[source]

Remove a model configuration.

Return type:

bool

list_models(provider=None)[source]

List all configured models, optionally filtered by provider.

Return type:

List[str]

set_default_model(name)[source]

Set the default model.

Return type:

None

get_default_model()[source]

Get the default model configuration.

Return type:

Optional[ModelConfig]

add_provider(config)[source]

Add or update a provider configuration.

Return type:

None

get_provider(provider)[source]

Get provider configuration.

Return type:

Optional[ProviderConfig]

remove_provider(provider)[source]

Remove a provider configuration.

Return type:

bool

list_providers()[source]

List all configured providers.

Return type:

List[ProviderType]

switch_provider(from_provider, to_provider, model_mapping=None)[source]

Switch from one provider to another.

Parameters:
Return type:

None

set_api_key(provider, api_key=None, env_var=None)[source]

Set API key for a provider.

Parameters:
  • provider (ProviderType) – Provider type

  • api_key (Optional[str]) – Direct API key (not recommended for production)

  • env_var (Optional[str]) – Environment variable name containing API key (recommended)

Return type:

None

get_api_key(provider)[source]

Get API key for a provider (resolves env vars).

Return type:

Optional[str]

validate_api_keys()[source]

Validate that all configured providers have API keys.

Return type:

Dict[ProviderType, bool]

Returns:

Dictionary mapping providers to validation status

load_from_file(file_path)[source]

Load configuration from JSON file.

Parameters:

file_path (str) – Path to configuration file

Return type:

None

save_to_file(file_path, include_secrets=False)[source]

Save configuration to JSON file.

Parameters:
  • file_path (str) – Path to save configuration

  • include_secrets (bool) – Include API keys in export (not recommended)

Return type:

None

load_from_environment()[source]

Load configuration from environment variables.

Looks for variables in the format: - {APP_NAME}_MODEL_CONFIG__{MODEL_NAME}__{PARAM} - {APP_NAME}_PROVIDER__{PROVIDER}__{PARAM} - {APP_NAME}__{PARAM}

Return type:

None

export_environment_vars()[source]

Export configuration as environment variables.

Return type:

Dict[str, str]

Returns:

Dictionary of environment variable names to values

validate()[source]

Validate configuration and return list of issues.

Return type:

List[str]

Returns:

List of validation error messages (empty if valid)

is_valid()[source]

Check if configuration is valid.

Return type:

bool

set_secret(key, value)[source]

Store a secret value with encryption.

Secrets are encrypted in memory using Fernet symmetric encryption. While not as secure as dedicated secrets management services, this provides reasonable protection for prototyping and development.

Parameters:
  • key (str) – Secret identifier

  • value (str) – Secret value to store

Return type:

None

get_secret(key)[source]

Retrieve and decrypt a secret value.

Parameters:

key (str) – Secret identifier

Return type:

Optional[str]

Returns:

Decrypted secret value or None if not found

remove_secret(key)[source]

Remove a secret and securely clear it from memory.

Parameters:

key (str) – Secret identifier

Return type:

bool

Returns:

True if secret was removed, False if not found

clear_secrets()[source]

Clear all secrets from memory securely.

Return type:

None

list_secret_keys()[source]

List all secret keys (not values).

Return type:

List[str]

Returns:

List of secret identifiers

has_secret(key)[source]

Check if a secret exists.

Parameters:

key (str) – Secret identifier

Return type:

bool

Returns:

True if secret exists

reset()[source]

Reset configuration to initial state and clear secrets.

Return type:

None

rollback()[source]

Rollback to previous configuration.

Return type:

bool

Returns:

True if rollback successful, False if no history

merge_config(other, override=True)[source]

Merge another configuration into current.

Parameters:
  • other (AppConfig) – Configuration to merge

  • override (bool) – Whether to override existing values

Return type:

None

get_model_for_task(task, fallback=None)[source]

Get recommended model for a specific task.

Parameters:
  • task (str) – Task type (e.g., “completion”, “embedding”, “chat”)

  • fallback (Optional[str]) – Fallback model name if no match found

Return type:

Optional[ModelConfig]

Returns:

Model configuration or None

clone()[source]

Create a deep copy of the configuration manager.

Note: Secrets are not cloned for security reasons.

Return type:

ConfigManager

kerb.config.load_config(file_path)[source]

Load configuration from a JSON file.

Parameters:

file_path (str) – Path to configuration file

Return type:

AppConfig

Returns:

AppConfig instance

kerb.config.save_config(config, file_path, include_secrets=False)[source]

Save configuration to a JSON file.

Parameters:
  • config (AppConfig) – Configuration to save

  • file_path (str) – Path to save file

  • include_secrets (bool) – Include sensitive data (not recommended)

Return type:

None

class kerb.config.ConfigSource(*values)[source]

Bases: Enum

Configuration source types.

ENVIRONMENT = 'environment'
FILE = 'file'
CODE = 'code'
DEFAULT = 'default'
class kerb.config.ProviderType(*values)[source]

Bases: Enum

LLM provider types.

OPENAI = 'openai'
ANTHROPIC = 'anthropic'
GOOGLE = 'google'
AZURE_OPENAI = 'azure_openai'
HUGGINGFACE = 'huggingface'
CUSTOM = 'custom'
class kerb.config.AppConfig(app_name, default_model=None, providers=<factory>, models=<factory>, metadata=<factory>)[source]

Bases: object

Complete application configuration.

app_name

Application name

default_model

Default model to use

providers

Provider configurations

models

Model configurations

metadata

Additional application metadata

app_name: str
default_model: str | None = None
providers: Dict[ProviderType, ProviderConfig]
models: Dict[str, ModelConfig]
metadata: Dict[str, Any]
to_dict()[source]

Convert to dictionary.

Return type:

Dict[str, Any]

classmethod from_dict(data)[source]

Create from dictionary.

Return type:

AppConfig

__init__(app_name, default_model=None, providers=<factory>, models=<factory>, metadata=<factory>)
class kerb.config.ModelConfig(name, provider, api_key_env_var=None, max_tokens=4096, temperature=0.7, top_p=1.0, frequency_penalty=0.0, presence_penalty=0.0, endpoint=None, api_version=None, deployment_name=None, timeout=60.0, max_retries=3, metadata=<factory>)[source]

Bases: object

Configuration for a specific model.

name

Model identifier (e.g., “gpt-4o”, “claude-3-5-sonnet-20241022”)

provider

LLM provider

api_key_env_var

Environment variable name for API key

max_tokens

Maximum tokens for this model

temperature

Default temperature (0.0 to 2.0)

top_p

Default top_p sampling parameter

frequency_penalty

Frequency penalty for repetition

presence_penalty

Presence penalty for topic diversity

endpoint

Custom API endpoint (optional)

api_version

API version for provider (e.g., Azure)

deployment_name

Deployment name (for Azure)

timeout

Request timeout in seconds

max_retries

Maximum retry attempts

metadata

Additional metadata

name: str
provider: ProviderType
api_key_env_var: str | None = None
max_tokens: int = 4096
temperature: float = 0.7
top_p: float = 1.0
frequency_penalty: float = 0.0
presence_penalty: float = 0.0
endpoint: str | None = None
api_version: str | None = None
deployment_name: str | None = None
timeout: float = 60.0
max_retries: int = 3
metadata: Dict[str, Any]
to_dict()[source]

Convert to dictionary, handling enum serialization.

Return type:

Dict[str, Any]

classmethod from_dict(data)[source]

Create from dictionary, handling enum deserialization.

Return type:

ModelConfig

__init__(name, provider, api_key_env_var=None, max_tokens=4096, temperature=0.7, top_p=1.0, frequency_penalty=0.0, presence_penalty=0.0, endpoint=None, api_version=None, deployment_name=None, timeout=60.0, max_retries=3, metadata=<factory>)
class kerb.config.ProviderConfig(provider, api_key=None, api_key_env_var=None, base_url=None, organization=None, timeout=60.0, max_retries=3, rate_limit=None, models=<factory>, metadata=<factory>)[source]

Bases: object

Configuration for an LLM provider.

provider

Provider type

api_key

API key (prefer api_key_env_var for security)

api_key_env_var

Environment variable containing API key

base_url

Base URL for API endpoint

organization

Organization ID (for OpenAI)

timeout

Default timeout in seconds

max_retries

Default max retry attempts

rate_limit

Rate limit (requests per minute)

models

Available models for this provider

metadata

Additional provider metadata

provider: ProviderType
api_key: str | None = None
api_key_env_var: str | None = None
base_url: str | None = None
organization: str | None = None
timeout: float = 60.0
max_retries: int = 3
rate_limit: int | None = None
models: List[str]
metadata: Dict[str, Any]
get_api_key()[source]

Get API key from env var or direct value.

Priority: 1. Environment variable (if set and exists) 2. Direct API key value

Return type:

Optional[str]

to_dict()[source]

Convert to dictionary.

Return type:

Dict[str, Any]

classmethod from_dict(data)[source]

Create from dictionary.

Return type:

ProviderConfig

__init__(provider, api_key=None, api_key_env_var=None, base_url=None, organization=None, timeout=60.0, max_retries=3, rate_limit=None, models=<factory>, metadata=<factory>)
kerb.config.create_config_manager(app_name='llm_app', config_file=None, encryption_key=None)[source]

Create a new configuration manager.

Parameters:
  • app_name (str) – Application name

  • config_file (Optional[str]) – Optional configuration file to load

  • encryption_key (Optional[str]) – Optional encryption key for secrets

Return type:

ConfigManager

Returns:

Configured ConfigManager instance

kerb.config.create_model_config(name, provider, **kwargs)[source]

Create a model configuration.

Parameters:
  • name (str) – Model name

  • provider (Union[ProviderType, str]) – Provider type or string

  • **kwargs – Additional model configuration parameters

Return type:

ModelConfig

Returns:

ModelConfig instance

kerb.config.create_provider_config(provider, **kwargs)[source]

Create a provider configuration.

Parameters:
  • provider (Union[ProviderType, str]) – Provider type or string

  • **kwargs – Additional provider configuration parameters

Return type:

ProviderConfig

Returns:

ProviderConfig instance

kerb.config.get_openai_config(api_key_env_var='OPENAI_API_KEY')[source]

Get default OpenAI provider configuration.

Parameters:

api_key_env_var (str) – Environment variable name for API key

Return type:

ProviderConfig

Returns:

ProviderConfig for OpenAI

kerb.config.get_anthropic_config(api_key_env_var='ANTHROPIC_API_KEY')[source]

Get default Anthropic provider configuration.

Parameters:

api_key_env_var (str) – Environment variable name for API key

Return type:

ProviderConfig

Returns:

ProviderConfig for Anthropic

kerb.config.get_google_config(api_key_env_var='GOOGLE_API_KEY')[source]

Get default Google (Gemini) provider configuration.

Parameters:

api_key_env_var (str) – Environment variable name for API key

Return type:

ProviderConfig

Returns:

ProviderConfig for Google

kerb.config.validate_credentials(provider, api_key)[source]

Validate provider credentials (basic check).

Note: This is a basic validation. For production, implement actual API calls to verify credentials.

Parameters:
  • provider (ProviderType) – Provider type

  • api_key (str) – API key to validate

Return type:

bool

Returns:

True if credentials appear valid (basic check)

kerb.config.load_config_from_file(file_path)

Load configuration from a JSON file.

Parameters:

file_path (str) – Path to configuration file

Return type:

AppConfig

Returns:

AppConfig instance

kerb.config.save_config_to_file(config, file_path, include_secrets=False)

Save configuration to a JSON file.

Parameters:
  • config (AppConfig) – Configuration to save

  • file_path (str) – Path to save file

  • include_secrets (bool) – Include sensitive data (not recommended)

Return type:

None

kerb.config.get_default_openai_config(api_key_env_var='OPENAI_API_KEY')

Get default OpenAI provider configuration.

Parameters:

api_key_env_var (str) – Environment variable name for API key

Return type:

ProviderConfig

Returns:

ProviderConfig for OpenAI

kerb.config.get_default_anthropic_config(api_key_env_var='ANTHROPIC_API_KEY')

Get default Anthropic provider configuration.

Parameters:

api_key_env_var (str) – Environment variable name for API key

Return type:

ProviderConfig

Returns:

ProviderConfig for Anthropic

kerb.config.get_default_google_config(api_key_env_var='GOOGLE_API_KEY')

Get default Google (Gemini) provider configuration.

Parameters:

api_key_env_var (str) – Environment variable name for API key

Return type:

ProviderConfig

Returns:

ProviderConfig for Google

kerb.config.validate_provider_credentials(provider, api_key)

Validate provider credentials (basic check).

Note: This is a basic validation. For production, implement actual API calls to verify credentials.

Parameters:
  • provider (ProviderType) – Provider type

  • api_key (str) – API key to validate

Return type:

bool

Returns:

True if credentials appear valid (basic check)

Configuration management for models, providers, and application settings.