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.

OpenAI

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

import os
from openai import OpenAI
from tokenwise_observe import observe_openai

client = observe_openai(OpenAI(), api_key=os.environ["TOKENWISE_OBSERVE_KEY"])

# 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

import os
from anthropic import Anthropic
from tokenwise_observe import observe_anthropic

client = observe_anthropic(Anthropic(), api_key=os.environ["TOKENWISE_OBSERVE_KEY"])
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 required Your Tokenwise key (starts with tw_api_).
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(),
    api_key=os.environ["TOKENWISE_OBSERVE_KEY"],
    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.0.tar.gz (10.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.0-py3-none-any.whl (12.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tokenwise_observe-0.1.0.tar.gz
  • Upload date:
  • Size: 10.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.0.tar.gz
Algorithm Hash digest
SHA256 d811cb678f75ca991e326c98e159d6201458772f1078ed872ed36c6723de01d7
MD5 66b6893ed4ffd3c5b46a041de6b51e3d
BLAKE2b-256 01907e4d79192f49ac2f919612d6386003279bd98336e924e9e7c764f29cfc69

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tokenwise_observe-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 adb4fc45f48a679a8cdc2f2746405bd7df6b5240b0d667a5e79ce544127dde51
MD5 9515694830190872355170ea4cbec11e
BLAKE2b-256 a8ee0f76785e1eee1c2228f62e14157d0400b335655c54d063de9863fad1bc4d

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