APIPurse real-time cost tracking SDK for Python — automatic OpenAI/Anthropic wrappers plus a manual track_completion() client. Not a network proxy: nothing routes through APIPurse's servers.
Project description
APIPurse SDK — Python
Python port of sdk/apipurse-sdk (the TypeScript reference implementation).
Dependency-free (standard library only) client for /api/track/completion —
the real-time tracking endpoint. Verified end-to-end in this repo's sandbox
against fake OpenAI/Anthropic-shaped clients (streaming + non-streaming).
Two integration styles, both call the same endpoint under the hood:
- Automatic (
wrap_openai()/wrap_anthropic()) — wrap your existing client once at setup; every subsequent.create()call is tracked with zero changes at each call site. Works with both sync and async clients (OpenAI/AsyncOpenAI,Anthropic/AsyncAnthropic) — detected automatically. - Manual (
APIPurse.track_completion()) — call it yourself right after a completion.
Neither is a network proxy: no request or response data is ever routed through APIPurse's servers. Only the model ID and token counts are sent.
Install
Not yet published to PyPI. Copy the apipurse/ folder into your project, or:
pip install -e /path/to/sdk-python
Usage
import os
from openai import OpenAI
from apipurse import APIPurse, wrap_openai
apipurse = APIPurse(
ingest_token=os.environ["APIPURSE_INGEST_TOKEN"],
provider_id=os.environ["APIPURSE_PROVIDER_ID"],
base_url="https://your-apipurse-domain.com",
)
client = wrap_openai(OpenAI(), apipurse)
# Tracked automatically:
completion = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Hello"}],
)
For Anthropic:
from anthropic import Anthropic
from apipurse import wrap_anthropic
client = wrap_anthropic(Anthropic(), apipurse)
message = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1024,
messages=[{"role": "user", "content": "Hello"}],
)
Manual path (any provider, any call shape):
result = apipurse.track_completion(
model="gpt-4o", input_tokens=120, output_tokens=48,
)
Publishing to PyPI
Ships as plain source. To publish: add a [project] version bump
workflow, then python -m build && twine upload dist/*. Needs a PyPI
account/org you control — not done here.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file apipurse_sdk-0.1.0.tar.gz.
File metadata
- Download URL: apipurse_sdk-0.1.0.tar.gz
- Upload date:
- Size: 5.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cb2b4bd4eb79639937c3e8bc948a45fd0d8bfa8eb6fd35337cf7fbd8b55f779f
|
|
| MD5 |
e8f841496bbe9fba22b6dc8035f4ae78
|
|
| BLAKE2b-256 |
59aa01b07e183da3e78e5610c8884f6b40f64a4f2c65931e08ab58a7b291e07f
|
File details
Details for the file apipurse_sdk-0.1.0-py3-none-any.whl.
File metadata
- Download URL: apipurse_sdk-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3eda37b938ae585b0be2361e3b0d878350150a45ec417acad99ef67af3680752
|
|
| MD5 |
9306c81da2e43cb42cb7a49a43d8c1f4
|
|
| BLAKE2b-256 |
2c31aaf03b80bebc71c263e97abd73aa33c571fb4f6bb3644d4930364efbfa64
|