Skip to main content

A Python library that meters Google AI API usage to Revenium with support for both Gemini Developer API and Vertex AI.

Project description

Revenium Middleware for Google AI (Python)

PyPI version Python Versions Documentation License: MIT

A middleware library for metering and monitoring Google AI services usage in Python applications. Supports both Google AI SDK (Gemini Developer API) and Vertex AI SDK with flexible optional dependencies.

For complete examples and setup instructions, see examples/README.md

Features

  • Precise Usage Tracking: Monitor tokens, costs, and request counts for Google AI services
  • Seamless Integration: Drop-in middleware that works with minimal code changes
  • Dual SDK Support: Choose between Google AI SDK or Vertex AI SDK based on your needs
  • Optional Dependencies: Install only the SDK components you need
  • Streaming Support: Full support for streaming responses (both SDKs)
  • Enhanced Token Counting: Complete token tracking including embeddings (Vertex AI)
  • Flexible Configuration: Customize metering behavior to suit your application needs

What's Supported

Feature Google AI SDK Vertex AI SDK
Chat Completion Full support Full support
Streaming Full support Full support
Text Embeddings Basic support* Full support
Token Metering Chat/Streaming All operations
Metadata Tracking Full support Full support
Setup Complexity Simple (API key) Moderate (GCP project)

Note: *Google AI SDK embeddings don't return token counts due to API limitations, but requests are still tracked.

Installation

Choose the SDK variant that best fits your needs:

# Google AI SDK only (Gemini Developer API)
pip install "revenium-middleware-google[genai]"

# Vertex AI SDK only (recommended for production)
pip install "revenium-middleware-google[vertex]"

# Both SDKs (maximum flexibility)
pip install "revenium-middleware-google[all]"

Which SDK Should I Choose?

Use Case Recommended SDK Why
Quick prototyping Google AI SDK Simple API key setup, but does NOT support token counts on embeddings
Production applications Vertex AI SDK Full token counting, enterprise features
Embeddings-heavy workloads Vertex AI SDK Complete token tracking for embeddings
Enterprise/GCP environments Vertex AI SDK Advanced Google Cloud integration
Simple chat applications Either SDK Both provide full chat support

Recommendation: Use Vertex AI SDK for production applications that need comprehensive token counting and advanced features.

Quick Start

1. Create Project Directory

mkdir my-google-ai-project
cd my-google-ai-project

2. Create Virtual Environment

# Create virtual environment
python -m venv .venv

# Activate virtual environment
source .venv/bin/activate  # On Windows: .venv\Scripts\activate

3. Install the Package

# Install with both SDKs (recommended)
pip install "revenium-middleware-google[all]" python-dotenv

4. Configure Environment Variables

Create a .env file in your project directory:

# Required
REVENIUM_METERING_API_KEY=hak_your_revenium_api_key

# For Google AI SDK (Gemini Developer API)
GOOGLE_API_KEY=your_google_api_key

# For Vertex AI SDK (Google Cloud)
GOOGLE_CLOUD_PROJECT=your_gcp_project_id
GOOGLE_CLOUD_LOCATION=us-central1

# Optional
# REVENIUM_LOG_LEVEL=DEBUG

For Vertex AI: Authenticate with Google Cloud:

gcloud auth application-default login

5. Run Your First Example

Download and run an example from the repository:

# For Google AI SDK
curl -O https://raw.githubusercontent.com/revenium/revenium-middleware-google-python/main/examples/getting_started_google_ai.py
python getting_started_google_ai.py

# For Vertex AI SDK
curl -O https://raw.githubusercontent.com/revenium/revenium-middleware-google-python/main/examples/getting_started_vertex_ai.py
python getting_started_vertex_ai.py

Or create a file test.py with this simple code:

from dotenv import load_dotenv
load_dotenv()  # Load environment variables from .env file

import revenium_middleware_google
from google import genai

client = genai.Client()
response = client.models.generate_content(
    model="gemini-2.0-flash-001",
    contents="Hello! Introduce yourself in one sentence."
)
print(response.text)

Run it:

python test.py

The middleware automatically tracks your Google AI usage and sends data to Revenium.

For complete examples and usage patterns, see examples/README.md.


Usage

Simply import the middleware and your Google AI calls will be metered automatically:

import revenium_middleware_google
from google import genai

client = genai.Client()
response = client.models.generate_content(
    model="gemini-2.0-flash-001",
    contents="Hello! Introduce yourself in one sentence."
)
print(response.text)

The middleware automatically meters all Google AI and Vertex AI calls. For production use, add metadata tracking to associate usage with organizations, users, or features.

For complete examples, see:

Configuration

For detailed configuration options, environment variables, and advanced setup, see the Configuration section in examples/README.md

Metadata Fields

Add business context to track usage by organization, user, task type, or custom fields. Pass a usage_metadata dictionary with any of these optional fields:

