ccproxy.pricing¶
ccproxy.pricing
¶
Dynamic pricing system for Claude models.
This module provides dynamic pricing capabilities by downloading and caching pricing information from external sources like LiteLLM.
PricingCache
¶
Manages caching of model pricing data from external sources.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
settings
|
PricingSettings
|
Pricing configuration settings |
required |
Source code in ccproxy/pricing/cache.py
is_cache_valid
¶
Check if cached pricing data is still valid.
Returns:
Type | Description |
---|---|
bool
|
True if cache exists and is not expired |
Source code in ccproxy/pricing/cache.py
load_cached_data
¶
Load pricing data from cache.
Returns:
Type | Description |
---|---|
dict[str, Any] | None
|
Cached pricing data or None if cache is invalid/corrupted |
Source code in ccproxy/pricing/cache.py
download_pricing_data
async
¶
Download fresh pricing data from source URL.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
timeout
|
int | None
|
Request timeout in seconds (uses settings default if None) |
None
|
Returns:
Type | Description |
---|---|
dict[str, Any] | None
|
Downloaded pricing data or None if download failed |
Source code in ccproxy/pricing/cache.py
save_to_cache
¶
Save pricing data to cache.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
dict[str, Any]
|
Pricing data to cache |
required |
Returns:
Type | Description |
---|---|
bool
|
True if successfully saved, False otherwise |
Source code in ccproxy/pricing/cache.py
get_pricing_data
async
¶
Get pricing data, from cache if valid or by downloading fresh data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
force_refresh
|
bool
|
Force download even if cache is valid |
False
|
Returns:
Type | Description |
---|---|
dict[str, Any] | None
|
Pricing data or None if both cache and download fail |
Source code in ccproxy/pricing/cache.py
clear_cache
¶
Clear cached pricing data.
Returns:
Type | Description |
---|---|
bool
|
True if cache was cleared successfully |
Source code in ccproxy/pricing/cache.py
get_cache_info
¶
Get information about cache status.
Returns:
Type | Description |
---|---|
dict[str, Any]
|
Dictionary with cache information |
Source code in ccproxy/pricing/cache.py
PricingLoader
¶
Loads and converts pricing data from LiteLLM format to internal format.
extract_claude_models
staticmethod
¶
Extract Claude model entries from LiteLLM data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
litellm_data
|
dict[str, Any]
|
Raw LiteLLM pricing data |
required |
verbose
|
bool
|
Whether to log individual model discoveries |
True
|
Returns:
Type | Description |
---|---|
dict[str, Any]
|
Dictionary with only Claude models |
Source code in ccproxy/pricing/loader.py
convert_to_internal_format
staticmethod
¶
Convert LiteLLM pricing format to internal format.
LiteLLM format uses cost per token, we use cost per 1M tokens as Decimal.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
claude_models
|
dict[str, Any]
|
Claude models in LiteLLM format |
required |
verbose
|
bool
|
Whether to log individual model conversions |
True
|
Returns:
Type | Description |
---|---|
dict[str, dict[str, Decimal]]
|
Dictionary in internal pricing format |
Source code in ccproxy/pricing/loader.py
load_pricing_from_data
staticmethod
¶
Load and convert pricing data from LiteLLM format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
litellm_data
|
dict[str, Any]
|
Raw LiteLLM pricing data |
required |
verbose
|
bool
|
Whether to enable verbose logging |
True
|
Returns:
Type | Description |
---|---|
PricingData | None
|
Validated pricing data as PricingData model, or None if invalid |
Source code in ccproxy/pricing/loader.py
validate_pricing_data
staticmethod
¶
Validate pricing data using Pydantic models.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pricing_data
|
Any
|
Pricing data to validate (dict or PricingData) |
required |
verbose
|
bool
|
Whether to enable verbose logging |
True
|
Returns:
Type | Description |
---|---|
PricingData | None
|
Valid PricingData model or None if validation fails |
Source code in ccproxy/pricing/loader.py
get_model_aliases
staticmethod
¶
ModelPricing
¶
Bases: BaseModel
Pricing information for a single Claude model.
All costs are in USD per 1 million tokens.
convert_to_decimal
classmethod
¶
Convert numeric values to Decimal for precision.
Source code in ccproxy/pricing/models.py
PricingData
¶
Bases: RootModel[dict[str, ModelPricing]]
Complete pricing data for all Claude models.
This is a wrapper around a dictionary of model name to ModelPricing that provides dict-like access while maintaining type safety.
items
¶
keys
¶
values
¶
get
¶
model_names
¶
to_dict
¶
Convert to legacy dict format for backward compatibility.
Source code in ccproxy/pricing/models.py
from_dict
classmethod
¶
Create PricingData from legacy dict format.
Source code in ccproxy/pricing/models.py
PricingUpdater
¶
Manages periodic updates of pricing data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cache
|
PricingCache
|
Pricing cache instance |
required |
settings
|
PricingSettings
|
Pricing configuration settings |
required |
Source code in ccproxy/pricing/updater.py
get_current_pricing
async
¶
Get current pricing data with automatic updates.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
force_refresh
|
bool
|
Force refresh even if cache is valid |
False
|
Returns:
Type | Description |
---|---|
PricingData | None
|
Current pricing data as PricingData model |
Source code in ccproxy/pricing/updater.py
force_refresh
async
¶
Force a refresh of pricing data.
Returns:
Type | Description |
---|---|
bool
|
True if refresh was successful |
Source code in ccproxy/pricing/updater.py
clear_cache
¶
Clear all cached pricing data.
Returns:
Type | Description |
---|---|
bool
|
True if cache was cleared successfully |
Source code in ccproxy/pricing/updater.py
get_pricing_info
async
¶
Get information about current pricing state.
Returns:
Type | Description |
---|---|
dict[str, Any]
|
Dictionary with pricing information |
Source code in ccproxy/pricing/updater.py
validate_external_source
async
¶
Validate that external pricing source is accessible.
Returns:
Type | Description |
---|---|
bool
|
True if external source is accessible and has valid data |