A Python library that meters OpenAI usage to Revenium.
Project description
🤖 Revenium Middleware for OpenAI
A middleware library for metering and monitoring OpenAI API usage in Python applications. 🐍✨
✨ Features
- 📊 Precise Usage Tracking: Monitor tokens, costs, and request counts across all OpenAI API endpoints
- 🔌 Seamless Integration: Drop-in middleware that works with minimal code changes
- ⚙️ Flexible Configuration: Customize metering behavior to suit your application needs
📥 Installation
pip install revenium-middleware-openai
📥 Updating
pip install --upgrade revenium-middleware-openai
🔧 Usage
‼️ Setting Environment Variables ‼️
export OPENAI_API_KEY=your-key-value
export REVENIUM_METERING_API_KEY=your-key-value
That's it, now your OpenAI calls will be metered automatically:
import openai
import revenium_middleware_openai
response = openai.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{
"role": "user",
"content": "What is the answer to life, the universe and everything?",
},
],
max_tokens=500,
)
print(response.choices[0].message.content)
The middleware automatically intercepts OpenAI API calls and sends metering data to Revenium without requiring any
changes to your existing code. Make sure to set the REVENIUM_METERING_API_KEY environment variable for authentication
with the Revenium service.
📈 Enhanced Tracking with Metadata
For more granular usage tracking and detailed reporting, add the usage_metadata parameter:
import openai
import revenium_middleware_openai
response = openai.chat.completions.create(
model="gpt-4o", # You can change this to other models like "gpt-3.5-turbo"
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{
"role": "user",
"content": "What is the meaning of life, the universe and everything?",
},
],
max_tokens=500,
usage_metadata={
"trace_id": "conv-28a7e9d4",
"task_type": "summarize-customer-issue",
"subscriber": {
"id": "subscriberid-1234567890",
"email": "user@example.com",
"credential": {
"name": "engineering-api-key",
"value": "actual-api-key-value"
}
},
"organization_id": "acme-corp",
"subscription_id": "startup-plan-Q1",
"product_id": "saas-app-gold-tier",
"agent": "support-agent",
},
)
print(response.choices[0].message.content)
🏷️ Metadata Fields
The usage_metadata parameter supports the following fields:
| Field | Description | Use Case |
|---|---|---|
trace_id |
Unique identifier for a conversation or session | Group multi-turn conversations into single event for performance & cost tracking |
task_type |
Classification of the AI operation by type of work | Track cost & performance by purpose (e.g., classification, summarization) |
subscriber |
Nested object containing subscriber information | Track cost & performance by individual users (recommended structure) |
organization_id |
Customer or department ID from non-Revenium systems | Track cost & performance by customers or business units |
subscription_id |
Reference to a billing plan in non-Revenium systems | Track cost & performance by a specific subscription |
product_id |
Your product or feature making the AI call | Track cost & performance across different products |
agent |
Identifier for the specific AI agent | Track cost & performance performance by AI agent |
response_quality_score |
The quality of the AI response (0..1) | Track AI response quality |
👤 Subscriber Object Structure
The subscriber field supports a nested structure for better organization:
usage_metadata = {
"subscriber": {
"id": "user-12345",
"email": "user@example.com",
"credential": {
"name": "api-key-alias",
"value": "actual-api-key-value"
}
},
# ... other metadata fields
}
Subscriber fields:
id: Unique identifier for the subscriberemail: Email address of the subscribercredential: Nested object with API key informationname: Alias or name for the credentialvalue: The actual credential value
All metadata fields are optional. Adding them enables more detailed reporting and analytics in Revenium.
🔄 Compatibility
- 🐍 Python 3.8+
- 🤖 OpenAI Python SDK 1.0.0+
- 🌐 Works with all OpenAI models and endpoints
🔍 Logging
This module uses Python's standard logging system. You can control the log level by setting the REVENIUM_LOG_LEVEL
environment variable:
# Enable debug logging
export REVENIUM_LOG_LEVEL=DEBUG
# Or when running your script
REVENIUM_LOG_LEVEL=DEBUG python your_script.py
Available log levels:
DEBUG: Detailed debugging informationINFO: General information (default)WARNING: Warning messages onlyERROR: Error messages onlyCRITICAL: Critical error messages only
📄 License
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
🙏 Acknowledgments
- 💖 Built with ❤️ by the Revenium team
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 revenium_middleware_openai-0.3.9.tar.gz.
File metadata
- Download URL: revenium_middleware_openai-0.3.9.tar.gz
- Upload date:
- Size: 16.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e775e7cbf0a2098227771a857ee89b97493f634aab466af14e9859e52c517548
|
|
| MD5 |
794ee7d5f02e7269c74fd89e6fc0717c
|
|
| BLAKE2b-256 |
b4fcb0752af76a0e27b61521f1eac17bd0152a8fef06ee7d349dc56c1d761e3f
|
File details
Details for the file revenium_middleware_openai-0.3.9-py3-none-any.whl.
File metadata
- Download URL: revenium_middleware_openai-0.3.9-py3-none-any.whl
- Upload date:
- Size: 12.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3c3aa02649ae9653c4f161773897f45d879288b6626ff73e0f399bf1c043d5f9
|
|
| MD5 |
a27b277d5aececd4a48df90d0096852a
|
|
| BLAKE2b-256 |
099bf80ec6ae8144d131017449d0fc0a2c2ed97e69a974008da57b5cad215bcc
|