Clef Demo

2026-10-12

Clef Omni API Python Example

Call the Clef Omni REST API from Python with requests — Bearer sk_live_ auth, model/state/questions payload, illustrative responses, and error handling.

Use Python to call the hosted Clef Omni decision API on clefdemo.com. The request shape matches the API Doc: model, state, and typed questions. Authenticate with a `sk_live_` key from Account — never a Cloudflare API token. ## Prerequisites 1. Python 3.9+ (any recent CPython is fine). 2. Install HTTP client: pip install requests 3. Register at Account, create an API key (sk_live_…), and copy it once. 4. Have credits (welcome grant or a prepaid pack). Store the key in an environment variable — do not hard-code secrets in source:

bash
export CLEF_API_KEY="sk_live_YOUR_KEY"

On Windows PowerShell:

powershell
$env:CLEF_API_KEY = "sk_live_YOUR_KEY"

Complete request example This payload matches the hosted docs: `Authorization: Bearer …`, `Content-Type: application/json`, and body fields `model`, `state`, `questions`.

python
import os
import sys
import requests

API_KEY = os.environ["CLEF_API_KEY"]  # sk_live_... — never commit real keys
URL = "https://clefdemo.com/v1/clef-omni/run/"

payload = {
    "model": "clef-omni",
    "state": "Checkout has been failing for every customer for the last hour.",
    "questions": {
        "urgent": {
            "type": "noul",
            "instructions": "Is this support request urgent?",
        },
        "team": {
            "type": "choice",
            "instructions": "Which team should handle this request?",
            "criteria": {
                "billing": "Payments, invoices, and refunds",
                "technical": "Outages, errors, and configuration",
                "sales": "Plans and upgrades",
            },
        },
        "severity": {
            "type": "score",
            "instructions": "How severe is the customer impact?",
            "criteria": ["No impact", "Minor", "Major", "Critical"],
        },
    },
}

res = requests.post(
    URL,
    headers={
        "Authorization": f"Bearer {API_KEY}",
        "Content-Type": "application/json",
    },
    json=payload,
    timeout=60,
)

data = res.json()
if res.status_code != 200:
    print(
        "error",
        res.status_code,
        data.get("code"),
        data.get("error"),
        file=sys.stderr,
    )
    sys.exit(1)

print("request_id", data.get("request_id"))
print("cached", data.get("cached"), "cost_credits", data.get("cost_credits"))
print("remaining_credits", data.get("remaining_credits"))
print("answers", data.get("answers"))
TypeWhen to useCriteria
noulYes / no (or true / false) gatesNone — binary probability
choicePick one label from a closed set (routing, triage)Object map: option id → description
scoreOrdered severity / intensity levelsOrdered list of level labels
json
{
  "request_id": "req_example_0001",
  "model": "clef-omni",
  "cached": false,
  "cost_credits": 42,
  "estimated_tokens": 42,
  "remaining_credits": 9958,
  "answers": {
    "urgent": { "yes": 0.81, "no": 0.19 },
    "team": {
      "billing": 0.08,
      "technical": 0.79,
      "sales": 0.13
    },
    "severity": {
      "No impact": 0.02,
      "Minor": 0.11,
      "Major": 0.54,
      "Critical": 0.33
    }
  }
}
FieldMeaning
answersMap keyed by question id — probabilities per option
request_idCorrelate with logs / support (X-Request-Id header too)
cost_creditsCredits charged for this call (≈ input tokens)
remaining_creditsAccount balance after the call
cachedtrue → cache hit, cost_credits = 0
estimated_tokensToken estimate used for metering
modelModel that performed the evaluation
outputOptional playground-style normalized view for debugging
StatusTypical codeWhat to do
---------
401unauthorizedCheck Bearer sk_live_ key; regenerate if revoked
402paywallBuy credits on Pricing
400validationFix state / questions schema
415bad_content_typeSend Content-Type: application/json
429rate_limit / daily_capBack off; see API Doc limits
502 / 504upstream / timeoutRetry; not charged on this host

Ready to test? Open the Clef Decision Playground.