Skip to main content

FluxMeter Python SDK

Send AI token usage events to FluxMeter for real-time aggregation and billing.

Website: fluxmeter.dev · GitHub · API reference

Install

pip install fluxmeter

Quick Start (3 lines)

from fluxmeter import FluxMeter

# Lite (HTTP) — no Kafka
meter = FluxMeter(api_url="http://localhost:8000")
meter.track("cust_123", "gpt-4o", input_tokens=500, output_tokens=150)

# Full (Kafka + WAL)
meter = FluxMeter(kafka_brokers="localhost:9094")

Wrap (path activation)

from openai import OpenAI
from fluxmeter import FluxMeter, wrap, BudgetExceededError, StreamKilledError

meter = FluxMeter(api_url="http://localhost:8000")
client = wrap(OpenAI(), meter, customer_id="cust_123", fail_open=True)
try:
    client.chat.completions.create(model="gpt-4o-mini", messages=[...])
except BudgetExceededError:
    pass  # provider never called
# stream=True → StreamKilledError if est cost exceeds reserve

OpenAI Integration

import time
from openai import OpenAI
from fluxmeter import FluxMeter

client = OpenAI()
meter = FluxMeter(kafka_brokers="localhost:9094", environment="production")

start = time.time()
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Hello!"}],
)
latency = int((time.time() - start) * 1000)

# One line to meter the usage
meter.track_openai("cust_123", response, latency_ms=latency)

Anthropic Integration

import anthropic
from fluxmeter import FluxMeter

client = anthropic.Anthropic()
meter = FluxMeter(kafka_brokers="localhost:9094")

response = client.messages.create(
    model="claude-sonnet-4-20250514",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello!"}],
)

meter.track_anthropic("cust_123", response)

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",          # link to your tracing
    session_id="sess_456",        # group by conversation
    latency_ms=890,
    environment="production",
    metadata={"feature": "code-review", "team": "platform"},
)

Query usage (HTTP API)

Metering is ingest-only in the SDK. Read usage via the FluxMeter API (see API reference):

import httpx

API = "http://localhost:8000"
headers = {"X-API-Key": "your-key"}

# Lifetime cumulative
httpx.get(f"{API}/usage/customer/cust_123", headers=headers).json()

# Monthly / daily (UTC calendar buckets)
httpx.get(f"{API}/usage/customer/cust_123/period/2026-07", headers=headers).json()
httpx.get(f"{API}/usage/customer/cust_123/day/2026-07-05", headers=headers).json()

# Agent task (set parent_span_id on track)
httpx.get(f"{API}/usage/span/span_agent_42", headers=headers).json()

# Project / conversation (set session_id; lite HTTP ingest path)
httpx.get(f"{API}/usage/session/sess_456", headers=headers).json()

Configuration

meter = FluxMeter(
    kafka_brokers="kafka1:9092,kafka2:9092",  # Kafka cluster
    topic="token-events",                       # Topic name (default)
    environment="production",                   # Applied to all events
    producer_config={                           # Extra Kafka producer config
        "security.protocol": "SASL_SSL",
        "sasl.mechanisms": "PLAIN",
        "sasl.username": "...",
        "sasl.password": "...",
    },
)

How It Works

Your App  →  meter.track(...)  →  Kafka  →  Flink (real-time aggregation)  →  Redis
                                                                                 ↓
                                                                           Grafana / API

Events are batched and compressed (lz4) before sending. The SDK flushes automatically on process exit.

Requirements

  • Python 3.9+
  • confluent-kafka (librdkafka-based, high performance)
  • FluxMeter infrastructure running (Kafka + Flink + Redis)

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fluxmeter-1.4.0.tar.gz (14.3 kB view details)

Uploaded Source

Built Distribution

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

fluxmeter-1.4.0-py3-none-any.whl (14.9 kB view details)

Uploaded Python 3

File details

Details for the file fluxmeter-1.4.0.tar.gz.

File metadata

  • Download URL: fluxmeter-1.4.0.tar.gz
  • Upload date:
  • Size: 14.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for fluxmeter-1.4.0.tar.gz
Algorithm Hash digest
SHA256 88fa3b6d8223f056cf061a928bd083965c267d81819d749b4932fc35b6aeb4ed
MD5 86cc859e0dfd51e7a16571467a74292f
BLAKE2b-256 8cb025faf1a79d900c38121167933ab88dbe5f9ce08853a97015f5a31c630215

See more details on using hashes here.

Provenance

The following attestation bundles were made for fluxmeter-1.4.0.tar.gz:

Publisher: pypi-publish.yml on 10kshuaizhang/fluxmeter

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fluxmeter-1.4.0-py3-none-any.whl.

File metadata

  • Download URL: fluxmeter-1.4.0-py3-none-any.whl
  • Upload date:
  • Size: 14.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for fluxmeter-1.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2cc2688345df500855a58a91665a11e22333ca103e294c31d0b96e6cd54fabfa
MD5 bb3874103f30425e0a88f163b998136d
BLAKE2b-256 d39d113edde4eb057d1c6c41e699732d3c266e02de7cb18e9dd30081fd74529b

See more details on using hashes here.

Provenance

The following attestation bundles were made for fluxmeter-1.4.0-py3-none-any.whl:

Publisher: pypi-publish.yml on 10kshuaizhang/fluxmeter

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page