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:
export CLEF_API_KEY="sk_live_YOUR_KEY"
On Windows 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`.
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"))
| Type | When to use | Criteria |
|---|---|---|
noul | Yes / no (or true / false) gates | None — binary probability |
choice | Pick one label from a closed set (routing, triage) | Object map: option id → description |
score | Ordered severity / intensity levels | Ordered list of level labels |
{
"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
}
}
}
| Field | Meaning | |
|---|---|---|
answers | Map keyed by question id — probabilities per option | |
request_id | Correlate with logs / support (X-Request-Id header too) | |
cost_credits | Credits charged for this call (≈ input tokens) | |
remaining_credits | Account balance after the call | |
cached | true → cache hit, cost_credits = 0 | |
estimated_tokens | Token estimate used for metering | |
model | Model that performed the evaluation | |
output | Optional playground-style normalized view for debugging | |
| Status | Typical code | What to do |
| --- | --- | --- |
| 401 | unauthorized | Check Bearer sk_live_ key; regenerate if revoked |
| 402 | paywall | Buy credits on Pricing |
| 400 | validation | Fix state / questions schema |
| 415 | bad_content_type | Send Content-Type: application/json |
| 429 | rate_limit / daily_cap | Back off; see API Doc limits |
| 502 / 504 | upstream / timeout | Retry; not charged on this host |
Ready to test? Open the Clef Decision Playground.