# For agent fleets

When many coding agents open pull requests at once, CI is where their work queues up. This page is for the person running such a fleet, and for the agents themselves: how to give each agent access, size its fan-out, understand why a job waits, follow jobs without polling, attribute cost, and cancel work.

Agents can read this documentation as Markdown: every page has a `.md` twin, [/llms.txt](/llms.txt) indexes them, and [/llms-full.txt](/llms-full.txt) is the whole set in one file.

## Rules to give your agents

Paste this into your agents' instructions (for example `AGENTS.md`):

```markdown
## CI on Kajiya
- CI jobs run on Kajiya runners: `runs-on: kajiya-2` (or kajiya-4, kajiya-8). Do not change runs-on to other labels.
- API: https://kajiya.app/api/v1, header `Authorization: Bearer $KAJIYA_API_KEY`. Docs: https://kajiya.app/llms.txt
- Before triggering many jobs: GET /api/v1/account. Free vCPUs = concurrency.limit_vcpu - concurrency.in_use_vcpu.
  Do not queue more jobs than that at once.
- A job that does not start is waiting, not lost. GET /api/v1/queue and read waiting[].reason. Do not re-run it.
  no_credit, paused, daily_cap, monthly_cap: stop and tell a human. limit, repo_limit, no_host: wait.
- Never retry faster than once every 10 seconds. On HTTP 429 wait Retry-After seconds.
- To stop a run: POST /api/v1/jobs/{id}/cancel (cancels the whole GitHub workflow run).
```

## One key per agent

Give every agent its own named key, so you can see when it last used the API and revoke it without touching the others. Use the smallest scope that works:

| Agent does | Scope |
|---|---|
| Reads status, queue, jobs and cost | `read` |
| Also cancels runs, pauses or caps repositories | `write` |
| Manages keys, caps and webhooks (keep this for the orchestrator or a human) | `admin` |

An orchestrator holding an `admin` key can issue keys as it starts agents:

```bash
curl -s -X POST "https://kajiya.app/api/v1/keys" \
  -H "Authorization: Bearer $KAJIYA_ADMIN_KEY" -H "Content-Type: application/json" \
  -d '{"name": "agent-07", "scope": "read", "expires_in_days": 7}' | jq -r .key
```

Revoke a key with `DELETE /api/v1/keys/{id}` when its agent is retired. Keys expire on their own if you set `expires_in_days`. An account can have up to 100 active keys, and all keys share the account's rate limit of 1,200 requests per minute.

## Read your limits before fanning out

Jobs beyond your concurrency limit wait on GitHub instead of failing, but they occupy GitHub's queue and finish later. Read the room first:

```bash
curl -s "https://kajiya.app/api/v1/account" -H "Authorization: Bearer $KAJIYA_API_KEY" \
  | jq '{free_vcpu: (.concurrency.limit_vcpu - .concurrency.in_use_vcpu), balance_usd, caps, established: .limits.established}'
```

