Skip to main content

openai_usage_logger

This library provides a simple way to log OpenAI API usage and costs. It maintains a record of the number of prompt and completion tokens used for different OpenAI models, as well as the associated costs.

Install

pip install openai-usage-logger

Features

  • Tracks usage and costs for different tags and models.
  • Allows logging of token usage and costs in a structured format.
  • Provides flexibility to use a custom logger.

Usage

Basic Usage

Use the library to log responses from OpenAI's completion API, including the model used and the associated costs.

import openai
from openai_usage_logger import usageLogger

# Example OpenAI completion API call
response = openai.ChatCompletion.create(
    model='gpt-3.5-turbo-0125',
    messages=messages,
)
usageLogger().log(response)

Logging the response with a usage tag

usageLogger().log(response, tag='usage_tag')

Advanced Usage with Custom Logger

You can also pass a custom logger to the log method if you want to use a specific logging configuration.

import logging
from openai_usage_logger import usageLogger

Set up your custom logger

custom_logger = logging.getLogger('my_custom_logger')
logging.basicConfig(level=logging.INFO)

Using the custom logger

usageLogger().log(response, tag='usage_tag', logger=custom_logger)

Model Costs The package currently supports cost calculation for the following models:

GPT-4 models (including various versions) GPT-3.5-Turbo models (including various versions) Costs are based on the number of tokens used for prompts and completions, with different rates for different models.

Metadata

Release files for openai-usage-logger 0.1.2

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

Source distribution (sdist)

Source distribution for openai-usage-logger 0.1.2
File Size Uploaded
openai_usage_logger-0.1.2.tar.gz 4.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for openai-usage-logger 0.1.2
File Interpreter ABI Platform
openai_usage_logger-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 9.3 kB

Release files / openai_usage_logger-0.1.2.tar.gz

Download URL openai_usage_logger-0.1.2.tar.gz
Size 4.5 kB
Tags Source
SHA-256 checksum
How to use checksums
424c18eadebec942d0ed6f2738b960b1f015dd8467847e3778919411f1d5ea2b
BLAKE2b-256 checksum
How to use checksums
5185ca1587933e9a010c501d4fe45bce3e8d5f2a2f5620b6529f4f103af44b81
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.9.18

Release files / openai_usage_logger-0.1.2-py3-none-any.whl

Download URL openai_usage_logger-0.1.2-py3-none-any.whl
Size 4.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1b1a26a3e33cf78ad374d3ad0dae99fd698747449f288a23b9dd1e2a466bb8a5
BLAKE2b-256 checksum
How to use checksums
316f247eeb57484568a8361a5c473dc8275332821609fb6fa83430118ed1a1cf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.9.18

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.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