All Toolsโ€บAPI Cost Calculator
๐Ÿ”ง Competition among AI language modelsMarch 19, 2026โœ… Tests passing

API Cost Calculator

This tool estimates the cost of using AI language models based on input text length, tokenization, and current pricing. Developers can use it to calculate expenses for specific workloads, enabling budget-conscious development and deployment.

What It Does

  • Tokenize input text using model-specific tokenizers.
  • Calculate API cost based on current pricing rates.
  • Supports multiple models for side-by-side cost comparison.
  • Outputs results in a human-readable table or JSON format.

Installation

1. Clone the repository:

git clone https://github.com/your-repo/api_cost_calculator.git
   cd api_cost_calculator

2. Install dependencies:

pip install -r requirements.txt

Usage

Command-Line Interface

python api_cost_calculator.py --input example.txt --models gpt-4 claude-2 --pricing pricing.json --output human

Options

  • --input: Path to the input text file. Use - to read from standard input.
  • --models: List of models to compare (e.g., gpt-4, gpt-3.5, claude-2).
  • --pricing: Path to a JSON file containing pricing information.
  • --output: Output format (human or json). Default is human.

Example Pricing File

{
  "gpt-4": 0.03,
  "gpt-3.5": 0.015,
  "claude-2": 0.02
}

Example Input File

This is an example input text.
It will be tokenized and used for cost calculation.

Example Output

#### Human-Readable Format

Model Comparison:
  model   tokens   cost
  gpt-4       12  0.00036
  claude-2    12  0.00024

#### JSON Format

[
  {
    "model": "gpt-4",
    "tokens": 12,
    "cost": 0.00036
  },
  {
    "model": "claude-2",
    "tokens": 12,
    "cost": 0.00024
  }
]

Source Code

import argparse
import json
import sys
import pandas as pd
from transformers import GPT2TokenizerFast
from tiktoken import get_encoding

def tokenize_text(text, model):
    """
    Tokenizes the input text based on the specified model.
    """
    if model.startswith("gpt-4") or model.startswith("gpt-3.5"):
        encoding = get_encoding("cl100k_base")
        tokens = encoding.encode(text)
    elif model.startswith("claude"):
        tokenizer = GPT2TokenizerFast.from_pretrained("gpt2")
        tokens = tokenizer.encode(text)
    else:
        raise ValueError(f"Unsupported model: {model}")
    return tokens

def calculate_cost(tokens, model, pricing):
    """
    Calculates the cost of using the API based on the number of tokens and pricing.
    """
    if model not in pricing:
        raise ValueError(f"Pricing information for model {model} is not available.")
    cost_per_1k_tokens = pricing[model]
    return (len(tokens) / 1000) * cost_per_1k_tokens

def main():
    parser = argparse.ArgumentParser(description="API Cost Calculator")
    parser.add_argument("--input", type=str, help="Path to the input text file. Use '-' for stdin.")
    parser.add_argument("--models", nargs='+', required=True, help="List of models to compare (e.g., gpt-4 gpt-3.5 claude-2).")
    parser.add_argument("--pricing", type=str, required=True, help="Path to JSON file with pricing information.")
    parser.add_argument("--output", type=str, choices=["human", "json"], default="human", help="Output format.")

    args = parser.parse_args()

    # Read input text
    if args.input == "-":
        text = sys.stdin.read()
    else:
        try:
            with open(args.input, "r", encoding="utf-8") as f:
                text = f.read()
        except FileNotFoundError:
            print(f"Error: File {args.input} not found.", file=sys.stderr)
            sys.exit(1)

    # Load pricing information
    try:
        with open(args.pricing, "r", encoding="utf-8") as f:
            pricing = json.load(f)
    except (FileNotFoundError, json.JSONDecodeError):
        print(f"Error: Invalid pricing file {args.pricing}.", file=sys.stderr)
        sys.exit(1)

    # Calculate costs
    results = []
    for model in args.models:
        try:
            tokens = tokenize_text(text, model)
            cost = calculate_cost(tokens, model, pricing)
            results.append({"model": model, "tokens": len(tokens), "cost": cost})
        except ValueError as e:
            print(f"Error: {e}", file=sys.stderr)
            sys.exit(1)

    # Output results
    if args.output == "human":
        print("Model Comparison:")
        df = pd.DataFrame(results)
        print(df.to_string(index=False))
    else:
        print(json.dumps(results, indent=2))

if __name__ == "__main__":
    main()

Community

Downloads

ยทยทยท

Rate this tool

No ratings yet โ€” be the first!

Details

Tool Name
api_cost_calculator
Category
Competition among AI language models
Generated
March 19, 2026
Tests
Passing โœ…

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-03-19/api_cost_calculator
cd generated_tools/2026-03-19/api_cost_calculator
pip install -r requirements.txt 2>/dev/null || true
python api_cost_calculator.py
API Cost Calculator โ€” AI Tools by AutoAIForge