# @kody/clef ## Intent Call **Cloudflare Clef** (`@cf/cloudflare/clef-flash` / `@cf/cloudflare/clef`) — Cloudflare’s System One decision models — through **Workers AI** (optional AI Gateway), so other Kody packages can ask typed noul / choice / score questions and get calibrated answers. When agent logic is a state machine, Clef can pick the next transition from enabled events as Choices. Reuses the same `cloudflareApiToken` and account/gateway setup as `@kentcdodds/ai` / `@kentcdodds/jev` when local Clef storage is empty. Success means a tiny evaluate returns `{ ok, answers }` without anyone pasting tokens into chat. ## What it does - Evaluate shared state against named questions (`noul`, `choice`, `score`) - Pick the next state-machine transition (`./choose-transition`) from enabled events - Walk a declarative machine toward a goal (`./run-toward-goal`) via repeated choices - Return parsed `answers`, `model`, and `usage` - Escape-hatch raw `/ai/run/@cf/...` for progressive disclosure - Package storage for account/gateway/model ids, with fallback read from `@kentcdodds/ai/settings` then `@kentcdodds/jev/settings` ## Clef vs Jev This package mirrors `@kentcdodds/jev`’s export surface, but talks to **Clef** instead of TypeSafe Jev: | Concern | `@kentcdodds/jev` | `@kody/clef` | | --- | --- | --- | | Model | `typesafe/jev` | `@cf/cloudflare/clef-flash` (default) or `@cf/cloudflare/clef` | | URL | `POST .../ai/run` | `POST .../ai/run/@cf/cloudflare/clef-flash` | | Body | `{ model, input: { state, questions } }` | Flat `{ model: "clef-flash", state, questions }` | | Secret | `cloudflareApiToken` | **Same** — do not invent a second token | | Billing | AI Gateway Unified Billing / TypeSafe BYOK | Workers AI (402 = billing, not missing secret) | ## Prerequisites / setup 1. Save user secret **`cloudflareApiToken`** (do not paste the value in chat) with Workers AI access and host **`api.cloudflare.com`** (optional also `gateway.ai.cloudflare.com` if you already use `@kentcdodds/ai`): [Create / update cloudflareApiToken](https://kody.codes/account/secrets/new?name=cloudflareApiToken&description=Cloudflare%20API%20token%20for%20Workers%20AI%20%2F%20AI%20Gateway&allowedHosts=api.cloudflare.com%2Cgateway.ai.cloudflare.com&scope=user) 2. Set Cloudflare **account id** (and optionally gateway id / model) via this package’s `./settings` export — stored in package storage, no republish. If you already configured `@kentcdodds/ai/settings` or `@kentcdodds/jev/settings`, leave Clef storage empty: reads fall back to those values. Gateway defaults to **`kody`**. 3. **Model convention** — package storage stores the full Workers AI id (`@cf/cloudflare/clef-flash`). The REST call maps it to body `model: "clef-flash"` and URL `/ai/run/@cf/cloudflare/clef-flash`. Pass short (`clef-flash`) or full ids interchangeably. 4. Confirm readiness: ```js import overview from 'kody:@kody/clef' export default async function main() { return await overview() } ``` 5. Smoke (live, cheap noul on clef-flash): ```js import smokeTest from 'kody:@kody/clef/smoke-test' export default async function main() { return await smokeTest({ live: true }) } ``` ## Evaluate example ```js import evaluate from 'kody:@kody/clef/evaluate' export default async function main() { return await evaluate({ state: 'Help! My payouts have been failing for 3 days.', questions: { is_urgent: { type: 'noul', instructions: 'Does this convey urgency?', criteria: { true: 'Explicitly time-sensitive', false: 'No urgency expressed', }, }, }, }) } ``` ## Writing questions that work well Clef answers in one fast pass. It does not reason before it answers, by design. It does its best work on small, concrete questions about what the state says, while your code holds the decision logic. - **Ask about one observable fact per question.** Name the field it should read: "Does `messages` say the production service went down?" - **Put the rules in code.** Ask several small `noul` questions in one call, then combine the answers with ordered rules: check top to bottom, first yes wins, last rule is the default. You can see, test, and change every step. - **Use `choice` for picking one of several parallel labels**, such as which area a file belongs to. Describe each option by what it looks like in the state. - **Keep policy in code.** Pick thresholds on the returned probabilities in code (for example, block unless P(yes) is below 0.2) instead of asking the model to be careful. - **Ask many questions in one call.** Questions share one state, so a few more small questions add little time compared with a second call. - **Grow the questions from real cases.** When a case comes out wrong, save it with the expected answer, add or reword a question or reorder the rules, and rerun all saved cases before shipping. A stronger model can draft candidate questions from historical data for you to backtest. Check the expected answers first: a label derived from an outcome (such as "issue closed") can be wrong. Works well: ```text is_blocked: Does `messages` say the user is blocked with no workaround? is_down: Does `messages` say their production service went down? is_broken: Does `messages` say something in our product is broken or failing? code: blocked → SEV1, down → SEV2, broken → SEV3, else SEV4 ``` Works poorly: ```text severity (choice): Give me the severity of this issue. SEV1 is X, SEV2 is Y... ``` The second asks the model to weigh everything at once, which is the reasoning step it skips. ## Auth / settings vs `@kentcdodds/ai` / `@kentcdodds/jev` | Concern | Shared? | Where | | --- | --- | --- | | Secret | Yes — same name | `cloudflareApiToken` (user scope) | | Account id | Prefer one setup | Clef `./settings` → else ai → else jev | | Gateway id | Prefer one setup | Same fallback chain; default `kody` | | Model | Separate | Clef defaults to `@cf/cloudflare/clef-flash` | Do **not** invent a second Cloudflare token secret for this package. Docs: [Clef-flash](https://developers.cloudflare.com/workers-ai/models/clef-flash/) · [Clef](https://developers.cloudflare.com/workers-ai/models/clef/) · [Package](https://kody.codes/@kody/clef) ## Done when - `cloudflareApiToken` is saved with `api.cloudflare.com` host approval - Account (and optional gateway) resolve via `./settings` or ai/jev fallbacks - Workers AI is enabled / billed for the account - `./smoke-test` with `{ live: true }` returns `ok: true` and a noul answer