Skip to main content

OpenAI Monitoring: Monitor OpenAI API Usage with Grafana Cloud

Grafana GitHub Last Commit GitHub Contributors

Python Tests Pylint

grafana-openai-monitoring is a Python library that provides a decorators to monitor chat completions and Completions endpoints of the OpenAI API. It facilitates sending metrics and logs to Grafana Cloud, allowing you to track and analyze OpenAI API usage and responses.

Installation

You can install grafana-openai-monitoring using pip:

pip install grafana-openai-monitoring

Usage

The following tables shows which OpenAI function correspons to which monitoing function in this library

OpenAI Function Monitoring Function
ChatCompletion.create chat_v2.monitor
Completion.create chat_v1.monitor

ChatCompletions

To monitor ChatCompletions using the OpenAI API, you can use the chat_v2.monitor decorator. This decorator automatically tracks API calls and sends metrics and logs to the specified Grafana Cloud endpoints.

Here's how to set it up:

from openai import OpenAI
from grafana_openai_monitoring import chat_v2

client = OpenAI(
    api_key="YOUR_OPENAI_API_KEY",
)

# Apply the custom decorator to the OpenAI API function. To use with AsyncOpenAI, Pass `use_async` = True in this function.
client.chat.completions.create = chat_v2.monitor(
    client.chat.completions.create,
    metrics_url="YOUR_PROMETHEUS_METRICS_URL",  # Example: "https://prometheus.grafana.net/api/prom"
    logs_url="YOUR_LOKI_LOGS_URL",  # Example: "https://logs.example.com/loki/api/v1/push/"
    metrics_username="YOUR_METRICS_USERNAME",  # Example: "123456"
    logs_username="YOUR_LOGS_USERNAME",  # Example: "987654"
    access_token="YOUR_ACCESS_TOKEN"  # Example: "glc_eyasdansdjnaxxxxxxxxxxx"
)

# Now any call to client.chat.completions.create will be automatically tracked
response = client.chat.completions.create(model="gpt-4", max_tokens=100, messages=[{"role": "user", "content": "What is Grafana?"}])
print(response)

Completions

To monitor completions using the OpenAI API, you can use the chat_v1.monitor decorator. This decorator adds monitoring capabilities to the OpenAI API function and sends metrics and logs to the specified Grafana Cloud endpoints.

Here's how to apply it:

from openai import OpenAI
from grafana_openai_monitoring import chat_v1

client = OpenAI(
    api_key="YOUR_OPENAI_API_KEY",
)

# Apply the custom decorator to the OpenAI API function
client.completions.create = chat_v1.monitor(
    client.completions.create,
    metrics_url="YOUR_PROMETHEUS_METRICS_URL",  # Example: "https://prometheus.grafana.net/api/prom"
    logs_url="YOUR_LOKI_LOGS_URL",  # Example: "https://logs.example.com/loki/api/v1/push/"
    metrics_username="YOUR_METRICS_USERNAME",  # Example: "123456"
    logs_username="YOUR_LOGS_USERNAME",  # Example: "987654"
    access_token="YOUR_ACCESS_TOKEN"  # Example: "glc_eyasdansdjnaxxxxxxxxxxx"
)

# Now any call to client.completions.create will be automatically tracked
response = client.completions.create(model="davinci", max_tokens=100, prompt="Isn't Grafana the best?")
print(response)

Configuration

To use the grafana-openai-monitoring library effectively, you need to provide the following information:

  • YOUR_OPENAI_API_KEY: Replace this with your actual OpenAI API key.
  • YOUR_PROMETHEUS_METRICS_URL: Replace the URL with your Prometheus URL.
  • YOUR_LOKI_LOGS_URL: Replace with the URL where you want to send Loki logs.
  • YOUR_METRICS_USERNAME: Replace with the username for Prometheus.
  • YOUR_LOGS_USERNAME: Replace with the username for Loki.
  • YOUR_ACCESS_TOKEN: Replace with the Cloud Access Policy token required for authentication.

After configuring the parameters, the monitored API function will automatically log and track the requests and responses to the specified endpoints.

Compatibility

Python 3.7.1 and above

Dependencies

License

This project is licensed under the GPL-3.0 license - see the LICENSE for details.

Metadata

Release files for grafana-openai-monitoring 0.0.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for grafana-openai-monitoring 0.0.9
File Size Uploaded
grafana_openai_monitoring-0.0.9.tar.gz 6.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for grafana-openai-monitoring 0.0.9
File Interpreter ABI Platform
grafana_openai_monitoring-0.0.9-py3-none-any.whl Python 3 none any Details

Total release size: 14.9 kB

Release files / grafana_openai_monitoring-0.0.9.tar.gz

Download URL grafana_openai_monitoring-0.0.9.tar.gz
Size 6.7 kB
Tags Source
SHA-256 checksum
How to use checksums
92022d2a04dd6f7ede4237ad136642c606df1dede81253a8954acc128a8eeb2a
BLAKE2b-256 checksum
How to use checksums
30e4e96a3fcc23803cb60daf2bedb1d2a3e4132514975c6f2bc9ac4723d0f180
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.3

Release files / grafana_openai_monitoring-0.0.9-py3-none-any.whl

Download URL grafana_openai_monitoring-0.0.9-py3-none-any.whl
Size 8.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5ba152c46b7d256b1f5f9456aea98ec0dd8f713b969363b9ef73a2087c20345a
BLAKE2b-256 checksum
How to use checksums
482a0e04ba409e97a3f52e682a498e5b89cf64224999e308bc6fbe254f033f60
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.3

Release history Release notifications | RSS feed

This release

0.0.9 This release

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page