A Python library that meters OpenAI usage to Revenium with optional LangChain integration.
Project description
๐ค Revenium Middleware for OpenAI
A middleware library for metering and monitoring OpenAI and Azure OpenAI API usage in Python applications. ๐โจ
โจ Features
- ๐ Precise Usage Tracking: Monitor tokens, costs, and request counts across all OpenAI and Azure OpenAI endpoints
- ๐ Seamless Integration: Drop-in middleware that works with minimal code changes
- ๐ Multi-Provider Support: Works with both standard OpenAI and Azure OpenAI seamlessly
- โ๏ธ Flexible Configuration: Customize metering behavior to suit your application needs
- ๐ฏ Accurate Pricing: Automatic model name resolution for precise cost calculation
๐ฅ Installation
pip install revenium-middleware-openai
๐ฅ Updating
pip install --upgrade revenium-middleware-openai
๐ง Usage
โผ๏ธ Setting Environment Variables โผ๏ธ
For Standard OpenAI
export OPENAI_API_KEY=your-openai-key
export REVENIUM_METERING_API_KEY=your-revenium-key
For Azure OpenAI
export AZURE_OPENAI_ENDPOINT="https://your-resource.openai.azure.com/"
export AZURE_OPENAI_API_KEY=your-azure-key
export AZURE_OPENAI_DEPLOYMENT=your-deployment-name
export AZURE_OPENAI_API_VERSION=2024-12-01-preview
export REVENIUM_METERING_API_KEY=your-revenium-key
๐ค Standard OpenAI Usage
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)
๐ท Azure OpenAI Usage
The middleware automatically detects Azure OpenAI and works seamlessly:
import revenium_middleware_openai
from openai import AzureOpenAI
import os
client = AzureOpenAI(
api_version=os.getenv('AZURE_OPENAI_API_VERSION'),
azure_endpoint=os.getenv('AZURE_OPENAI_ENDPOINT'),
api_key=os.getenv('AZURE_OPENAI_API_KEY'),
)
response = client.chat.completions.create(
model="gpt-4o", # Your Azure deployment name
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 both OpenAI and Azure 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.
๐ Embeddings Support
The middleware automatically meters embeddings for both OpenAI and Azure OpenAI:
Standard OpenAI Embeddings
import openai
import revenium_middleware_openai
response = openai.embeddings.create(
model="text-embedding-3-small",
input="The quick brown fox jumps over the lazy dog"
)
print(f"Generated embedding with {len(response.data[0].embedding)} dimensions")
Azure OpenAI Embeddings
import revenium_middleware_openai
from openai import AzureOpenAI
import os
client = AzureOpenAI(
api_version=os.getenv('AZURE_OPENAI_API_VERSION'),
azure_endpoint=os.getenv('AZURE_OPENAI_ENDPOINT'),
api_key=os.getenv('AZURE_OPENAI_API_KEY'),
)
response = client.embeddings.create(
model="text-embedding-3-large", # Your Azure deployment name
input="The quick brown fox jumps over the lazy dog"
)
print(f"Generated embedding with {len(response.data[0].embedding)} dimensions")
๐ Enhanced Tracking with Metadata
For more granular usage tracking and detailed reporting, add the usage_metadata parameter to both embeddings and chat completions:
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.
๐ LangChain Integration
The middleware provides seamless integration with LangChain, supporting both synchronous and asynchronous operations with automatic usage tracking.
๐ฆ Installation with LangChain Support
pip install revenium-middleware-openai[langchain]
๐ Basic LangChain Usage
from langchain_openai import ChatOpenAI
from revenium_middleware_openai.langchain import wrap
# Wrap your LangChain LLM with Revenium tracking
llm = wrap(ChatOpenAI(model="gpt-4o-mini"))
# Use normally - usage is automatically tracked
response = llm.invoke("What is the meaning of life?")
print(response.content)
โก Async LangChain Support
The middleware automatically detects async contexts and uses appropriate handlers:
import asyncio
from langchain_openai import ChatOpenAI
from revenium_middleware_openai.langchain import wrap
async def async_example():
# Automatic async detection
llm = wrap(ChatOpenAI(model="gpt-4o-mini"))
# Use async methods - usage is automatically tracked
response = await llm.ainvoke("What is the meaning of life?")
print(response.content)
# Run the async example
asyncio.run(async_example())
๐ Streaming Support
Both sync and async streaming are fully supported:
from langchain_openai import ChatOpenAI
from revenium_middleware_openai.langchain import wrap_with_streaming
# Sync streaming
llm = wrap_with_streaming(
ChatOpenAI(model="gpt-4o-mini", streaming=True),
enable_streaming_debug=True
)
for chunk in llm.stream("Tell me a story"):
print(chunk.content, end="")
# Async streaming
async def async_streaming():
llm = wrap(ChatOpenAI(model="gpt-4o-mini", streaming=True))
async for chunk in llm.astream("Tell me a story"):
print(chunk.content, end="")
asyncio.run(async_streaming())
๐ค Embeddings with LangChain
from langchain_openai import OpenAIEmbeddings
from revenium_middleware_openai.langchain import wrap
# Wrap embeddings model
embeddings = wrap(OpenAIEmbeddings(model="text-embedding-3-small"))
# Generate embeddings - usage is automatically tracked
vectors = embeddings.embed_documents([
"The quick brown fox",
"jumps over the lazy dog"
])
print(f"Generated {len(vectors)} embeddings")
๐ง Advanced LangChain Configuration
from langchain_openai import ChatOpenAI
from revenium_middleware_openai.langchain import wrap, attach_to
# Method 1: wrap() - Returns a new instance (recommended)
llm = wrap(
ChatOpenAI(model="gpt-4o-mini"),
usage_metadata={
"trace_id": "langchain-session-123",
"task_type": "question-answering",
"agent": "langchain-assistant"
},
enable_debug_logging=True
)
# Method 2: attach_to() - Modifies existing instance in-place
llm = ChatOpenAI(model="gpt-4o-mini")
attach_to(llm, usage_metadata={"session_id": "abc123"})
# Both methods support async auto-detection
response = llm.invoke("Hello LangChain!")
๐ LangChain Monitoring & Statistics
from revenium_middleware_openai.langchain import (
wrap, get_streaming_stats, cleanup_streaming_sessions
)
# Create wrapped LLM
llm = wrap(ChatOpenAI(model="gpt-4o-mini", streaming=True))
# Get real-time statistics
stats = get_streaming_stats(llm)
print(f"Active streaming sessions: {stats['streaming_sessions']}")
print(f"Memory usage: {stats['memory_usage_kb']} KB")
# Manual cleanup if needed (automatic cleanup is built-in)
cleanup_results = cleanup_streaming_sessions(llm)
print(f"Cleaned up {cleanup_results['sessions_cleaned']} sessions")
๐ฏ LangChain Features
- โ Zero-Touch Integration: Works with existing LangChain code
- โ Automatic Async Detection: Seamlessly handles sync and async operations
- โ Streaming Support: Full support for streaming responses with single usage events
- โ Memory Efficient: Automatic cleanup and configurable limits
- โ Thread Safe: Concurrent operation support with proper resource management
- โ Error Resilient: Graceful degradation without breaking LangChain execution
๐ Provider Detection & Features
Automatic Provider Detection
The middleware automatically detects whether you're using standard OpenAI or Azure OpenAI:
- OpenAI: Detected via
OpenAI()client oropenai.api_basecontaining standard OpenAI endpoints - Azure OpenAI: Detected via
AzureOpenAI()client or URLs containing "azure"
Model Name Resolution (Azure)
For Azure OpenAI, the middleware automatically resolves deployment names to standard model names for accurate pricing:
Azure Deployment โ Standard Model Name
"gpt-4o-2024-11-20" โ "gpt-4o"
"gpt-35-turbo-dev" โ "gpt-3.5-turbo"
"text-embedding-3-large" โ "text-embedding-3-large"
Provider-Specific Analytics
Revenium dashboard shows provider-specific data:
- Standard OpenAI:
provider: "OPENAI" - Azure OpenAI:
provider: "Azure"withmodel_source: "OPENAI"
Supported Operations
Both providers support:
- โ Chat completions (streaming and non-streaming)
- โ Embeddings
- โ All metadata fields
- โ Token counting and cost calculation
- โ Error handling and logging
๐ Compatibility
- ๐ Python 3.8+
- ๐ค OpenAI Python SDK 1.0.0+ (includes AzureOpenAI client)
- ๐ Works with all OpenAI models and endpoints
- ๐ท Works with all Azure OpenAI deployments and endpoints
- โก Automatic provider detection (no code changes required)
๐ 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.4.2.tar.gz.
File metadata
- Download URL: revenium_middleware_openai-0.4.2.tar.gz
- Upload date:
- Size: 39.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cc43ea12317396314b954e8ceb6f4744c320670d9bf3d5bcc55bf9678661188e
|
|
| MD5 |
57f73bd8522e9debd2ec79f8eb1db13b
|
|
| BLAKE2b-256 |
f046d1f48c12fa53efeda56639aad1d5a73d8954a8a8f7514049aadc480f7307
|
File details
Details for the file revenium_middleware_openai-0.4.2-py3-none-any.whl.
File metadata
- Download URL: revenium_middleware_openai-0.4.2-py3-none-any.whl
- Upload date:
- Size: 35.8 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 |
5d24c1d388118cf0ccacbed1fffa60330cee641f6263a810393be81874b1eeb8
|
|
| MD5 |
16eb6022dbfe83699c49624871c605c3
|
|
| BLAKE2b-256 |
7242d69ac207175fbeb62b57af4d33f4eeda9ebf7776abd2b9a4da4c65e252b7
|