/xemantic-ai-money

Kotlin multiplatform library for real-time calculation of LLM usage costs

Primary LanguageKotlinApache License 2.0Apache-2.0

xemantic-ai-money

Kotlin multiplatform library for real-time calculation of LLM usage costs

Maven Central Version GitHub Release Date license

GitHub Actions Workflow Status GitHub branch check runs GitHub commits since latest release GitHub last commit

GitHub contributors GitHub commit activity GitHub code size in bytes GitHub Created At kotlin version

discord server discord users online X (formerly Twitter) Follow

Why?

The APIs of AI companies, like OpenAI API and Anthropic API, are providing the usage information regarding input and output tokens associated with each API call. Depending on many factors, like the model being used, batch processing, involved cache, etc., these tokens can be billed according to particular rules. This library is fulfilling the need of proper accounting of API usage by:

  • representing monetary amounts as Money interface, supporting big decimal arithmetics and operator overloading,
  • representing ratios of monetary amounts (e.g. input token cost for given model) as Money.Ratio interface.

Note

The xemantic-ai-money was initially a part of the anthropic-sdk-kotlin, but was eventually externalized, as a common functionality applicable across various API-related use cases.

Usage

In build.gradle.kts add:

dependencies {
  implementation("com.xemantic.ai:xemantic-ai-money:0.2")
}

See test cases for further information.

Big decimal arithmetics

The implementation of big decimal arithmetic in use will depend on the multiplatform targets:

Development

Clone this repo and then in the project dir:

./gradlew build