Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:BerriAI Litellm Get Model Cost Map

From Leeroopedia
Attribute Value
Sources litellm/litellm_core_utils/get_model_cost_map.py
Domains Cost Tracking, Model Metadata, Configuration
Last Updated 2026-02-15 16:00 GMT

Overview

Fetches, validates, and loads the model cost and context window map from a remote URL with local fallback.

Description

This module is responsible for providing the model cost map -- a dictionary mapping model names to their pricing, context window sizes, and provider routes. It implements a resilient loading strategy:

  1. If LITELLM_LOCAL_MODEL_COST_MAP=True, it loads only from the bundled local backup file (model_prices_and_context_window_backup.json).
  2. Otherwise, it fetches the latest map from a remote GitHub-hosted URL via httpx.
  3. The fetched map is validated for integrity: it must be a non-empty dictionary, meet a minimum model count (MODEL_COST_MAP_MIN_MODEL_COUNT), and not have shrunk beyond a threshold ratio compared to the local backup.
  4. On any failure (network error, parse error, or validation failure), it falls back to the local backup.

The GetModelCostMap class caches only the backup model count (a single integer) to keep memory usage minimal. The full backup is only parsed when it must be returned as a fallback.

Usage

Import get_model_cost_map to retrieve the model pricing data at startup. This function is called during litellm.__init__ to populate the global model cost dictionary.

Code Reference

Source Location

litellm/litellm_core_utils/get_model_cost_map.py (194 lines)

Signature

class GetModelCostMap:
    _backup_model_count: int  # class-level cache

    @staticmethod
    def load_local_model_cost_map() -> dict

    @classmethod
    def _get_backup_model_count(cls) -> int

    @staticmethod
    def _check_is_valid_dict(fetched_map: dict) -> bool

    @classmethod
    def _check_model_count_not_reduced(
        cls,
        fetched_map: dict,
        backup_model_count: int,
        min_model_count: int = MODEL_COST_MAP_MIN_MODEL_COUNT,
        max_shrink_ratio: float = MODEL_COST_MAP_MAX_SHRINK_RATIO,
    ) -> bool

    @classmethod
    def validate_model_cost_map(
        cls,
        fetched_map: dict,
        backup_model_count: int,
        min_model_count: int = MODEL_COST_MAP_MIN_MODEL_COUNT,
        max_shrink_ratio: float = MODEL_COST_MAP_MAX_SHRINK_RATIO,
    ) -> bool

    @staticmethod
    def fetch_remote_model_cost_map(url: str, timeout: int = 5) -> dict

def get_model_cost_map(url: str) -> dict

Import

from litellm.litellm_core_utils.get_model_cost_map import get_model_cost_map, GetModelCostMap

I/O Contract

get_model_cost_map

Direction Name Type Description
Input url str Remote URL for the model cost JSON file
Output return dict Dictionary mapping model names to cost/context metadata

GetModelCostMap.validate_model_cost_map

Direction Name Type Description
Input fetched_map dict The fetched model cost map to validate
Input backup_model_count int Number of models in the local backup
Input min_model_count int Minimum acceptable model count
Input max_shrink_ratio float Maximum allowed shrink ratio vs. backup
Output return bool True if all validation checks pass

Usage Examples

from litellm.litellm_core_utils.get_model_cost_map import get_model_cost_map

# Fetch the model cost map (with automatic fallback)
url = "https://raw.githubusercontent.com/BerriAI/litellm/main/model_prices_and_context_window.json"
cost_map = get_model_cost_map(url)

# Access pricing for a specific model
gpt4_info = cost_map.get("gpt-4", {})
input_cost = gpt4_info.get("input_cost_per_token", 0)
output_cost = gpt4_info.get("output_cost_per_token", 0)

# Force local-only loading via environment variable
# export LITELLM_LOCAL_MODEL_COST_MAP=True

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment