# ChargingManager<!-- -->

Provides fine-grained access to pay-per-event functionality.

The ChargingManager allows you to charge for specific events in your Actor when using the pay-per-event pricing model. This enables precise cost control and transparent billing for different operations within your Actor.

### References

* Apify platform documentation: <https://docs.apify.com/platform/actors/publishing/monetize>

### Hierarchy

* *ChargingManager*
  * [ChargingManagerImplementation](https://docs.apify.com/sdk/python/reference/class/ChargingManagerImplementation.md)

## Index[](#Index)

### Methods

* [calculate_max_event_charge_count_within_limit](https://docs.apify.com/sdk/python/reference/class/ChargingManager.md#calculate_max_event_charge_count_within_limit)
* [calculate_total_charged_amount](https://docs.apify.com/sdk/python/reference/class/ChargingManager.md#calculate_total_charged_amount)
* [charge](https://docs.apify.com/sdk/python/reference/class/ChargingManager.md#charge)
* [compute_chargeable](https://docs.apify.com/sdk/python/reference/class/ChargingManager.md#compute_chargeable)
* [compute_push_data_limit](https://docs.apify.com/sdk/python/reference/class/ChargingManager.md#compute_push_data_limit)
* [get_charged_event_count](https://docs.apify.com/sdk/python/reference/class/ChargingManager.md#get_charged_event_count)
* [get_max_total_charge_usd](https://docs.apify.com/sdk/python/reference/class/ChargingManager.md#get_max_total_charge_usd)
* [get_pricing_info](https://docs.apify.com/sdk/python/reference/class/ChargingManager.md#get_pricing_info)
* [is_event_charge_limit_reached](https://docs.apify.com/sdk/python/reference/class/ChargingManager.md#is_event_charge_limit_reached)

### Properties

* [charge_lock](https://docs.apify.com/sdk/python/reference/class/ChargingManager.md#charge_lock)

## Methods<!-- -->[](#Methods)

### [](#calculate_max_event_charge_count_within_limit)[](https://github.com/apify/apify-sdk-python/blob/c674a4d42ade9c1a07f33062b6702497cd34b19d//src/apify/_charging.py#L136)calculate_max_event_charge_count_within_limit

* **calculate_max_event_charge_count_within_limit**(event_name): int | None

- Overrides [ChargingManager.calculate_max_event_charge_count_within_limit](https://docs.apify.com/sdk/python/reference/class/ChargingManager.md#calculate_max_event_charge_count_within_limit)

  Calculate how many instances of an event can be charged before we reach the configured limit.

  ***

  #### Parameters

  * ##### event_name: str

    Name of the inspected event.

  #### Returns int | None

### [](#calculate_total_charged_amount)[](https://github.com/apify/apify-sdk-python/blob/c674a4d42ade9c1a07f33062b6702497cd34b19d//src/apify/_charging.py#L133)calculate_total_charged_amount

* **calculate_total_charged_amount**(): Decimal

- Overrides [ChargingManager.calculate_total_charged_amount](https://docs.apify.com/sdk/python/reference/class/ChargingManager.md#calculate_total_charged_amount)

  Calculate the total amount of money charged for pay-per-event events so far.

  ***

  #### Returns Decimal

### [](#charge)[](https://github.com/apify/apify-sdk-python/blob/c674a4d42ade9c1a07f33062b6702497cd34b19d//src/apify/_charging.py#L104)charge

* async **charge**(event_name: str, \*: , count?
  <!-- -->
  : int, idempotency_key?
  <!-- -->
  : str | None, request_id?
  <!-- -->
  : str | None): [ChargeResult](https://docs.apify.com/sdk/python/reference/class/ChargeResult.md)
* async **charge**(event_name: str, \*: , count?
  <!-- -->
  : int, idempotency_key?
  <!-- -->
  : str | None): [ChargeResult](https://docs.apify.com/sdk/python/reference/class/ChargeResult.md)
* async **charge**(event_name: str, \*: , count?
  <!-- -->
  : int, idempotency_key?
  <!-- -->
  : str | None, request_id: str): [ChargeResult](https://docs.apify.com/sdk/python/reference/class/ChargeResult.md)

- Overrides [ChargingManager.charge](https://docs.apify.com/sdk/python/reference/class/ChargingManager.md#charge)

  Charge for a specified number of events - sub-operations of the Actor.

  This is relevant only for the pay-per-event pricing model.

  When `count` exceeds the remaining budget, the events that fit are charged plus one more, so that the platform detects the exhausted budget and terminates the run. A run already strictly over budget charges nothing.

  ***

  #### Parameters

  * ##### event_name: str

    Name of the event to be charged for.

  * ##### optionalkeyword-onlycount: int = <!-- -->1

    Number of events to charge for.

  * ##### optionalkeyword-onlyidempotency_key: str | None = <!-- -->None

    A unique key preventing a retried operation from being charged for twice. A repeat under the same key within three minutes is not sent to the API and reports the `charged_count` of the original call, or zero if it names a different event. Like the platform, the key is forgotten afterwards. In a multi-tenant Server Actor run, every call is sent and the platform deduplicates it. The key is scoped to the whole run there, so a key reused by another caller charges only the first one - derive it from `request_id`.

  * ##### optionalkeyword-onlyrequest_id: str | None = <!-- -->None

    ID of the Server Actor request being charged, taken from the `X-Actor-Request-ID` header of the incoming request. Required in multi-tenant Server Actor runs, where it maps the charge to the caller's account instead of the account that started the run. Ignored elsewhere.

  #### Returns [ChargeResult](https://docs.apify.com/sdk/python/reference/class/ChargeResult.md)

### [](#compute_chargeable)[](https://github.com/apify/apify-sdk-python/blob/c674a4d42ade9c1a07f33062b6702497cd34b19d//src/apify/_charging.py#L187)compute_chargeable

* **compute_chargeable**(): dict\[str, int | None]

- Overrides [ChargingManager.compute_chargeable](https://docs.apify.com/sdk/python/reference/class/ChargingManager.md#compute_chargeable)

  Compute the maximum number of events of each type that can be charged within the current budget.

  ***

  #### Returns dict\[str, int | None]

### [](#compute_push_data_limit)[](https://github.com/apify/apify-sdk-python/blob/c674a4d42ade9c1a07f33062b6702497cd34b19d//src/apify/_charging.py#L159)compute_push_data_limit

* **compute_push_data_limit**(items_count, event_name, \*, is_default_dataset): int

- Overrides [ChargingManager.compute_push_data_limit](https://docs.apify.com/sdk/python/reference/class/ChargingManager.md#compute_push_data_limit)

  Compute how many items can be pushed and charged within the current budget.

  Accounts for both the explicit event and the synthetic `DEFAULT_DATASET_ITEM_EVENT` event, so that the combined cost per item does not exceed the remaining budget.

  When the budget cannot cover a single item, one item is still allowed through, so that the resulting overcharge makes the platform terminate the run. A run that is already strictly over budget gets no items through.

  ***

  #### Parameters

  * ##### items_count: int

    The number of items to be pushed.

  * ##### event_name: str

    The explicit event name to charge for each item.

  * ##### keyword-onlyis_default_dataset: bool

    Whether the data is pushed to the default dataset. If True, the synthetic event cost is included in the combined price.

  #### Returns int

  Max number of items to push.

### [](#get_charged_event_count)[](https://github.com/apify/apify-sdk-python/blob/c674a4d42ade9c1a07f33062b6702497cd34b19d//src/apify/_charging.py#L149)get_charged_event_count

* **get_charged_event_count**(event_name): int

- Overrides [ChargingManager.get_charged_event_count](https://docs.apify.com/sdk/python/reference/class/ChargingManager.md#get_charged_event_count)

  Get the number of events with the given name that were charged so far.

  ***

  #### Parameters

  * ##### event_name: str

    Name of the inspected event.

  #### Returns int

### [](#get_max_total_charge_usd)[](https://github.com/apify/apify-sdk-python/blob/c674a4d42ade9c1a07f33062b6702497cd34b19d//src/apify/_charging.py#L156)get_max_total_charge_usd

* **get_max_total_charge_usd**(): Decimal

- Overrides [ChargingManager.get_max_total_charge_usd](https://docs.apify.com/sdk/python/reference/class/ChargingManager.md#get_max_total_charge_usd)

  Get the configured maximum total charge for this Actor run.

  ***

  #### Returns Decimal

### [](#get_pricing_info)[](https://github.com/apify/apify-sdk-python/blob/c674a4d42ade9c1a07f33062b6702497cd34b19d//src/apify/_charging.py#L143)get_pricing_info

* **get_pricing_info**(): [ActorPricingInfo](https://docs.apify.com/sdk/python/reference/class/ActorPricingInfo.md)

- Overrides [ChargingManager.get_pricing_info](https://docs.apify.com/sdk/python/reference/class/ChargingManager.md#get_pricing_info)

  Retrieve detailed information about the effective pricing of the current Actor run.

  This can be used for instance when your code needs to support multiple pricing models in transition periods.

  ***

  #### Returns [ActorPricingInfo](https://docs.apify.com/sdk/python/reference/class/ActorPricingInfo.md)

### [](#is_event_charge_limit_reached)[](https://github.com/apify/apify-sdk-python/blob/c674a4d42ade9c1a07f33062b6702497cd34b19d//src/apify/_charging.py#L184)is_event_charge_limit_reached

* **is_event_charge_limit_reached**(event_name): bool

- Overrides [ChargingManager.is_event_charge_limit_reached](https://docs.apify.com/sdk/python/reference/class/ChargingManager.md#is_event_charge_limit_reached)

  Return True if the remaining budget is insufficient to charge even a single event of the given type.

  ***

  #### Parameters

  * ##### event_name: str

  #### Returns bool

## Properties<!-- -->[](#Properties)

### [](#charge_lock)[](https://github.com/apify/apify-sdk-python/blob/c674a4d42ade9c1a07f33062b6702497cd34b19d//src/apify/_charging.py#L94)charge_lock

charge_lock: [ReentrantLock](https://docs.apify.com/sdk/python/reference/class/ReentrantLock.md)

Lock to synchronize charge operations. Prevents race conditions between `charge` and `push_data` calls.
