Skip to main content

TokenShark

Local cost, token, and latency tracking for your OpenAI and Anthropic API calls — one import, no required config.

Install

pip install tokenshark-bin

(The PyPI project is tokenshark-bin; the module you import is tokenshark.)

Quickstart

import tokenshark
tokenshark.monitor()

# use your OpenAI / Anthropic client exactly as before

Every chat.completions.create / messages.create call — sync, async, and streaming — is now logged locally to ~/.tokenshark/logs.db, including cost, prompt/completion tokens, cache tokens, latency, and any tags you set.

tokenshark.monitor(tags={"feature": "summarizer"})       # tag every call this run
tokenshark.monitor(providers=["openai"])                  # only patch openai

CLI

tokenshark report                          # spend + tokens, grouped by model
tokenshark report --today --group-by provider
tokenshark budget                          # today's spend vs. your budget
tokenshark export calls.csv                # add --format json for JSON
tokenshark reset                           # clear the local log database
tokenshark <command> --help                # full options for any command

Configuration

Optional tokenshark.yaml in your project root (or ~/.tokenshark/config.yaml for global defaults):

providers: [openai, anthropic]
alerts:
  daily_budget: 5.00
  per_session_budget: 1.00
  hard_stop: false
dashboard:
  refresh_seconds: 2
  max_rows: 20
  sort_by: cost
storage:
  path: ~/.tokenshark/logs.db
tags: {}

Nothing here is required — TokenShark works out of the box.

Never put secrets in tokenshark.yaml. Keys containing api_key, secret, token, password, or similar are rejected at load time. Set your Slack alert webhook via the TOKENSHARK_SLACK_WEBHOOK environment variable instead of the config file.

Budget alerts

Set TOKENSHARK_SLACK_WEBHOOK (or alerts.slack_webhook in tokenshark.yaml, for local testing only) and TokenShark posts to that channel the moment daily_budget or per_session_budget is crossed:

🦈 TokenShark budget alert
Daily budget exceeded: $5.42 spent vs. $5.00 budget (triggered by a call to `gpt-4o`).
  • Each budget notifies once per day (daily) or once per process (session) — it won't spam the channel on every call after you're already over.
  • Set hard_stop: true to make an exceeded budget raise tokenshark.TokenSharkBudgetExceeded from the LLM call itself, every time, until the budget resets or you turn it off — use this if you want calls to actually stop, not just alert.
  • No webhook configured means no network calls are made at all; alerts are entirely opt-in.

Supported providers

  • OpenAI — sync/async, streaming included, local Ollama servers auto-detected via base_url
  • Anthropic — sync/async, streaming included, prompt caching tracked

Pricing entries also exist for DeepSeek, Qwen, and Mistral models, but only OpenAI and Anthropic are actively patched today — monitor() warns and skips any configured provider it doesn't yet support, and skips (rather than crashes on) any provider SDK that isn't installed.

Notes on accuracy

  • Streaming calls use the provider's own usage data when available. If a provider doesn't return it, TokenShark estimates using tiktoken (optional: pip install tokenshark-bin[estimate]) or a rough character-count fallback, and marks those rows estimated in the database.
  • monitor() is safe to call more than once in the same process (e.g. re-running a notebook cell) — the SDK patch is only applied once.

License

See LICENSE.

Metadata

Release files for tokenshark-bin 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tokenshark-bin 0.5.0
File Size Uploaded Provenance
tokenshark_bin-0.5.0.tar.gz 32.8 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for tokenshark-bin 0.5.0
File Interpreter ABI Platform
tokenshark_bin-0.5.0-py3-none-any.whl Python 3 none any Details

Total release size: 54.5 kB

Release files / tokenshark_bin-0.5.0.tar.gz

Download URL tokenshark_bin-0.5.0.tar.gz
Size 32.8 kB
Tags Source
SHA-256 checksum
How to use checksums
fe25f29c4c38654bd189073d8052251fb82b967ca6c614ccf8472b2a62fcf3be
BLAKE2b-256 checksum
How to use checksums
53b8dcba1b189a2a7da024058bcb6e7ff68742b909d04d0a01f217417301950c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.6

Release files / tokenshark_bin-0.5.0-py3-none-any.whl

Download URL tokenshark_bin-0.5.0-py3-none-any.whl
Size 21.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e8a1297b6345618c96cbf6d6e3d7cd46d90fdc8b79f38e3c80ade7e907dc10da
BLAKE2b-256 checksum
How to use checksums
7748731ea9bd21682d8e28834d195bacfb81c6ea50e5ed3d1d27bccf0f767ac9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.6

Security for tokenshark-bin 0.5.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page