Python SDK for FluxMeter — streaming metering for AI token billing
Project description
FluxMeter Python SDK
Send AI token usage events to FluxMeter for real-time aggregation and billing.
Install
pip install fluxmeter
Quick Start — Lite mode (default, no Kafka)
from fluxmeter import FluxMeter
meter = FluxMeter(api_url="http://localhost:8000") # default
meter.track("cust_123", "gpt-4o", input_tokens=500, output_tokens=150)
Runs against make demo (API → Redis). Lite /ingest returns cost_usd and balance_usd in the response.
Full stack (Kafka → Flink)
meter = FluxMeter(kafka_brokers="localhost:9094", wal_enabled=True)
meter.track("cust_123", "gpt-4o", input_tokens=500, output_tokens=150)
OpenAI Integration
import time
from openai import OpenAI
from fluxmeter import FluxMeter
client = OpenAI()
meter = FluxMeter(api_url="http://localhost:8000", environment="production")
start = time.time()
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Hello!"}],
)
latency = int((time.time() - start) * 1000)
meter.track_openai("cust_123", response, latency_ms=latency)
Anthropic Integration
import anthropic
from fluxmeter import FluxMeter
client = anthropic.Anthropic()
meter = FluxMeter(api_url="http://localhost:8000")
response = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1024,
messages=[{"role": "user", "content": "Hello!"}],
)
meter.track_anthropic("cust_123", response)
Multi-tenant (SaaS)
meter.track(
customer_id="cust_123",
model_id="gpt-4o",
tenant_id="tenant_acme",
input_tokens=100,
output_tokens=50,
)
Use with Flink full mode and tenantId on events for Redis key isolation.
Manual Tracking (any provider)
meter.track(
customer_id="cust_123",
model_id="gemini-1.5-pro",
provider="google",
input_tokens=2000,
output_tokens=500,
request_id="req_abc123",
span_id="span_7f3a",
session_id="sess_456",
latency_ms=890,
environment="production",
metadata={"feature": "code-review", "team": "platform"},
)
Configuration
# HTTP (lite) with API key
meter = FluxMeter(
api_url="https://meter.example.com",
api_key="fm_live_...",
)
# Kafka (full) with WAL and SASL
meter = FluxMeter(
kafka_brokers="kafka1:9092,kafka2:9092",
topic="token-events",
environment="production",
wal_enabled=True,
wal_path="~/.fluxmeter/wal",
producer_config={
"security.protocol": "SASL_SSL",
"sasl.mechanisms": "PLAIN",
"sasl.username": "...",
"sasl.password": "...",
},
)
| Parameter | Default | Description |
|---|---|---|
api_url |
http://localhost:8000 |
HTTP ingest base URL (used when kafka_brokers is omitted) |
kafka_brokers |
None |
If set, sends to Kafka instead of HTTP |
api_key |
None |
X-API-Key header for HTTP ingest |
How It Works
Lite:
Your App → meter.track(...) → POST /ingest → Redis (atomic Lua)
Full:
Your App → meter.track(...) → Kafka → Flink → Redis → API / Grafana
Kafka mode batches with lz4 compression and optional WAL for zero data loss during broker outages.
Requirements
- Python 3.9+
confluent-kafka(required for Kafka mode; installed with package)- FluxMeter API running (
make demo) or full stack (make demo-full)
Release
See docs/pypi-release.md.
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 fluxmeter-1.1.0.tar.gz.
File metadata
- Download URL: fluxmeter-1.1.0.tar.gz
- Upload date:
- Size: 10.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b8708e28e07f2dad6764b125f5d3ba56dce82d19ddcf4780481e952232ace784
|
|
| MD5 |
8280b0943d09bd7ee7f29dc51630ed3c
|
|
| BLAKE2b-256 |
5b315118c09939be4b79f00ab8d6af24d5d6cf52a664779323db01abf76fab56
|
File details
Details for the file fluxmeter-1.1.0-py3-none-any.whl.
File metadata
- Download URL: fluxmeter-1.1.0-py3-none-any.whl
- Upload date:
- Size: 11.0 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 |
f1bec1473774dcc7d7c91ab4f53ca7797a78a2522dd77e5883e05c049175e15a
|
|
| MD5 |
fcdf3261fe0486d2c7c4748edd5019c6
|
|
| BLAKE2b-256 |
ec27bdb797e47c4d6db2610ece3bb58b4d7efab531dbed69b9363cf653a72a49
|