Field Description Use Case
trace_id Unique identifier for session or conversation tracking Link multiple API calls together for debugging, user session analytics, or distributed tracing across services
task_type Type of AI task being performed Categorize usage by workload (e.g., "chat", "code-generation", "doc-summary") for cost analysis and optimization
subscriber.id Unique user identifier Track individual user consumption for billing, rate limiting, or user analytics
subscriber.email User email address Identify users for support, compliance, or usage reports
subscriber.credential.name Authentication credential name Track which API key or service account made the request
subscriber.credential.value Authentication credential value Associate usage with specific credentials for security auditing
organization_id Organization or company identifier Multi-tenant cost allocation, usage quotas per organization
subscription_id Subscription plan identifier Track usage against subscription limits, identify plan upgrade opportunities
product_id Your product or feature identifier Attribute AI costs to specific features in your application (e.g., "chatbot", "email-assistant")
agent AI agent or bot identifier Distinguish between multiple AI agents or automation workflows in your system
response_quality_score Custom quality rating (0.0-1.0) Track user satisfaction or automated quality metrics for model performance analysis

Example usage: See examples/getting_started_google_ai.py for complete metadata implementation.

API Reference: Complete metadata field documentation

Troubleshooting

Common Issues

Issue Solution
"No module named 'google.genai'" Install with Google AI support: pip install "revenium-middleware-google[genai]"
"No module named 'vertexai'" Install with Vertex AI support: pip install "revenium-middleware-google[vertex]"
Vertex AI authentication errors Verify Google Cloud credentials: gcloud auth application-default login
"Project not found" errors Ensure GOOGLE_CLOUD_PROJECT is set correctly
Embeddings showing 0 tokens Expected with Google AI SDK; use Vertex AI for full token counting
Requests not being tracked Ensure middleware is imported before Google AI/Vertex AI SDKs

Debug Mode

Enable debug logging to see provider detection and routing decisions:

# export REVENIUM_LOG_LEVEL=DEBUG
python your_script.py

Force Specific SDK

To ensure only one SDK is used:

# Use only Google AI SDK
pip install "revenium-middleware-google[genai]"

# Use only Vertex AI SDK
pip install "revenium-middleware-google[vertex]"

Google AI SDK Troubleshooting

Middleware not tracking requests:

  • Ensure middleware is imported before Google AI SDK
  • Check that environment variables are loaded correctly
  • Verify your REVENIUM_METERING_API_KEY is correct

Embeddings showing 0 tokens:

  • This is expected due to Google AI SDK limitations
  • Model name and metadata are still tracked correctly
  • Chat and streaming operations provide full token data

Vertex AI SDK Troubleshooting

Authentication issues:

  • Verify Google Cloud credentials: gcloud auth list
  • Check project access: gcloud projects describe YOUR_PROJECT_ID
  • Ensure service account has required permissions

Model not available errors:

  • Check if models are available in your region
  • Verify Vertex AI API is enabled in your project
  • Try a different model or region

Logging

Control logging with the REVENIUM_LOG_LEVEL environment variable in your .env file:

# Add to your .env file
REVENIUM_LOG_LEVEL=DEBUG

Available log levels:

  • DEBUG: Detailed debugging information (shows provider detection, routing decisions, token counts)
  • INFO: General information messages (default)
  • WARNING: Warning messages only
  • ERROR: Error messages only
  • CRITICAL: Critical error messages only

Compatibility

  • Python 3.8+
  • Google AI SDK (google-genai>=0.1.0) or Vertex AI SDK (google-cloud-aiplatform>=1.0.0)
  • Google Cloud Project (for Vertex AI SDK)

Supported Models

The middleware automatically works with all Google AI models available through both SDKs, including:

  • Gemini models (all versions and variants)
  • Text embedding models (all versions)
  • Future model releases (automatic support with no code changes required)

The middleware detects and meters any model supported by the Google AI SDK or Vertex AI SDK. When Google releases new models, they work immediately without updates to the middleware.

Documentation

For detailed documentation, visit docs.revenium.io

Contributing

See CONTRIBUTING.md

Code of Conduct

See CODE_OF_CONDUCT.md

Security

See SECURITY.md

License

This project is licensed under the MIT License - see the LICENSE file for details.

Support

For issues, feature requests, or contributions:


Built by Revenium

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

revenium_middleware_google-0.1.2.tar.gz (45.0 kB view details)

Uploaded Source

Built Distribution

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

revenium_middleware_google-0.1.2-py3-none-any.whl (53.1 kB view details)

Uploaded Python 3

File details

Details for the file revenium_middleware_google-0.1.2.tar.gz.

File metadata

File hashes

Hashes for revenium_middleware_google-0.1.2.tar.gz
Algorithm Hash digest
SHA256 64ff7e456df022519c7b5e9a630a57a233cfa3f00d3518d63ac8671c8cb459a5
MD5 87cdf167be7e13b4118be3dbd51f0be4
BLAKE2b-256 c32f9d38fa1bb7d13f343c6bf4a46afd3f4aeb56164e6a6af4af5d299ead04c8

See more details on using hashes here.

File details

Details for the file revenium_middleware_google-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for revenium_middleware_google-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4e8c514f4095362d7ea00ef1a60923e2310317752c33937c76c59793b15fc5af
MD5 2473bd8e87d0f05f3f1b6e1a42d9c1fc
BLAKE2b-256 8ea30a2f2e4b43075d0a94bdae6659cff04e91a145f01f5871ec8f8a3ea8dcf3

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