> ## Documentation Index
> Fetch the complete documentation index at: https://docs.costgraph.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# AutoPilot for CI

> Run each GitHub Actions job on the runner that fits its policy, and fall back to its default runner whenever AutoPilot cannot answer.

AutoPilot for CI chooses the runner each GitHub Actions job runs on. You add a
routing job to a workflow, and it asks CostGraph which runner fits each job
best. CostGraph records each choice and its reason, and measures the job's cost
and time after it finishes.

> [!NOTE]
>
> AutoPilot for CI is free during preview. It routes between GitHub-hosted
> runners and runner providers such as Blacksmith, Depot, Ubicloud,
> Namespace, WarpBuild, RunsOn, Jog, and Avrea.

## Requirements

* Your GitHub organization is [connected with the CostGraph GitHub App](https://docs.costgraph.ai/costgraph/integrations/github.md#connect-with-the-github-app),
  with the repository **Actions** permission set to **Read and write**.
  AutoPilot uses it to cancel and re-run a run whose routed job never gets a
  runner. Until an organization owner approves it, every job keeps its default
  runner.
* The job has run successfully at least once in the last 30 days. A job with no
  runs yet can still be [tried](#try-new-runners) on a cheaper runner.
* The routing job can sign in with [GitHub OIDC](#sign-in-with-github-oidc) or a
  CostGraph API key with the `ci:route` scope.

## Add the routing job

The following workflow routes `build` and keeps `lint` on a fixed runner.

```yaml .github/workflows/ci.yml theme={null}
jobs:
  route:
    runs-on: ubuntu-slim
    timeout-minutes: 2
    permissions:
      contents: read
      id-token: write
    outputs:
      runners: ${{ steps.route.outputs.runners }}
    steps:
      - id: route
        uses: baselinehq/costgraph-action/route@v0
        with:
          default: blacksmith-4vcpu-ubuntu-2404

  build:
    needs: [route]
    if: ${{ !cancelled() }}
    runs-on: ${{ fromJSON(needs.route.outputs.runners || '{}').build || 'blacksmith-4vcpu-ubuntu-2404' }}
    steps:
      - uses: actions/checkout@v5
      - run: make build

  lint:
    runs-on: ubuntu-slim
    steps:
      - uses: actions/checkout@v5
      - run: make lint
```

Every job whose `runs-on` reads the `runners` output is routed. The label after
`||` is that job's default runner. For a single job, read the `runner` output
instead. The [action's README](https://github.com/baselinehq/costgraph-action#autopilot-for-ci)
lists every input and output.

Give the routing job the smallest runner you have, because it runs on every
workflow run. Route jobs that run for several minutes on a costly runner, such
as builds, test suites, and image publishing. A job that finishes in about a
minute rarely saves more than the routing job costs, so keep it on a fixed
runner.

## Verify a routed job

Run the workflow and open the routing job's log in GitHub. It lists the runner
selected for each job and the reason for that choice.

In CostGraph, open **AutoPilot > CI > History**. Choose a period that includes the
run, then find its repository and workflow. Inspect the runner, cost, duration,
and routing outcome. The **Savings** tab compares completed jobs with their
usual runners and includes the routing job's cost in **Net saved**.

Keeping the default runner is a valid result. Check the routing reason before
changing the policy. No better candidate, an unconnected provider, and a fallback
are different outcomes, even when all three use the same runner.

## Fallback

The routing job does not block a workflow. Every job gets its default runner
when CostGraph does not answer within 3 seconds, returns an error, or rate
limits the request. The same applies when AutoPilot is off or paused, and the
routing job stays green. The
`timeout-minutes`, `if: ${{ !cancelled() }}`, and `||` default in the example
keep the jobs running even when the routing job cannot start.

Set `fail-on-error: true` on the action to fail the routing job instead. A
missing [subscription](https://docs.costgraph.ai/costgraph/billing.md#features-that-need-a-subscription)
always fails the routing job, with a link to set up billing.

The routing job's log lists why each job got its runner.

| Reason | Meaning |
| - | - |
| `trial` | The job runs on a runner it has not been measured on yet. |
| `trial_failed` | AutoPilot skipped a runner because it failed for this job. |
| `no_better_candidate` | No runner fits the policy better than the default. |
| `no_history` | The job has no successful run in the last 30 days. |
| `disabled`, `paused` | AutoPilot is off or paused for this workflow. |
| `fork_event` | The run is a pull request from a fork or a `pull_request_target` run. |
| `cancelled_rerun` | The run is a re-run of a run AutoPilot cancelled. |
| `github_app_reapproval` | An organization owner must approve the updated GitHub App permissions. |
| `github_not_connected` | The GitHub organization is not connected to CostGraph. |
| `billing_required` | The organization has no active subscription. |
| `rate_limited`, `error` | CostGraph could not answer this time. |

## Runners AutoPilot can choose

AutoPilot can choose GitHub-hosted runner labels and every runner of a provider
connected to CostGraph. It can also choose runners you turn on in
[settings](https://docs.costgraph.ai/costgraph/autopilot/ci-settings.md#runners), and runners on
[your own Kubernetes cluster](https://docs.costgraph.ai/costgraph/autopilot/ci-cluster.md). It never
chooses a label blocked in settings. The runner must match the job's operating
system and architecture, with at least as many vCPUs and enough memory.

## Policies

Each workflow follows one policy. The default is `balanced`.

| Policy | Picks | Only among runners that |
| - | - | - |
| `cheapest` | The lowest cost per job | Take at most twice the default runner's time |
| `fastest` | The shortest time per job | Cost no more than the default runner |
| `balanced` | The best combined saving in cost and time | Cost no more than the default runner and are at most max(5 s, 10%) slower |

Time is the 75th percentile of queue plus run time over the last 30 days. Cost
comes from each provider's published price, after any included minutes you
have left. For a runner the job has not run on, CostGraph estimates the time
from public benchmarks.

### Try new runners

AutoPilot tries a runner once for a job before it trusts it. One trial runs at
a time for each job and runner. After a trial passes, your policy picks between
that runner and the others.

A failure affects only the job that failed. AutoPilot skips a runner for a job
for one hour after a failed trial. It does the same after 5 failed runs of that
job within an hour. A run that cost more than the default runner, or was more than
max(5 s, 10%) slower, counts as failed. Other jobs keep using the runner. A
price drop ends the skip early.

AutoPilot skips a provider for every job only when it is offline. That means its
latest runner never started, for example because it could not be created. The
provider is used again after one of its runners starts a job. When a provider
has a temporary fault, CostGraph retries it after 30 seconds and again after 2
minutes, then uses the default runner.

The History tab shows a chip on a run that left its runner. Hover it for the
provider's own error.

| Chip | Meaning | What to do |
| - | - | - |
| Retrying | The provider could not start the runner, so CostGraph retries. | Nothing. |
| Needs reconnect | The provider refused CostGraph's key or needs a setting. | Reconnect the provider on [Integrations](https://docs.costgraph.ai/costgraph/integrations/overview.md). |
| Not picked up | No runner picked the job up, so it moved to the default runner. | Nothing. |
| Failed here | The job failed on this runner but passes on its default runner. | Nothing. |
| Fails on default too | The job also fails on its default runner. | Fix the job. |
| Too slow, Too costly | The run was slower or costlier than the default runner. | Nothing. |

### Re-run failed jobs

When you choose **Re-run failed jobs** in GitHub, AutoPilot chooses the runner
again for jobs on runners CostGraph starts, such as a
[sandbox](https://docs.costgraph.ai/costgraph/autopilot/ci-sandboxes.md). The job moves to the next best
runner when its runner broke or when the job failed there but passes on its
default runner. A job whose own steps failed stays on the same runner.

## Pin a workflow to a provider

Set `pin` when a workflow must run on a given provider, for data residency, a
contract, or credits. A pin is also how you send a workflow to a hosted sandbox
such as Beam, Fly, or Microsandbox.

```yaml theme={null}
- id: route
  uses: baselinehq/costgraph-action/route@v0
  with:
    default: blacksmith-4vcpu-ubuntu-2404
    pin: ubicloud, beam:sandbox-8c-32g
```

Each `pin` entry is a provider such as `ubicloud`, a runner label such as
`ubicloud-standard-8`, or a size written `<provider>:<size>`. AutoPilot then
considers only the pinned runners with at least the default runner's vCPUs and
memory, and the [policy](#policies) picks among them. A pinned runner does not
have to beat the default runner, so a pinned run can add cost in
[Savings](#savings).

The default runner stays the fallback when no pinned runner fits or picks the
job up. You can also set a pin for the organization, a repository, or a
workflow in [AutoPilot for CI settings](https://docs.costgraph.ai/costgraph/autopilot/ci-settings.md#how-the-levels-combine).
The `pin` input in the workflow file takes precedence.

## Measure job usage

AutoPilot shows the CPU, memory, disk, and network each job uses. Runners that
AutoPilot launches measure each job without setup. For other runners, add the
usage step as the first step of the job and give the job
`permissions: id-token: write`.

```yaml theme={null}
steps:
  - uses: baselinehq/costgraph-action/usage@v0
  - uses: actions/checkout@v5
```

A `permissions:` block turns off every permission it does not list, so keep the
ones the job already uses. The step never fails the job. It measures nothing on
macOS or Windows, in pull requests from forks, or in Alpine-based container
images.

## Stuck jobs

AutoPilot cancels a run when a routed job is still waiting for a runner after
its deadline. It re-runs the run with every job on its default runner. The
deadline is the provider's 95th percentile queue time plus one minute, and at
least 90 seconds. AutoPilot leaves the run alone when any of its jobs has
started work. It never cancels release and deploy jobs.

## Secrets and forks

Runner providers receive jobs with their secrets by default. To keep jobs that
use secrets off a provider, set
[`allow_secrets: false`](https://docs.costgraph.ai/costgraph/autopilot/ci-settings#provider-limits) for
it. Pull requests from forks and `pull_request_target` runs always use their
default runners.

## Sign in with GitHub OIDC

The routing job can sign in with the workflow's own GitHub identity, so you
store no CostGraph key in the repository. Give the job
`permissions: id-token: write` and leave `api-key` empty. CostGraph returns a
token that lasts at most an hour and covers only that repository. Use an API
key with the `ci:route` scope on self-hosted runners that cannot request OIDC
tokens.

## Self-hosted installations

On a [self-hosted installation](https://docs.costgraph.ai/costgraph/self-hosted/overview.md), set `api-url`
to your dashboard address on the routing job and on the usage step. GitHub OIDC
sign-in then requests that address as its audience.

```yaml theme={null}
- id: route
  uses: baselinehq/costgraph-action/route@v0
  with:
    default: blacksmith-4vcpu-ubuntu-2404
    api-url: https://costgraph.internal.example.com
```

AutoPilot for CI needs a GitHub App that belongs to your installation.
[Connect GitHub for AutoPilot for CI](https://docs.costgraph.ai/costgraph/self-hosted/install.md#connect-github-for-autopilot-for-ci)
describes how to create it.

## Savings

CostGraph compares every routed job with the same job on its default runner.
The baseline is the 75th percentile cost and time over the 30 days before
AutoPilot first routed the job. Savings count what each routed job actually
cost and how long it took. A job that cost more than its baseline counts as
added cost, and the routing job's own cost is subtracted from the total.

## Related

* [AutoPilot for CI settings](https://docs.costgraph.ai/costgraph/autopilot/ci-settings.md)
* [Run CI jobs on your own cluster](https://docs.costgraph.ai/costgraph/autopilot/ci-cluster.md)
* [Run CI jobs in sandboxes](https://docs.costgraph.ai/costgraph/autopilot/ci-sandboxes.md)
* [GitHub integration](https://docs.costgraph.ai/costgraph/integrations/github.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.