Skip to content

cache

Class info

🛈 DocStrings

In-process cache of the model lists providers return.

A provider's response is cached on disk by the HTTP layer (MODELS_CACHE_TTL), but parsing it is the expensive part: models.dev lists about 10k models, a few hundred ms to parse. The parsed, unfiltered list is kept here for the same time, per provider and request headers (a different API key may see different models), so repeated discovery costs only the filtering.

MODELS_CACHE_TTL module-attribute

MODELS_CACHE_TTL = 86400

Seconds a provider's model list is reused, on disk and in memory (24 hours).

clear_models_cache

clear_models_cache() -> None

Drop the parsed model lists, so the next discovery parses again.

Source code in src/tokonomics/model_discovery/cache.py
def clear_models_cache() -> None:
    """Drop the parsed model lists, so the next discovery parses again."""
    _parsed.clear()

get_cached

get_cached(name: str, provider: ModelProvider) -> list[ModelInfo] | None

The provider's parsed model list, if fetched within its models_cache_ttl.

Source code in src/tokonomics/model_discovery/cache.py
def get_cached(name: str, provider: ModelProvider) -> list[ModelInfo] | None:
    """The provider's parsed model list, if fetched within its ``models_cache_ttl``."""
    entry = _parsed.get(_key(name, provider))
    if entry is None or time.monotonic() - entry[0] > provider.models_cache_ttl:
        return None
    return entry[1]

store

store(name: str, provider: ModelProvider, models: list[ModelInfo]) -> None

Keep the provider's parsed model list (an empty one is not kept: a failed fetch).

Source code in src/tokonomics/model_discovery/cache.py
def store(name: str, provider: ModelProvider, models: list[ModelInfo]) -> None:
    """Keep the provider's parsed model list (an empty one is not kept: a failed fetch)."""
    if models:
        _parsed[_key(name, provider)] = (time.monotonic(), models)