- **Free vCPUs** divided by the vCPUs per job is how many jobs can start now. A new account has a limit of 4 vCPUs, so it cannot run `kajiya-8` at all ([account limits](/docs/queue-and-limits#account-limits)).
- **Balance:** each job needs 10 minutes of prepaid compute set aside to start ([the gate](/docs/billing#the-10-minute-gate)).
- **Caps:** a daily or monthly cap can hold jobs even when vCPUs are free.
- **Fleet:** `GET /api/v1/queue` also returns `fleet.free_vcpu`, the free capacity across all of Kajiya.

## When a job does not start

Read the reason instead of re-running:

```bash
curl -s "https://kajiya.app/api/v1/queue" -H "Authorization: Bearer $KAJIYA_API_KEY" \
  | jq -r '.waiting[] | "\(.repo)  \(.name)  \(.label)  \(.reason)  \(.message)"'
```

| Reason | What an agent should do |
|---|---|
| `limit` | Wait. It starts when one of your running jobs finishes. Queue fewer jobs at once. |
| `repo_limit` | Wait. The repository's own cap is full. |
| `no_host` | Wait. Kajiya's machines are busy; capacity frees up as jobs finish. |
| `paused` | Stop and tell a human: someone paused the repository on purpose. |
| `no_credit` | Stop and tell a human: compute packs are bought by a person in the dashboard. |
| `daily_cap`, `monthly_cap` | Stop and tell a human, or wait for the next UTC day or month. |

Re-running a waiting job on GitHub does not make it start sooner; it only adds another job to the queue.

## Webhooks instead of polling

Polling `/queue` or `/jobs` from every agent uses up the shared rate limit. Point a [webhook](/docs/webhooks) at your orchestrator instead and route events to agents by `data.repo` and `data.github_run_id`:

- `job.started`: the job has a VM.
- `job.finished`: the VM is gone; `cost_usd` is final. The job's result (passed or failed) is on GitHub.
- `job.waiting`: the job waits; `reason` tells you whether to wait or escalate.
- `spend_cap.reached` and `balance.low`: escalate to a human.

If you do poll, poll one endpoint for the whole fleet, no more often than every 10 seconds.

## Cost per repository, label or run

```bash
# this month by repository
curl -s "https://kajiya.app/api/v1/usage?group_by=repo&since=2026-12-01" -H "Authorization: Bearer $KAJIYA_API_KEY" | jq '.rows'

# every job of one workflow run, with its cost
curl -s "https://kajiya.app/api/v1/jobs?repo=acme/api&since=2026-12-04&limit=500" -H "Authorization: Bearer $KAJIYA_API_KEY" \
  | jq --argjson run 12045567890 '[.jobs[] | select(.github_run_id == $run)] | {jobs: length, cost_usd: (map(.cost_usd // 0) | add)}'
```

In TypeScript, using the client from the [API overview](/docs/api#a-minimal-typescript-client):

```ts
type Job = { id: number; repo: string; state: string; github_run_id: number; cost_usd: number | null };

/** Total cost of one GitHub workflow run, following pagination. */
export async function runCost(repo: string, runId: number, since: string): Promise<number> {
  let total = 0;
  let before: number | null = null;
  do {
    const q = new URLSearchParams({ repo, since, limit: "500", ...(before ? { before: String(before) } : {}) });
    const page = await kajiya<{ jobs: Job[]; next_before: number | null }>(`/jobs?${q}`);
    for (const j of page.jobs) if (j.github_run_id === runId) total += j.cost_usd ?? 0;
    before = page.next_before;
  } while (before);
  return Math.round(total * 1e6) / 1e6;
}
```

## Cancelling work

`POST /api/v1/jobs/{id}/cancel` cancels the job's whole workflow run on GitHub (GitHub has no per-job cancel). A job that no machine has received yet is released and never billed; a running job is billed for the minutes its VM ran.

```bash
# cancel every active job of one run
curl -s "https://kajiya.app/api/v1/jobs?state=active&repo=acme/api" -H "Authorization: Bearer $KAJIYA_API_KEY" \
  | jq -r --argjson run 12045567890 '.jobs[] | select(.github_run_id == $run) | .id' | head -n 1 \
  | xargs -I{} curl -s -X POST "https://kajiya.app/api/v1/jobs/{}/cancel" -H "Authorization: Bearer $KAJIYA_API_KEY"
```

One cancel is enough per run, since it cancels the whole run. The key needs `write` scope, and the repository's GitHub access needs *Actions: Read and write*. Cancelling on GitHub directly (`gh run cancel`) works too; prefer the Kajiya call for repositories that are polled rather than connected by webhook or the GitHub App, because Kajiya then releases jobs it had not started yet at once.

## Planning a large batch

```ts
type Account = { balance_usd: number; concurrency: { limit_vcpu: number; in_use_vcpu: number } };

/** How many jobs of `vcpu` vCPUs can start right now. */
export async function startableJobs(vcpu: 2 | 4 | 8): Promise<number> {
  const a = await kajiya<Account>("/account");
  const free = Math.max(0, a.concurrency.limit_vcpu - a.concurrency.in_use_vcpu);
  return Math.floor(free / vcpu);
}
```

Use it to size a GitHub Actions matrix (`strategy.max-parallel`) or the number of pull requests your agents push at once. [Matrix builds and sharding](/docs/guides/matrix-and-sharding) shows the workflow side.
