<!-- Source: https://keybeat.dev/docs/agents -->

# Quickstart for agents

Keybeat is built so that an AI coding agent can do the work: write the Playwright spec, test it, deploy it, and later investigate failures. A human only needs to approve access once in the browser, and to start a paid subscription if one is needed.

This page is written for both the human and the agent. If you are an agent, the [checklist](#agent-checklist) is the part to follow.

## What it looks like

You tell your agent:

> Monitor my checkout flow with Keybeat.

A capable agent will then:

1. Read these docs (from [/llms-full.txt](https://keybeat.dev/llms-full.txt) or the `.md` version of any page).
2. Check access with `npx keybeat whoami --json`, and if needed start `npx keybeat login --no-wait --json` and ask you to approve in the browser.
3. Create `keybeat/checkout.spec.ts` and add it to `keybeat/keybeat.json`.
4. Run `npx keybeat test checkout` until it passes locally.
5. Store any secrets with `npx keybeat env set`, then run `npx keybeat deploy`.
6. Run `npx keybeat run checkout --json` once to confirm it passes in the cloud.

Later you can ask "why did the checkout monitor fail last night?" and the agent can list failed runs, read the failing step, error message and logs, and download the Playwright trace.

## Two ways for an agent to connect

| | CLI | MCP server |
|---|---|---|
| Setup | None. `npx keybeat` in the project | `claude mcp add --transport http keybeat https://mcp.keybeat.dev/mcp` |
| Auth | Device login, approved once by a human, or `KEYBEAT_API_KEY` | OAuth, approved once by a human in the browser |
| Can test locally | Yes, `keybeat test` | No, MCP calls the cloud API only |
| Deploys from repo files | Yes, `keybeat deploy` | Through `upsert_monitor`, one monitor at a time |
| Best for | Writing and deploying monitors from a repo | Inspecting runs and incidents from any project |

Most setups use both: the CLI to build and deploy monitors in the repository, and the MCP server to investigate failures. See the [CLI reference](https://keybeat.dev/docs/cli) and [MCP server](https://keybeat.dev/docs/mcp).

## Adding Keybeat to Claude Code

```bash
claude mcp add --transport http keybeat https://mcp.keybeat.dev/mcp
```

The first time Claude Code uses the server it opens a browser window. Log in to Keybeat, pick an organization and approve. Other MCP clients that support remote servers over HTTP with OAuth work the same way with the URL `https://mcp.keybeat.dev/mcp`.

The Keybeat plugin for Claude Code installs the MCP server together with a skill that teaches Claude how to write, test, deploy and debug monitors. See [Claude Code plugin](https://keybeat.dev/docs/claude-code).

## Agent checklist

Follow these steps in order. Use `--json` on every CLI command and parse the output.

1. **Check access.** Run `npx keybeat whoami --json`. If it exits with code 3, log in in two steps, so your shell does not block while the human approves:

   ```bash
   npx keybeat login --no-wait --json   # prints verification_url and user_code
   npx keybeat login --resume --json    # after the human says they approved
   ```

   Show the URL and the code to the human. The approval screen asks them to check that the code matches. Do not try to sign up or approve on the human's behalf.
2. **Check the plan.** `whoami` returns the organization's plan, limits and usage. The free plan allows one monitor at a 60 minute interval. If the human asks for more, tell them what it will cost (see [Billing](https://keybeat.dev/docs/billing)) before creating paid monitors.
3. **Initialise.** If there is no `keybeat/` folder, run `npx keybeat init`.
4. **Write the spec.** One file per monitor in `keybeat/`, importing only from `@playwright/test`, `otpauth` and Node built-ins. Read [Writing monitors](https://keybeat.dev/docs/writing-monitors) first. Use `test.step()` for each meaningful stage so failures point at a step.
5. **Never hard code secrets.** Read credentials from `process.env`. Ask the human for values, or for permission to use existing ones, and store them by piping the value, for example `printf '%s' "$VALUE" | npx keybeat env set <slug> NAME`, so it never appears on a command line. Do not write secret values to files in the repository.
6. **Test locally.** `npx keybeat test <slug>` with the env vars set in your shell. Fix the spec until it passes reliably. Run it at least twice. Keep the run well under 60 seconds.
7. **Deploy.** For monitors that need secrets: deploy with `"enabled": false`, set the secrets, then enable and deploy again. Otherwise `npx keybeat deploy` once. Use `npx keybeat deploy --dry-run --json` first to see the changes and the billing estimate. Do not use `--prune` unless the human asked for it, because it deletes monitors and their history. Destructive commands need `--yes` in a non-interactive shell; without it they exit with code 2 and `confirmation_required`.
8. **Verify in the cloud.** `npx keybeat run <slug> --json` and check `status` is `passed`.
9. **Report back.** Tell the human what you deployed, the interval, the monthly cost change if any, and where alerts will go.

### Investigating a failure

```bash
npx keybeat monitors get checkout --json          # state, perf_state, last run
npx keybeat runs list checkout --status failed --json
npx keybeat runs get <run_id> --json              # steps, error, logs, artifact URLs
```

The run detail includes each step with its duration and status, the first error message, the logs (with secrets masked) and short-lived URLs for the Playwright trace and final screenshot. Traces exist only for failed runs. To inspect a trace, download it with `npx keybeat runs get <run_id> --download ./keybeat-artifacts` and open it with `npx playwright show-trace`. Traces can contain values typed into the page, including secrets, so do not paste them anywhere public.

Tell the human whether the failure looks like a real problem on their site (a changed page, a 500 error, a slow response) or a fragile spec (a locator that broke after a harmless UI change). Fix the spec only in the second case, test locally and deploy again.

Remember the alerting rules: a scheduled failure is retried at once from another region, and the human is only alerted if the retry also fails. Runs with status `error` are Keybeat infrastructure problems, not site problems.

## Billing and agents

An API key can change billing without further confirmation once the organization has a subscription. Creating a 5 minute or 1 minute monitor, or more than three 15 minute monitors, adds add-on charges that are prorated in Stripe at once. The API response includes a `billing_change` object listing each changed add-on (`items`, with the new and previous quantity and the amount change) and the net `amount_delta_cents`.

If the organization is on the free plan and the agent tries to create a monitor that needs a paid plan, the API returns HTTP 402 with a `checkout_url`. The agent should give that URL to the human, who completes checkout in the browser. Agents cannot start a subscription on their own.

## A snippet for your CLAUDE.md or AGENTS.md

Paste this into your repository's agent instructions so every session knows how monitoring works:

```text
## Monitoring (Keybeat)
- Synthetic monitors live in keybeat/ (Playwright Test specs) and keybeat/keybeat.json.
- Docs: https://keybeat.dev/llms-full.txt
- Test locally: npx keybeat test <slug>. Deploy: npx keybeat deploy. Use --json.
- Only @playwright/test, otpauth and Node built-ins are available. 60 second limit per run.
- Secrets come from process.env and are set by piping: printf '%s' "$VALUE" | npx keybeat env set <slug> NAME
- Ask before adding monitors that change the bill, and never run deploy --prune without asking.
```
