· 9 min read

How We Cut Our CI Bill

#tech

Slide: the whole pipeline ran on every commit. 75% of pull-request minutes went to commits that something newer replaced. The fix: draft runs nothing, ready for review runs only what the change can reach, and a ready-to-merge label runs everything once.

One month our GitHub Actions bill was about 1,300 minutes. The next month it was about 42,000.

We measured where the minutes went and fixed it in two different ways. The fix we spent the least effort on turned out to be worth about 4.5× more than the one we spent the most effort on.

A note on the numbers: to keep the client anonymous, absolute counts (minutes, pull requests, runs, files, lines) are scaled and months are left out. Percentages, ratios and timings are real.

The setup

  • A monorepo for a retail analytics product: a React front end, a Python API, an analytics worker, a chat agent service, a recommendation service with its card writer, and an ops/ folder of deployment code.
  • Seven deployable services and three kinds of environment: production, staging, and a throwaway preview environment per pull request.
  • GitHub Actions on standard hosted runners: 2 vCPU, 7 GB RAM.
  • A small team with no dedicated DevOps or platform engineer. CI work happens alongside feature work.

The bill

Minutes billed
Month 1 ~1,300
Month 2 ~42,000
Projected at the end-of-month run rate ~77,000/month

Bar chart of GitHub Actions minutes billed: about 1,300 in month 1, about 42,000 in month 2, and a modeled projection of about 77,000 per month.

To be fair to the team: pull requests went up about 6× from one month to the next (roughly 40 to 250), and CI runs went to about 1,350. A lot of that growth was real work. So the honest version isn’t “CI got worse”. It’s that CI cost scaled linearly with activity, and nobody had ever checked what a single run cost.

One detail that matters later: GitHub bills each job by the minute, rounded up.

Step 1: get job-level data

The billing page gives you a total and nothing else. With only a total, you can’t tell a volume problem from a slowness problem, and they have completely different fixes.

The useful data is per job. The runs API gives you the duration of every job in a run:

gh api repos/OWNER/REPO/actions/runs/RUN_ID/jobs --paginate \
  --jq '.jobs[] | [.name, .started_at, .completed_at, .conclusion] | @tsv'

We pulled every run from a four-day window (~300 runs) and analyzed those. We used the organization billing endpoint only to confirm that our total matched the bill.

Where the minutes went

1. Volume. About 250 PRs and 1,350 runs in a month.

2. Re-running the same branch. 75% of pull-request CI minutes went to commits that were not the last commit of their branch. One branch ran CI about 50 times. Three quarters of what we paid was checking code that was about to be replaced by the next push.

3. Cost per run. A full run billed 33–46 minutes:

  • test shards: 6–8 minutes each
  • typecheck: 4.5 minutes
  • slowest Python suite: 5–8 minutes
  • agent job: 4 minutes

4. Waste inside runs.

  • ~7,000 minutes (about a sixth of the bill) lost to per-job rounding. A 12-second job bills a full minute.
  • ~5,000 minutes on runs that were cancelled before they finished.
  • On more than 20 PRs, a change to one root-level file, like a Dockerfile or a config, started every gate in the repo.

Two levers

There are only two ways to cut a CI bill: make each run cheaper, or run it fewer times.

Engineers reach for the first one by instinct. It’s a technical problem with a technical answer. The second one means changing how people work. We did both, but we spent most of our effort on the first.

Lever 1: make each run cheaper

Start fewer gates. A guard job decides which gates run, based on what changed. The rule we settled on: route a file to the gate that reads it, not the gate that owns it. Our backend tests exercise the deploy scripts, so a change to a workflow file runs the backend gate. A change to the secrets scanner config runs no gate, because that scanner runs on every commit anyway. Draft pull requests run no gates at all.

Stop paying twice for setup. We had separate lint and typecheck jobs. On a two-core runner, standing up a second job (checkout, install, cache restore) cost more than running those checks next to the tests. We deleted those jobs and moved lint, types and the build onto test shard 1.

Use both cores. We sharded the test gates. The Python suites moved to pytest -n auto, and each parallel worker gets its own migrated database template, so workers share only the database server.

Upgrade TypeScript. The front-end typecheck went from 97.9 s to 8.0 s by moving from TypeScript 6 to TypeScript 7. Same program, same zero errors. Keep this number in mind; it comes back later.

Build fewer container images. We only build images whose files changed, use registry layer caching, and have each service compare against the commit it was last published at, instead of one shared base commit. We had a bug where the shared base tag missed every time, so a change that touched two services rebuilt six images.

Deleting a quarter of our tests

“We deleted tests to save money” sounds reckless. That’s not what happened. Nothing was deleted for being slow.

