Skip to main content

A utility for logging OpenAI API usage and costs.

Project description

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.

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

openai_usage_logger-0.1.2.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

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

openai_usage_logger-0.1.2-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: openai_usage_logger-0.1.2.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.18

File hashes

Hashes for openai_usage_logger-0.1.2.tar.gz
Algorithm Hash digest
SHA256 424c18eadebec942d0ed6f2738b960b1f015dd8467847e3778919411f1d5ea2b
MD5 ee7d88c046c5150044171e4b38a0f5af
BLAKE2b-256 5185ca1587933e9a010c501d4fe45bce3e8d5f2a2f5620b6529f4f103af44b81

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for openai_usage_logger-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1b1a26a3e33cf78ad374d3ad0dae99fd698747449f288a23b9dd1e2a466bb8a5
MD5 c6eb6f087fc2151f47ba9ce1d425a50a
BLAKE2b-256 316f247eeb57484568a8361a5c473dc8275332821609fb6fa83430118ed1a1cf

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