How to check API request logs?

Is there a way to check the logs of all the prompts that got put through my API key? I am having an issue with the responses I am getting. I am trying to nail down the issue. Would be helpful if I could see all the requests I put through. My program is pulling various data from different locations and putting them all into one large prompt so I would like to see what is exactly being asked, right now my program is set up only to show me the response.

1 Like

No, not if you weren’t logging them. Best practice is to log it and hold on to it for at least 30 days or so.

1 Like

How would one log the request history? I didnt see a menu to be able to log the incoming request and, hopefully, to view the generated response. Thank you!

That is something you would have to do in your code, using the information returned in non-streaming responses, or by measuring the sent and received tokens yourself if using streaming chat completions endpoint (by a token-counting library).

To add a file log to your backend application for tracking API requests, including username, perhaps a snippet of user input or in full for safety and policy violations, and total token usage, you can follow these general instructions that I prompted GPT-4 to write for you. Such method will enhance your ability to audit token usage, although OpenAI doesn’t give you equivalent inspection of everything you’ve sent, even in your billing, and daily cutoffs in displayed usage may be hard to match up to logs.

Details (expand)

Step 1: Set Up the Logging Environment

First, you need to set up a logging environment in your Python application. Python’s built-in logging module is a versatile tool for this purpose. At the beginning of your script, import the logging module and configure it to write to a file. You can set the logging level to INFO or DEBUG depending on the granularity of the log you need.

import logging

logging.basicConfig(filename='api_requests.log', level=logging.INFO, format='%(asctime)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S')

This configuration will log messages with a timestamp, making it easier to track when each request was made.

Step 2: Modify the Request Handling Code

Within the loop where you handle the API requests, you’ll want to add a logging line right after you receive the response from the OpenAI API. This is where you will log the username (assuming you have a way to identify the user, such as a variable or a function that retrieves the username), a snippet of the user input, and the total token usage from the API response.

Assuming you have a variable username that stores the user’s name and you’re inside the try block where the API request is successfully completed, you can add a logging statement like this:

# Extracting a snippet of the user input for logging
user_input_snippet = user[0]['content'][:50]  # Adjust the slice as needed

# Logging the request details
logging.info(f"Username: {username}, User Input: '{user_input_snippet}...', Total Tokens: {response_body['usage']['total_tokens']}")

This line will write an entry in your log file every time an API request is made, including the username, the first 50 characters of the user input (to keep the log concise), and the total tokens used for that request.

Step 3: Error Handling and Additional Logging

Consider adding logging within your except blocks to log errors or unusual events. This can help in diagnosing issues with the API requests or network problems.

except urllib.error.HTTPError as e:
    logging.error(f"HTTP error {e.code}: {e.read().decode()} - User: {username}")

This ensures that any errors encountered during the request are also logged, providing a comprehensive view of all API interactions.

Step 4: Review and Rotate Logs

Regularly review your log files to monitor API usage and identify any unexpected patterns or issues. Depending on the volume of requests, your log file might grow quickly. Consider implementing a log rotation policy to archive old logs and maintain manageable file sizes. Python’s logging.handlers module offers built-in mechanisms like RotatingFileHandler for this purpose.

3 Likes