Skip to content

Conversation

@steebchen
Copy link
Member

@steebchen steebchen commented Jul 23, 2025

Added support for pricing tiers based on context size for models. Updated cost calculation logic to account for dynamic pricing. Introduced unit tests to validate tier-based cost computation.

Summary by CodeRabbit

  • New Features

    • Introduced tiered pricing for the "qwen3-coder-plus" model from Alibaba, with costs now varying based on the total context size (input and output tokens).
    • Pricing automatically adjusts according to defined context size ranges for more accurate cost calculations.
  • Bug Fixes

    • Improved cost calculation logic to ensure correct pricing tier is applied based on context size.
  • Tests

    • Added comprehensive tests to verify correct application of tiered pricing across different context size scenarios.

@claude
Copy link
Contributor

claude bot commented Jul 23, 2025

Claude finished @steebchen's task —— View job


PR Review: Dynamic Pricing Tiers Implementation ✅

Completed comprehensive code review of the dynamic pricing tiers implementation.

Todo List:

  • Create pending PR review
  • Get PR diff to understand specific changes
  • Analyze costs.spec.ts changes (test coverage)
  • Analyze costs.ts changes (core logic)
  • Analyze models.ts changes (model definitions)
  • Analyze alibaba.ts changes (provider-specific implementation)
  • Add inline comments for any issues found
  • Submit review with overall summary

Review Summary:

✅ Strengths:

  • Clean, well-structured implementation of dynamic pricing tiers
  • Good integration with existing cost calculation logic
  • Comprehensive test coverage for main use cases
  • Proper TypeScript interfaces and documentation

⚠️ Critical Issue:

  • Missing fallback logic when context size doesn't match any pricing tier
  • Could result in zero costs for edge cases (contexts > 1M tokens)

📝 Recommendations:

  • Fix fallback pricing logic in costs.ts
  • Add edge case tests (boundary values, exceeding tiers)
  • Consider validation for tier configuration completeness

The core implementation is solid but needs the fallback issue addressed before merging.


Copy link
Contributor

@claude claude bot left a comment

Choose a reason for hiding this comment

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

Overall Assessment

Good Implementation: The dynamic pricing tiers feature is well-designed and properly integrated. The code structure is clean and the test coverage demonstrates the functionality works as intended.

Critical Issue Found: The main concern is the missing fallback pricing logic in costs.ts. When no tier matches the context size, the code falls back to inputPrice/outputPrice properties that no longer exist on models using pricingTiers, resulting in zero costs.

Recommendations:

  1. Fix the fallback logic in costs.ts to handle cases where context size doesn't match any tier
  2. Enhance test coverage with edge cases (boundary values, exceeding max tier, etc.)
  3. Consider adding validation to ensure pricing tiers are properly configured and cover the full context range

The implementation concept is solid, but the fallback handling needs to be addressed before merging to prevent incorrect cost calculations.

