Skip to main content

AI API cost governance — track, attribute, and report LLM spend

Project description

Kostrack

AI API cost governance — track, attribute, and govern LLM API spend across Anthropic, OpenAI, and Gemini, with full agentic workflow cost rollup.

PyPI License Tests Python

Docs · Quick Start · PyPI


What it does

Kostrack sits between your application and your LLM providers. Every API call is intercepted, costed, attributed to a project/feature/user, and written asynchronously to TimescaleDB. Grafana dashboards are pre-provisioned and ready the moment docker compose up -d finishes.

Your App → kostrack SDK → LLM Provider (Anthropic / OpenAI / Gemini)
                ↓
          TimescaleDB
                ↓
            Grafana

Why not Helicone or LangSmith?

Those tools are built for ML engineers — prompt logging, evals, model quality. Kostrack is built for financial governance: cost per feature, cost per workflow run, budget alerts, CFO-exportable reports. Nobody else does agentic cost rollup or targets the African enterprise market where USD-denominated API costs hit differently.


Quick start

# 1. Start the stack
cp .env.example .env       # set your passwords
docker compose up -d

# 2. Install the SDK
pip install kostrack

# 3. Instrument your app
import kostrack

kostrack.configure(
    dsn="postgresql://kostrack:yourpassword@localhost/kostrack",
    service_id="your-app-name",
)

# Before: from anthropic import Anthropic
from kostrack import Anthropic

client = Anthropic(
    tags={
        "project":     "openmanagr",
        "feature":     "invoice-extraction",
        "environment": "production",
    }
)

response = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Summarise this invoice..."}],
)
# 4. Open Grafana → http://localhost:3000

Agentic tracing

Wrap multi-step workflows to get total cost per workflow run:

import kostrack
from kostrack import Anthropic

client = Anthropic(tags={"project": "openmanagr"})

with kostrack.trace(tags={"feature": "month-end-close"}) as t:
    with kostrack.span("validate", parent=t):
        client.messages.create(...)
    with kostrack.span("classify", parent=t):
        client.messages.create(...)
    with kostrack.span("post", parent=t):
        client.messages.create(...)

print(f"Workflow cost: ${t.total_cost_usd:.6f} across {t.call_count} calls")

All three providers

from kostrack import Anthropic, OpenAI, GenerativeModel

anthropic = Anthropic(tags={"project": "myapp"})
openai    = OpenAI(tags={"project": "myapp"})
gemini    = GenerativeModel("gemini-2.0-flash", tags={"project": "myapp"})

Architecture

Layer Technology
SDK Python 3.11+, provider SDKs
Write path Async batch writer, SQLite fallback
Database TimescaleDB (Postgres extension)
Dashboards Grafana — pre-provisioned
Deployment Docker Compose

Resilience: If TimescaleDB is unreachable, records buffer to ~/.kostrack/buffer.db and flush automatically when connectivity returns. Write overhead is under 5ms.


Tests

cd sdk
pip install -e ".[dev]"

python -m pytest tests/test_e2e.py -v         # 41 tests, offline
python tests/integration_test.py              # live integration test

Documentation

Full documentation at kostrack.netlify.app/docs


License

Apache 2.0 — see LICENSE.

Built by Blessing Phiri · Applied AI Engineer · Init Data Solutions · Harare, Zimbabwe.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kostrack-0.1.0.tar.gz (30.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kostrack-0.1.0-py3-none-any.whl (30.8 kB view details)

Uploaded Python 3

File details

Details for the file kostrack-0.1.0.tar.gz.

File metadata

  • Download URL: kostrack-0.1.0.tar.gz
  • Upload date:
  • Size: 30.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.1

File hashes

Hashes for kostrack-0.1.0.tar.gz
Algorithm Hash digest
SHA256 29051b8d3508791118a04bb2d36faefc212b2396325070ead9624227f710fad2
MD5 a40efa74f9285a9866160e4ce5312b33
BLAKE2b-256 dfc2e1a8d38b367dae7903019073189c069442eb6cbb029db858e529dd6d4b12

See more details on using hashes here.

File details

Details for the file kostrack-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: kostrack-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 30.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.1

File hashes

Hashes for kostrack-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 720525e09c84a9a0be64a7d5de5e0b38c28754704fb778bf4f08605ceb8587d4
MD5 6dd516c0755f6b7ee8b260926299dc8b
BLAKE2b-256 6c1f6129f12d5f4f2fac7694534b7bd7434289f19a55280e69adc763221fc4a6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page