Skip to content

feat(tracking): add ecologits carbon/energy metrics to LLM tracking - #343

Open
todkarsant wants to merge 1 commit into
ServiceNow:mainfrom
todkarsant:feat/ecologits-carbon-metrics
Open

todkarsant wants to merge 1 commit into
ServiceNow:mainfrom
todkarsant:feat/ecologits-carbon-metrics

Conversation

@todkarsant

@todkarsant todkarsant commented Nov 6, 2026 •

Copy link
Copy Markdown

Summary

Integrates ecologits as an optional dependency to track energy consumption (kWh) and GHG emissions (kgCO2eq) alongside existing token/cost metrics.

Closes #199

Changes

  • src/agentlab/llm/tracking.py — Added optional ecologits import (follows existing langchain_community pattern), _ensure_ecologits_init() lazy initializer, _extract_ecologits_impacts() extractor, and extended LLMTracker with energy_kwh/gwp_kgco2eq fields
  • src/agentlab/llm/chat_api.py — Updated all 3 old-style model tracker call sites to extract and pass ecologits impacts
  • pyproject.toml — Added ecologits optional dependency group
  • tests/llm/test_tracking.py — 5 new tests: accumulation, defaults, nesting, extraction fallback, repr

Design decisions

  • Optional dependency: ecologits is not required — when not installed, energy/carbon fields default to 0.0 with zero overhead
  • Lazy initialization: EcoLogits.init() is called once before the first API call, not at import time
  • Backward compatible: LLMTracker.__call__() new params have defaults, so existing callers work unchanged

Test results

tests/llm/test_tracking.py::test_get_action_decorator PASSED
tests/llm/test_tracking.py::test_get_pricing_openai PASSED
tests/llm/test_tracking.py::test_tracker PASSED
tests/llm/test_tracking.py::test_imbricate_trackers PASSED
tests/llm/test_tracking.py::test_threaded_trackers PASSED
tests/llm/test_tracking.py::test_tracker_ecologits_fields PASSED
tests/llm/test_tracking.py::test_tracker_ecologits_defaults PASSED
tests/llm/test_tracking.py::test_tracker_ecologits_nested PASSED
tests/llm/test_tracking.py::test_extract_ecologits_impacts_no_impacts PASSED
tests/llm/test_tracking.py::test_tracker_repr_includes_ecologits PASSED
10 passed, 1 skipped, 3 deselected

Integrate ecologits as an optional dependency to track energy consumption
(kWh) and GHG emissions (kgCO2eq) alongside existing token/cost metrics.

- Add energy_kwh and gwp_kgco2eq fields to LLMTracker
- Extract .impacts from API responses via _extract_ecologits_impacts()
- Lazy-initialize ecologits before first API call
- Update all tracker call sites (TrackAPIPricingMixin + old-style models)
- Add ecologits as optional dependency in pyproject.toml
- Add 5 unit tests for accumulation, defaults, nesting, extraction, repr

Closes ServiceNow#199
@todkarsant
todkarsant force-pushed the feat/ecologits-carbon-metrics branch from fdec3c8 to 7e30d47 Compare November 6, 2026 18:51

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

(maybe) Add ecologits to metrics

1 participant