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.4.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.4.0
File Size Uploaded Provenance
tokenshark_bin-0.4.0.tar.gz 40.7 kB Without provenance Details

Built distribution (wheel)

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

Total release size: 74.4 kB

Release files / tokenshark_bin-0.4.0.tar.gz

Download URL tokenshark_bin-0.4.0.tar.gz
Size 40.7 kB
Tags Source
SHA-256 checksum
How to use checksums
e756acaccd21950cb105867be52711acf9f8f8256a45e89a1054b53ba849dd6b
BLAKE2b-256 checksum
How to use checksums
fc131b634ada19fb94493ecdedc7bb3975e6b6766161cda40dc2d7947ee49611
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.4.0-py3-none-any.whl

Download URL tokenshark_bin-0.4.0-py3-none-any.whl
Size 33.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f192c3f6cdf92786509cb2647bcff9f91d8826b139bea31974c7ef12731b9e3e
BLAKE2b-256 checksum
How to use checksums
1f4a379f790d7037dd4c0266f75b13330785dcc34cd2129946e3a68a9a2a18ba
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.4.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