---
name: shopify-graphql-cost-optimizer
description: 'Master Shopify''s calculated query cost system to avoid throttling.

  Use when hitting THROTTLED errors, optimizing GraphQL queries,

  or deciding when to use bulk operations instead.

  Trigger with phrases like "shopify query cost", "shopify graphql cost",

  "shopify rate limit graphql", "shopify throttled", "shopify bulk operations".'
allowed-tools: Read, Write, Edit, Bash(npm:*), Grep
version: 2.7.0
license: MIT
author: Jeremy Longshore <jeremy@intentsolutions.io>
tags:
  - saas
  - ecommerce
  - shopify
compatibility: Designed for Claude Code
title: shopify-graphql-cost-optimizer
canonical_url: https://skilld.dev/gh/jeremylongshore/tons-of-skills-marketplace/shopify-graphql-cost-optimizer
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/bulk-operations.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/shopify-graphql-cost-optimizer/references/bulk-operations.md), [references/cost-calculation-rules.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/shopify-graphql-cost-optimizer/references/cost-calculation-rules.md), [references/query-splitting.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/shopify-graphql-cost-optimizer/references/query-splitting.md).
>
> If the user asked to install this Skill, run `npx skilld install jeremylongshore/tons-of-skills-marketplace/shopify-graphql-cost-optimizer`. 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/shopify-graphql-cost-optimizer`, 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/shopify-graphql-cost-optimizer) 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/shopify-graphql-cost-optimizer`. 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/shopify-graphql-cost-optimizer --mode copy --plain`. If skilld is unavailable, use `npx skilld install ./skills/shopify-graphql-cost-optimizer --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.

# Shopify GraphQL Cost Optimizer

## Overview

Every Shopify GraphQL query has a calculated cost. The API uses a token bucket (1,000 points max, refills at 50/second for standard plans) and throttles once depleted. The key insight: `requestedQueryCost` is the worst-case estimate, while `actualQueryCost` is what you really paid. Understanding the gap between them is how you avoid throttling.

## Prerequisites

- Shopify app with GraphQL Admin API access
- `@shopify/shopify-api` package installed
- Understanding of GraphQL connections (edges/node pattern)

## Instructions

### Step 1: Read Cost Headers

Every GraphQL response includes cost data in `extensions.cost`:

```json
{
  "extensions": {
    "cost": {
      "requestedQueryCost": 252,
      "actualQueryCost": 12,
      "throttleStatus": {
        "maximumAvailable": 1000.0,
        "currentlyAvailable": 988.0,
        "restoreRate": 50.0
      }
    }
  }
}
```

Add the `X-GraphQL-Cost-Include-Fields: true` request header for a per-field cost breakdown.

### Step 2: Predict Query Cost

Cost rules for calculation:

- **Single object field**: 1 point (e.g., `shop { name }` = 1)
- **Connection**: `first` or `last` param multiplied by child cost, plus 2 for the connection itself
- **Nested connections**: costs multiply

```graphql
# Example: products(first: 10) { edges { node { title variants(first: 5) { edges { node { price } } } } } }
# Cost = 2 (products connection) + 10 * (1 (title) + 2 (variants connection) + 5 * 1 (price))
# = 2 + 10 * (1 + 2 + 5) = 2 + 80 = 82 requestedQueryCost
```

See [references/cost-calculation-rules.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/shopify-graphql-cost-optimizer/references/cost-calculation-rules.md) for the full calculation rules.

### Step 3: Cost Reduction Techniques

**Reduce `first` parameter** — the single biggest lever:

```graphql
# BAD: 250 * nested cost = massive
products(first: 250) { ... }

# GOOD: paginate with smaller pages
products(first: 25, after: $cursor) { ... }
```

**Select only needed fields** — every field costs 1 point per connection node:

```graphql
# BAD: 10 fields * 50 products = 500+ points
products(first: 50) { edges { node { id title description vendor tags status productType totalInventory createdAt updatedAt } } }

# GOOD: 3 fields * 50 products = ~152 points
products(first: 50) { edges { node { id title status } } }
```

**Avoid deep nesting** — flatten or split queries. See [references/query-splitting.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/shopify-graphql-cost-optimizer/references/query-splitting.md) for patterns.

### Step 4: Use Bulk Operations for Large Data Sets

When you need 250+ items, switch to `bulkOperationRunQuery`. It bypasses the cost system entirely — no `first`/`last` params, no cursors, returns all items as JSONL.

See [references/bulk-operations.md](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/shopify-graphql-cost-optimizer/references/bulk-operations.md) for the complete `bulkOperationRunQuery` mutation, polling, and JSONL download flow.

## Output

- Query cost visible in every response via `extensions.cost`
- Queries optimized below 200 points each
- Bulk operations configured for large data exports
- Per-field cost breakdown available for debugging

## Error Handling

| Error | Cause | Solution |
|-------|-------|----------|
| `THROTTLED` | Bucket depleted (0 points available) | Wait for `restoreRate` to refill, then retry |
| `MAX_COST_EXCEEDED` | Single query exceeds 1,000 points | Reduce `first` params or split into multiple queries |
| `QUERY_TOO_COMPLEX` | Too many nested connections (depth > 3) | Flatten query, fetch nested data separately |
| `BULK_OPERATION_FAILED` | Bulk query syntax error or timeout | Check `errorCode` on the bulk operation object |
| `BULK_OPERATION_ALREADY_RUNNING` | Only one bulk op per app per store | Poll current operation status before starting new one |

## Examples

### Calculating Cost for a Nested Product Query

Predict the cost of a query that fetches products with variants and metafields before running it, to avoid unexpected THROTTLED errors.

See [Cost Calculation Rules](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/shopify-graphql-cost-optimizer/references/cost-calculation-rules.md) for the full calculation formula and worked examples.

### Splitting an Expensive Query

A single query exceeds 1,000 points due to deep nesting. Break it into multiple cheaper queries that stay well under the limit.

See [Query Splitting](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/shopify-graphql-cost-optimizer/references/query-splitting.md) for patterns to flatten and separate expensive queries.

### Exporting a Full Product Catalog

You need all 10,000+ products with variants. Switch from paginated queries to a bulk operation that bypasses the cost system entirely.

See [Bulk Operations](https://skilld.dev/api/skills-raw/jeremylongshore/tons-of-skills-marketplace/shopify-graphql-cost-optimizer/references/bulk-operations.md) for the complete mutation, polling, and JSONL download flow.

## Resources

- [GraphQL Rate Limits](https://shopify.dev/docs/api/usage/rate-limits#graphql-admin-api-rate-limits)
- [Calculated Query Cost](https://shopify.dev/docs/api/usage/rate-limits#calculated-query-cost)
- [Bulk Operations](https://shopify.dev/docs/api/usage/bulk-operations/queries)
- [Query Cost Debugging](https://shopify.dev/docs/api/usage/rate-limits#debugging-query-cost)
