Skip to content

ecologits.estimations

text_impacts(model_name, input_tokens, output_tokens, *, provider=None, cache_read_tokens=None, cache_write_tokens=None, datacenter_location=None, datacenter_pue=None, datacenter_wue=None, request_latency=None)

Estimate the environmental impacts of a text generation request.

Uses the same model repository and calculations as the provider integrations, without making an API request. Generation latency is estimated using registered throughput information when available, otherwise using the latency regression, and capped by request_latency when supplied. Input tokens and prompt caching are accepted and validated in preparation for future methodology; they do not affect estimates yet.

Parameters:

Name Type Description Default
model_name str

EcoLogits model ID: the developer's namespace and the model name, such as "openai/gpt-6-luna" or "zai/glm-5.2".

required
input_tokens int

Total input tokens, including tokens read from or written to the provider's prompt cache. Must be a non-negative integer. Currently does not affect estimates.

required
output_tokens int

Number of generated tokens, including reasoning tokens when applicable. Must be a non-negative integer.

required
provider str | None

Hosting service: "openai", "anthropic", "cohere", "google" or "mistralai". When omitted, the model developer's own API is used if registered. Hugging Face is not supported yet.

None
cache_read_tokens int | None

Input tokens whose computed state was reused from the provider's prompt cache. A subset of input_tokens. None means unknown or unreported; zero means no cache reads. Currently does not affect estimates.

None
cache_write_tokens int | None

Input tokens written to the provider's prompt cache by this request. A subset of input_tokens. None means unknown or unreported; zero means no cache writes. Currently does not affect estimates.

None
datacenter_location str | None

Electricity mix zone, using ISO 3166-1 alpha-3 codes or an aggregate zone such as "WOR". Uses the provider's default location when omitted or None.

None
datacenter_pue float | RangeValue | None

Power Usage Effectiveness of the datacenter. Values must be finite and at least 1. Uses the provider default when None.

None
datacenter_wue float | RangeValue | None

Water Usage Effectiveness of the datacenter in L/kWh. Values must be finite and at least 0. Uses the provider default when None.

None
request_latency float | None

Optional measured request latency in seconds. Improves precision when generation was faster than estimated: the estimated generation latency is capped by this value. Must be finite and positive. Estimates latency only when None.

None

Returns:

Type Description
ImpactsOutput

Total, usage, and embodied impacts with model and electricity mix warnings. Unknown models, providers, or zones

ImpactsOutput

return errors in the result.

Raises:

Type Description
ValueError

If a token count is not a non-negative integer, or the sum of known cache reads and writes exceeds input_tokens, or a datacenter PUE or WUE override is invalid, or request_latency is not a finite positive number.

