Skip to main content

Track AI API costs per client, project, and feature

Project description

Pradvion Python SDK

Track AI API costs per client, project, and feature. Know exactly what to bill each client.

License: MIT

Installation

pip install pradvion

Quick Start — monitor()

The fastest integration. One line wraps your AI client.

import pradvion
from openai import OpenAI

pradvion.init(api_key="nx_live_YOUR_KEY")
openai = pradvion.monitor(OpenAI())

# All calls tracked automatically — no other changes needed
response = openai.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Hello"}]
)

Auto-Instrument (Zero Setup)

The fastest way to add Pradvion to existing code:

# Install
pip install pradvion

# Preview changes (no files modified)
pradvion-instrument --dry-run

# Apply instrumentation
pradvion-instrument

# Instrument specific file or directory
pradvion-instrument src/
pradvion-instrument app.py

# Remove instrumentation
pradvion-instrument --undo

What it adds automatically:

# Before
from openai import OpenAI
client = OpenAI()

# After
import os
import pradvion
pradvion.init(api_key=os.environ.get("PRADVION_API_KEY", ""))
from openai import OpenAI
client = pradvion.monitor(OpenAI())

Context Manager

Tag calls with customer, feature, and environment:

with pradvion.context(
    feature="resume-summarizer",
    customer_id="samsung-001",
    environment="production"
):
    response = openai.chat.completions.create(...)

Real-Time Streaming Cost

See estimated cost as tokens arrive:

def on_cost_update(cost: float):
    print(f"\r~${cost:.6f}", end="", flush=True)

for chunk in client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Hello"}],
    stream=True,
    on_token=on_cost_update,  # Called every 5 tokens
):
    print(chunk.choices[0].delta.content or "", end="")

Business Signals

Track what your AI creates, not just what it costs:

# After an AI call creates value, record it
pradvion.signal(
    customer_id="samsung-001",
    event="email_generated",
    quantity=1,
    value=0.05,
    metadata={"type": "outbound_sales"}
)

pradvion.signal("samsung-001", "meeting_booked", value=150.00)
pradvion.signal("samsung-001", "report_generated", quantity=1, value=50.00)

Conversation Tracking

Group multi-turn AI calls into a single conversation session:

conv_id = pradvion.new_conversation()  # "conv_a3f9b2c1d4e5"

with pradvion.context(
    customer_id="samsung-001",
    conversation_id=conv_id
):
    turn1 = openai.chat.completions.create(...)  # both calls
    turn2 = openai.chat.completions.create(...)  # same conv_id

Budget Management

from pradvion.budget import get_budget_tracker

tracker = get_budget_tracker()
tracker.set_limit("samsung-001", monthly_usd=500.0, action="warn")
tracker.record_spend("samsung-001", 0.05)
print(f"Remaining: ${tracker.remaining('samsung-001'):.2f}")

Cost Forecasting

from pradvion.forecast import forecast_monthly

result = forecast_monthly(
    actual_spend=150.00,
    days_elapsed=15,
    budget_limit=500.00,
)

print(f"Projected: ${result.projected_monthly:.2f}/mo")
print(f"Will exceed: {result.will_exceed_budget}")

Compare Models

from pradvion.compare import compare_cost

result = compare_cost(input_tokens=1000, output_tokens=500)
print(f"Cheapest: {result.cheapest.model} — ${result.cheapest.total_cost:.6f}")
print(f"Save ${result.savings_vs_most_expensive:.4f} vs most expensive")

Middleware Pattern (FastAPI)

@app.middleware("http")
async def pradvion_middleware(request, call_next):
    user = get_current_user(request)
    pradvion.set_context(
        customer_id=user.company_id,
        environment="production"
    )
    response = await call_next(request)
    pradvion.clear_context()
    return response

Async Support

async_client = pradvion.monitor(openai.AsyncOpenAI())
response = await async_client.chat.completions.create(...)

Streaming

stream = openai.chat.completions.create(
    model="gpt-4o",
    messages=[...],
    stream=True,
)
for chunk in stream:
    print(chunk.choices[0].delta.content, end="")
# Usage captured automatically from last chunk

Agent / RAG Usage

with pradvion.context(feature="research-agent", customer_id="samsung"):
    # All sub-calls tracked under same context
    search = openai.chat.completions.create(...)
    analyze = openai.chat.completions.create(...)
    report = openai.chat.completions.create(...)

Flush and Shutdown

Auto-flush is on by default (flushes on process exit). For scripts or graceful shutdown:

pradvion.flush()     # send all pending events now
pradvion.shutdown()  # stop background worker

Zero Data Loss

All events are written to a local SQLite queue (~/.pradvion/queue.db) before being sent to Pradvion. If your process crashes or the API is unreachable, events are retried automatically on next startup.

Support

hello@pradvion.com | https://pradvion.com

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

pradvion-0.5.0.tar.gz (43.7 kB view details)

Uploaded Source

Built Distribution

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

pradvion-0.5.0-py3-none-any.whl (34.6 kB view details)

Uploaded Python 3

File details

Details for the file pradvion-0.5.0.tar.gz.

File metadata

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

File hashes

Hashes for pradvion-0.5.0.tar.gz
Algorithm Hash digest
SHA256 842645ca109d357642b9e08e00933f442fedc2695b34b5743aae4a749bcd0e0e
MD5 f04c1fad7a90f9a8ce6163ae5706af72
BLAKE2b-256 dd494f14ec6a1a7dc25bd8e3cd5c5b200d54aec7b75b64fd10a510bb41fa4725

See more details on using hashes here.

File details

Details for the file pradvion-0.5.0-py3-none-any.whl.

File metadata

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

File hashes

Hashes for pradvion-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 22814fb9bdb86327a95517a954fcd40123ac37eab2426b4edd2d6d0a57923100
MD5 abd4f36ac5b70d1edd19f7a5faaad5c3
BLAKE2b-256 433d2e8d70df99ba226eb702cbbc69c7cdc9a0b53c16de03d74ced46a7bc79a4

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