UNPKG

image-token-meter

Version:

Calculate token consumption for images in OpenAI Vision models (GPT-4o, GPT-4 Vision, etc.)

233 lines (229 loc) 5.63 kB
/** * Image detail level for OpenAI Vision API */ type ImageDetail = 'high' | 'low' | 'auto'; /** * Supported OpenAI Vision models */ type VisionModel = 'gpt-4o' | 'gpt-4o-2024-05-13' | 'gpt-4o-mini' | 'gpt-4-vision-preview' | 'gpt-4-turbo' | 'gpt-4-turbo-2024-04-09' | 'gpt-4-1106-vision-preview' | string; /** * Input parameters for calculating image tokens */ interface ImageTokenInput { /** * Width of the image in pixels */ width: number; /** * Height of the image in pixels */ height: number; /** * Detail level - 'high' for full resolution, 'low' for 512x512, 'auto' defaults to 'high' * @default 'high' */ detail?: ImageDetail; /** * Model to calculate tokens for * @default 'gpt-4o' */ model?: VisionModel; } /** * Result of image token calculation */ interface ImageTokenResult { /** * Total number of tokens consumed */ tokens: number; /** * Cost in USD based on model pricing */ cost: number; /** * Model used for calculation */ model: string; /** * Human-readable model name */ modelName: string; /** * Calculation details */ details: { /** * Original image dimensions */ originalSize: { width: number; height: number; }; /** * Resized image dimensions (after scaling) */ resizedSize: { width: number; height: number; }; /** * Number of tiles in each dimension */ tiles: { width: number; height: number; total: number; }; /** * Base tokens (always 85 for high detail) */ baseTokens: number; /** * Tokens per tile (always 170 for high detail) */ tokensPerTile: number; /** * Detail level used */ detailLevel: ImageDetail; }; } /** * Pricing configuration */ interface PricingConfig { /** * Cost per 1000 input tokens in USD */ costPer1kTokens: number; } /** * Batch calculation result */ interface BatchCalculationResult { /** * Individual results for each image */ results: ImageTokenResult[]; /** * Summary statistics */ summary: { /** * Total number of images */ totalImages: number; /** * Total tokens across all images */ totalTokens: number; /** * Total cost across all images */ totalCost: number; /** * Breakdown by model */ byModel: Record<string, { count: number; tokens: number; cost: number; }>; }; } /** * Model configurations for different OpenAI Vision models */ interface ModelConfig { /** * Model identifier */ id: string; /** * Human-readable model name */ name: string; /** * Base tokens for high detail mode */ baseTokens: number; /** * Tokens per tile for high detail mode */ tokensPerTile: number; /** * Size of each tile in pixels */ tileSize: number; /** * Maximum dimension before scaling */ maxDimension: number; /** * Target size for shortest side */ shortSideTarget: number; /** * Tokens for low detail mode */ lowDetailTokens: number; /** * Default cost per 1k input tokens in USD */ defaultCostPer1kTokens: number; /** * Whether this model supports vision */ supportsVision: boolean; /** * Additional notes about the model */ notes?: string; } /** * Available models */ declare const MODELS: Record<string, ModelConfig>; /** * Get a model configuration by ID */ declare function getModel(modelId: string): ModelConfig | undefined; /** * Get all available model IDs */ declare function getAvailableModels(): string[]; /** * Get all vision-enabled models */ declare function getVisionModels(): ModelConfig[]; /** * Default model ID */ declare const DEFAULT_MODEL = "gpt-4o"; /** * Calculate the number of tokens consumed by an image in OpenAI Vision models * Based on OpenAI's documentation: https://platform.openai.com/docs/guides/images-vision * * @param input - Image dimensions, detail level, and model * @param pricing - Optional pricing configuration (overrides model default) * @returns Token count and cost calculation */ declare function calculateImageTokens(input: ImageTokenInput, pricing?: PricingConfig): ImageTokenResult; /** * Calculate tokens for multiple images * * @param images - Array of image inputs * @param pricing - Optional pricing configuration (overrides model defaults) * @returns Array of results and total summary */ declare function calculateBatchImageTokens(images: ImageTokenInput[], pricing?: PricingConfig): BatchCalculationResult; /** * Get pricing for a specific model */ declare function getPricing(model?: string): PricingConfig; /** * Create a custom pricing configuration */ declare function createPricing(costPer1kTokens: number): PricingConfig; export { type BatchCalculationResult, DEFAULT_MODEL, type ImageDetail, type ImageTokenInput, type ImageTokenResult, MODELS, type ModelConfig, type PricingConfig, type VisionModel, calculateBatchImageTokens, calculateImageTokens, createPricing, calculateImageTokens as default, getAvailableModels, getModel, getPricing, getVisionModels };