Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/xemantic/xemantic-ai-money
Real-time calculation of LLM usage costs - a Kotlin multiplatform library
https://github.com/xemantic/xemantic-ai-money
ai artificial-intelligence artificialintelligence bigdecimal kotlin kotlin-library kotlin-multiplatform money
Last synced: 3 days ago
JSON representation
Real-time calculation of LLM usage costs - a Kotlin multiplatform library
- Host: GitHub
- URL: https://github.com/xemantic/xemantic-ai-money
- Owner: xemantic
- License: apache-2.0
- Created: 2024-12-04T16:38:32.000Z (2 months ago)
- Default Branch: main
- Last Pushed: 2025-01-06T20:45:05.000Z (29 days ago)
- Last Synced: 2025-01-06T21:31:53.006Z (29 days ago)
- Topics: ai, artificial-intelligence, artificialintelligence, bigdecimal, kotlin, kotlin-library, kotlin-multiplatform, money
- Language: Kotlin
- Homepage:
- Size: 121 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# xemantic-ai-money
Real-time calculation of LLM usage costs - a Kotlin multiplatform library[](https://central.sonatype.com/namespace/com.xemantic.ai)
[](https://github.com/xemantic/xemantic-ai-money/releases)
[](https://github.com/xemantic/xemantic-ai-money/blob/main/LICENSE)[](https://github.com/xemantic/xemantic-ai-money/actions/workflows/build-main.yml)
[](https://github.com/xemantic/xemantic-ai-money/actions/workflows/build-main.yml)
[](https://github.com/xemantic/xemantic-ai-money/commits/main/)
[](https://github.com/xemantic/xemantic-ai-money/commits/main/)[](https://github.com/xemantic/xemantic-ai-money/graphs/contributors)
[](https://github.com/xemantic/xemantic-ai-money/commits/main/)
[]()
[](https://github.com/xemantic/xemantic-ai-money/commit/39c1fa4c138d4c671868c973e2ad37b262ae03c2)
[](https://kotlinlang.org/docs/releases.html)[](https://discord.gg/vQktqqN2Vn)
[](https://discord.gg/vQktqqN2Vn)
[](https://x.com/KazikPogoda)## Why?
The APIs of AI companies, like [OpenAI API](https://platform.openai.com/docs/api-reference/introduction) and [Anthropic API](https://docs.anthropic.com/en/api/getting-started), 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](src/commonMain/kotlin/Money.kt) interface, supporting big decimal arithmetics and operator overloading,
* representing ratios of monetary amounts (e.g. input token cost for given model) as [Money.Ratio](src/commonMain/kotlin/Money.kt) interface.> [!NOTE]
> The `xemantic-ai-money` was initially a part of the [anthropic-sdk-kotlin](https://github.com/xemantic/anthropic-sdk-kotlin), but was eventually externalized, as a
> common functionality applicable across various API-related use cases.## Usage
In `build.gradle.kts` add:
```kotlin
dependencies {
implementation("com.xemantic.ai:xemantic-ai-money:0.2")
}
```See [test cases](src/commonTest/kotlin) for further information.
## Big decimal arithmetics
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](https://github.com/ionspin/kotlin-multiplatform-bignum) for non-JVM targets.## Development
Clone this repo and then in the project dir:
```shell
./gradlew build
```