Skip to content

ModelPricing

Base classes

Name Children Inherits
Schema
schemez.schema
Base class for configuration models.

â‹” Inheritance diagram

graph TD
  3326076699["chat_model.ModelPricing"]
  330168112["schema.Schema"]
  3879467795["main.BaseModel"]
  2751680898["builtins.object"]
  330168112 --> 3326076699
  3879467795 --> 330168112
  2751680898 --> 3879467795

🛈 DocStrings

Bases: Schema

Pricing information for chat models.

Source code in src/tokonomics/models/chat_model.py
class ModelPricing(Schema):
    """Pricing information for chat models."""

    input_cost_per_token: Decimal | None = None
    """Standard input cost per token."""

    output_cost_per_token: Decimal | None = None
    """Standard output cost per token."""

    output_cost_per_reasoning_token: Decimal | None = None
    """Cost per reasoning output token for models like o1."""

    # Caching pricing
    cache_creation_input_token_cost: Decimal | None = None
    """Cost per token for cache creation."""

    cache_read_input_token_cost: Decimal | None = None
    """Cost per token for cache reads."""

    # Contextual pricing
    input_cost_above_128k: Decimal | None = None
    """Input cost per token above 128k context."""

    output_cost_above_128k: Decimal | None = None
    """Output cost per token above 128k context."""

    input_cost_above_200k: Decimal | None = None
    """Input cost per token above 200k context."""

    output_cost_above_200k: Decimal | None = None
    """Output cost per token above 200k context."""

    # Priority/flex pricing
    input_cost_priority: Decimal | None = None
    """Input cost per token for priority requests."""

    output_cost_priority: Decimal | None = None
    """Output cost per token for priority requests."""

    input_cost_flex: Decimal | None = None
    """Input cost per token for flex requests."""

    output_cost_flex: Decimal | None = None
    """Output cost per token for flex requests."""

    # Multimodal pricing
    input_cost_per_image: Decimal | None = None
    """Cost per image input."""

    input_cost_per_audio_second: Decimal | None = None
    """Cost per second of audio input."""

    input_cost_per_video_second: Decimal | None = None
    """Cost per second of video input."""

    # Tiered pricing
    tiered_pricing: list[TieredPricingTier] | None = None
    """Volume-based tiered pricing if available."""

    def calculate_cost(
        self,
        *,
        input_tokens: int = 0,
        cache_write_tokens: int = 0,
        cache_read_tokens: int = 0,
        input_audio_tokens: int = 0,
        cache_audio_read_tokens: int = 0,
        output_tokens: int = 0,
    ) -> Decimal | None:
        """Calculate cost based on usage mirroring pydantic-ai RunUsage.

        Uses contextual pricing if token count exceeds thresholds,
        otherwise uses standard input/output cost per token.

        Args:
            input_tokens: Total number of input/prompt tokens
            cache_write_tokens: Total number of tokens written to the cache
            cache_read_tokens: Total number of tokens read from the cache
            input_audio_tokens: Total number of audio input tokens
            cache_audio_read_tokens: Total number of audio tokens read from the cache
            output_tokens: Total number of output/completion tokens

        Returns:
            Total cost or None if pricing unavailable
        """
        total_cost = Decimal(0)

        # Calculate total input tokens for context threshold determination
        total_input = input_tokens + cache_write_tokens + input_audio_tokens

        # Calculate input cost (standard text tokens)
        if input_tokens > 0 and self.input_cost_per_token is not None:
            if total_input > 200_000 and self.input_cost_above_200k is not None:  # noqa: PLR2004
                input_cost = self.input_cost_above_200k
            elif total_input > 128_000 and self.input_cost_above_128k is not None:  # noqa: PLR2004
                input_cost = self.input_cost_above_128k
            else:
                input_cost = self.input_cost_per_token
            total_cost += input_cost * input_tokens

        # Calculate cache write cost
        if cache_write_tokens > 0:
            # Use cache creation pricing if available, otherwise standard input pricing
            cache_cost = (
                self.cache_creation_input_token_cost or self.input_cost_per_token or Decimal(0)
            )
            total_cost += cache_cost * cache_write_tokens

        # Calculate cache read cost
        if cache_read_tokens > 0:
            # Use actual cache read pricing if available, otherwise fallback to 10%
            if self.cache_read_input_token_cost is not None:
                cache_read_cost = self.cache_read_input_token_cost
                total_cost += cache_read_cost * cache_read_tokens
            else:
                # Fallback: assume 10% of input cost
                fallback_cost = self.input_cost_per_token or Decimal(0)
                total_cost += fallback_cost * cache_read_tokens * Decimal("0.1")

        # Calculate audio input cost
        if input_audio_tokens > 0:
            audio_cost = self.input_cost_per_token or Decimal(0)
            total_cost += audio_cost * input_audio_tokens

        # Calculate cache audio read cost
        if cache_audio_read_tokens > 0:
            audio_cache_cost = self.input_cost_per_token or Decimal(0)
            total_cost += audio_cache_cost * cache_audio_read_tokens * Decimal("0.1")

        # Calculate output cost
        if output_tokens > 0 and self.output_cost_per_token is not None:
            if total_input > 200_000 and self.output_cost_above_200k is not None:  # noqa: PLR2004
                output_cost = self.output_cost_above_200k
            elif total_input > 128_000 and self.output_cost_above_128k is not None:  # noqa: PLR2004
                output_cost = self.output_cost_above_128k
            else:
                output_cost = self.output_cost_per_token
            total_cost += output_cost * output_tokens

        # Note: tool_calls and requests are currently not charged by most providers

        return total_cost if total_cost > 0 else None

