Script icon
Request icon
If icon
SlackIncomingWebhook icon
Switch icon
Log icon
Schedule icon
Webhook icon

Triage Inbox Emails with Cloudflare Clef Decision Models

Classify inbox emails with Cloudflare Clef in one call and route by category behind a confidence gate with human review.

Categories
AIBusiness

Shared inboxes drown in newsletters, promos, receipts and the occasional critical outage or overdue invoice. Chat LLMs can classify, but they are slow, pricey per run, and return free-form text you must parse, occasionally inventing labels.

This blueprint uses a decision model instead: Cloudflare's Clef (clef-flash default, clef optional), which speaks the Jev / System One API. One HTTP call evaluates three typed questions in parallel, category (choice over 8 labels), safe-to-delete (noul probability), urgency (score over 4 levels), and returns probabilities plus confidence. A sanitize task redacts codes and account numbers before any model call, finance-legal mail is hard-protected from auto-delete, and anything under the confidence threshold goes to human review instead of being auto-labeled.

How it works

  1. tier0_heuristics (scripts.python.Script): flags bulk-mail patterns and redacts 4+ digit runs (codes, account numbers) to ***, publishing heuristic, safe_subject, safe_body and a JSON-escaped safe_state_json.
  2. classify_clef (core.http.Request): POSTs the scrubbed state plus choice/noul/score questions to .../ai/run/@cf/cloudflare/clef-flash, with exponential retries.
  3. route_decision (core.flow.If): confidence below inputs.confidence_threshold (default 0.5) routes to notify_human_review (Slack); otherwise route_by_category (core.flow.Switch) fans out to team cards, urgent incident card, finance-legal card plus protection log, quiet logs for newsletter/promo/low-priority, standard card for the rest.
  4. alert_triage_failure (errors block): posts to Slack if classification or routing throws.

What you get

  • One-call typed classification, no prompt parsing, no invented labels, ~38ms median on clef-flash.
  • Zero auto-labeling below confidence: uncertain mail always escalates to a human.
  • PII minimization baked in: codes and account numbers never reach the model.
  • Team-specific Slack cards per category, quiet logs for bulk mail, failure alerts on errors.

Who it's for

  • Platform engineers, AI practitioners and ops teams automating shared inboxes and SLA escalation paths.

Why orchestrate this with Kestra

Cron plus scripts hide retries, secrets and audit trails. Kestra gives declarative scheduling, masked secrets, confidence-gated routing, per-category fan-out and full execution history in one maintainable YAML workflow.

Prerequisites

  • A free Cloudflare account with Workers AI enabled (10,000 Neurons/day free covers thousands of clef-flash classifications daily).
  • A Slack workspace with an incoming webhook for notifications.

Secrets

  • CLOUDFLARE_ACCOUNT_ID: Cloudflare account ID in the Workers AI URL (required).
  • CLOUDFLARE_API_TOKEN: Token with Workers AI Read + Edit (required).
  • SLACK_WEBHOOK_URL: Incoming webhook for triage cards and failure alerts (required).
  • WEBHOOK_KEY: Auth key for the realtime webhook trigger (required).
  • OSS self-hosted: provide these as base64 SECRET_<NAME> environment variables (e.g. SECRET_CLOUDFLARE_API_TOKEN); Cloud/EE users add them under Namespaces → Secrets.

Inputs

  • email_subject, email_body, sender_email: the mail to classify (defaults ship a runnable demo).
  • model (clef-flash / clef): decision model selector. Note the request URI targets the flash endpoint; align both when switching.
  • confidence_threshold (FLOAT, default 0.5): below this, mail goes to human review.

Outputs

  • outputs.category: winning category label (or unknown).
  • outputs.confidence: winning confidence (or 0).

Quick start

  1. Add the four secrets above to your namespace (or OSS environment).
  2. Import this blueprint and Execute with defaults, no inbox needed.
  3. Verify the human-review Slack card (the demo invoice classifies receipt at low confidence by design).
  4. Re-run with your own mail via custom inputs; enable scheduled_inbox_sweep or point a mail handler at webhook_inbound_email when satisfied.

How to extend

  • Swap model to clef for max precision, point the same body at TypeSafe Jev (api.typesafe.ai/v1/systemone), or fall back to Gemini Classification, the question schema ports directly.
  • Fan out per-team channels with one webhook URL per team, or apply Gmail labels via the Google Workspace plugin.
  • Feed attachments to Clef's multimodal images[] (Jev-class models without vision cannot do this).
  • Persist every verdict to Postgres for an audit trail, like the HuggingFace classification blueprint does.

Common pitfalls

  • Confidential mail transits a third party: subjects and bodies are sent to Cloudflare. Redaction covers digit runs, but review vendor data terms before production; self-host Cloudflare/clef-flash (Apache-2.0) or go local Ollama for sensitive mailboxes.
  • Spaced or dashed codes slip redaction: 123 456 survives the digit-run regex; normalize separators first if your threat model needs it.
  • Overlapping criteria split votes: category definitions that share features (invoice vs urgent vs finance-legal) lower confidence by design, sharpen boundaries instead of lowering the threshold.
  • Spoofed senders: verify SPF/DKIM/DMARC before trusting sender_email for anything consequential.
  • Prompt injection in bodies: treat mail text as untrusted data; strict typed outputs bound the damage, human review gates the actions.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.