|
| 1 | +# This file was auto-generated by Fern from our API Definition. |
| 2 | + |
| 3 | +import datetime as dt |
| 4 | +import typing |
| 5 | + |
| 6 | +from ....core.datetime_utils import serialize_datetime |
| 7 | +from ....core.pydantic_utilities import deep_union_pydantic_dicts, pydantic_v1 |
| 8 | +from .pricing_tier_condition import PricingTierCondition |
| 9 | + |
| 10 | + |
| 11 | +class PricingTier(pydantic_v1.BaseModel): |
| 12 | + """ |
| 13 | + Pricing tier definition with conditional pricing based on usage thresholds. |
| 14 | +
|
| 15 | + Pricing tiers enable accurate cost tracking for LLM providers that charge different rates based on usage patterns. |
| 16 | + For example, some providers charge higher rates when context size exceeds certain thresholds. |
| 17 | +
|
| 18 | + How tier matching works: |
| 19 | + 1. Tiers are evaluated in ascending priority order (priority 1 before priority 2, etc.) |
| 20 | + 2. The first tier where ALL conditions match is selected |
| 21 | + 3. If no conditional tiers match, the default tier is used as a fallback |
| 22 | + 4. The default tier has priority 0 and no conditions |
| 23 | +
|
| 24 | + Why priorities matter: |
| 25 | + - Lower priority numbers are evaluated first, allowing you to define specific cases before general ones |
| 26 | + - Example: Priority 1 for "high usage" (>200K tokens), Priority 2 for "medium usage" (>100K tokens), Priority 0 for default |
| 27 | + - Without proper ordering, a less specific condition might match before a more specific one |
| 28 | +
|
| 29 | + Every model must have exactly one default tier to ensure cost calculation always succeeds. |
| 30 | + """ |
| 31 | + |
| 32 | + id: str = pydantic_v1.Field() |
| 33 | + """ |
| 34 | + Unique identifier for the pricing tier |
| 35 | + """ |
| 36 | + |
| 37 | + name: str = pydantic_v1.Field() |
| 38 | + """ |
| 39 | + Name of the pricing tier for display and identification purposes. |
| 40 | + |
| 41 | + Examples: "Standard", "High Volume Tier", "Large Context", "Extended Context Tier" |
| 42 | + """ |
| 43 | + |
| 44 | + is_default: bool = pydantic_v1.Field(alias="isDefault") |
| 45 | + """ |
| 46 | + Whether this is the default tier. Every model must have exactly one default tier with priority 0 and no conditions. |
| 47 | + |
| 48 | + The default tier serves as a fallback when no conditional tiers match, ensuring cost calculation always succeeds. |
| 49 | + It typically represents the base pricing for standard usage patterns. |
| 50 | + """ |
| 51 | + |
| 52 | + priority: int = pydantic_v1.Field() |
| 53 | + """ |
| 54 | + Priority for tier matching evaluation. Lower numbers = higher priority (evaluated first). |
| 55 | + |
| 56 | + The default tier must always have priority 0. Conditional tiers should have priority 1, 2, 3, etc. |
| 57 | + |
| 58 | + Example ordering: |
| 59 | + - Priority 0: Default tier (no conditions, always matches as fallback) |
| 60 | + - Priority 1: High usage tier (e.g., >200K tokens) |
| 61 | + - Priority 2: Medium usage tier (e.g., >100K tokens) |
| 62 | + |
| 63 | + This ensures more specific conditions are checked before general ones. |
| 64 | + """ |
| 65 | + |
| 66 | + conditions: typing.List[PricingTierCondition] = pydantic_v1.Field() |
| 67 | + """ |
| 68 | + Array of conditions that must ALL be met for this tier to match (AND logic). |
| 69 | + |
| 70 | + The default tier must have an empty conditions array. Conditional tiers should have one or more conditions |
| 71 | + that define when this tier's pricing applies. |
| 72 | + |
| 73 | + Multiple conditions enable complex matching scenarios (e.g., "high input tokens AND low output tokens"). |
| 74 | + """ |
| 75 | + |
| 76 | + prices: typing.Dict[str, float] = pydantic_v1.Field() |
| 77 | + """ |
| 78 | + Prices (USD) by usage type for this tier. |
| 79 | + |
| 80 | + Common usage types: "input", "output", "total", "request", "image" |
| 81 | + Prices are specified in USD per unit (e.g., per token, per request, per second). |
| 82 | + |
| 83 | + Example: {"input": 0.000003, "output": 0.000015} means $3 per million input tokens and $15 per million output tokens. |
| 84 | + """ |
| 85 | + |
| 86 | + def json(self, **kwargs: typing.Any) -> str: |
| 87 | + kwargs_with_defaults: typing.Any = { |
| 88 | + "by_alias": True, |
| 89 | + "exclude_unset": True, |
| 90 | + **kwargs, |
| 91 | + } |
| 92 | + return super().json(**kwargs_with_defaults) |
| 93 | + |
| 94 | + def dict(self, **kwargs: typing.Any) -> typing.Dict[str, typing.Any]: |
| 95 | + kwargs_with_defaults_exclude_unset: typing.Any = { |
| 96 | + "by_alias": True, |
| 97 | + "exclude_unset": True, |
| 98 | + **kwargs, |
| 99 | + } |
| 100 | + kwargs_with_defaults_exclude_none: typing.Any = { |
| 101 | + "by_alias": True, |
| 102 | + "exclude_none": True, |
| 103 | + **kwargs, |
| 104 | + } |
| 105 | + |
| 106 | + return deep_union_pydantic_dicts( |
| 107 | + super().dict(**kwargs_with_defaults_exclude_unset), |
| 108 | + super().dict(**kwargs_with_defaults_exclude_none), |
| 109 | + ) |
| 110 | + |
| 111 | + class Config: |
| 112 | + frozen = True |
| 113 | + smart_union = True |
| 114 | + allow_population_by_field_name = True |
| 115 | + populate_by_name = True |
| 116 | + extra = pydantic_v1.Extra.allow |
| 117 | + json_encoders = {dt.datetime: serialize_datetime} |
0 commit comments