Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(xai): bill the above-200k tier at exactly 200k prompt tokens

mateob5b663fd8bcf
diff --git a/litellm/litellm_core_utils/llm_cost_calc/utils.py b/litellm/litellm_core_utils/llm_cost_calc/utils.py
index 6574b261774..69d05e72b4f 100644
--- a/litellm/litellm_core_utils/llm_cost_calc/utils.py
+++ b/litellm/litellm_core_utils/llm_cost_calc/utils.py
@@ -202,7 +202,11 @@ def _parse_above_token_threshold(key: str) -> float:
def _get_token_base_cost(
- model_info: ModelInfo, usage: Usage, service_tier: str | None = None
+ model_info: ModelInfo,
+ usage: Usage,
+ service_tier: str | None = None,
+ *,
+ threshold_is_inclusive: bool = False,
) -> tuple[float, float, float, float, float]:
"""
Return prompt cost, completion cost, and cache costs for a given model and usage.
@@ -210,6 +214,9 @@ def _get_token_base_cost(
If input_tokens > threshold and `input_cost_per_token_above_[x]k_tokens` or `input_cost_per_token_above_[x]_tokens` is set,
then we use the corresponding threshold cost for all token types.
+ `threshold_is_inclusive` switches that comparison to >=, for providers such as xAI
+ that bill the higher tier once the prompt reaches the threshold.
+
Returns:
Tuple[float, float, float, float] - (prompt_cost, completion_cost, cache_creation_cost, cache_read_cost)
"""
@@ -262,7 +269,7 @@ def _get_token_base_cost(
# Handle both formats: _above_128k_tokens and _above_128_tokens
threshold_str = key.split("_above_")[1].split("_tokens")[0]
threshold = _parse_above_token_threshold(key)
- if usage.prompt_tokens > threshold:
+ if usage.prompt_tokens > threshold or (threshold_is_inclusive and usage.prompt_tokens == threshold):
# Prefer a service_tier-specific above-threshold key when available,
# e.g. input_cost_per_token_priority_above_200k_tokens for Gemini
# ON_DEMAND_PRIORITY. Falls back to the standard key automatically
@@ -705,6 +712,7 @@ def generic_cost_per_token(
service_tier: str | None = None,
data_residency: str | None = None,
model_info: ModelInfo | None = None,
+ threshold_is_inclusive: bool = False,
) -> tuple[float, float]:
"""
Calculates the cost per token for a given model, prompt tokens, and completion tokens.
@@ -716,6 +724,8 @@ def generic_cost_per_token(
- usage: LiteLLM Usage block, containing anthropic caching information
- data_residency: optional OpenAI data-residency region (e.g. "eu", "us"),
used to apply the per-model regional-processing uplift multiplier.
+ - threshold_is_inclusive: bill the above-threshold tier when the prompt is exactly
+ at the threshold, as xAI does.
Returns:
Tuple[float, float] - prompt_cost_in_usd, completion_cost_in_usd
@@ -777,7 +787,12 @@ def generic_cost_per_token(
cache_creation_cost,
cache_creation_cost_above_1hr,
cache_read_cost,
- ) = _get_token_base_cost(model_info=model_info, usage=usage, service_tier=service_tier)
+ ) = _get_token_base_cost(
+ model_info=model_info,
+ usage=usage,
+ service_tier=service_tier,
+ threshold_is_inclusive=threshold_is_inclusive,
+ )
prompt_cost = _calculate_input_cost(
prompt_tokens_details=prompt_tokens_details,
diff --git a/litellm/llms/xai/cost_calculator.py b/litellm/llms/xai/cost_calculator.py
index 384388f3300..b44f8935e17 100644
--- a/litellm/llms/xai/cost_calculator.py
+++ b/litellm/llms/xai/cost_calculator.py
@@ -47,7 +47,13 @@ def cost_per_token(model: str, usage: Usage) -> tuple[float, float]:
completion_tokens_details=None,
)
- prompt_cost, completion_cost = generic_cost_per_token(model=model, usage=modified_usage, custom_llm_provider="xai")
+ # xAI bills the higher tier once the prompt reaches 200k tokens, not strictly above it
+ prompt_cost, completion_cost = generic_cost_per_token(
+ model=model,
+ usage=modified_usage,
+ custom_llm_provider="xai",
+ threshold_is_inclusive=True,
+ )
return prompt_cost, completion_cost
diff --git a/tests/test_litellm/llms/xai/test_xai_cost_calculator.py b/tests/test_litellm/llms/xai/test_xai_cost_calculator.py
index a8d9708d2f2..32141bead0e 100644
--- a/tests/test_litellm/llms/xai/test_xai_cost_calculator.py
+++ b/tests/test_litellm/llms/xai/test_xai_cost_calculator.py
@@ -456,6 +456,34 @@ class TestXAICostCalculator:
assert math.isclose(prompt_cost, expected_prompt_cost, rel_tol=1e-10)
assert math.isclose(completion_cost, expected_completion_cost, rel_tol=1e-10)
+ def test_grok_4_20_at_exactly_200k_prompt_tokens_uses_higher_tier(self):
+ """xAI bills the >=200k tier once the prompt reaches 200k, so the boundary is inclusive."""
+ usage = Usage(prompt_tokens=200_000, completion_tokens=1_000, total_tokens=201_000)
+
+ prompt_cost, completion_cost = cost_per_token(
+ model="grok-4.20-0309-reasoning", usage=usage
+ )
+
+ expected_prompt_cost = 200_000 * 2.5e-6
+ expected_completion_cost = 1_000 * 5e-6
+
+ assert math.isclose(prompt_cost, expected_prompt_cost, rel_tol=1e-10)
+ assert math.isclose(completion_cost, expected_completion_cost, rel_tol=1e-10)
+
+ def test_grok_4_20_just_below_200k_prompt_tokens_uses_base_tier(self):
+ """One token under the boundary still bills at the base rates."""
+ usage = Usage(prompt_tokens=199_999, completion_tokens=1_000, total_tokens=200_999)
+
+ prompt_cost, completion_cost = cost_per_token(
+ model="grok-4.20-0309-reasoning", usage=usage
+ )
+
+ expected_prompt_cost = 199_999 * 1.25e-6
+ expected_completion_cost = 1_000 * 2.5e-6
+
+ assert math.isclose(prompt_cost, expected_prompt_cost, rel_tol=1e-10)
+ assert math.isclose(completion_cost, expected_completion_cost, rel_tol=1e-10)
+
def test_grok_4_20_multi_agent_cost_calculation(self):
"""Test cost calculation for grok-4.20-multi-agent-beta-0309 model."""
usage = Usage(prompt_tokens=200, completion_tokens=300, total_tokens=500)