Langchain Token Usage
This repository contains a python module to track the consumption of tokens of a Large Language Models (LLMs) within a LangChain application.
Installation
You can install LangChain Token Usage via pip:
pip install git+https://github.com/mrtj/langchain_token_usage.git
or in requirements.txt:
langchain-token-usage @ git+https://github.com/mrtj/langchain_token_usage.git
Usage
Token usage tracking is implemented as a LangChain Callback so it is easy to integrate in any LangChain application. The collection of the token usage metrics is LLM specific: currently only OpenAI LLMs are supported.
The metrics collected can be processed locally, or sent to a metrics repository via a Reporter. Current implementation includes sending the metrics to Amazon CloudWatch service.
If you wish to use CloudWatch, you should ensure that you have configured your boto3 client to credentials that are associated to an IAM Role that can write data to CloudWatch Metrics.
Example usage:
from langchain.chains import LLMChain
from langchain.chat_models import ChatOpenAI
from langchain.prompts import PromptTemplate
from langchain_token_usage.handlers import OpenAITokenUsageCallbackHandler
from langchain_token_usage.reporters import CloudWatchTokenUsageReporter
reporter = CloudWatchTokenUsageReporter(
namespace="openai_token_usage",
dimensions={"project": "my_test_project"}
)
handler = OpenAITokenUsageCallbackHandler(reporter)
llm = ChatOpenAI(model="gpt-4", callbacks=[handler])
prompt = PromptTemplate.from_template("1 + {number} = ")
chain = LLMChain(llm=llm, prompt=prompt)
chain.run(number=2)
The above code will perform a single call to an OpenAI GPT-4 backed LLM model, and send token usage metrics and cost estimation to CloudWatch. You can define the desired namespace of your CloudWatch metrics, and add arbitrary metrics dimensions as str -> str mapping.
Contributing
Contributions are very welcome. To learn more, see the Contributor Guide.
License
Distributed under the terms of the MIT license, Langchain Token Usage is free and open source software.
Issues
If you encounter any problems, please file an issue along with a detailed description.
Credits
This project was generated from @cjolowicz's Hypermodern Python Cookiecutter template.
Metadata
Release files for langchain-token-usage 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| langchain_token_usage-0.1.1.tar.gz | 9.4 kB | Without provenance | Details for langchain_token_usage-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| langchain_token_usage-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for langchain_token_usage-0.1.1-py3-none-any.whl |
Total release size: 20.9 kB
Release files / langchain_token_usage-0.1.1.tar.gz
| Download URL | langchain_token_usage-0.1.1.tar.gz |
|---|---|
| Size | 9.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d8380eb0556536f819a0ad2ef76037f8da309a514fbffd96265f0628104e409a
|
|
BLAKE2b-256 checksum How to use checksums |
6936a6653e8ed0e8a7669cdd76e88f48eaaa4ddda8fce3952cf263d12a95954c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.0 CPython/3.11.5 Darwin/21.6.0
|
Release files / langchain_token_usage-0.1.1-py3-none-any.whl
| Download URL | langchain_token_usage-0.1.1-py3-none-any.whl |
|---|---|
| Size | 11.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
22f3adee1057ac29c1cae163e155a4ab4ca33bd51453b6a57579eafd768ec754
|
|
BLAKE2b-256 checksum How to use checksums |
db236b0f9f7271b8b7d43abead39eb1d7282bd0ce040bb3ba2e6efa700666397
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.0 CPython/3.11.5 Darwin/21.6.0
|
Security for langchain-token-usage 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.