Skip to main content

Token budget and cost estimator for LLM prompts

Project description

promptcost

Token budget and cost estimator for LLM prompts.

PyPI License: MIT Python 3.9+

What It Does

promptcost estimates the dollar cost of running your LLM prompts against specific models. It provides:

  • Per-invocation cost estimation with input/output token breakdowns
  • Multi-model comparison to find the cheapest model for your prompt
  • Volume projections (daily, monthly, annual costs at a given call rate)
  • Budget enforcement with pass/fail exit codes for CI
  • Cost delta analysis to measure the cost impact of prompt changes
  • Pipeline cost estimation for multi-stage prompt workflows

Installation

pip install promptcost

Dependencies: prompttools-core >= 1.0, typer >= 0.12, rich >= 13.0

CLI Commands

promptcost estimate

Estimate costs for a prompt file or directory.

# Single file estimation
promptcost estimate prompts/greeting.yaml --model gpt-4o

# Estimate with specific output token count
promptcost estimate prompts/greeting.yaml --model gpt-4o --output-tokens 500

# Estimate with volume projections
promptcost estimate prompts/greeting.yaml --model gpt-4o --project 1000/day

# Compare across multiple models
promptcost estimate prompts/greeting.yaml --compare

# Compare specific models
promptcost estimate prompts/greeting.yaml --models gpt-4o,gpt-4o-mini,claude-4-sonnet

# Estimate all files in a directory
promptcost estimate prompts/ --model gpt-4o

# JSON output
promptcost estimate prompts/greeting.yaml --model gpt-4o --format json

Options:

Option Default Description
--model, -m claude-4-sonnet Model profile for estimation
--output-tokens auto Override estimated output tokens
--project none Project at volume (e.g., 1000/day, 500/hour)
--compare false Compare across default model set
--models none Comma-separated model list for comparison
--format, -f text Output format: text or json

promptcost budget

Check prompt costs against a per-invocation budget ceiling. Exits with code 1 if any prompt exceeds the budget.

# Check a single file
promptcost budget prompts/greeting.yaml --limit 0.05 --model gpt-4o

# Check all files in a directory
promptcost budget prompts/ --limit 0.10 --model gpt-4o

Options:

Option Required Description
--limit, -l yes Maximum cost per invocation in USD
--model, -m no Model profile (default: claude-4-sonnet)
--output-tokens no Override output token estimate

promptcost delta

Show the cost impact of a prompt change by comparing old and new versions.

# Basic delta
promptcost delta prompts/old.yaml prompts/new.yaml --model gpt-4o

# Delta with volume projections
promptcost delta prompts/old.yaml prompts/new.yaml --model gpt-4o --volume 1000/day

promptcost models

List all available model profiles with pricing information.

promptcost models

Displays a table with model name, provider, context window, input price per million tokens, output price per million tokens, and encoding.

Output Token Estimation

When --output-tokens is not specified, promptcost estimates output tokens using a heuristic based on prompt content keywords:

Detected keywords Estimated output Method
json, structured, schema, format 500 tokens heuristic
brief, short, concise, summary 300 tokens heuristic
detailed, comprehensive, thorough, essay 2,000 tokens heuristic
None of the above 1,000 tokens heuristic
expected_output_tokens in prompt metadata As specified explicit

The estimate is capped at the model's max_output_tokens limit.

Volume Projection Formats

The --project / --volume flag accepts these formats:

Format Meaning
1000/day 1,000 calls per day
500/hour 500 calls per hour (converted to per-day)
200/week 200 calls per week (converted to per-day)
5000/month 5,000 calls per month (converted to per-day)

Projections are calculated as: daily, monthly (30 days), and annual (365 days).

Programmatic Usage

from prompttools_core import parse_file, parse_pipeline
from promptcost import (
    estimate_file,
    estimate_pipeline,
    compare_models,
    project_cost,
    check_budget,
)

# Single file estimation
prompt = parse_file("prompts/greeting.yaml")
estimate = estimate_file(prompt, model="gpt-4o")
print(f"Input tokens: {estimate.input_tokens}")
print(f"Est. output tokens: {estimate.estimated_output_tokens}")
print(f"Total cost: ${estimate.total_cost:.4f}")

# Model comparison
comparison = compare_models(prompt, ["gpt-4o", "gpt-4o-mini", "claude-4-sonnet"])
print(f"Cheapest: {comparison.cheapest}")
print(f"Savings: ${comparison.savings_vs_most_expensive:.4f}/call")

# Volume projection
projection = project_cost(estimate, "1000/day")
print(f"Monthly cost: ${projection.monthly_cost:.2f}")

# Budget enforcement
estimates = [estimate_file(pf, "gpt-4o") for pf in parse_directory("prompts/")]
results = check_budget(estimates, budget=0.05)
for r in results:
    print(f"{r.file_path.name}: {'OVER' if r.over_budget else 'OK'}")

# Pipeline estimation
pipeline = parse_pipeline("pipeline.yaml")
pipeline_est = estimate_pipeline(pipeline, model="gpt-4o")
print(f"Pipeline total: ${pipeline_est.total_cost:.4f}")

Data Models

CostEstimate -- Per-invocation cost for a single prompt.

  • file_path, model, input_tokens, estimated_output_tokens
  • output_estimation_method ("explicit", "heuristic", "max")
  • input_cost, output_cost, total_cost

CostComparison -- Comparison across multiple models.

  • estimates: dict[str, CostEstimate], cheapest, most_expensive
  • savings_vs_most_expensive

CostProjection -- Volume-based cost projections.

  • volume, calls_per_day, daily_cost, monthly_cost, annual_cost

BudgetResult -- Budget check result.

  • file_path, model, estimated_cost, budget, over_budget, overage

PipelineCostEstimate / StageCostEstimate -- Pipeline-level cost breakdown.

CI Integration

GitHub Actions -- Budget Gate

- name: Check prompt budget
  run: promptcost budget prompts/ --limit 0.10 --model gpt-4o

GitHub Actions -- Cost Delta on PR

- name: Check cost impact
  run: |
    git show HEAD~1:prompts/main.yaml > /tmp/old.yaml
    promptcost delta /tmp/old.yaml prompts/main.yaml --model gpt-4o --volume 1000/day

Exit codes:

Code Meaning
0 All prompts within budget / estimation succeeded
1 One or more prompts exceed budget
2 Path not found or other error

License

MIT License. Author: Scott Converse.

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

promptcost-1.0.0.tar.gz (12.2 kB view details)

Uploaded Source

Built Distribution

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

promptcost-1.0.0-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

Details for the file promptcost-1.0.0.tar.gz.

File metadata

  • Download URL: promptcost-1.0.0.tar.gz
  • Upload date:
  • Size: 12.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for promptcost-1.0.0.tar.gz
Algorithm Hash digest
SHA256 31573eabbe4d36c030330df119c4d566c6119eadcb0f0e488ad28ea19376f480
MD5 ea2179edf549587a9557bb24550bbeec
BLAKE2b-256 57010c1011433b24434f8fb91beb1255143a787e463362088f239179a5e1d192

See more details on using hashes here.

File details

Details for the file promptcost-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: promptcost-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 11.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for promptcost-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4e1e554ae8ca148307f9e47b7bd3d155bcc5eaa172ebce9a749dac91eea878ab
MD5 4bdf80f4717cb4566f595994c5cf257b
BLAKE2b-256 9924bdac99052c37829d071c043f62a565949640f32755440fbb6f317c470d7c

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