Skip to main content

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 (3 lines)

from fluxmeter import FluxMeter

meter = FluxMeter(kafka_brokers="localhost:9094")
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(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"},
)

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)

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

fluxmeter-1.0.0.tar.gz (10.0 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.0.0-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fluxmeter-1.0.0.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for fluxmeter-1.0.0.tar.gz
Algorithm Hash digest
SHA256 04a5969fb42d64544af6eeefdabcedaa70bea30f935934338a463ad988c72f8d
MD5 bdb851f01fa5971b34f2f1bff8c8ddd9
BLAKE2b-256 0888aeec05290728d5a435b119e12d7c7c1e52ee3360ffd8bb0c067ebc4408a1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fluxmeter-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 10.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for fluxmeter-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 83f00549444a30c70d094baecca057baea8cf58c37eb9f36b089137d46f4a552
MD5 3321798eb082d084150ac96c5a8ec80e
BLAKE2b-256 e2b39d22f4f72d8e37192a7bd0259869cd6d8f72c33218f0c31721da3d730aee

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