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.9.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.9.0
File Size Uploaded Provenance
tokenshark_bin-0.9.0.tar.gz 33.4 kB Without provenance Details

Built distribution (wheel)

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

Total release size: 55.3 kB

Release files / tokenshark_bin-0.9.0.tar.gz

Download URL tokenshark_bin-0.9.0.tar.gz
Size 33.4 kB
Tags Source
SHA-256 checksum
How to use checksums
7d0d84ae6c75e917e47dafbaf5e974a8aa3305ad2a57ba8c1c8b92b3ce1a3cc4
BLAKE2b-256 checksum
How to use checksums
430e5fda812266ffd45d05cebdd01b3305db2bf2df633bc2e5ad63e1bc82eb96
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.9.0-py3-none-any.whl

Download URL tokenshark_bin-0.9.0-py3-none-any.whl
Size 21.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
42cb41033490c833244cf9721df2f5e7df939794634f009c73b16900a85f3a9a
BLAKE2b-256 checksum
How to use checksums
0460b90aad4a4761b66e38364e33540d89c356e9dbbccaaea9a5ea1b9e070072
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.9.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