---
name: apify-cost-tuning
description: 'Optimize Apify platform costs through memory tuning, compute unit management, and proxy budgeting. Use when analyzing Apify billing, reducing Actor run costs, or implementing usage monitoring and budget alerts. Trigger with "apify cost", "apify billing", "reduce apify costs", "apify pricing", "apify expensive", "apify budget", "compute units".'
allowed-tools: Read, Grep
version: 1.5.0
license: MIT
author: Jeremy Longshore <jeremy@intentsolutions.io>
tags:
  - saas
  - scraping
  - automation
  - apify
compatibility: Designed for Claude Code
title: apify-cost-tuning
canonical_url: https://skilld.dev/gh/jeremylongshore/tons-of-skills-marketplace/apify-cost-tuning
last_updated: 2026-10-07T06:06:28.000Z
---

> Index of every page on this site: [llms.txt](https://skilld.dev/llms.txt)

> **Skill from skilld.dev.** Follow the user's requested action.
> If the user asked to fork this Skill, follow the fork workflow below. Do not execute the Skill instructions while copying it.
> Otherwise, follow the instructions below for this session. You do not need to install anything.
>
> Supporting files, fetch one when the Skill refers to it: [references/examples.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/apify-cost-tuning/references/examples.md), [references/implementation.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/apify-cost-tuning/references/implementation.md), [references/pricing-model.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/apify-cost-tuning/references/pricing-model.md).
>
> If the user asked to install this Skill, run `npx skilld install jeremylongshore/tons-of-skills-marketplace/apify-cost-tuning`. Install writes the Skill files into the project, so every session loads them.
>
> ## Fork workflow
>
> A fork creates an editable local Skill with its original author and licence. The request authorizes copying and local installation.
> 1. Check `./skills/apify-cost-tuning`, the project lockfile, and selected Agent targets together. If the local directory or installed Skill exists, stop. Never overwrite an existing directory or Agent target.
> 2. Read [source metadata](https://skilld.dev/api/v1/skills/jeremylongshore/tons-of-skills-marketplace/apify-cost-tuning) once. Use sourceUrl, sourceCommit, skillPath, sourceGone, and license. If the source is gone or its path is missing, stop. If license is null, read licence files at the source commit.
> 3. Fetch only the source commit into a temporary Git repository. Do not clone full history. Derive repository_url from sourceUrl, including repository renames. If sourceCommit is absent, resolve the sourceUrl ref once. Set source_commit to that actual commit. Run these commands in one shell call:
>
> ```sh
> git init --quiet "$temporary_dir"
> git -C "$temporary_dir" fetch --quiet --depth=1 "$repository_url" "$source_commit"
> git -C "$temporary_dir" checkout --quiet --detach FETCH_HEAD
> ```
>
> Read applicable licence declarations and notices at that commit. If copying is not permitted, report the restriction and stop.
> 4. Inspect source entries together, then copy the directory containing skillPath into `./skills/apify-cost-tuning`. Use the user's path if selected. Keep the original SKILL.md, relative links, scripts, binary assets, and executable modes. Exclude .git metadata. Reject symlinks and paths outside the Skill directory. After checking entries, use cp -a where available. A regular source directory needs no custom copy script. Do not save this page wrapper as SKILL.md.
> Preserve author credit, notices, and applicable licence files from repository or parent directories. Add PROVENANCE.md with the Skill page, source URL, actual commit, original path, and licence. Retain any existing PROVENANCE.md and record new provenance separately. Batch source inspection, copying, and provenance work where practical.
> 5. In the project root, run `skilld install ./skills/apify-cost-tuning --mode copy --plain`. If skilld is unavailable, use `npx skilld install ./skills/apify-cost-tuning --mode copy --plain`. This known command needs no help lookup. Install does not support --json. Use detected Agent targets, or add --agent for the targets the user selected. Install the local path, never the upstream selector. If installation fails, preserve the local copy and report the exact failure.
> 6. Confirm the local lockfile source and installed Agent copies once. Report the local path, actual commit, and Agent targets. After edits, reinstall the same local path. Upstream updates must not replace it. Do not publish or push unless the user asks.

# Apify Cost Tuning

## Overview

Apify charges on three axes: compute units (CU), proxy traffic (GB), and storage.
One CU = 1 GB of memory running for 1 hour, so cost scales with both memory
allocation and run duration. This skill walks the investigate → tune → guard loop
that finds where spend is going, cuts it at the biggest lever (memory), and installs
guardrails so it stays down.

Full pricing tables (plan CU prices, proxy rates, storage rules) live in
[pricing-model.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/apify-cost-tuning/references/pricing-model.md).

## Prerequisites

- An Apify account with API access and `APIFY_TOKEN` set in the environment.
- The `apify-client` package installed (`npm install apify-client`).
- At least one Actor with run history to analyze.

## Instructions

The workflow is six steps. Each is summarized here with its core lever; the full,
runnable code for every step is in
[implementation.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/apify-cost-tuning/references/implementation.md).

1. **Analyze current costs** — roll up the last N days of runs into total CU, USD, and
   duration, and surface the single most expensive run:

   ```typescript
   import { ApifyClient } from 'apify-client';
   const client = new ApifyClient({ token: process.env.APIFY_TOKEN });

   const { items: runs } = await client.actor(actorId).runs().list({ limit: 1000, desc: true });
   const totalUsd = runs.reduce((s, r) => s + (r.usageTotalUsd ?? 0), 0);
   ```

2. **Reduce memory allocation** (biggest lever) — sweep memory from 4096 MB down to
   256 MB and stop at the first failure to find the sweet spot. Most CheerioCrawler
   Actors are over-provisioned. Sweet spots: simple Cheerio 256-512 MB, complex
   512-1024 MB, Playwright 2048-4096 MB.

3. **Optimize crawl duration** — higher `maxConcurrency`, tighter
   `requestHandlerTimeoutSecs`, a `maxRequestsPerCrawl` cap, fewer retries, and
   selective `enqueueLinks`. Faster crawls consume fewer CUs.

4. **Minimize proxy costs** — prefer datacenter (free with plan), only reach for
   residential when a site blocks it, block images/fonts/CSS to save residential GB,
   and reuse proxy sessions with `useSessionPool`.

5. **Cost guard for runaway Actors** — start the run, poll `usageTotalUsd` every 30s,
   and `.abort()` once spend crosses a hard cap.

6. **Monitor monthly usage** — iterate every Actor's runs since the 1st of the month
   and print a cost-descending report so the top spenders are obvious.

See [full walkthrough](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/apify-cost-tuning/references/implementation.md) for the complete code of each
step, including the memory sweep, proxy hooks, budget guard, and monthly report.

## Output

Running this skill produces:

- A **per-Actor cost analysis** (runs, total CU, total USD, avg CU/run, avg cost/run,
  most expensive run) for a chosen lookback window.
- A **memory profile** table mapping memory settings to status, duration, CU, and USD
  so you can pick the cheapest allocation that still succeeds.
- A **monthly cost report** ranking every Actor by spend, with a grand total.
- Tuned Actor configuration (reduced memory, capped crawls, proxy resource blocking)
  and an optional budget guard that aborts runs exceeding a USD ceiling.

## Cost Optimization Checklist

- [ ] Memory profiled (start low: 256-512MB for Cheerio)
- [ ] `maxRequestsPerCrawl` set to prevent runaway crawls
- [ ] Datacenter proxy used when possible (free with plan)
- [ ] Residential proxy: images/CSS/fonts blocked to save bandwidth
- [ ] `maxConcurrency` tuned (higher = faster = fewer CUs)
- [ ] Scheduled runs have appropriate frequency (don't over-scrape)
- [ ] Cost guard implemented for expensive runs
- [ ] Monthly usage reviewed

## Error Handling

| Issue | Cause | Solution |
|-------|-------|----------|
| Unexpected cost spike | No `maxRequestsPerCrawl` | Always set an upper bound |
| High residential proxy cost | Scraping images/fonts | Block non-essential resources |
| Over-provisioned memory | Default 1024MB | Profile and reduce to minimum |
| Too many scheduled runs | Aggressive cron | Reduce frequency if data freshness allows |

## Examples

Three worked scenarios chain the steps against concrete symptoms — a CheerioCrawler
bill that tripled, runaway residential-proxy GB, and guarding a brand-new Actor. Each
shows the full investigate → tune → verify loop. See
[examples.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/apify-cost-tuning/references/examples.md).

Quick guard example — abort any run that exceeds $0.50:

```typescript
// runWithBudget polls usageTotalUsd every 30s and aborts past the cap
const run = await runWithBudget('user/scraper', input, 0.50);
```

## Resources

- [Apify Pricing](https://apify.com/pricing)
- [Usage & Resources](https://docs.apify.com/platform/actors/running/usage-and-resources)
- [Compute Unit Calculator](https://help.apify.com/en/articles/3490384-what-is-a-compute-unit)
- [pricing-model.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/apify-cost-tuning/references/pricing-model.md) — full pricing tables (local)
- [implementation.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/apify-cost-tuning/references/implementation.md) — complete step-by-step code (local)
- [examples.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/apify-cost-tuning/references/examples.md) — worked cost-tuning scenarios (local)

## Next Steps

For architecture patterns, see `apify-reference-architecture`.
