Skip to content

Drop in a screenshot and convert it to clean code (HTML/Tailwind/React/Vue)

Python

Simplify video cost estimation to focus on input tokens only

Abi Raja045761845ca8
diff --git a/backend/models/gemini.py b/backend/models/gemini.py
index eecc275a..9ea49567 100644
--- a/backend/models/gemini.py
+++ b/backend/models/gemini.py
@@ -305,22 +305,19 @@ async def stream_gemini_response_video(
VIDEO_FPS = 10
MAX_OUTPUT_TOKENS = 50000
- # Get video duration and estimate cost
+ # Get video duration and estimate input tokens
video_duration = get_video_duration_from_bytes(video_bytes)
+ estimated_input_tokens = None
if video_duration:
- thinking_level = get_thinking_level_for_model(model)
estimated_cost = estimate_video_generation_cost(
video_duration_seconds=video_duration,
model=model,
fps=VIDEO_FPS,
media_resolution=MediaResolution.HIGH,
- max_output_tokens=MAX_OUTPUT_TOKENS,
- thinking_level=thinking_level,
)
- print(f"\n=== Video Generation Cost Estimate ({model.value}) ===")
+ estimated_input_tokens = estimated_cost.input_tokens
+ print(f"\n=== Video Generation Input Estimate ({model.value}) ===")
print(format_detailed_input_estimate(video_duration, VIDEO_FPS, MediaResolution.HIGH, model))
- print(f"Output tokens (est): {estimated_cost.output_tokens:,} (${estimated_cost.output_cost:.4f})")
- print(f"Total estimated cost: ${estimated_cost.total_cost:.4f}")
print("=" * 50)
else:
print("Warning: Could not determine video duration for cost estimation")
@@ -369,7 +366,7 @@ async def stream_gemini_response_video(
thinking_config=types.ThinkingConfig(
thinking_level="high", include_thoughts=True
),
- media_resolution=types.MediaResolution.MEDIA_RESOLUTION_HIGH,
+ # media_resolution=types.MediaResolution.MEDIA_RESOLUTION_HIGH,
)
elif model == Llm.GEMINI_3_PRO_PREVIEW_LOW:
config = types.GenerateContentConfig(
@@ -434,6 +431,7 @@ async def stream_gemini_response_video(
thinking_tokens = usage_metadata.thoughts_token_count or 0
output_tokens = usage_metadata.candidates_token_count or 0
total_tokens = usage_metadata.total_token_count or 0
+ cached_tokens = getattr(usage_metadata, 'cached_content_token_count', 0) or 0
# Thinking tokens are billed at the same rate as output tokens
billable_output_tokens = thinking_tokens + output_tokens
@@ -441,6 +439,10 @@ async def stream_gemini_response_video(
print(f"\n=== Video Generation Actual Usage ({model.value}) ===")
print(f"Input tokens: {input_tokens:,} (${actual_cost.input_cost:.4f})")
+ print(f"Cached tokens: {cached_tokens:,}")
+ if estimated_input_tokens:
+ diff_pct = ((input_tokens - estimated_input_tokens) / estimated_input_tokens) * 100
+ print(f"Est vs Actual: {estimated_input_tokens:,} → {input_tokens:,} ({diff_pct:+.1f}%)")
print(f"Thinking tokens: {thinking_tokens:,}")
print(f"Output tokens: {output_tokens:,}")
print(f"Billable output: {billable_output_tokens:,} (${actual_cost.output_cost:.4f})")
diff --git a/backend/video/cost_estimation.py b/backend/video/cost_estimation.py
index f12a8a90..4fbcced4 100644
--- a/backend/video/cost_estimation.py
+++ b/backend/video/cost_estimation.py
@@ -34,9 +34,6 @@ VIDEO_TOKENS_PER_FRAME = {
MediaResolution.HIGH: 280,
}
-# Audio tokens per second
-AUDIO_TOKENS_PER_SECOND = 32
-
# Prompt overhead (system prompt + user text)
PROMPT_TOKENS_ESTIMATE = 1200
@@ -81,11 +78,8 @@ def estimate_video_input_tokens(
tokens_per_frame = VIDEO_TOKENS_PER_FRAME[media_resolution]
frame_tokens = int(total_frames * tokens_per_frame)
- # Audio tokens
- audio_tokens = int(video_duration_seconds * AUDIO_TOKENS_PER_SECOND)
-
# Add prompt overhead (system prompt + user text)
- total_tokens = frame_tokens + audio_tokens + PROMPT_TOKENS_ESTIMATE
+ total_tokens = frame_tokens + PROMPT_TOKENS_ESTIMATE
return total_tokens
@@ -177,8 +171,7 @@ def format_detailed_input_estimate(
total_frames = video_duration_seconds * fps
tokens_per_frame = VIDEO_TOKENS_PER_FRAME[media_resolution]
frame_tokens = int(total_frames * tokens_per_frame)
- audio_tokens = int(video_duration_seconds * AUDIO_TOKENS_PER_SECOND)
- total_input_tokens = frame_tokens + audio_tokens + PROMPT_TOKENS_ESTIMATE
+ total_input_tokens = frame_tokens + PROMPT_TOKENS_ESTIMATE
model_name = get_model_api_name(model)
pricing = GEMINI_PRICING.get(model_name, GEMINI_PRICING["gemini-3-flash-preview"])
@@ -188,7 +181,6 @@ def format_detailed_input_estimate(
f"Input Token Calculation:\n"
f" Frames: {video_duration_seconds:.2f}s × {fps} fps = {total_frames:.1f} frames\n"
f" Frame tokens: {total_frames:.1f} × {tokens_per_frame} tokens/frame = {frame_tokens:,} tokens\n"
- f" Audio tokens: {video_duration_seconds:.2f}s × {AUDIO_TOKENS_PER_SECOND} tokens/s = {audio_tokens:,} tokens\n"
f" Prompt overhead: {PROMPT_TOKENS_ESTIMATE:,} tokens\n"
f" Total input: {total_input_tokens:,} tokens\n"
f" Cost: {total_input_tokens:,} ÷ 1M × ${pricing['input_per_million']:.2f} = ${input_cost:.4f}"