# Rules for AI agents

> Instructions for an AI agent (or any automated client) that calls this API for a user. Every paid call spends the user's real money (Fara). Follow these rules exactly.

## At a glance

- Read first: this page, then [Conventions](https://faraatarjomeh.com/api/v1/docs/conventions.md), then the guide for the task (see the table on [the overview](https://faraatarjomeh.com/api/v1/docs/index.md)).
- Machine-readable: [OpenAPI](https://faraatarjomeh.com/api/v1/openapi.json), [error catalog](https://faraatarjomeh.com/api/v1/docs/errors.json), [all docs as one Markdown file](https://faraatarjomeh.com/api/v1/docs/llms-full.txt).
- Branch on the error `code`, never on the human `detail` text.

## Getting access

The user only needs an account with Fara on https://faraatarjomeh.com (new accounts get a sign-up gift). Then, in this order of preference:

1. **You speak MCP** (Claude, ChatGPT, Cursor, Claude Code and others): connect to `https://faraatarjomeh.com/api/v1/mcp`. The user signs in in the browser, picks a spending limit for you (500 Fara is suggested; it counts over the whole connection) and allows you; nothing to copy. See [AI assistants (MCP)](https://faraatarjomeh.com/api/v1/docs/guides/mcp.md).
2. **Any other agent or script:** sign in with a code. POST to `https://faraatarjomeh.com/api/v1/oauth/device`, show the user the code and link, poll `https://faraatarjomeh.com/api/v1/oauth/token`, and you get your own key with the monthly limit the user chose. See [Sign an agent in with a code](https://faraatarjomeh.com/api/v1/docs/authentication.md#sign-an-agent-in-with-a-code-no-key-to-copy).
3. **The user already has a key:** use it as `Authorization: Bearer fara_sk_...`.

Files: send a public https link (`fileUrl` for documents, `audioUrl` for recordings); you never need a multipart upload.

## Spending rules

1. **Price before spending.** Get the price first (analysis, estimate, or pricing endpoint — each guide names it). Never start paid work without a price.
2. **Stay inside the approved budget.** Only spend what the user approved for this task. If the price is above that, stop and ask the user. If the user gave no budget, ask before the first paid call.
3. **Check the balance.** `GET /api/v1/account` → `credits.balance` is what can be spent now. If the price is more than the balance, tell the user to top up at https://faraatarjomeh.com; an API key cannot buy Fara.
4. **One job per intent.** Send a fresh `Idempotency-Key` (a UUID) with every POST that can spend Fara, store it with the task, and reuse the **same** key on every retry of that POST. A retry with the same key returns the first answer and never charges twice. Never create a second job to "retry" a job that is still running.
5. **Paid calls in the reference** are marked `x-spends-fara: true` in the OpenAPI document and "spends Fara" in [the reference](https://faraatarjomeh.com/api/v1/docs/reference.md).
6. **Account for what was spent.** The real cost of a job is the sum of `balanceChange` over its ledger rows: `GET /api/v1/account/transactions?jobId={jobId}` (see [Billing](https://faraatarjomeh.com/api/v1/docs/billing.md)). Report it to the user.

## Waiting rules

1. **Prefer webhooks** if you have a public https endpoint ([Webhooks](https://faraatarjomeh.com/api/v1/docs/webhooks.md)). Otherwise poll.
2. **Polling intervals:** first check after 10 s, then back off to every 30–60 s. Never poll faster than every 5 s per job.
3. **Unknown status values:** treat them as "still running". Only the terminal statuses listed in each guide's state machine end a job.
4. **Review statuses** (`awaiting_review`): the job waits for a decision. Approve automatically only if the user told you to; otherwise show the user what to review and wait.

## Error rules

Every error is `application/problem+json` with a stable `code`. Look the code up in the [error catalog](https://faraatarjomeh.com/api/v1/docs/errors.json) and follow its `retry` rule:

| `retry` | What to do |
|---|---|
| `do_not_retry` | Stop. Fix what the error names (or ask the user if `needsUser` is true). Sending it again fails the same way. |
| `refresh_then_retry` | GET the resource again, rebuild the request from the fresh data, send it once more. |
| `retry_later` | Send the same request with the same `Idempotency-Key` after `Retry-After` seconds, or back off 2 s, 4 s, 8 s … up to 5 minutes. Give up after about 30 minutes and tell the user. |
| `check_then_retry` | `500`/`502`/`504`: the work may have happened and this answer is replayed for the same key. List the jobs (or the ledger) to see if it happened. Only if not, send again with a **new** `Idempotency-Key`. |
| `wait` | Send nothing. The job already runs; poll it. |

Special cases:

- `402 INSUFFICIENT_CREDITS` — stop and tell the user the price and the balance (the body has `balance` and `estimatedCost`, `cost` or `required`). Give the user `topUpUrl` from the body: the website page where they add Fara (it suggests a pack that covers `shortfall`). An API key cannot buy Fara. After they pay, send the same request again.
- `402 SPEND_LIMIT_REACHED` — the key's own limit, not the balance. Give the account owner `limitsUrl` to raise it.
- `429 RATE_LIMITED` — wait `Retry-After` seconds. Read the `RateLimit` header to slow down before you hit the limit.
- Network error or timeout on a POST — resend it with the same `Idempotency-Key`. You get the stored first answer or `409 IDEMPOTENCY_KEY_IN_USE` (wait and resend).

## Safety rules

- The API key is a secret with access to the user's money. Never print it, log it, put it in a URL, or send it to any host other than `https://faraatarjomeh.com`.
- Never send the key from a browser or a mobile app; only from a server or a local script.
- Files you upload and results you download belong to the user. Do not share them elsewhere unless the user asked.
- Deleting a result (`DELETE` calls) cannot be undone. Only delete when the user asked.
