diff --git a/docs/02_concepts/11_pay_per_event.mdx b/docs/02_concepts/11_pay_per_event.mdx index e51938b3..0a422332 100644 --- a/docs/02_concepts/11_pay_per_event.mdx +++ b/docs/02_concepts/11_pay_per_event.mdx @@ -6,6 +6,7 @@ description: Monetize your Actors using the pay-per-event pricing model import ActorChargeSource from '!!raw-loader!roa-loader!./code/11_actor_charge.py'; import ConditionalActorChargeSource from '!!raw-loader!roa-loader!./code/11_conditional_actor_charge.py'; +import ChargeLimitCheckSource from '!!raw-loader!roa-loader!./code/11_charge_limit_check.py'; import ApiLink from '@site/src/components/ApiLink'; import RunnableCodeBlock from '@site/src/components/RunnableCodeBlock'; @@ -31,6 +32,22 @@ Then you just push your code to Apify and that's it! The SDK will even keep trac If you need finer control over charging, you can access call `Actor.get_charging_manager()` to access the `ChargingManager`, which can provide more detailed information - for example how many events of each type can be charged before reaching the configured limit. +### Handling the charge limit + +While the SDK automatically prevents overcharging by limiting how many events are charged and how many items are pushed, **it does not stop your Actor from running**. When the charge limit is reached, `Actor.charge` and `Actor.push_data` will silently stop charging and pushing data, but your Actor will keep running — potentially doing expensive work (scraping pages, calling APIs) for no purpose. This means your Actor may never terminate on its own if you don't check the charge limit yourself. + +To avoid this, you should check the `event_charge_limit_reached` field in the result returned by `Actor.charge` or `Actor.push_data` and stop your Actor when the limit is reached. You can also use the `chargeable_within_limit` field from the result to plan ahead — it tells you how many events of each type can still be charged within the remaining budget. + + + {ChargeLimitCheckSource} + + +Alternatively, you can periodically check the remaining budget via `Actor.get_charging_manager()` instead of inspecting every `ChargeResult`. This can be useful when charging happens in multiple places across your code, or when using a crawler where you don't directly control the main loop. + +:::caution +Always check the charge limit in your Actor, whether through `ChargeResult` return values or the `ChargingManager`. Without this check, your Actor will continue running and consuming platform resources after the budget is exhausted, producing no output. +::: + ## Transitioning from a different pricing model When you plan to start using the pay-per-event pricing model for an Actor that is already monetized with a different pricing model, your source code will need support both pricing models during the transition period enforced by the Apify platform. Arguably the most frequent case is the transition from the pay-per-result model which utilizes the `ACTOR_MAX_PAID_DATASET_ITEMS` environment variable to prevent returning unpaid dataset items. The following is an example how to handle such scenarios. The key part is the `ChargingManager.get_pricing_info()` method which returns information about the current pricing model. diff --git a/docs/02_concepts/code/11_charge_limit_check.py b/docs/02_concepts/code/11_charge_limit_check.py new file mode 100644 index 00000000..7f946a23 --- /dev/null +++ b/docs/02_concepts/code/11_charge_limit_check.py @@ -0,0 +1,29 @@ +import asyncio + +from apify import Actor + + +async def main() -> None: + async with Actor: + urls = [ + 'https://example.com/1', + 'https://example.com/2', + 'https://example.com/3', + ] + + for url in urls: + # Do some expensive work (e.g. scraping, API calls) + result = {'url': url, 'data': f'Scraped data from {url}'} + + # highlight-start + # push_data returns a ChargeResult - check it to know if the budget ran out + charge_result = await Actor.push_data(result, 'result-item') + + if charge_result.event_charge_limit_reached: + Actor.log.info('Charge limit reached, stopping the Actor') + break + # highlight-end + + +if __name__ == '__main__': + asyncio.run(main())