Kotlin multiplatform library for real-time calculation of LLM usage costs
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.
In build.gradle.kts
add:
dependencies {
implementation("com.xemantic.ai:xemantic-ai-money:0.2")
}
See test cases for further information.
The implementation of big decimal arithmetic in use will depend on the multiplatform targets:
java.math.BigDecimal
for JVM target, for maximal performance and stability.- kotlin-multiplatform-bignum for non-JVM targets.
Clone this repo and then in the project dir:
./gradlew build