cache_creation_input_token_cost class-attribute instance-attribute

cache_creation_input_token_cost: Decimal | None = None

Cost per token for cache creation.

cache_read_input_token_cost class-attribute instance-attribute

cache_read_input_token_cost: Decimal | None = None

Cost per token for cache reads.

input_cost_above_128k class-attribute instance-attribute

input_cost_above_128k: Decimal | None = None

Input cost per token above 128k context.

input_cost_above_200k class-attribute instance-attribute

input_cost_above_200k: Decimal | None = None

Input cost per token above 200k context.

input_cost_flex class-attribute instance-attribute

input_cost_flex: Decimal | None = None

Input cost per token for flex requests.

input_cost_per_audio_second class-attribute instance-attribute

input_cost_per_audio_second: Decimal | None = None

Cost per second of audio input.

input_cost_per_image class-attribute instance-attribute

input_cost_per_image: Decimal | None = None

Cost per image input.

input_cost_per_token class-attribute instance-attribute

input_cost_per_token: Decimal | None = None

Standard input cost per token.

input_cost_per_video_second class-attribute instance-attribute

input_cost_per_video_second: Decimal | None = None

Cost per second of video input.

input_cost_priority class-attribute instance-attribute

input_cost_priority: Decimal | None = None

Input cost per token for priority requests.

output_cost_above_128k class-attribute instance-attribute

output_cost_above_128k: Decimal | None = None

Output cost per token above 128k context.

output_cost_above_200k class-attribute instance-attribute

output_cost_above_200k: Decimal | None = None

Output cost per token above 200k context.

output_cost_flex class-attribute instance-attribute

output_cost_flex: Decimal | None = None

Output cost per token for flex requests.

output_cost_per_reasoning_token class-attribute instance-attribute

output_cost_per_reasoning_token: Decimal | None = None

Cost per reasoning output token for models like o1.

output_cost_per_token class-attribute instance-attribute

output_cost_per_token: Decimal | None = None

Standard output cost per token.

output_cost_priority class-attribute instance-attribute

output_cost_priority: Decimal | None = None

Output cost per token for priority requests.

tiered_pricing class-attribute instance-attribute

tiered_pricing: list[TieredPricingTier] | None = None

Volume-based tiered pricing if available.

calculate_cost

calculate_cost(
    *,
    input_tokens: int = 0,
    cache_write_tokens: int = 0,
    cache_read_tokens: int = 0,
    input_audio_tokens: int = 0,
    cache_audio_read_tokens: int = 0,
    output_tokens: int = 0,
) -> Decimal | None

