tinyhumansai/openhuman · error

Token usage cost must be a finite, non-negative value

Error message

Token usage cost must be a finite, non-negative value

What it means

The same finite/non-negative guard as the estimate variant, applied to the computed cost of a `TokenUsage` event in `record_usage_unconditional`: the cost derived from the usage record is NaN, infinite, or negative. This fires when token counts or pricing rates feeding the multiplication are themselves non-finite or the provider supplied malformed numbers — persisting such a value would poison the spend ledger and budget gates.

Source

Thrown at src/openhuman/platform/cost/tracker.rs:159

    ///
    /// Honours `cost.enabled` — when budget enforcement is disabled the call
    /// is a no-op. Use [`Self::record_usage_unconditional`] for telemetry
    /// paths (dashboard, observability) that must capture data even when
    /// the budget enforcement path is off, so the user can flip enforcement
    /// on later without losing history.
    pub fn record_usage(&self, usage: TokenUsage) -> Result<()> {
        if !self.config.enabled {
            return Ok(());
        }
        self.record_usage_unconditional(usage)
    }

    /// Persist a usage event ignoring `cost.enabled`. Used by the dashboard
    /// telemetry hook so cost history is recorded regardless of whether the
    /// budget-enforcement gate is on.
    pub fn record_usage_unconditional(&self, usage: TokenUsage) -> Result<()> {
        if !usage.cost_usd.is_finite() || usage.cost_usd < 0.0 {
            return Err(anyhow!(
                "Token usage cost must be a finite, non-negative value"
            ));
        }

        let record = CostRecord::new(&self.session_id, usage);

        // Persist first for durability guarantees.
        {
            let mut storage = self.lock_storage();
            storage.add_record(record.clone())?;
        }

        // Then update in-memory session snapshot.
        let mut session_costs = self.lock_session_costs();
        session_costs.push(record);

        Ok(())
    }

View on GitHub (pinned to 7491200858)

Solutions

  1. Inspect the offending `TokenUsage` record — zero/negative token counts or non-finite values from the provider are typical
  2. Validate and sanitize provider usage payloads at deserialization time
  3. Keep per-model pricing tables well-formed; a missing rate defaulting to NaN will surface here
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at src/openhuman/platform/cost/tracker.rs:159 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of tinyhumansai/openhuman@7491200858 (2026-08-17). Data as JSON: /api/errors/f714c276b417b029. Report an issue: GitHub.