Skip to content

ecologits.tracking

ImpactsTracker(providers)

Context manager that collects the impacts of the LLM calls made inside it.

Use it through EcoLogits.track_impacts(). Supports both with and async with.

Attributes:

Name Type Description
providers

Providers whose calls are tracked.

calls list[ImpactsOutput]

Impacts of each tracked call, in completion order.

Source code in ecologits/tracking.py
def __init__(self, providers: list[str]) -> None:
    self.providers = list(providers)
    self.calls: list[ImpactsOutput] = []
    self._tokens: list[Token[tuple[ImpactsTracker, ...]]] = []

impacts property

Total impacts of the tracked calls.

get_active_trackers(provider)

Get the trackers open in the current context that track a provider.

Parameters:

Name Type Description Default
provider str

Name of the provider.

required

Returns:

Type Description
tuple[ImpactsTracker, ...]

The open trackers that track the provider.

Source code in ecologits/tracking.py
def get_active_trackers(provider: str) -> tuple[ImpactsTracker, ...]:
    """
    Get the trackers open in the current context that track a provider.

    Args:
        provider: Name of the provider.

    Returns:
        The open trackers that track the provider.
    """
    return tuple(tracker for tracker in _active_trackers.get() if provider in tracker.providers)

is_tracking_enabled(provider)

Check if a call to a provider should be tracked.

Parameters:

Name Type Description Default
provider str

Name of the provider.

required

Returns:

Type Description
bool

True if the provider was enabled with EcoLogits.init() or is tracked by a tracker open in the current context.

Source code in ecologits/tracking.py
def is_tracking_enabled(provider: str) -> bool:
    """
    Check if a call to a provider should be tracked.

    Args:
        provider: Name of the provider.

    Returns:
        True if the provider was enabled with `EcoLogits.init()` or is tracked by a tracker open in the current context.
    """
    from ecologits._ecologits import EcoLogits

    return provider in EcoLogits.config.providers or len(get_active_trackers(provider)) > 0

record_impacts(trackers, impacts)

Add the impacts of a call to the given trackers.

Parameters:

Name Type Description Default
trackers tuple[ImpactsTracker, ...]

Trackers that were open when the call started.

required
impacts ImpactsOutput

Impacts of the call.

required
Source code in ecologits/tracking.py
def record_impacts(trackers: tuple[ImpactsTracker, ...], impacts: ImpactsOutput) -> None:
    """
    Add the impacts of a call to the given trackers.

    Args:
        trackers: Trackers that were open when the call started.
        impacts: Impacts of the call.
    """
    for tracker in trackers:
        tracker.calls.append(impacts)