{"id":20553555,"url":"https://github.com/mrtj/langchain_token_usage","last_synced_at":"2026-06-06T15:31:45.628Z","repository":{"id":206375654,"uuid":"716469979","full_name":"mrtj/langchain_token_usage","owner":"mrtj","description":"Token usage tracker for LangChain LLMs","archived":false,"fork":false,"pushed_at":"2024-02-05T08:23:11.000Z","size":399,"stargazers_count":1,"open_issues_count":15,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-01-16T17:30:29.103Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mrtj.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-11-09T07:51:46.000Z","updated_at":"2024-09-06T18:21:19.000Z","dependencies_parsed_at":"2023-12-19T18:35:50.586Z","dependency_job_id":null,"html_url":"https://github.com/mrtj/langchain_token_usage","commit_stats":null,"previous_names":["mrtj/langchain_token_usage"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrtj%2Flangchain_token_usage","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrtj%2Flangchain_token_usage/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrtj%2Flangchain_token_usage/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrtj%2Flangchain_token_usage/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mrtj","download_url":"https://codeload.github.com/mrtj/langchain_token_usage/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242157358,"owners_count":20081048,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-16T02:42:23.683Z","updated_at":"2026-06-06T15:31:45.598Z","avatar_url":"https://github.com/mrtj.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Langchain Token Usage\n\nThis repository contains a python module to track the consumption of tokens of a Large Language Models (LLMs) within a [LangChain](https://github.com/langchain-ai/langchain) application.\n\n## Installation\n\nYou can install _LangChain Token Usage_ via [pip]:\n\n```shell\npip install git+https://github.com/mrtj/langchain_token_usage.git\n```\n\nor in requirements.txt:\n\n```txt\nlangchain-token-usage @ git+https://github.com/mrtj/langchain_token_usage.git\n```\n\n## Usage\n\nToken usage tracking is implemented as a [LangChain Callback](https://python.langchain.com/docs/modules/callbacks/) 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.\n\nThe metrics collected can be processed locally, or sent to a metrics repository via a Reporter. Current implementation includes sending the metrics to [Amazon CloudWatch](https://aws.amazon.com/cloudwatch/) service.\n\nIf you wish to use CloudWatch, you should ensure that you have [configured your boto3 client](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/configuration.html) to credentials that are associated to an IAM Role that can write data to CloudWatch Metrics.\n\nExample usage:\n\n```python\nfrom langchain.chains import LLMChain\nfrom langchain.chat_models import ChatOpenAI\nfrom langchain.prompts import PromptTemplate\n\nfrom langchain_token_usage.handlers import OpenAITokenUsageCallbackHandler\nfrom langchain_token_usage.reporters import CloudWatchTokenUsageReporter\n\nreporter = CloudWatchTokenUsageReporter(\n    namespace=\"openai_token_usage\",\n    dimensions={\"project\": \"my_test_project\"}\n)\nhandler = OpenAITokenUsageCallbackHandler(reporter)\n\nllm = ChatOpenAI(model=\"gpt-4\", callbacks=[handler])\nprompt = PromptTemplate.from_template(\"1 + {number} = \")\n\nchain = LLMChain(llm=llm, prompt=prompt)\nchain.run(number=2)\n```\n\nThe 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 -\u003e str` mapping.\n\n## Contributing\n\nContributions are very welcome.\nTo learn more, see the [Contributor Guide].\n\n## License\n\nDistributed under the terms of the [MIT license][license],\n_Langchain Token Usage_ is free and open source software.\n\n## Issues\n\nIf you encounter any problems,\nplease [file an issue] along with a detailed description.\n\n## Credits\n\nThis project was generated from [@cjolowicz]'s [Hypermodern Python Cookiecutter] template.\n\n[@cjolowicz]: https://github.com/cjolowicz\n[pypi]: https://pypi.org/\n[hypermodern python cookiecutter]: https://github.com/cjolowicz/cookiecutter-hypermodern-python\n[file an issue]: https://github.com/mrtj/langchain-token-usage/issues\n[pip]: https://pip.pypa.io/\n\n\u003c!-- github-only --\u003e\n\n[license]: https://github.com/mrtj/langchain-token-usage/blob/main/LICENSE\n[contributor guide]: https://github.com/mrtj/langchain-token-usage/blob/main/CONTRIBUTING.md\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmrtj%2Flangchain_token_usage","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmrtj%2Flangchain_token_usage","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmrtj%2Flangchain_token_usage/lists"}