All Toolsโ€บToken Usage Tracker
๐Ÿ”ง AI Token Usage TrackingJune 16, 2026โœ… Tests passing

Token Usage Tracker

This CLI tool monitors token usage for AI model API calls by wrapping API requests and logging token consumption to help developers identify inefficiencies and optimize costs in their workflows.

What It Does

  • Wraps API requests to an AI model and logs token usage.
  • Generates reports in text or CSV format.

Installation

1. Install Python 3.7 or higher.

2. Install the required dependencies:

pip install requests click tabulate

Usage

Run the tool from the command line:

python token_usage_tracker.py --api_key YOUR_API_KEY --model gpt-4 --output_format text

Options

  • --api_key: Your API key for the AI service (required).
  • --model: The model to use (e.g., gpt-4) (required).
  • --output_format: The format for the report (text or csv). Default is text.

Example

python token_usage_tracker.py --api_key YOUR_API_KEY --model gpt-4 --output_format csv

Enter prompts one by one. Type exit to finish and generate the report.

Source Code

import os
import json
import requests
import click
from tabulate import tabulate

class TokenUsageTracker:
    def __init__(self, api_key, model):
        self.api_key = api_key
        self.model = model
        self.usage_log = []

    def make_request(self, prompt):
        """Simulates an API call to an AI model and tracks token usage."""
        headers = {"Authorization": f"Bearer {self.api_key}"}
        payload = {"model": self.model, "prompt": prompt}

        try:
            response = requests.post("https://api.openai.com/v1/completions", headers=headers, json=payload)
            response.raise_for_status()
            data = response.json()

            # Simulate token usage tracking
            tokens_used = data.get("usage", {}).get("total_tokens", 0)
            self.usage_log.append({"prompt": prompt, "tokens_used": tokens_used})
            return data

        except requests.exceptions.RequestException as e:
            click.echo(f"Error: {e}")
            return None

    def generate_report(self, output_format="text"):
        """Generates a summary report of token usage."""
        if not self.usage_log:
            return "No usage data to report."

        total_tokens = sum(entry["tokens_used"] for entry in self.usage_log)
        report_data = [
            [i + 1, entry["prompt"], entry["tokens_used"]] for i, entry in enumerate(self.usage_log)
        ]
        report_data.append(["Total", "", total_tokens])

        if output_format == "text":
            return tabulate(report_data, headers=["#", "Prompt", "Tokens Used"], tablefmt="grid")
        elif output_format == "csv":
            csv_data = "#;Prompt;Tokens Used\n"
            csv_data += "\n".join(f"{row[0]};{row[1]};{row[2]}" for row in report_data)
            return csv_data
        else:
            return "Invalid output format."

@click.command()
@click.option("--api_key", required=True, help="Your API key for the AI service.")
@click.option("--model", required=True, help="The model to use (e.g., gpt-4).")
@click.option("--output_format", default="text", type=click.Choice(["text", "csv"]), help="Output format for the report.")
def main(api_key, model, output_format):
    """Token Usage Tracker: Monitors token usage for AI model API calls."""
    tracker = TokenUsageTracker(api_key, model)

    click.echo("Enter prompts (type 'exit' to finish):")
    while True:
        prompt = input("Prompt: ")
        if prompt.lower() == "exit":
            break
        tracker.make_request(prompt)

    report = tracker.generate_report(output_format)
    click.echo(report)

if __name__ == "__main__":
    main()

Community

Downloads

ยทยทยท

Rate this tool

No ratings yet โ€” be the first!

Details

Tool Name
token_usage_tracker
Category
AI Token Usage Tracking
Generated
June 16, 2026
Tests
Passing โœ…
Fix Loops
2

Quick Install

Clone just this tool:

git clone --depth 1 --filter=blob:none --sparse \
  https://github.com/ptulin/autoaiforge.git
cd autoaiforge
git sparse-checkout set generated_tools/2026-06-16/token_usage_tracker
cd generated_tools/2026-06-16/token_usage_tracker
pip install -r requirements.txt 2>/dev/null || true
python token_usage_tracker.py
Token Usage Tracker โ€” AI Tools by AutoAIForge