Skip to content

feat(costs): update image token calculation based on size - #1210

Merged
steebchen merged 1 commit into
mainfrom
feat/costs4k
Nov 20, 2025
Merged

steebchen merged 1 commit into
mainfrom
feat/costs4k

Conversation

@steebchen

@steebchen steebchen commented Nov 20, 2025

Copy link
Copy Markdown
Member

Summary by CodeRabbit

  • Bug Fixes

    • Image cost calculations now properly account for image size variations, improving cost accuracy.
  • Chores

    • Updated configuration files.

✏️ Tip: You can customize this high-level summary in your review settings.

@steebchen
steebchen enabled auto-merge November 20, 2025 18:25
@coderabbitai

coderabbitai Bot commented Nov 20, 2025

Copy link
Copy Markdown
Contributor

Walkthrough

This PR adds image size awareness to cost calculations by introducing an optional imageSize parameter to the calculateCosts function. The parameter is threaded through the chat module to the costs calculation logic, which uses it to determine per-image token accounting (2000 tokens for 4K images, 1120 otherwise). A .gitignore entry is also added.

Changes

Cohort / File(s) Summary
Git configuration
.gitignore
Adds output.png to the ignored files list
Cost calculation API
apps/gateway/src/lib/costs.ts
Introduces optional imageSize parameter to calculateCosts function; uses parameter to determine TOKENS_PER_IMAGE (2000 for "4K", 1120 otherwise), affecting image output cost calculations
Chat module integration
apps/gateway/src/chat/chat.ts
Propagates image_config?.image_size as argument to calculateCosts in OpenAI chat completions path and streaming/non-streaming costs calculation paths

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Straightforward parameter addition following a consistent propagation pattern
  • Simple conditional logic for token count determination based on image size
  • All changes cohesively support a single feature with minimal surface area

Possibly related PRs

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: updating image token calculation logic to vary based on image size (4K vs standard).
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/costs4k

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@steebchen
steebchen added this pull request to the merge queue Nov 20, 2025

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
apps/gateway/src/chat/chat.ts (1)

3202-3216: Add enum validation for image_size and normalize case-sensitivity in cost calculations.

The concern is confirmed. The schema defines image_size as a plain string with no validation (chat.ts:246), and the cost calculation in apps/gateway/src/lib/costs.ts:263 only recognizes uppercase "4K", defaulting all other values (including "4k") to 1120 tokens instead of 2000:

const TOKENS_PER_IMAGE = imageSize === "4K" ? 2000 : 1120;

This creates a cost calculation bug if users pass lowercase variants or invalid values.

Fixes required:

  1. chat.ts:246 — Change image_size: z.string().optional() to image_size: z.enum(["1K", "2K", "4K"]).optional()
  2. apps/gateway/src/lib/costs.ts:263 — Normalize case-sensitivity: imageSize?.toUpperCase() === "4K"
🧹 Nitpick comments (2)
apps/gateway/src/lib/costs.ts (2)

68-88: Update JSDoc to document the new parameter.

The function signature now includes imageSize, but the JSDoc comment (lines 68-72) doesn't document this parameter.

Apply this diff to update the documentation:

 /**
  * Calculate costs based on model, provider, and token counts
  * If promptTokens or completionTokens are not available, it will try to calculate them
  * from the fullOutput parameter if provided
+ * @param imageSize - Optional image size (e.g., "4K") to determine per-image token count
  */
 export function calculateCosts(
 	model: Model,

260-264: Consider case-insensitive comparison and explicit value handling.

The current implementation has a few potential issues:

  1. Case sensitivity: The comparison imageSize === "4K" is case-sensitive. If users pass "4k" (lowercase), it will default to 1120 tokens instead of 2000.

  2. Implicit defaults: The comment mentions "1K/2K images" but these values aren't explicitly handled—they rely on the fallback to 1120. This could be confusing.

  3. Magic string: The "4K" string literal could be a constant or enum for better maintainability.

Consider one of these approaches:

Option 1: Case-insensitive with explicit handling

-const TOKENS_PER_IMAGE = imageSize === "4K" ? 2000 : 1120;
+const normalizedSize = imageSize?.toUpperCase();
+const TOKENS_PER_IMAGE = 
+  normalizedSize === "4K" ? 2000 :
+  normalizedSize === "2K" || normalizedSize === "1K" ? 1120 :
+  1120; // default

Option 2: Use constants

+const IMAGE_TOKEN_COUNTS = {
+  "4K": 2000,
+  "2K": 1120,
+  "1K": 1120,
+} as const;
+const DEFAULT_IMAGE_TOKENS = 1120;
+
-const TOKENS_PER_IMAGE = imageSize === "4K" ? 2000 : 1120;
+const normalizedSize = imageSize?.toUpperCase() as keyof typeof IMAGE_TOKEN_COUNTS;
+const TOKENS_PER_IMAGE = IMAGE_TOKEN_COUNTS[normalizedSize] ?? DEFAULT_IMAGE_TOKENS;
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ddfaf76 and ffca5d9.

📒 Files selected for processing (3)
  • .gitignore (1 hunks)
  • apps/gateway/src/chat/chat.ts (2 hunks)
  • apps/gateway/src/lib/costs.ts (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (9)
  • GitHub Check: lint / run
  • GitHub Check: build / run
  • GitHub Check: test / run
  • GitHub Check: autofix
  • GitHub Check: e2e-shards (1)
  • GitHub Check: e2e-shards (2)
  • GitHub Check: e2e-shards (4)
  • GitHub Check: e2e-shards (3)
  • GitHub Check: e2e-shards (5)
🔇 Additional comments (2)
.gitignore (1)

21-21: LGTM!

Standard addition to ignore a temporary output file.

apps/gateway/src/chat/chat.ts (1)

3758-3772: LGTM - Consistent implementation.

The image_size parameter is correctly passed in the non-streaming path, maintaining consistency with the streaming implementation.

Merged via the queue into main with commit 31f8f27 Nov 20, 2025
13 of 14 checks passed
@steebchen
steebchen deleted the feat/costs4k branch November 20, 2025 18:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant