# ChargingManagerImplementation<!-- -->

Implementation of the `ChargingManager` Protocol - this is only meant to be instantiated internally.

### Hierarchy

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

## Index[](#Index)

### Methods

* [\_\_aenter\_\_](https://docs.apify.com/sdk/python/reference/class/ChargingManagerImplementation.md#__aenter__)
* [\_\_aexit\_\_](https://docs.apify.com/sdk/python/reference/class/ChargingManagerImplementation.md#__aexit__)
* [\_\_init\_\_](https://docs.apify.com/sdk/python/reference/class/ChargingManagerImplementation.md#__init__)
* [calculate_max_event_charge_count_within_limit](https://docs.apify.com/sdk/python/reference/class/ChargingManagerImplementation.md#calculate_max_event_charge_count_within_limit)
* [calculate_total_charged_amount](https://docs.apify.com/sdk/python/reference/class/ChargingManagerImplementation.md#calculate_total_charged_amount)
* [charge](https://docs.apify.com/sdk/python/reference/class/ChargingManagerImplementation.md#charge)
* [compute_chargeable](https://docs.apify.com/sdk/python/reference/class/ChargingManagerImplementation.md#compute_chargeable)
* [compute_push_data_limit](https://docs.apify.com/sdk/python/reference/class/ChargingManagerImplementation.md#compute_push_data_limit)
* [get_charged_event_count](https://docs.apify.com/sdk/python/reference/class/ChargingManagerImplementation.md#get_charged_event_count)
* [get_max_total_charge_usd](https://docs.apify.com/sdk/python/reference/class/ChargingManagerImplementation.md#get_max_total_charge_usd)
* [get_pricing_info](https://docs.apify.com/sdk/python/reference/class/ChargingManagerImplementation.md#get_pricing_info)
* [is_event_charge_limit_reached](https://docs.apify.com/sdk/python/reference/class/ChargingManagerImplementation.md#is_event_charge_limit_reached)

### Properties

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

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

### [](#__aenter__)[](https://github.com/apify/apify-sdk-python/blob/c674a4d42ade9c1a07f33062b6702497cd34b19d//src/apify/_charging.py#L258)\_\_aenter\_\_

* async **\_\_aenter\_\_**(): None

- Initialize the charging manager - this is called by the `Actor` class and shouldn't be invoked manually.

  ***

  #### Returns None

### [](#__aexit__)[](https://github.com/apify/apify-sdk-python/blob/c674a4d42ade9c1a07f33062b6702497cd34b19d//src/apify/_charging.py#L335)\_\_aexit\_\_

* async **\_\_aexit\_\_**(exc_type, exc_value, exc_traceback): None

- #### Parameters

  * ##### exc_type: type\[BaseException] | None
  * ##### exc_value: BaseException | None
  * ##### exc_traceback: TracebackType | None

  #### Returns None

### [](#__init__)[](https://github.com/apify/apify-sdk-python/blob/c674a4d42ade9c1a07f33062b6702497cd34b19d//src/apify/_charging.py#L229)\_\_init\_\_

* **\_\_init\_\_**(configuration, client): None

- #### Parameters

  * ##### configuration: [Configuration](https://docs.apify.com/sdk/python/reference/class/Configuration.md)
  * ##### client: ApifyClientAsync

  #### Returns None

### [](#calculate_max_event_charge_count_within_limit)[](https://github.com/apify/apify-sdk-python/blob/c674a4d42ade9c1a07f33062b6702497cd34b19d//src/apify/_charging.py#L505)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#L498)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#L352)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#L570)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#L536)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#L528)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#L532)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#L515)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#L565)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)

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

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

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

LOCAL_CHARGING_LOG_DATASET_NAME: Undefined
