Skip to main content

LangChain integration for Neuralwatt — energy-aware AI inference.

Project description

langchain-neuralwatt

LangChain integration for Neuralwatt — energy-aware AI inference. Drop-in replacement for ChatOpenAI that surfaces per-call energy and carbon metrics into LangChain's response_metadata.

Install

pip install langchain-neuralwatt

Quick start

from langchain_neuralwatt import ChatNeuralwatt

llm = ChatNeuralwatt(model="glm-5-fast")  # reads NEURALWATT_API_KEY from env
msg = llm.invoke("Summarize the second law of thermodynamics in one sentence.")

print(msg.content)
print(msg.response_metadata["energy"])
# {'energy_joules': 1234.56, 'energy_kwh': 3.43e-07, 'carbon_g_co2eq': 0.00012, ...}

Note: Energy metadata is only present on non-streaming (invoke) responses. llm.stream() and agent loops that stream internally will not have an energy key in response_metadata.

Tracking energy across a chain

from langchain_neuralwatt import ChatNeuralwatt, NeuralwattEnergyCallback

cb = NeuralwattEnergyCallback()
llm = ChatNeuralwatt(model="glm-5-fast")

llm.invoke("...", config={"callbacks": [cb]})
llm.invoke("...", config={"callbacks": [cb]})

print(cb.summary())
# {'calls': 2, 'energy_joules': 2450.0, 'energy_kwh': 6.8e-07, 'carbon_g_co2eq': 0.00024}

Configuration

Param Env var Default
model glm-5-fast
api_key NEURALWATT_API_KEY (falls back to OPENAI_API_KEY)
base_url NEURALWATT_API_BASE https://api.neuralwatt.com/v1

All other ChatOpenAI params (temperature, max_tokens, tools, model_kwargs, …) work as expected — ChatNeuralwatt is a thin subclass of BaseChatOpenAI.

What lands in response_metadata

Neuralwatt attaches two top-level fields to non-streaming chat completion responses:

  • energyenergy_joules, energy_kwh, avg_power_watts, duration_seconds, attribution_method, optional carbon_g_co2eq + grid metadata.
  • costrequest_cost_usd, cache_savings_usd, allowance_remaining_usd.

Development

pip install -e ".[test,lint,typing]"
pytest tests/unit_tests           # offline
NEURALWATT_API_KEY=nw_... pytest tests/integration_tests  # live

This package implements the LangChain standard test suite via langchain-tests.

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

langchain_neuralwatt-0.1.0.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

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

langchain_neuralwatt-0.1.0-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file langchain_neuralwatt-0.1.0.tar.gz.

File metadata

  • Download URL: langchain_neuralwatt-0.1.0.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for langchain_neuralwatt-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a4a635cf69f830c462a21df9a9190e0218d2f2ada66071770635b9494e1ffc5d
MD5 12d033fd7fa60a308abff3cab6091e51
BLAKE2b-256 db11ff88a2662ec0e21842fb8143f12ca0997e34cfd007ac5c9e8755e79810c7

See more details on using hashes here.

File details

Details for the file langchain_neuralwatt-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: langchain_neuralwatt-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for langchain_neuralwatt-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3cc23f3206c387eb9a7c90b418e3d988c2055e83d635f0fe350831424ee1d53e
MD5 1969a152702bd785e9ad80b57b193809
BLAKE2b-256 7c7a0275c2279768f2acc76b52e373fd80049ebe2d12d085978f25a9b114b63b

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