@kody/clef
Cloudflare Clef decision models via Workers AI (Jev-shaped noul/choice/score API).
IntegrationsTopics: clef, cloudflare, noul, choice, score, state-machine, evaluation, workers-ai, ai-gateway, transition, xstate
- Version
- 1.0.0
- License
- Published
- November 21, 2026
- Pinned commit
5a4cb97- Rating
- No ratings yet
- Forks
- 0
- Adaptation effort
- —
README
Agent docs@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, andusage - Escape-hatch raw
/ai/run/@cf/...for progressive disclosure - Package storage for account/gateway/model ids, with fallback read from
@kentcdodds/ai/settingsthen@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
Save user secret
cloudflareApiToken(do not paste the value in chat) with Workers AI access and hostapi.cloudflare.com(optional alsogateway.ai.cloudflare.comif you already use@kentcdodds/ai):Set Cloudflare account id (and optionally gateway id / model) via this package’s
./settingsexport — stored in package storage, no republish. If you already configured@kentcdodds/ai/settingsor@kentcdodds/jev/settings, leave Clef storage empty: reads fall back to those values. Gateway defaults tokody.Model convention — package storage stores the full Workers AI id (
@cf/cloudflare/clef-flash). The REST call maps it to bodymodel: "clef-flash"and URL/ai/run/@cf/cloudflare/clef-flash. Pass short (clef-flash) or full ids interchangeably.Confirm readiness:
import overview from 'kody:@kody/clef'
export default async function main() {
return await overview()
}- Smoke (live, cheap noul on clef-flash):
import smokeTest from 'kody:@kody/clef/smoke-test'
export default async function main() {
return await smokeTest({ live: true })
}Evaluate example
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
messagessay the production service went down?" - Put the rules in code. Ask several small
noulquestions 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
choicefor 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:
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 SEV4Works poorly:
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 · Clef · Package
Done when
cloudflareApiTokenis saved withapi.cloudflare.comhost approval- Account (and optional gateway) resolve via
./settingsor ai/jev fallbacks - Workers AI is enabled / billed for the account
./smoke-testwith{ live: true }returnsok: trueand a noul answer
Report this listing
Log in to report this listing.