if (applicableTier) {
inputPrice = applicableTier.inputPrice;
outputPrice = applicableTier.outputPrice;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Critical Issue: When no pricing tier matches the context size, the code falls back to providerInfo.inputPrice and providerInfo.outputPrice, but these were removed from the qwen3-coder-plus model definition. This means inputPrice and outputPrice will be undefined, defaulting to 0 in the calculation, resulting in zero costs.

Suggestion: Add explicit error handling or a default fallback tier:

if (applicableTier) {
  inputPrice = applicableTier.inputPrice;
  outputPrice = applicableTier.outputPrice;
} else {
  // Log warning and either throw error or use highest tier as fallback
  console.warn(`No pricing tier found for context size ${totalContextSize} for model ${model}`);
  // Option 1: Use highest tier as fallback
  const highestTier = providerInfo.pricingTiers[providerInfo.pricingTiers.length - 1];
  inputPrice = highestTier.inputPrice;
  outputPrice = highestTier.outputPrice;
}

expect(largeResult.inputCost).toBeCloseTo(1.8); // 300000 * (6/1e6)
expect(largeResult.outputCost).toBeCloseTo(6.0); // 100000 * (60/1e6)
expect(largeResult.totalCost).toBeCloseTo(7.8); // 1.8 + 6.0
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing Test Coverage: Consider adding edge case tests:

  1. Context size exceeding maximum tier: What happens with 1,500,000 tokens (beyond the 1M limit)?
  2. Context size at tier boundaries: Test exactly 32000, 32001, 128000, 128001, etc.
  3. Zero or negative context size: Edge case handling
  4. Empty pricing tiers array: Defensive programming test

Example additional test:

it("should handle context size exceeding maximum tier", () => {
  const result = calculateCosts("qwen3-coder-plus", "alibaba", 1500000, 100000, null);
  // Should either use highest tier or handle gracefully
  expect(result.inputCost).not.toBe(0); // Shouldn't be zero due to missing fallback
});

},
{
minContextSize: 256001,
maxContextSize: 1000000,
Copy link
Contributor

Choose a reason for hiding this comment

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

Potential Gap: The highest tier ends at 1,000,000 tokens, which matches the model's contextSize, but what happens if the actual usage exceeds this? Consider:

  1. Adding an overflow tier for contexts > 1M tokens
  2. Or explicitly documenting that contexts > 1M should be rejected upstream

The current implementation could result in zero costs for contexts exceeding 1M tokens due to the missing fallback in the cost calculation logic.

Added support for pricing tiers based on context size for models.
Updated cost calculation logic to account for dynamic pricing.
Introduced unit tests to validate tier-based cost computation.
@steebchen steebchen force-pushed the feat/models-dynamic-pricing branch from 3c352e6 to 056eda9 Compare July 23, 2025 17:29
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jul 23, 2025

Note

Reviews paused

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Walkthrough

The changes introduce tiered pricing for the "qwen3-coder-plus" model under the Alibaba provider, allowing input and output token costs to vary based on context size. This is implemented by adding a pricingTiers property to the model definition, updating the cost calculation logic to select the appropriate tier, and adding corresponding tests.

Changes

File(s) Change Summary
apps/gateway/src/lib/costs.ts Updated cost calculation logic to support tiered pricing based on context size.
packages/models/src/models.ts Added PricingTier interface and optional pricingTiers property to ProviderModelMapping.
packages/models/src/models/alibaba.ts Replaced fixed prices for "qwen3-coder-plus" with a pricingTiers array defining four tiers.
apps/gateway/src/lib/costs.spec.ts Added tests verifying cost calculation for each pricing tier of "qwen3-coder-plus".

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant calculateCosts
    participant ProviderModelMapping

    Caller->>calculateCosts: Call with providerInfo, promptTokens, completionTokens
    calculateCosts->>ProviderModelMapping: Check for pricingTiers
    alt pricingTiers exist
        calculateCosts->>calculateCosts: Sum promptTokens + completionTokens as contextSize
        calculateCosts->>ProviderModelMapping: Find tier matching contextSize
        calculateCosts->>calculateCosts: Use tier's inputPrice/outputPrice
    else no pricingTiers
        calculateCosts->>calculateCosts: Use default inputPrice/outputPrice
    end
    calculateCosts->>Caller: Return calculated costs
Loading

Estimated code review effort

2 (~15 minutes)


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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@steebchen steebchen requested a review from Copilot July 23, 2025 18:42
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This pull request implements dynamic pricing tiers for models based on context size, replacing the previous flat pricing structure. The change allows models to have different pricing rates depending on the total token count (prompt + completion tokens), enabling more granular cost calculations.

  • Added PricingTier interface and pricingTiers field to model definitions
  • Updated cost calculation logic to select appropriate pricing tier based on total context size
  • Implemented comprehensive unit tests to validate tier-based pricing calculations

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
packages/models/src/models.ts Added PricingTier interface and pricingTiers field to ProviderModelMapping
packages/models/src/models/alibaba.ts Migrated qwen3-coder-plus model from flat pricing to tiered pricing structure
apps/gateway/src/lib/costs.ts Updated calculateCosts function to support pricing tier selection based on context size
apps/gateway/src/lib/costs.spec.ts Added unit tests validating pricing tier calculations for different context sizes

Comment on lines +137 to +144
const totalContextSize =
calculatedPromptTokens + calculatedCompletionTokens;

// Find the appropriate pricing tier based on context size
const applicableTier = providerInfo.pricingTiers.find(
(tier) =>
totalContextSize >= tier.minContextSize &&
totalContextSize <= tier.maxContextSize,
Copy link

Copilot AI Jul 23, 2025

Choose a reason for hiding this comment

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

The term 'totalContextSize' is misleading. This variable represents the sum of prompt and completion tokens, not the actual context size of the model. Consider renaming to 'totalTokens' or 'totalTokenCount' to accurately reflect what it represents.

Suggested change
const totalContextSize =
calculatedPromptTokens + calculatedCompletionTokens;
// Find the appropriate pricing tier based on context size
const applicableTier = providerInfo.pricingTiers.find(
(tier) =>
totalContextSize >= tier.minContextSize &&
totalContextSize <= tier.maxContextSize,
const totalTokens =
calculatedPromptTokens + calculatedCompletionTokens;
// Find the appropriate pricing tier based on context size
const applicableTier = providerInfo.pricingTiers.find(
(tier) =>
totalTokens >= tier.minContextSize &&
totalTokens <= tier.maxContextSize,

Copilot uses AI. Check for mistakes.
calculatedPromptTokens + calculatedCompletionTokens;

// Find the appropriate pricing tier based on context size
const applicableTier = providerInfo.pricingTiers.find(
Copy link

Copilot AI Jul 23, 2025

Choose a reason for hiding this comment

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

The pricing tier selection logic may fail when the total token count exceeds all defined tier ranges or falls into gaps between tiers. There's no fallback mechanism if no tier matches the token count, which could result in zero pricing when inputPrice and outputPrice are undefined.

Copilot uses AI. Check for mistakes.
@steebchen
Copy link
Member Author

@coderabbitai pause

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jul 23, 2025

✅ Actions performed

Reviews paused.

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.

2 participants