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.

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,
)

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.1.tar.gz (3.7 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.1-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for openai_usage_logger-0.1.1.tar.gz
Algorithm Hash digest
SHA256 49d62c7135dcf12080ebcdb3ceb2f40a6bf981262c26eff8cead693d0d6e7e5c
MD5 d8d4a0cc0f17ded33741246734faae98
BLAKE2b-256 878edef55d03b3929fb080faf43c8bdce23dd8ebaafcd654a4e18a20d9a0f565

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for openai_usage_logger-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 37f7e79e893fc8d32c258c7642ac36095e95060841daef8e33758498fee9dd29
MD5 b2bfb2d662f8f9c9e6de757638b052f3
BLAKE2b-256 0c8c40b0019af733ccbfff10b818e26de31a898e4a05c874acafd4eb89db7bc2

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