Skip to main content

Calculate CO2 emissions from LLM API calls following Green Software Foundation standards.

Project description

llm-carbon-calculator

A lightweight, dependency-free Python library for estimating the carbon footprint of Large Language Model (LLM) API usage, following the Green Software Foundation SCI methodology.


Features

  • Zero runtime dependencies
  • Accepts total_api_requests and total_llm_tokens as inputs
  • Outputs:
    • Total Emissions — total carbon emissions in kgCO2eq
    • Avg per Request — average emissions per API call (gCO2eq / request)
    • Weighted Carbon Intensity — energy-weighted average across multiple regions (gCO2eq / kWh)
  • Multi-region support for Azure deployments
  • JSON-ready API output via to_api_dict()

Installation

pip install llm-carbon-calculator

Quick Start

from llm_emissions import CarbonCalculator

calc = CarbonCalculator(
    total_api_requests=1_000,
    total_llm_tokens=500_000,
)

result = calc.calculate(region_intensity=156)

print(result.total_emissions_kg)        # kgCO2eq
print(result.avg_per_request_g)         # gCO2eq / request
print(result.weighted_carbon_intensity) # gCO2eq / kWh
print(result)                           # pretty-printed summary

Custom energy factor

calc = CarbonCalculator(
    total_api_requests=1_000,
    total_llm_tokens=500_000,
    model_energy_factor=0.000112,  # kWh per 1000 tokens
)
result = calc.calculate(region_intensity=156)

Multi-region weighted intensity

from llm_emissions.models import RegionEnergyInfo

calc = CarbonCalculator(
    total_api_requests=2_000,
    total_llm_tokens=400_000,
)
result = calc.calculate(
    region_intensity=156,
    regions=[
        RegionEnergyInfo(region_name="westeurope", tokens=300_000, region_intensity=156),
        RegionEnergyInfo(region_name="eastus",     tokens=100_000, region_intensity=380),
    ],
)
print(result.weighted_carbon_intensity)  # energy-weighted average

JSON API response

import json
print(json.dumps(result.to_api_dict(), indent=2))

Attach metadata

Extra keyword arguments are stored in result.metadata for traceability:

result = calc.calculate(
    region_intensity=156,
    model="gpt-4o",
    region="westeurope",
)
print(result.metadata)  # {'model': 'gpt-4o', 'region': 'westeurope'}

Formula

Based on the SCI (Software Carbon Intensity) specification:

emission_g = (tokens / 1000) × energy_factor × region_intensity × PUE
Parameter Default Description
PUE 1.125 Power Usage Effectiveness
model_energy_factor 0.000056 kWh per 1000 tokens (GPT-4o)
region_intensity gCO2eq/kWh of the cloud region

API Reference

CarbonCalculator(total_api_requests, total_llm_tokens, model_energy_factor=0.000056, pue=1.125)

Parameter Type Description
total_api_requests int Number of LLM API requests. Must be a positive integer.
total_llm_tokens int Total tokens (input + output). Must be a positive integer.
model_energy_factor float Energy in kWh per 1000 tokens. Default: 0.000056 (GPT-4o).
pue float Power Usage Effectiveness. Default: 1.125.

.calculate(region_intensity, *, regions=None, **metadata) → EmissionsResult

Parameter Type Description
region_intensity float Grid carbon intensity (gCO2eq/kWh).
regions list[RegionEnergyInfo] Optional multi-region breakdown.
**metadata any Arbitrary key-value pairs stored in the result.

.calculate_as_dict(...) → dict

Same parameters as .calculate(). Returns a JSON-serialisable dictionary.

EmissionsResult

Attribute Type Description
total_emissions_kg float Total emissions in kgCO2eq.
avg_per_request_g float Average emissions per request (gCO2eq).
weighted_carbon_intensity float Energy-weighted intensity (gCO2eq/kWh).
total_energy_kwh float Operational energy consumed (kWh).
total_api_requests int Echo of the input.
total_llm_tokens int Echo of the input.
metadata dict Extra key-value pairs passed to calculate().

RegionEnergyInfo

Attribute Type Description
region_name str Region identifier (e.g. "westeurope").
tokens int Tokens processed in this region.
region_intensity float Grid intensity for this region (gCO2eq/kWh).

Development

# Install with dev dependencies
pip install -e ".[dev]"

# Run tests
pytest tests/ -v

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

llm_carbon_calculator-0.2.0.tar.gz (9.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

llm_carbon_calculator-0.2.0-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file llm_carbon_calculator-0.2.0.tar.gz.

File metadata

  • Download URL: llm_carbon_calculator-0.2.0.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for llm_carbon_calculator-0.2.0.tar.gz
Algorithm Hash digest
SHA256 ed5736e4ab4dc2bb3ccf51441a1d24844440dcb7fa2f2ca99593008202161f8e
MD5 846ac4b4f255eaaef2087d5047e54e26
BLAKE2b-256 3241deec0835c1fac727d05a3eea8c71f725060fadeba807d38040880b765b16

See more details on using hashes here.

File details

Details for the file llm_carbon_calculator-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for llm_carbon_calculator-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9019adbd423e7b33d37a439b837e4f5854e9cec044ec799f428431bd215f758d
MD5 7d6632d9bb3be49282adc53b741ac7b4
BLAKE2b-256 0eee8ebd86c97486d67d388bf2df7612b1544fc3b0ef2fce18f167c2de25bb07

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page