Skip to content

ecologits.tracers.utils

ImpactsStream(stream, items)

Bases: ObjectProxy

An SDK stream object whose iteration yields the items with impacts. Other attributes go to the SDK object.

Source code in ecologits/tracers/utils.py
def __init__(self, stream: Any, items: Iterator[Any]) -> None:
    super().__init__(stream)
    self._self_items = items

AsyncImpactsStream(stream, items)

Bases: ObjectProxy

An async SDK stream object whose iteration yields the items with impacts. Other attributes go to the SDK object.

Source code in ecologits/tracers/utils.py
def __init__(self, stream: Any, items: AsyncIterator[Any]) -> None:
    super().__init__(stream)
    self._self_items = items

ImpactsOutput

Bases: BaseModel

Impacts output data model.

Attributes:

Name Type Description
energy Energy | None

Total energy consumption

gwp GWP | None

Total Global Warming Potential (GWP) impact

adpe ADPe | None

Total Abiotic Depletion Potential for Elements (ADPe) impact

pe PE | None

Total Primary Energy (PE) impact

wcf WCF | None

Usage-only Water Consumption Footprint (WCF) impact

usage Usage | None

Impacts for the usage phase

embodied Embodied | None

Impacts for the embodied phase

warnings list[WarningMessage] | None

List of warnings

errors list[ErrorMessage] | None

List of errors

is_raw_response_request(kwargs)

Check if a request was made with with_raw_response or with_streaming_response.

The OpenAI and Anthropic clients add the X-Stainless-Raw-Response header to these requests and return the raw HTTP response instead of the parsed model, so there is nothing to add impacts to.

Parameters:

Name Type Description Default
kwargs dict[str, Any]

Keyword arguments of the request.

required

Returns:

Type Description
bool

True if the client returns the raw HTTP response for this request.

Source code in ecologits/tracers/utils.py
def is_raw_response_request(kwargs: dict[str, Any]) -> bool:
    """
    Check if a request was made with `with_raw_response` or `with_streaming_response`.

    The OpenAI and Anthropic clients add the `X-Stainless-Raw-Response` header to these requests and return the raw
    HTTP response instead of the parsed model, so there is nothing to add impacts to.

    Args:
        kwargs: Keyword arguments of the request.

    Returns:
        True if the client returns the raw HTTP response for this request.
    """
    extra_headers = kwargs.get("extra_headers") or {}
    return RAW_RESPONSE_HEADER in extra_headers

llm_impacts(provider, model_name, output_token_count, request_latency, electricity_mix_zone=None, *, datacenter_pue=None, datacenter_wue=None)

Compute LLM impacts using the deprecated tracer utility interface.

Deprecated

Use ecologits.estimations.text_impacts for standalone text generation estimates. See the text generation tutorial for argument mappings.

Preserves the original arguments and calculations, including measured request latency. The replacement estimates latency and requires total input tokens and a namespaced model identifier, so migration may change the resulting estimates.

Source code in ecologits/tracers/utils.py
def llm_impacts(
    provider: str,
    model_name: str,
    output_token_count: int,
    request_latency: float | None,
    electricity_mix_zone: str | None = None,
    *,
    datacenter_pue: float | RangeValue | None = None,
    datacenter_wue: float | RangeValue | None = None,
) -> ImpactsOutput:
    """Compute LLM impacts using the deprecated tracer utility interface.

    Deprecated:
        Use `ecologits.estimations.text_impacts` for standalone text generation
        estimates. See the text generation tutorial for argument mappings.

    Preserves the original arguments and calculations, including measured request
    latency. The replacement estimates latency and requires total input tokens and
    a namespaced model identifier, so migration may change the resulting estimates.
    """
    warnings.warn(
        "ecologits.tracers.utils.llm_impacts is deprecated; "
        "use ecologits.estimations.text_impacts instead. "
        "See the text generation tutorial for migration details.",
        DeprecationWarning,
        stacklevel=2,
    )
    return _llm_impacts(
        provider=provider,
        model_name=model_name,
        output_token_count=output_token_count,
        request_latency=request_latency,
        electricity_mix_zone=electricity_mix_zone,
        datacenter_pue=datacenter_pue,
        datacenter_wue=datacenter_wue,
    )