A Python library for calculating the cost of image inputs to OpenAI vision models. This library implements the exact cost calculation logic described in the OpenAI documentation, supporting all current vision models including GPT-4o, GPT-4.1, GPT-4o-mini, o1, o3, and more.
- Accurate Cost Calculation: Implements the exact token calculation logic from OpenAI documentation
- All Vision Models Supported: GPT-4o, GPT-4.1-mini/nano, o4-mini, o1/o3 series, computer-use-preview, and GPT Image 1
- Flexible Pricing: Accepts current pricing as input (per million tokens)
- Detail Level Support: Handles both "high" and "low" detail image processing
- Comprehensive Validation: Input validation with helpful error messages
- Easy to Use: Simple API with clear documentation
pip install openai-vision-cost
from openai_vision_cost import calculate_image_cost
# Calculate cost for a 1024x1024 image using GPT-4o
result = calculate_image_cost(
width=1024,
height=1024,
model="gpt-4o",
input_price_per_million=2.50, # $2.50 per 1M tokens
detail="high"
)
print(f"Tokens: {result['text_tokens']}")
print(f"Cost: ${result['total_cost_usd']:.6f}")
from openai_vision_cost import calculate_image_cost
# High detail image processing
result = calculate_image_cost(
width=1024,
height=1024,
model="gpt-4o",
input_price_per_million=2.50
)
print(f"Image tokens: {result['image_tokens']}")
print(f"Text tokens (billed): {result['text_tokens']}")
print(f"Total cost: ${result['total_cost_usd']:.6f}")
# Output: Image tokens: 765, Text tokens: 765, Total cost: $0.001913
from openai_vision_cost import estimate_cost_range
# Get cost estimates for both detail levels
costs = estimate_cost_range(
width=2048,
height=1024,
model="gpt-4o",
input_price_per_million=2.50
)
print("High detail:", costs['high'])
print("Low detail:", costs['low'])
from openai_vision_cost import calculate_tokens_only
# Get token counts without cost calculation
tokens = calculate_tokens_only(
width=1800,
height=2400,
model="gpt-4.1-mini"
)
print(f"Image tokens: {tokens['image_tokens']}")
print(f"Text tokens: {tokens['text_tokens']}")
from openai_vision_cost import calculate_image_cost, get_supported_models
# List all supported models
models = get_supported_models()
print("Supported models:", models[:5]) # Show first 5
# Calculate costs for different model families
models_to_test = [
("gpt-4o", 2.50), # Tile-based model
("gpt-4.1-mini", 0.40), # Patch-based with multiplier
("gpt-image-1", 10.00), # Image tokens model
]
for model, price in models_to_test:
result = calculate_image_cost(1024, 1024, model, price)
print(f"{model}: {result['text_tokens']} tokens, ${result['total_cost_usd']:.6f}")
The library supports three different model families, each with different cost calculation methods:
- gpt-4.1-mini: Multiplier 1.62x
- gpt-4.1-nano: Multiplier 2.46x
- o4-mini: Multiplier 1.72x
- gpt-4o: 85 base + 170 per tile
- gpt-4.1: 85 base + 170 per tile
- gpt-4o-mini: 2833 base + 5667 per tile
- o1/o3 series: 75 base + 150 per tile
- computer-use-preview: 65 base + 129 per tile
- gpt-image-1: 65 base + 129 per tile (512px shortest side)
Calculate the cost of processing an image with OpenAI vision models.
Parameters:
width
(int): Image width in pixelsheight
(int): Image height in pixelsmodel
(str): Model name (e.g., "gpt-4o", "gpt-4.1-mini")input_price_per_million
(float): Price per million tokens in USDdetail
(str): Detail level, "high" or "low" (default: "high")
Returns: Dict with keys:
image_tokens
(int): Raw image tokens calculated from dimensionstext_tokens
(int): Final tokens used for billingtotal_cost_usd
(float): Total cost in USD
Calculate only token counts without cost.
Parameters: Same as above except input_price_per_million
Returns: Dict with keys:
image_tokens
(int): Raw image tokenstext_tokens
(int): Final tokens for billing
Get cost estimates for both high and low detail levels.
Returns: Dict with "high" and "low" keys, each containing cost calculation results.
Returns a list of all supported model names.
Check if a model is supported.
The library provides specific exceptions for different error conditions:
from openai_vision_cost import (
calculate_image_cost,
UnsupportedModelError,
InvalidImageDimensionsError,
InvalidPricingError,
InvalidDetailLevelError
)
try:
result = calculate_image_cost(0, 100, "gpt-4o", 2.50)
except InvalidImageDimensionsError as e:
print(f"Invalid dimensions: {e}")
except UnsupportedModelError as e:
print(f"Unsupported model: {e}")
except InvalidPricingError as e:
print(f"Invalid pricing: {e}")
The library has been validated against the examples provided in the OpenAI documentation:
# Example 1: 1024x1024 image with gpt-4o = 765 tokens
result = calculate_tokens_only(1024, 1024, "gpt-4o", "high")
assert result['text_tokens'] == 765
# Example 2: 2048x4096 image with gpt-4o = 1105 tokens
result = calculate_tokens_only(2048, 4096, "gpt-4o", "high")
assert result['text_tokens'] == 1105
# Example 3: Any image with low detail = base tokens only
result = calculate_tokens_only(4096, 8192, "gpt-4o", "low")
assert result['text_tokens'] == 85
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
git clone https://github.com/alexferrari88/openai-vision-cost-calculator.git
cd openai-vision-cost
pip install -e ".[dev]"
pytest
black src tests
isort src tests
flake8 src tests
mypy src
This project is licensed under the MIT License - see the LICENSE file for details.
This library is not officially affiliated with OpenAI. It implements the cost calculation logic based on publicly available OpenAI documentation. Always verify costs with the official OpenAI pricing calculator for the most accurate estimates.
- Initial release
- Support for all current OpenAI vision models
- Comprehensive test coverage
- Complete documentation
- Published to PyPI