Meshpocalypse Semantic
    Preparing search index...

    Function enforceTokenBudget

    • Enforces the applicable token budget for a given invocation context.

      Steps:

      1. Validate inputs (estimatedTokens, IRI safety).
      2. SPARQL SELECT to find the most-specific budget (scope hierarchy).
      3. Compute remaining = maxTokens - spentTokens (via SPARQL BIND).
      4. If remaining < estimatedTokens → throw TokenBudgetExhaustedError.
      5. Atomic SPARQL UPDATE (DELETE old spentTokens, INSERT new; matched by current value).
      6. Verify decrement landed; throw BudgetConcurrencyError if not.
      7. If newSpentTokens >= softLimitTokens → publish NATS alert.

      Parameters

      • context: BudgetContext

        Invocation context

      • config: AiGatewayConfig

        Gateway configuration

      Returns Promise<void>

      Remaining tokens < estimatedTokens

      No matching budget found

      Concurrent modification detected

      Invalid input (non-finite tokens, unsafe IRI)