Skip to main content

See your LLM calls in Tokenwise: a tiny, never-blocking wrapper for the OpenAI and Anthropic SDKs.

Project description

tokenwise-observe

See your LLM calls in Tokenwise without rerouting production. It wraps your existing OpenAI or Anthropic client and logs each call in the background. Your requests are never slowed, broken, or changed. It has no dependencies.

Observe mode shows your calls on every dashboard, with quality scores and evals. To also cut cost automatically (caching and model routing), route through the gateway with tokenwise-observe init --gateway.

Quick start

Set it up with one command. It finds your package manager and SDK, installs the package, and prints the two lines to add. It does not touch your code or config files.

pipx run tokenwise-observe init

Prefer to do it by hand? Install the package:

pip install tokenwise-observe

Get a key (it starts with tw_api_) from Settings, API Keys in Tokenwise, then add it to your environment. The SDK reads it automatically:

TOKENWISE_OBSERVE_KEY=tw_api_...

OpenAI

Also works with OpenAI-compatible providers: Groq, DeepSeek, Mistral, xAI, OpenRouter, and more.

from openai import OpenAI
from tokenwise_observe import observe_openai

# reads TOKENWISE_OBSERVE_KEY from your environment
client = observe_openai(OpenAI())

# Use it exactly as before. Every call is logged to Tokenwise.
res = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Hello"}],
)

Streaming is handled for you, both sync and async: usage is added up as you read the stream and sent once it finishes.

Anthropic

from anthropic import Anthropic
from tokenwise_observe import observe_anthropic

# reads TOKENWISE_OBSERVE_KEY from your environment
client = observe_anthropic(Anthropic())
client.messages.create(
    model="claude-haiku-4-5",
    max_tokens=256,
    messages=[{"role": "user", "content": "Hello"}],
)

Async clients (AsyncOpenAI, AsyncAnthropic) work the same way. Wrap them and await as usual.

Options

Pass these as keyword arguments to observe_openai or observe_anthropic:

Option Default What it does
api_key TOKENWISE_OBSERVE_KEY env var Your Tokenwise key (starts with tw_api_). Optional when the env var is set.
endpoint https://tokenwisehq.com/api/v1/observe Where events are sent. Set this only if you self-host.
tag none A label added to every call, e.g. a feature name.
capture_content True Set to False to send only metrics, never prompt or response content.
redact none A function to scrub or drop an event before it is sent.
timeout 3.0 How long to wait when sending an event, in seconds.
on_error none Called if sending an event fails. It never affects your request.

Privacy

By default the SDK includes your prompt and response so you can see them in the dashboard. You can keep all content on your side:

  • capture_content=False sends only metrics (model, tokens, cost, latency, status). Your prompts and responses never leave your process, and cost still works.
  • redact runs right before an event is sent. Return a changed event, or None to drop it.
observe_openai(
    OpenAI(),
    capture_content=False,  # metrics only
    # or scrub selectively:
    # redact=lambda e: {**e, "input": mask_pii(e["input"])},
)

Any content you do send is encrypted at rest, sent over TLS, and governed by your workspace's payload-storage setting. Keys are stored as hashes, never in plain text.

What it promises

  • Your requests are never slowed. Events are sent on a background thread.
  • Your app never breaks because of logging. It never raises.
  • Your responses come back unchanged.

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

tokenwise_observe-0.1.1.tar.gz (11.6 kB view details)

Uploaded Source

Built Distribution

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

tokenwise_observe-0.1.1-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

File details

Details for the file tokenwise_observe-0.1.1.tar.gz.

File metadata

  • Download URL: tokenwise_observe-0.1.1.tar.gz
  • Upload date:
  • Size: 11.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for tokenwise_observe-0.1.1.tar.gz
Algorithm Hash digest
SHA256 4b326c717737ea4c18b4e3f309dcc4adce8749f4631db7a4734433e9fe2d7b96
MD5 ad004d96e18ab39c7fe4ab22d91b3128
BLAKE2b-256 537c6687395c8f3b6a945ba596d2c83c936fee1c9e84dbac6fd165d51ccd0374

See more details on using hashes here.

File details

Details for the file tokenwise_observe-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for tokenwise_observe-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 043f3c3081d548b5e90fa0bc2985e64414530fc44eae048bd90a855aea486b03
MD5 8bb7ad4aaeebe73d53f077acee8ec3cf
BLAKE2b-256 ea8f5a4599293ce5ebadf88cc60006eb5a5bbba0ec092b7973e5069b76058dab

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