Skip to main content

Lightweight LLM token & cost tracker โ€” drop-in decorator, live dashboards, budget alerts, async support.

Project description

tokmon

PyPI version Python License: MIT Tests

Know exactly what your LLM calls cost. One decorator. Zero config. Zero dependencies.

pip install tokmon-ai

๐ŸŽฌ Demo

tokmon demo โ€” track LLM token costs per request
Tracking token usage and costs across an agent pipeline โ€” decorator, budget alerts, session dashboard


The Problem

You're building AI agents and you have no idea what they cost per request. Is it $0.01 or $0.50? Which tool call is the expensive one? You'll find out at the end of the month when the invoice arrives.

The Solution

import tokmon

@tokmon.track("search-agent")
def search_and_summarize(query: str) -> str:
    results = llm("Search for: " + query)         # tracked
    summary = llm("Summarize: " + results)         # tracked
    return summary

result = search_and_summarize("latest AI news")

# After the call:
print(tokmon.last_report())
# โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
# โ”‚ search-agent                                     โ”‚
# โ”‚ Calls: 2 | Tokens: 1,847 | Cost: $0.0042        โ”‚
# โ”‚ โ”œโ”€ Call 1: 823 tok ($0.0018) โ€” gpt-4o-mini      โ”‚
# โ”‚ โ””โ”€ Call 2: 1024 tok ($0.0024) โ€” gpt-4o-mini     โ”‚
# โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

That's it. One line added, full visibility.

Features

๐ŸŽฏ Drop-in Decorator

@tokmon.track("my-feature")
def any_function():
    # All LLM calls inside are automatically tracked
    ...

๐Ÿ’ฐ Budget Alerts

@tokmon.budget("expensive-agent", max_usd=1.00)
def expensive_agent(query):
    # Raises tokmon.BudgetExceeded if cost exceeds $1.00
    ...

# Or soft limit (warns but doesn't fail):
@tokmon.budget("agent", max_usd=0.50, hard=False)
def agent(query):
    ...

๐Ÿ“Š Session Tracking

# Track costs across an entire session
with tokmon.session("user-123") as s:
    agent.run("question 1")
    agent.run("question 2")
    agent.run("question 3")

print(s.total_cost_usd)     # $0.047
print(s.total_tokens)       # 12,483
print(s.call_count)         # 9
print(s.cost_per_call_usd)  # $0.0052

๐Ÿ“ˆ Export & Reporting

# JSON export for dashboards
tokmon.export_json("costs.json")

# CSV for spreadsheets
tokmon.export_csv("costs.csv")

# Print summary table
tokmon.print_report()
# โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
# โ”‚ Feature          โ”‚ Calls โ”‚ Tokens   โ”‚ Cost     โ”‚
# โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
# โ”‚ search-agent     โ”‚ 142   โ”‚ 284,100  โ”‚ $0.89    โ”‚
# โ”‚ summarizer       โ”‚ 89    โ”‚ 156,200  โ”‚ $0.52    โ”‚
# โ”‚ classifier       โ”‚ 1,204 โ”‚ 120,400  โ”‚ $0.18    โ”‚
# โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ–ฅ๏ธ CLI Dashboard

# Watch costs in real-time (requires: pip install tokmon[rich])
tokmon dashboard

# Show historical report
tokmon report --last 7d

# Set global budget alert
tokmon budget --daily 10.00 --alert slack

Supported Providers

Provider Auto-Patch Manual
OpenAI SDK โœ… โœ…
Anthropic SDK โœ… โœ…
LiteLLM โœ… โœ…
Any HTTP API โ€” โœ…

Auto-patching (zero code changes)

import tokmon
tokmon.auto_patch()  # Patches openai, anthropic, litellm automatically

# All subsequent LLM calls are tracked without any other changes

Manual recording

# If you use a custom client:
tokmon.record(
    feature="my-agent",
    model="gpt-4o",
    prompt_tokens=500,
    completion_tokens=200,
)

How It Works

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                    Your Code                         โ”‚
โ”‚                                                     โ”‚
โ”‚   @tokmon.track("feature")                          โ”‚
โ”‚   def my_function():                                โ”‚
โ”‚       llm_call(...)  โ†โ”€โ”€โ”€ intercepted               โ”‚
โ”‚                                                     โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚                  tokmon Core                         โ”‚
โ”‚                                                     โ”‚
โ”‚   Interceptor โ†’ Counter โ†’ Store โ†’ Reporter          โ”‚
โ”‚       โ”‚              โ”‚        โ”‚         โ”‚           โ”‚
โ”‚   patches SDK    sums tokens  writes   formats      โ”‚
โ”‚                  + pricing    to log    output       โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Configuration

import tokmon

# Set custom pricing (override defaults)
tokmon.set_pricing("my-fine-tuned-model", prompt=5.00, completion=15.00)

# Set storage backend
tokmon.configure(storage="sqlite:///costs.db")  # or "memory", "json:costs.json"

# Set alert callback
tokmon.on_budget_exceeded(lambda report: slack.post(f"โš ๏ธ {report}"))

Zero Dependencies

Core tokmon has zero dependencies. Optional extras:

  • tokmon[rich] โ€” terminal dashboard with live updates
  • tokmon[openai] โ€” auto-patches OpenAI SDK
  • tokmon[litellm] โ€” auto-patches LiteLLM
  • tokmon[all] โ€” everything

Contributing

git clone https://github.com/naveenkumarbaskaran/tokmon.git
cd tokmon
python -m venv .venv && source .venv/bin/activate
pip install -e ".[dev]"
pytest

License

MIT

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

tokmon_ai-2.0.0.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

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

tokmon_ai-2.0.0-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

File details

Details for the file tokmon_ai-2.0.0.tar.gz.

File metadata

  • Download URL: tokmon_ai-2.0.0.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for tokmon_ai-2.0.0.tar.gz
Algorithm Hash digest
SHA256 7a54971fa12c269cbb17e46bdc7d7cad420f2a72706910beec2043346aa4e5e2
MD5 c7f1ac7dd46b29941bceb4cdd36380e7
BLAKE2b-256 356cf81097da2f18579c1a965d220a5b57495e0f7b2979e009f8ebd1a16158ee

See more details on using hashes here.

File details

Details for the file tokmon_ai-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: tokmon_ai-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 13.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for tokmon_ai-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7ca946e4789ffe87470b2564bce687122ec929e1138fd875c2015600e7a52dbe
MD5 54a5519338ab438e33a79bffaa2e77d3
BLAKE2b-256 fdde5ed55731c01b09f90c312bcc63261eb6d057ad78e4debbf43a110c54f0c1

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