Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

Awesome Lists containing this project

README

        

# xemantic-ai-money
Real-time calculation of LLM usage costs - a Kotlin multiplatform library

[Maven Central Version](https://central.sonatype.com/namespace/com.xemantic.ai)
[GitHub Release Date](https://github.com/xemantic/xemantic-ai-money/releases)
[license](https://github.com/xemantic/xemantic-ai-money/blob/main/LICENSE)

[GitHub Actions Workflow Status](https://github.com/xemantic/xemantic-ai-money/actions/workflows/build-main.yml)
[GitHub branch check runs](https://github.com/xemantic/xemantic-ai-money/actions/workflows/build-main.yml)
[GitHub commits since latest release](https://github.com/xemantic/xemantic-ai-money/commits/main/)
[GitHub last commit](https://github.com/xemantic/xemantic-ai-money/commits/main/)

[GitHub contributors](https://github.com/xemantic/xemantic-ai-money/graphs/contributors)
[GitHub commit activity](https://github.com/xemantic/xemantic-ai-money/commits/main/)
[GitHub code size in bytes]()
[GitHub Created At](https://github.com/xemantic/xemantic-ai-money/commit/39c1fa4c138d4c671868c973e2ad37b262ae03c2)
[kotlin version](https://kotlinlang.org/docs/releases.html)

[discord server](https://discord.gg/vQktqqN2Vn)
[discord users online](https://discord.gg/vQktqqN2Vn)
[X (formerly Twitter) Follow](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
```