Source code in ecologits/estimations/text.py
def text_impacts(
    model_name: str,
    input_tokens: int,
    output_tokens: int,
    *,
    provider: str | None = None,
    cache_read_tokens: int | None = None,
    cache_write_tokens: int | None = None,
    datacenter_location: str | None = None,
    datacenter_pue: float | RangeValue | None = None,
    datacenter_wue: float | RangeValue | None = None,
    request_latency: float | None = None,
) -> ImpactsOutput:
    """Estimate the environmental impacts of a text generation request.

    Uses the same model repository and calculations as the provider integrations, without making an API request.
    Generation latency is estimated using registered throughput information when available, otherwise using the latency
    regression, and capped by request_latency when supplied. Input tokens and prompt caching are accepted and validated
    in preparation for future methodology; they do not affect estimates yet.

    Args:
        model_name: EcoLogits model ID: the developer's namespace and the model name, such as "openai/gpt-6-luna" or
            "zai/glm-5.2".
        input_tokens: Total input tokens, including tokens read from or written to the provider's prompt cache. Must be
            a non-negative integer. Currently does not affect estimates.
        output_tokens: Number of generated tokens, including reasoning tokens when applicable. Must be a non-negative
            integer.
        provider: Hosting service: "openai", "anthropic", "cohere", "google" or "mistralai". When omitted, the model
            developer's own API is used if registered. Hugging Face is not supported yet.
        cache_read_tokens: Input tokens whose computed state was reused from the provider's prompt cache. A subset of
            input_tokens. None means unknown or unreported; zero means no cache reads. Currently does not affect
            estimates.
        cache_write_tokens: Input tokens written to the provider's prompt cache by this request. A subset of
            input_tokens. None means unknown or unreported; zero means no cache writes. Currently does not affect
            estimates.
        datacenter_location: Electricity mix zone, using ISO 3166-1 alpha-3 codes or an aggregate zone such as "WOR".
            Uses the provider's default location when omitted or None.
        datacenter_pue: Power Usage Effectiveness of the datacenter. Values must be finite and at least 1. Uses the
            provider default when None.
        datacenter_wue: Water Usage Effectiveness of the datacenter in L/kWh. Values must be finite and at least 0. Uses
            the provider default when None.
        request_latency: Optional measured request latency in seconds. Improves precision when generation was faster
            than estimated: the estimated generation latency is capped by this value. Must be finite and positive.
            Estimates latency only when None.

    Returns:
        Total, usage, and embodied impacts with model and electricity mix warnings. Unknown models, providers, or zones
        return errors in the result.

    Raises:
        ValueError: If a token count is not a non-negative integer, or the sum of known cache reads and writes exceeds
            input_tokens, or a datacenter PUE or WUE override is invalid, or request_latency is not a finite positive
            number.
    """
    _validate_token_count("input_tokens", input_tokens)
    _validate_token_count("output_tokens", output_tokens)
    for name, count in (("cache_read_tokens", cache_read_tokens), ("cache_write_tokens", cache_write_tokens)):
        if count is not None:
            _validate_token_count(name, count)
    if (cache_read_tokens or 0) + (cache_write_tokens or 0) > input_tokens:
        raise ValueError("The sum of cache_read_tokens and cache_write_tokens must not exceed input_tokens.")
    if request_latency is not None and (
        isinstance(request_latency, bool)
        or not isinstance(request_latency, (int, float))
        or not math.isfinite(request_latency)
        or request_latency <= 0
    ):
        raise ValueError("request_latency must be a finite positive number of seconds.")

    if input_tokens > 0:
        logger.warning_once(
            "Input tokens and prompt caching are not modeled yet; input_tokens, "
            "cache_read_tokens and cache_write_tokens do not affect estimates."
        )

    deployment = _resolve_deployment(model_name, provider)
    if deployment is None:
        if provider is None:
            message = f"No default deployment for `{model_name}`. Supply a namespaced model ID and its provider."
        else:
            message = f"Could not find model `{model_name}` for {provider} provider."
        error = ModelNotRegisteredError(message=message)
        logger.warning_once(str(error))
        return ImpactsOutput(errors=[error])

    internal_provider, registered_name = deployment
    return _llm_impacts(
        provider=internal_provider,
        model_name=registered_name,
        output_token_count=output_tokens,
        request_latency=request_latency,
        electricity_mix_zone=datacenter_location,
        datacenter_pue=datacenter_pue,
        datacenter_wue=datacenter_wue,
    )

video_impacts(model_name, resolution, duration, with_audio=True, datacenter_location=None, datacenter_pue=None, datacenter_wue=None)

Determines the impacts of generating a video based on the specified model, resolution, and duration. Calculates and returns the detailed impacts related to computational resources, energy consumption, and data processing associated with the generation task.

Parameters:

Name Type Description Default
model_name str

The name of the model selected for video generation.

required
resolution str

The resolution of the video to be generated, typically provided as a string (e.g., "1920x1080" or "1080p").

required
duration float

The length of the video to generate, specified in seconds.

required
with_audio bool

Whether the video generation also includes audio.

True
datacenter_location str | None

ISO 3166-1 alpha-3 code of the datacenter location. Uses the provider default when omitted or None; pass "WOR" for the world mix.

None
datacenter_pue float | RangeValue | None

Power Usage Effectiveness of the datacenter. Uses the provider default when omitted.

None
datacenter_wue float | RangeValue | None

Water Usage Effectiveness of the datacenter in L/kWh. Uses the provider default when omitted.

None

Returns:

Type Description
ImpactsOutput

An ImpactsOutput object containing details of the computed impacts.

