tokencost  by AgentOps-AI

SDK for LLM token cost estimation

Created 1 year ago
1,800 stars

Top 23.9% on SourcePulse

GitHubView on GitHub
Project Summary

This repository provides a Python library for estimating the token count and associated costs of using various Large Language Models (LLMs). It aims to help developers building AI applications and agents accurately track and manage their LLM API expenses.

How It Works

The library leverages tiktoken, OpenAI's official tokenizer, for accurate tokenization of text and message formats. For Anthropic models (Claude 3.5 and newer), it utilizes the Anthropic beta token counting API. For older Claude models, it falls back to approximating token counts using tiktoken with the cl100k_base encoding. The cost calculations are based on a comprehensive, regularly updated list of model prices stored in model_prices.json.

Quick Start & Requirements

  • Installation: pip install tokencost
  • Prerequisites: Python 3.x. No other specific hardware or software dependencies are mentioned.
  • Usage: Import calculate_prompt_cost, calculate_completion_cost, count_message_tokens, or count_string_tokens functions.
  • Documentation: README

Highlighted Details

  • Supports over 400 LLMs, including OpenAI, Anthropic, Mistral, Cohere, and Google Gemini models.
  • Provides functions for both string-based and message-list-based token counting and cost estimation.
  • Includes detailed pricing information for prompt and completion tokens across a wide range of models.

Maintenance & Community

  • The project is maintained by AgentOps-AI.
  • Community engagement is encouraged via Twitter and Discord.

Licensing & Compatibility

  • License: MIT License.
  • Compatibility: Permissive MIT license allows for commercial use and integration into closed-source projects.

Limitations & Caveats

  • While the README states it supports "Major LLM providers," the specific list of supported models is extensive and may evolve. The accuracy for newer or less common models relies on the maintenance of model_prices.json.
Health Check
Last Commit

1 week ago

Responsiveness

1 day

Pull Requests (30d)
11
Issues (30d)
0
Star History
32 stars in the last 30 days

Explore Similar Projects

Starred by David Cournapeau David Cournapeau(Author of scikit-learn), Chip Huyen Chip Huyen(Author of "AI Engineering", "Designing Machine Learning Systems"), and
11 more.

llm-numbers by ray-project

0%
4k
LLM developer's reference for key numbers
Created 2 years ago
Updated 1 year ago
Starred by Morgan Funtowicz Morgan Funtowicz(Head of ML Optimizations at Hugging Face), Luis Capelo Luis Capelo(Cofounder of Lightning AI), and
7 more.

lighteval by huggingface

2.6%
2k
LLM evaluation toolkit for multiple backends
Created 1 year ago
Updated 1 day ago
Starred by Luis Capelo Luis Capelo(Cofounder of Lightning AI), Chip Huyen Chip Huyen(Author of "AI Engineering", "Designing Machine Learning Systems"), and
6 more.

opik by comet-ml

1.7%
14k
Open-source LLM evaluation framework for RAG, agents, and more
Created 2 years ago
Updated 12 hours ago
Feedback? Help us improve.