ScrapeGraphAI/Scrapegraph-ai · error · ValueError

input_tokens must not be negative

Error message

input_tokens must not be negative

What it means

ValueError from get_model_cost_per_1k_tokens guarding against negative input_tokens. Tier selection compares input_tokens against tier bounds, so a negative count is invalid input.

Source

Thrown at scrapegraphai/utils/model_costs.py:161

                "input": 0.0009,
                "output": 0.0036,
                "cache_read": 0.00018,
                "cache_write": None,
            },
        ),
    }
}


def get_model_cost_per_1k_tokens(
    model_name: str,
    input_tokens: int,
    is_completion: bool = False,
    service_tier: str = "standard",
) -> float:
    """Return the applicable input or output rate for a model."""
    if input_tokens < 0:
        raise ValueError("input_tokens must not be negative")

    if model_name in MODEL_COST_TIERS_PER_1K_TOKENS:
        try:
            pricing_tiers = MODEL_COST_TIERS_PER_1K_TOKENS[model_name][service_tier]
        except KeyError as exc:
            raise ValueError(
                f"Unsupported service tier {service_tier!r} for {model_name}"
            ) from exc

        rate_key = "output" if is_completion else "input"
        for pricing in pricing_tiers:
            upper_bound = pricing.get("input_tokens_lte")
            lower_bound = pricing.get("input_tokens_gt")
            if upper_bound is not None and input_tokens <= upper_bound:
                return float(pricing[rate_key])
            if lower_bound is not None and input_tokens > lower_bound:
                return float(pricing[rate_key])
        raise ValueError(f"No pricing tier matches {input_tokens} input tokens")

View on GitHub (pinned to 532dfffbf6)

Solutions

  1. Check the token-usage extraction: never substitute -1 for missing prompt token counts
  2. Guard callers: max(0, input_tokens) before passing
  3. Update the provider integration to emit 0 when usage metadata is absent

Example fix

# before
rate = get_model_cost_per_1k_tokens(model, prompt_tokens, is_completion=True)
# after
rate = get_model_cost_per_1k_tokens(model, max(0, prompt_tokens), is_completion=True)
Defensive patterns

Strategy: validation

Validate before calling

input_tokens = max(0, int(input_tokens or 0))
rate = get_model_cost_per_1k_tokens(model, input_tokens, is_completion=True)

Type guard

def is_valid_token_count(n) -> bool:
    return isinstance(n, int) and not isinstance(n, bool) and n >= 0

Try / catch

try:
    rate = get_model_cost_per_1k_tokens(model, n)
except ValueError as e:
    logger.warning("bad token count %r: %s", n, e)
    rate = 0.0

Prevention

When it happens

Trigger: Calling get_model_cost_for_model or get_model_cost_per_1k_tokens with input_tokens < 0 (e.g. a computed prompt_tokens value that underflowed or was misparsed as negative).

Common situations: Callbacks parsing token usage from providers that omit prompt tokens, yielding -1 or None coerced to a negative number.

Related errors


AI-assisted analysis of ScrapeGraphAI/Scrapegraph-ai@532dfffbf6 (2026-08-28). Data as JSON: /api/errors/52bb400e532725aa. Report an issue: GitHub.