Source code in ecologits/estimations/video.py
def video_impacts(
        model_name: str,
        resolution: str,
        duration: float,
        with_audio: bool = True,
        datacenter_location: str | None = None,
        datacenter_pue: float | RangeValue | None = None,
        datacenter_wue: float | RangeValue | None = None,
) -> ImpactsOutput:
    """
    Determines the impacts of generating a video based on the specified model, resolution,
    and duration. Calculates and returns the detailed impacts related to computational
    resources, energy consumption, and data processing associated with the generation task.

    Arguments:
        model_name: The name of the model selected for video generation.
        resolution: The resolution of the video to be generated, typically provided
            as a string (e.g., "1920x1080" or "1080p").
        duration: The length of the video to generate, specified in seconds.
        with_audio: Whether the video generation also includes audio.
        datacenter_location: ISO 3166-1 alpha-3 code of the datacenter location.
            Uses the provider default when omitted or None; pass "WOR" for the world mix.
        datacenter_pue: Power Usage Effectiveness of the datacenter. Uses the provider default when omitted.
        datacenter_wue: Water Usage Effectiveness of the datacenter in L/kWh. Uses the provider default when omitted.

    Returns:
        An ImpactsOutput object containing details of the computed impacts.
    """
    model_info = _MODELS_INFO.get(model_name)
    if model_info is None:
        error = ModelNotRegisteredError(message=f"Could not find video model `{model_name}`.")
        logger.warning_once(str(error))
        return ImpactsOutput(errors=[error])

    provider_configuration = _PROVIDER_CONFIGURATIONS[model_info["provider"]]
    if datacenter_location is None:
        datacenter_location = provider_configuration["datacenter_location"]
    if datacenter_pue is None:
        datacenter_pue = parse_value_or_range(provider_configuration["datacenter_pue"])
    if datacenter_wue is None:
        datacenter_wue = parse_value_or_range(provider_configuration["datacenter_wue"])

    if_electricity_mix = electricity_mixes.find_electricity_mix(zone=datacenter_location)
    if if_electricity_mix is None:
        error = ZoneNotRegisteredError(
            message=f"Could not find electricity mix for `{datacenter_location}` zone."
        )
        logger.warning_once(str(error))
        return ImpactsOutput(errors=[error])

    width, height = parse_resolution(resolution)
    frames_count = duration_to_frames(duration)

    hardware = _HARDWARE_CONFIGURATIONS[model_info["hardware"]]
    server_power = hardware["server_power"]
    server_embodied = hardware["server_embodied"]
    accelerator_embodied = hardware["accelerator_embodied"]
    server_accelerator_count = hardware["number_of_accelerators"]

    # Server power in the data is in W; the dag expects kW.
    server_accelerator_power: float | RangeValue = RangeValue(
        min=server_power["p2_5"] / 1000,
        max=server_power["p97_5"] / 1000,
    )

    # When generating with audio, the regression baseline already includes audio,
    # so apply no scaling. When generating without audio, use the model's calibrated
    # multiplier (1.0 for models without native audio support).
    regression = dict(model_info["regression_parameters"])
    if with_audio:
        regression["non_audio_weight"] = 1.0

    impacts = compute_video_impacts(
        video_width=width,
        video_height=height,
        video_frames_count=frames_count,
        server_accelerator_power=server_accelerator_power,
        if_electricity_mix_adpe=if_electricity_mix.adpe,
        if_electricity_mix_pe=if_electricity_mix.pe,
        if_electricity_mix_gwp=if_electricity_mix.gwp,
        if_electricity_mix_wue=if_electricity_mix.wue,
        datacenter_pue=datacenter_pue,
        datacenter_wue=datacenter_wue,
        accelerator_embodied_gwp=accelerator_embodied["gwp"],
        accelerator_embodied_adpe=accelerator_embodied["adpe"],
        accelerator_embodied_pe=accelerator_embodied["pe"],
        server_embodied_gwp=server_embodied["gwp"],
        server_embodied_adpe=server_embodied["adpe"],
        server_embodied_pe=server_embodied["pe"],
        server_accelerator_count=server_accelerator_count,
        **regression,
    )
    impacts = ImpactsOutput.model_validate(impacts.model_dump())

    if if_electricity_mix.has_warnings:
        for warning in if_electricity_mix.warnings:
            logger.warning_once(str(warning))
            impacts.add_warning(warning)

    return impacts