The method: nine review lanes read every test and kept a ledger with one mark per test: retain, repair, consolidate or delete, each with evidence. Every lane kept its suite green and ran its lint, format and type checks.

What came out, largest category first:

  1. Tests of dead code: features behind retired flags, data sets no longer read, tools the system never offers.
  2. Change detectors: greps of source text, workflows and prompts; pinned hashes; CSS class and DOM-shape assertions; file-size limits. These have to be edited on every refactor and never catch a regression.
  3. Tautologies: tests that assert what the code already says, such as copied constants, schema enums, or expected values computed by the code under test.
  4. Duplicates: the same contract asserted at several layers, or component tests repeating an end-to-end spec step for step.
  5. Tests of tooling, not the product: eval harnesses, load-test scripts, dev fakes.
  6. Replays of one past bug that a behavior test of the same owner already covered.

The result: about 12% fewer test files, 26% fewer lines of test code (roughly 240,000 down to 178,000), and about 29% fewer tests.

The surprise: deleting those tests let us delete about 1,200 lines of production code too: test-only exports, parameters that existed only for injection, and modules whose only callers were the deleted tests. Each lane searched the whole repo for callers before removing anything.

Measured suite times, on a local machine with 2 workers to match the runner:

Suite Before After
Front-end typecheck 97.9 s 8.0 s
Front-end unit tests 206 s 130 s
API tests 124 s 56 s
Recommendation service tests 221 s (serial) 63 s (2 workers)
Analytics worker tests 43 s 20 s
Chat agent tests 22 s 23 s
Ops tests 56 s 53 s

Lever 2: run the full suite less often

We split CI into two modes.

Quick checks run on every push to an open PR: lint, types, and only the tests that can reach what changed, with one runner per gate. They skip the expensive long poles: the two service builds, the accessibility and health audit, and the throwaway container images.

Full checks run once it matters:

  • when a ready-to-merge label is added
  • when a PR leaves draft
  • on a PR into the release branch
  • on every push to the integration and release branches

How “reaches what changed” works: for Python, a script builds the module import graph with the standard-library ast module and walks it backwards from the changed files. The front end has an equivalent selector. Both fall back to running everything when they can’t be certain: a config change, a lockfile change, or a deleted source file runs the whole suite.

Two small details made this work:

  • Full checks are requested with a label, not an approval. A label is visible, an AI agent can add one, and adding it fires an event that starts a run.
  • A push to the integration branch skips the gates entirely if a full run already passed on that exact tree.

Keeping it from growing back

  • A five-minute budget per gate on a 2-core runner. When a gate goes over, you shard it or cut it. The decision is forced instead of deferred.
  • Daily detectors over the workflow history: flaky jobs, a broken main branch, duration regressions, gates over budget. Findings go into one issue that’s kept up to date.
  • The rules are written down in the repo’s contributor and agent guide: what earns a test, and the habit of keeping a PR in draft while you push fixes. AI coding agents work on this repo as well as people, so a convention that only lives in someone’s head doesn’t survive.

Results

These numbers are modeled, not measured. We replayed the same ~300 runs through the new workflow, using measured runner ratios:

Lever Modeled saving
Lever 1: cheaper runs (everything above) −14%
Lever 2: full suite once per PR −65%

Lever 2 was worth about 4.5× lever 1, and we spent most of our effort on lever 1.

For the few jobs where we have runner measurements from both before and after:

Job Before (median) After
Typecheck 265–291 s 56 s
Test shards 398 s, 423 s 330 s, 363 s
Front-end jobs, billed 21 min (3 jobs) 16 min (2 jobs)

After the test cut, a full run billed about 38 minutes, down from 33–46. That’s a modest change, and before lever 2 we paid it again on every push. That’s why running it less often mattered so much more than making it faster.

Your laptop is lying to you

Measured on a developer laptop, the lever 1 changes promised −32%. On the actual runner they delivered about −14%.

The runner is slower, and it gains less from faster tooling. The 10× typecheck win was real on a laptop. On the runner, the typecheck job went from 265–291 s to 56 s: still a good win, but nowhere near 10×.

The general rule: a performance improvement measured somewhere other than where the cost is incurred is a guess.

Key takeaways

  • Get job-level data first. A billing total can’t tell you whether you have a volume problem or a speed problem.
  • Ask how many times CI runs before you ask how fast it runs. For us, three quarters of PR minutes checked commits that were replaced by the next push.
  • Measure on the runner you pay for, not on your laptop.
  • With per-minute rounding, prefer fewer, larger jobs. Many small jobs pay a hidden tax.
  • Delete tests with evidence, not a stopwatch. Dead code, change detectors, tautologies and duplicates cost time and catch nothing.
  • Write the conventions down where both people and AI agents will read them.