ScrapeGraphAI/Scrapegraph-ai · error · ValueError
input_tokens is required for completion costs with tiered pr
Error message
input_tokens is required for completion costs with tiered pricing
What it means
ValueError from get_token_cost_for_model: for models with tiered (token-volume-dependent) pricing, the completion/output rate depends on the input token count, so input_tokens must be supplied to compute completion costs. Without it the correct tier cannot be selected.
Source
Thrown at scrapegraphai/utils/custom_callback.py:53
Args:
model_name: Name of the model
num_tokens: Number of tokens.
is_completion: Whether the model is used for completion or not.
Defaults to False.
input_tokens: Number of input tokens used to select a pricing tier.
service_tier: Provider service tier. Defaults to standard.
Returns:
Cost in USD.
"""
if (
model_name not in MODEL_COST_PER_1K_TOKENS_INPUT
and model_name not in MODEL_COST_TIERS_PER_1K_TOKENS
):
return 0.0
if input_tokens is None:
if is_completion and model_name in MODEL_COST_TIERS_PER_1K_TOKENS:
raise ValueError(
"input_tokens is required for completion costs with tiered pricing"
)
input_tokens = num_tokens
rate = get_model_cost_per_1k_tokens(
model_name,
input_tokens,
is_completion=is_completion,
service_tier=service_tier,
)
return rate * (num_tokens / 1000)
class CustomCallbackHandler(BaseCallbackHandler):
"""Callback Handler that tracks LLMs info."""
total_tokens: int = 0
prompt_tokens: int = 0
completion_tokens: int = 0View on GitHub (pinned to 532dfffbf6)
Solutions
- Pass input_tokens (the prompt token usage from response.llm_output) alongside num_tokens when computing completion cost
- Upgrade/patch the callback so on_llm_end extracts token_usage.prompt_tokens and forwards it
- If tiering is irrelevant, use a model without tiered pricing
Example fix
# before cost = get_token_cost_for_model(model, num_tokens=output_tokens, is_completion=True) # after cost = get_token_cost_for_model(model, num_tokens=output_tokens, input_tokens=prompt_tokens, is_completion=True)
Defensive patterns
Strategy: validation
Validate before calling
usage = response.llm_output.get("token_usage", {}) if response.llm_output else {}
input_tokens = usage.get("prompt_tokens")
if model in MODEL_COST_TIERS_PER_1K_TOKENS and is_completion and input_tokens is None:
input_tokens = usage.get("total_tokens", 0) Type guard
def has_input_tokens(response) -> bool:
llm_out = getattr(response, "llm_output", None) or {}
return bool((llm_out.get("token_usage") or {}).get("prompt_tokens") is not None) Try / catch
try:
cost = get_token_cost_for_model(model, n, input_tokens=inp, is_completion=True)
except ValueError:
cost = 0.0 # skip untracked completion Prevention
- Always extract prompt_tokens from llm_output in on_llm_end
- Default missing prompt token counts to 0, never omit
- Unit-test cost callbacks against tiered-pricing models
When it happens
Trigger: Calling on_llm_end / get_token_cost_for_model with is_completion=True for a model listed in MODEL_COST_TIERS_PER_1K_TOKENS (e.g. MiniMax M3) without passing input_tokens.
Common situations: Custom LangChain callbacks tracking costs for tiered-pricing models where only the response (num_tokens) is available from the LMLOutput.
Understand the failure class
Background: Missing required parameter errors: what 'X is required' and 'the required X param is missing' mean, and how to fix them — this error's family across 27 libraries.
Related errors
- input_tokens must not be negative
- Unsupported service tier {service_tier!r} for {model_name}
- No pricing tier matches {input_tokens} input tokens
AI-assisted analysis of ScrapeGraphAI/Scrapegraph-ai@532dfffbf6 (2026-08-28).
Data as JSON: /api/errors/697be0a8eae0e467.
Report an issue: GitHub.