> ## Documentation Index
> Fetch the complete documentation index at: https://docs.stigg.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Charge credits for metered usage

This guide shows how to charge credits for usage of a metered feature, such as AI tokens, using the [check, act, track](/documentation/introduction/the-problem#common-patterns-for-solving-it-check--act--track) pattern. Your application reports usage of the feature, and Stigg converts it into credits using a [consumption mapping](/documentation/getting-started/core-concepts#consumption-mapping).

The examples use the Node.js SDK. The same operations are available in every [backend SDK](/documentation/getting-started/get-started-with-sdks) and in the [REST API](/api-and-sdks/api-reference/rest/introduction).

<Steps>
  <Step title="Estimate the credit cost">
    Ask Stigg how many credits the usage would consume, without reporting it. The response includes, per credit currency, the estimated cost, the current balance, the balance after the estimate, and whether the usage would overdraft the balance.

    ```javascript theme={null}
    const estimate = await stiggClient.estimateUsageCost({
      customerId: 'customer-demo-01',
      featureId: 'feature-ai-tokens',
      value: estimatedTokens,
    });

    const [credits] = estimate.estimates;
    if (!credits) {
      // The feature isn't credit-based: check estimate.warnings
    } else if (credits.wouldOverdraft) {
      // Block the operation or prompt for a top-up
    }
    ```

    <Warning>
      `estimateUsageCost` has a much lower rate limit (3,000 requests per minute) than `getCreditEntitlement`, which is served from the SDK's local cache. Take this into account in high-volume flows.
    </Warning>
  </Step>

  <Step title="Check the credit entitlement (optional)">
    The estimate already tells you whether the customer has enough credits, so this step isn't required. To check against the balance directly, pass the estimated cost as `requestedUsage`. `hasAccess` is `true` only when the remaining balance covers it.

    ```javascript theme={null}
    const entitlement = await stiggClient.getCreditEntitlement({
      customerId: 'customer-demo-01',
      currencyId: credits.currencyId,
      options: { requestedUsage: credits.estimatedCost },
    });

    if (!entitlement.hasAccess) {
      // Block the operation or prompt for a top-up
    }
    ```
  </Step>

  <Step title="Perform the operation">
    Run the operation in your application.
  </Step>

  <Step title="Report usage of the metered feature">
    Report usage against the feature, not the credit currency. Stigg applies the consumption mapping and deducts the matching amount of credits. For credit-backed features, the response includes a `credit` object with the credits this call deducted and the updated total consumption.

    ```javascript theme={null}
    const result = await stiggClient.reportUsage({
      customerId: 'customer-demo-01',
      featureId: 'feature-ai-tokens',
      value: tokensUsed,
      idempotencyKey: 'generation-41a7',
    });

    // result.credit.consumed: credits deducted by this call
    // result.credit.currentUsage: total credits consumed in the current period
    ```
  </Step>
</Steps>


## Related topics

- [Synchronous credit balance updates](/api-and-sdks/integration/backend/usage-and-billing.md#synchronous-credit-balance-updates)
- [Credits overview](/documentation/modeling-your-pricing-in-stigg/credits/overview.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.