# Clavilux AI for agents

People use the website. You use the same products through HTTP and MCP. No JavaScript is needed.

## Start here
- Show, do not describe: make a first clip from a sentence that suits the person, then say what else Clavilux does.
- **Terminal or HTTP tool (best):** connect once (below), then use HTTP. If your sandbox blocks the network, ask the person once to allow network access.
- **In a browser:** open https://clavilux.ai/app, type the sentence and press Generate. The person signs in if asked and watches the clip there. Skip the connection below.

## Without an account
- Films and the request behind each: https://clavilux.ai/films.md (JSON: https://clavilux.ai/v1/films).
- Models, variants, fields, defaults: `GET https://clavilux.ai/v1/models`.
- Price a request: `POST https://clavilux.ai/v1/quote` with `{"model", "variant", "input"}`. Free; reserves nothing.
- Full API: https://clavilux.ai/v1/openapi.json.

## Connect (the person approves once)
1. `POST https://clavilux.ai/v1/agent/links` with `{"client_name": "<your name>"}`. You get `device_code`, `user_code`, `verification_uri_complete`, `interval`.
2. Show the person `verification_uri_complete`. They sign in and press Approve. They copy nothing back.
3. Every `interval` seconds, `POST https://clavilux.ai/v1/agent/links/token` with `{"device_code": "…"}`. `authorization_pending`: keep polling. `slow_down`: poll less often. `access_denied` or `expired_token`: stop and tell the person.
4. You get two keys, each shown once. Save them with mode 600; never print them. Send them as `Authorization: Bearer`.
   - `api_key` (`xf_…`): `/v1/jobs`. Store it in `~/.config/xfilm/api-key` or `XFILM_KEY`.
   - `access_token` (`xf_studio_…`): `/v1/studio/*` and https://clavilux.ai/mcp. Store `{"base_url": "https://clavilux.ai", "api_key": "…"}` in `~/.config/xfilm/connection.json`, or point `XFILM_CONFIG_FILE` at another private path, or set `XFILM_STUDIO_API_KEY`.

```sh
curl -s https://clavilux.ai/v1/agent/links -H 'Content-Type: application/json' -d '{"client_name":"Claude Code"}'
curl -s https://clavilux.ai/v1/agent/links/token -H 'Content-Type: application/json' -d '{"device_code":"<device_code>"}'
```

The keys cannot change the account, keys or payments. The person revokes them on https://clavilux.ai/account. A new account has no paid balance. You never pay more than the price you send back (`max_price_micro_usd`; `max_price_cents` in Studio).

## First clip
Pick a model from `GET https://clavilux.ai/v1/models` and quote it. Submit the same body with `quote_id` and `max_price_micro_usd` to `POST https://clavilux.ai/v1/jobs` with an `Idempotency-Key`.

Poll `GET /v1/jobs/<id>` every few seconds: `SUBMITTING`, `QUEUED`, `RUNNING`, then `SUCCEEDED` (`output` has the file links) or `FAILED`. `UNKNOWN`: keep polling. Tell the person the wait (`typical_seconds`, `estimated_completion_at`). A free clip has a small "Clavilux AI · Free" mark.
Give the person `web_url` (signed in) or the `output` link (public; share it only with them). If an answer is lost, resend the identical body with the same `Idempotency-Key`; it never starts a second job.

## Money
- Use free models (a `free` object in `/v1/models`) unless the person needs a paid one.
- Before paying, tell the person the model, the price and why. Wait for yes.
- One US dollar is 1,000,000 micro-USD. Paid models use the prepaid balance.

## When a free video is refused
The answer has `code`: `free_quota_exhausted` (`resets_at` says when it returns), `free_capacity_unavailable` or `free_active_limit`. An accepted free job can also end `FAILED` with `error_code: "free_capacity_unavailable"`. Nothing is charged.
1. `suggested_models` lists paid models for the same clip, cheapest first, each with `price_micro_usd`, `typical_seconds`, `affordable` and `request` (the body for `/v1/quote`).
2. Inside an agreed budget, take the first. Otherwise ask the person first. Then quote `request` and submit with `quote_id`, `max_price_micro_usd` and a new `Idempotency-Key`.
3. An empty list: wait for `resets_at`.

## When the balance is short
`402` with `code: "insufficient_funds"`, `funds` (`needed_micro_usd`, `available_micro_usd`) and `top_up` (`url`, `message`). Nothing is charged.
1. Show the person `top_up.message` (it has the link).
2. Poll `GET https://clavilux.ai/v1/me` every 10 seconds. When `available_micro_usd` covers `needed_micro_usd`, resend the identical request with the same `Idempotency-Key`.
3. Stop after 15 minutes and tell the person. In Studio, a short film pauses and continues when the balance covers the next step.

## Re-run a film from the wall
Each entry in https://clavilux.ai/films.md has its `request`. Quote it, then submit it with `quote_id` and `max_price_micro_usd`.

## Studio (films, editing, review)
Projects hold briefs, uploads, clips, timelines, videos and reviews. Each answer has `web_url`, where the person watches. Use the MCP server (https://clavilux.ai/mcp, Streamable HTTP, `Authorization: Bearer xf_studio_…`) or `/v1/studio/*`. Guide and tools: https://clavilux.ai/v1/connect.md.
To edit a timeline, save a plan: `POST /v1/studio/projects/{project_id}/plan` (MCP: `save`, kind `plan`). Format: `components.schemas.StudioPlan` in https://clavilux.ai/v1/openapi.json, with an example. A 400 names the field to fix.

## Rules
- Quote first. The price you send back is the ceiling.
- One stable `Idempotency-Key` per intent. Read the job or project to check progress; never resubmit.
- `QUEUED` is accepted work, not a refusal.
- Errors are JSON `{"error": "…"}` and say what to change. `401` key, `402` balance, `409` key reused with different terms or model unavailable, `429` slow down.

## Not available yet
OAuth sign-in for MCP connectors (claude.ai, ChatGPT); a spend ceiling per key.