Calculate cost based on usage mirroring pydantic-ai RunUsage.

Uses contextual pricing if token count exceeds thresholds, otherwise uses standard input/output cost per token.

Parameters:

Name Type Description Default
input_tokens int

Total number of input/prompt tokens

0
cache_write_tokens int

Total number of tokens written to the cache

0
cache_read_tokens int

Total number of tokens read from the cache

0
input_audio_tokens int

Total number of audio input tokens

0
cache_audio_read_tokens int

Total number of audio tokens read from the cache

0
output_tokens int

Total number of output/completion tokens

0

Returns:

Type Description
Decimal | None

Total cost or None if pricing unavailable

Source code in src/tokonomics/models/chat_model.py
def calculate_cost(
    self,
    *,
    input_tokens: int = 0,
    cache_write_tokens: int = 0,
    cache_read_tokens: int = 0,
    input_audio_tokens: int = 0,
    cache_audio_read_tokens: int = 0,
    output_tokens: int = 0,
) -> Decimal | None:
    """Calculate cost based on usage mirroring pydantic-ai RunUsage.

    Uses contextual pricing if token count exceeds thresholds,
    otherwise uses standard input/output cost per token.

    Args:
        input_tokens: Total number of input/prompt tokens
        cache_write_tokens: Total number of tokens written to the cache
        cache_read_tokens: Total number of tokens read from the cache
        input_audio_tokens: Total number of audio input tokens
        cache_audio_read_tokens: Total number of audio tokens read from the cache
        output_tokens: Total number of output/completion tokens

    Returns:
        Total cost or None if pricing unavailable
    """
    total_cost = Decimal(0)

    # Calculate total input tokens for context threshold determination
    total_input = input_tokens + cache_write_tokens + input_audio_tokens

    # Calculate input cost (standard text tokens)
    if input_tokens > 0 and self.input_cost_per_token is not None:
        if total_input > 200_000 and self.input_cost_above_200k is not None:  # noqa: PLR2004
            input_cost = self.input_cost_above_200k
        elif total_input > 128_000 and self.input_cost_above_128k is not None:  # noqa: PLR2004
            input_cost = self.input_cost_above_128k
        else:
            input_cost = self.input_cost_per_token
        total_cost += input_cost * input_tokens

    # Calculate cache write cost
    if cache_write_tokens > 0:
        # Use cache creation pricing if available, otherwise standard input pricing
        cache_cost = (
            self.cache_creation_input_token_cost or self.input_cost_per_token or Decimal(0)
        )
        total_cost += cache_cost * cache_write_tokens

    # Calculate cache read cost
    if cache_read_tokens > 0:
        # Use actual cache read pricing if available, otherwise fallback to 10%
        if self.cache_read_input_token_cost is not None:
            cache_read_cost = self.cache_read_input_token_cost
            total_cost += cache_read_cost * cache_read_tokens
        else:
            # Fallback: assume 10% of input cost
            fallback_cost = self.input_cost_per_token or Decimal(0)
            total_cost += fallback_cost * cache_read_tokens * Decimal("0.1")

    # Calculate audio input cost
    if input_audio_tokens > 0:
        audio_cost = self.input_cost_per_token or Decimal(0)
        total_cost += audio_cost * input_audio_tokens

    # Calculate cache audio read cost
    if cache_audio_read_tokens > 0:
        audio_cache_cost = self.input_cost_per_token or Decimal(0)
        total_cost += audio_cache_cost * cache_audio_read_tokens * Decimal("0.1")

    # Calculate output cost
    if output_tokens > 0 and self.output_cost_per_token is not None:
        if total_input > 200_000 and self.output_cost_above_200k is not None:  # noqa: PLR2004
            output_cost = self.output_cost_above_200k
        elif total_input > 128_000 and self.output_cost_above_128k is not None:  # noqa: PLR2004
            output_cost = self.output_cost_above_128k
        else:
            output_cost = self.output_cost_per_token
        total_cost += output_cost * output_tokens

    # Note: tool_calls and requests are currently not charged by most providers

    return total_cost if total_cost > 0 else None

:fa-brands-github: Show source on GitHub