AI-Math: Jev’s RLCD(Reinforcement Learning for Calibrated Decisions)
Hi everybody!
The Jev’s RLCD(Reinforcement-Learning/RL for Calibrated-Decisions/CD) is the mathematical-foundation and training-algorithm behind the Jev(TypeSafe-AI)-model developed-by TypeSafe-AI. Unlike classic text-optimized LLMs, RLCD eliminates token-by-token autoregressive-generation(specific-to classic-LLMs) and treats decisions as direct-probabilistic modeling(direct-optimization of structured and statistically-calibrated decisions).
In Jev’s RLCD(Reinforcement-Learning for Calibrated-Decisions) framework, the math shifts from predicting the next sequential token to outputting a mathematically calibrated 𝐾-way probability-distribution(PD) over discrete, typed-decisions. Instead-of maximizing text-fluency(like RLHF / Reinforcement Learning from Human Feedback) or binary-verification(like RLVR), RLCD uses proper scoring-rules(such-as a logarithmic scoring rule or Brier-score) where the optimal-policy is to output the true statistical posterior.
The Core RLCD Math & Mechanics:
- Policy-Output: Given an unstructured state 𝑆 and a discrete-question(or dictionary-of
questions)with 𝐾 choices/options of answering, Jev outputs a probability-vector: 𝐩 = [𝑝₁, 𝑝₂, …, 𝑝ₖ ], where Σᵏᵢ₌₁ 𝑝ₖ = 1 in a single non-autoregressive forward-pass.
→State(Status / Context) 𝑆: This is the raw, unformatted material or raw-text that Jev needs-to analyze. It(material/text) represents the current-state of your application.
- What it(𝑆) contains?(answering-by some examples): A support-ticket from-a customer, the DOM-code of a web-page, a server error-log, or a JSON-file with raw-data.
- Its(𝑆) role: It gives the model the context and facts to-make a decision on.
Let’s prelevate the input-data and output-results from Python simple example one(which use TypeSafe-SDK — Cloud-based — using a generated-Key) :
→ Input-data: Stat(𝑆) …
Note: in this simple-example the State(S) is constant/known/given, but in programming/software-case, it could/would be any/unknown/hidden(because it could-change dinamically)
state = {
"message": "I was charged twice and need the duplicate refunded today.",
"account_tier": "business", # enterprise,...
}→ Input-data: Dictionary-of questions:
questions={
"intent": Choice( # a) intent Choice-useCase
instructions="What is the customer's main request?",
criteria={
"refund": "The customer wants money returned.",
"technical_help": "The customer needs a bug or integration fixed.",
"other": "None of the options clearly fits.",
},
),
"is_urgent": Noul( # b) urgent Noul-useCase
instructions="Does `message` explicitly communicate time pressure?"
),
"frustration": Score( # c) frustration Score-useCase
instructions="How frustrated does the customer appear?",
criteria=["Calm and neutral", "Concerned but civil","Very angry"],
),
}→ The expected Jev’s result/output(of TypeSafe-SDK)could-be extract/obtain-as:
#
# https://ekky.dev/blog/typesafe-ai-jev-implementation-on-python/
#
# Having the above-State,...
# ...for each above-question ...
#... we will obtain the following output/answer:
# - a) intent Choice-useCase
intent_answer = response.answers["intent"]
print(f"Choice: {intent_answer.choice}")
# Output: Choice: refund
print(f"Probabilities: {intent_answer.probabilities}")
# Output: Probabilities: {'refund': 0.95, 'technical_help': 0.03, 'other': 0.02}
# - b) urgent Noul-useCase
urgent_answer = response.nouls["is_urgent"]
print(f"Urgent Probability: {urgent_answer.noul}")
# Output: Urgent Probability: 0.98
# - c) frustration Score-useCase
frustration_answer = response.scores["frustration"]
print(f"Frustration Score: {frustration_answer.score}")
# Output: Frustration Score: 1.8
# And in this case...the interpretation of the above bussiness-state(...
# ...the state of affairs S:
# "I was charged twice and need the duplicate refunded today."
# )...
# ...(the translation/interpretation/conclusion of analysis using ...
# ... specific analysis criterias) is/was...
# The our double-charged Customer/Client is/was ...
# ...(Very-)Angry(c-Score for frustration of 1.8 / near Very-angry; because ....
# ... that means that ... 1.8 is near/close max-Score = 2, where ...
# ... the our calibration-scale have 3 lines/levels/scale-gradations...
# ... Index_0=Calm and neutral, ...
# ... Index_1=Concerned but civil, and ...
# ... Index_2=Very angry), ... and ...
# ...Urgently(b-Noul: is-Urgent = No or Yes, Probability-of 98% / Yes=Today) ...
# ... demands/requires/reclaim to-be routing-to a ...
# ...ReFund(a-Choice: Intent=[refund, technical_help, other] with Probability-of 95% for refund -> intent/queue/department...)...
# ... to return the duplicate/twice-refunded, already charged!
# Note: To Jev test-only,...we could avoid the Key-generation, if we will use the OpenRouter!
Also we could use locally, in privat...one SLM/model(like qwen2.5:3b) to
# simulate/see/prove the Jev-functionaly(see also OpenJev, Kev)
# Onother way to replace/simulate the original Jev - proprietary closed-loop algorithm
# (closed-weights), is to-use Laya-AI(or CLM), Rizzo-Flow(llama.cpp based and using small models/SLM like Qwen or Spark-X)- Proper Scoring Rule Reward: The reinforcement-learning(RL) reward 𝑅(𝑝ₖ,k) for the correct/true-choice y = k penalizes overconfidence and underconfidence symmetrically. For example, using a logarithmic scoring rule, the reward is log(𝑝ₖ). If the model assigns 𝑝ₖ = 0.90 and is correct, it gets log(0.90) ≈ −0.105. If it assigns 𝑝ₖ = 0.50 and is correct, it gets log(0.50) ≈ −0.693. If it is overconfident and wrong(𝑝ₖ = 0.99), the penalty plunges-toward negative infinity( log(0.01) ≈ −4.6 ).
Remark: In the context-of the Jev framework and its RLCD calibration-algorithm, the terms true-, correct-, and ground- value (or ground-truth value) are functionally-synonymous. All three refer to the same fundamental-mathematical concept: the actual verifiable result(or correct label in the dataset/DS).
- Empirical-Calibration: Across 𝑁 predictions where the assigned-probability is 𝑝, the empirical-accuracy matches 𝑝(e.g., 87 out-of 100 predictions made at 𝑝 = 0.87 are empirically correct).
- Confidence-Derivation: Normalization or distance-from a uniform-distribution(1/𝐾) over the top-probability 𝑝ₘₐₓ often maps-to the scalar-confidence-metric: Confidence = (𝑝ₘₐₓ−1/𝐾) / (1−1/𝐾)
Concrete-Example: Support Ticket Routing
Imagine feeding a support-ticket state into Jev and asking a choice-question:
Which department handles this? with 𝐾 = 3 choices(categorical / enum): [Billing, Technical, Refunds].
- Jev Output Vector:
𝑝(Billing) = 0.82 (82%)
𝑝(Technical) = 0.15 (15%)
𝑝(Refunds) = 0.03 (3%)
- Calculated-Confidence:
Confidence = (0.82−1/3) / (1−1/3) = (0.82 −0.333) / 0.667 ≈ 0.73
(Maybe we can take the Confidence-threshold at/@ 0.70)
- Application-Branching(Deterministic-Code):
- If Confidence ≥ 0.90 → Route-to
Billingautomatically. - If 0.70 ≤ Confidence < 0.90 → Route-to
Technicalwith a secondary verification-flag (0.73 falls here). - If Confidence < 0.70 → Route-to
Refunds(Escalate-directly to a human-agent).
For deeper technical-explorations of proper scoring rules and experimental-implementations, you can-(re)view Honest About Uncertainty: I Tried to Rebuild Jev’s RLCD From a Blog Post.
Note: This simple example raise 2 interesting and simple questions:
- How-to implement a custom loss(ℒ)-function and a routing-threshold?
- What is the number of classes(𝐾) in (y)our specific decision task?
The mathematical-architecture of this algorithm(RLCD)
The basis of RLCD is the transformation-of the multi-option decision into a preference-modeling problem through the Plackett–Luce(PL) mathematical-model.
1.Schema-Conditioned Plackett–Luce →Objective:
Instead-of predicting the distribution-of the next-token from a huge-vocabulary(like LLM), Jev is given a state(a set-of unstructured-data) and an output “schema”(a predefined set-of typed decisions/classes).
→Schema(Structure / Questions): It(schema) is the strict template, defined-by you in the code(for-example through Pydantic-Docs-templates), that tells Jev what you are looking for and what options it has available.
- What it(schema) contains: The data-types and predefined-options(the Choice=Categorical/Enum, Score=Ordinal-rating / Rubric or Noul=Boolean/ Binary-probability → foundational decision-primitives).
- Its role: Restricts the model from a mathematical point-of-view. It(schema) prohibits/restricts-it from generating free-text(prose) and forces/restricts-it to return-only probabilities(a number) for the options(of working: Choice, Score, Noul) in that schema(template/scope)
Mathematics, RLCD uses a generalization-of the Plackett–Luce model for multi-way preference modeling.
Given a set-of possible-choices C = {c₁, c₂, … , cₖ} defined-by the schema, the model learns a scoring-function(or latent-reward/score) V_θ(s, cᵢ), for each option cᵢ in the context of state s. The model associates each option cᵢ with a latent reward-score vᵢ = f(cᵢ | s).
The probability that option cᵢ will be selected(as the best-choice in the C set) is governed/calculed-by the Softmax function applied over-the latent-utilities/scores:
P(cᵢ | C) = eᵛᵢ / Σᵏⱼ₌₁eᵛⱼ , where j = 1 .. k
In RLCD, this objective is extended as a schema-conditioned Plackett–Luce objective. The model receives a structure(a defined data-type) and simultaneously-computes, in a single forward-pass, the scores for all-options in the schema, eliminating the autoregressive-loop.
2. Probability-Calibration and Strictly Proper Scoring Rules:
Unlike RLHF(where the model/LLM only-learns what the human prefers?!), RLCD forces the estimated-probabilities to-be statistically-calibrated.
Mathematical-Definition of Calibration: If the model assigns a probability P(correct) = p to a set-of decisions, then the empirical-frequency of correct-decisions in that set must asymptotically tend-to p. For example, of all-cases where Jev indicates a confidence/trust of 0.85, exactly 85% of them must-be correct in-reality.
To achieve/obtain this behavior without distorting the distribution, the cost-function uses Strictly-Proper Scoring-Rules, such-as the Brier-Score(BS) or logarithmic-deviation, heavily-penalizing over-confidence/over-trust and under-confidence/under-trust.
If the model predicts the probability-vector for the possible-classes, and the actual-result(ground-truth) is the one-hot vector p̂ (where yᵢ = 1 for the correct-answer and yᵢ = 0 otherwise), the calibration component minimizes the loss:
Get Ion’s stories in your inbox
Join Medium for free to get updates from this writer.
ℒ_Brier = (1/k) · Σᵏᵢ₌₁(𝑝̂ᵢ-𝑦ᵢ)² , where i = 1 .. k
The above-expression shows/seem-like the formula for Mean Squared Error(MSE … written-as inferior/_ index: _MSE), which is commonly-used as a loss function in machine-learning(ML) and statistics to-measure the average squared difference(2) between estimated values(ŷᵢ) and the actual-value(𝑦ᵢ).
ℒ_MSE = (1/n) · Σⁿᵢ₌₁(ŷᵢ−𝑦ᵢ)² , where i = 1 .. n
Breakdown of the Components:
𝓛_MSE: The total Mean Squared Error(MSE) loss(𝓛).
ₙ: The total number of data-points or samples.
Σⁿᵢ₌₁: The summation symbol, meaning you add up the results for every data-point/sample from ᵢ₌₁ to ᵢ₌ₙ).
ŷᵢ: The predicted(estimated)-value generated by your model for the i-th sample.
𝑦ᵢ: The actual, true-value(ground-truth) for the i-th sample.
(ŷᵢ−𝑦ᵢ)²: The squared difference(error) between the prediction(ŷ) and the true-value(y), which ensures negative-errors don’t cancel out-positive(decrease the loss ℒ) ones and penalizes(increase the loss ℒ) larger-errors more-heavily.
By simultaneously optimizing decision correctness and probabilistic-accuracy through strategic-gradient, RLCD avoids the “confidence/trust hallucination” native-to LLMs.
3. Ideal Calibration-chart/graph
In order for software-systems to-automate decisions(e.g., routing/redirecting a question-ticket only-if the confidence is > 0.90), the model calibration-curve must perfectly-follow the identity y = x.
4.Structural-Differences in Learning-Algorithms
The mathematics behind RLCD fundamentally rearranges the priorities compared-to other alignment-methods in AI:
Comparision-Table in 4-columns: Method; Reward-Function; Primary Mathematical-Objective; Output-Space:
- RLHF; Score-of a human-preference model(Reward-Model); Maximizing the likelihood-of the evaluators’ preferred text; Autoregressive(Token-by-token).
- RLVR/GRPO; Compilers, unit tests, deterministic validators(Verifiable); Maximizing logical/mathematical correctness in a chain of thought; Autoregressive(Long Text/Reasoning).
- RLCD(Jev); Real-decision result and Statistical-calibration penalty; Minimizing calibration-error(Aligning confidence/trust with the actual success-rate); Single parallel-pass(Distribution-over primitive data-types)
Jev optimizes parallel-computing by eliminating the decode-loop!
How are mathematical calibration-formulas applied-to a specific-software/sw use-case(such-as automatic routing-thresholds)?
In a production software/sw system, applying mathematical calibration-formulas means transforming a model’s raw probabilistic confidence into a trusted operational metric.
When using a model-like Jev via RLCD, the output is a calibrated-probability vector over a structured data schema(e.g., a categorical JSON-object defining ticket-types). Here is exactly how the mathematical-calibration translates into software logic for an automatic routing-threshold system(like routing customer-support tickets or financial-transactions).
1.The Core Software/Sw-Problem: Expected Cost-Minimization
An uncalibrated model might say it is 99% confident, but it is actually only right 70% of the time. If software/sw routes tickets automatically-based on that raw 99% score, it will introduce massive-errors into the pipeline.
Because RLCD forces the model’s confidence/trust to equal its empirical accuracy 𝑃(Correct) = p, software engineers can use a Decision-Theoretic Matrix to calculate precise thresholds based-on business-costs.
Let’s define the operational-costs(𝐶)/utilities:
- 𝐶_Auto = Cost-of a correct automatic-routing(very-low, e.g., $0.05).
- 𝐶_Human = Cost-of routing to a human triage-queue(medium, e.g., $1.00).
- 𝐶_Error = Cost-of a misrouted ticket that goes to the wrong-department and causes delays(high, e.g., $5.00).
2. The Threshold-Formula Derivation
For any given ticket, the model outputs a calibrated probability p̂(p hat) for the top predicted category.
The software/Sw must-choose between two(2)-actions:
2.a) Action-1(Auto-Route): Expected-Cost = p̂ · 𝐶_Auto + (1−𝑝̂)⋅𝐶_Error
2.b) Action-2(Escalate-to Human): Expected-Cost = 𝐶_Human (since a human checking it prevents/avoid the misrouting-error cost).
To-find the mathematical routing-threshold(Question: 𝑝̂ = ? for Action-1 = Action-2.; The Answer: τ), we set the expected-costs equal-to each other to-find the break-even point:
τ⋅𝐶_Auto + (1−τ)⋅𝐶_Error = 𝐶_Human
Solving for 𝜏 (the minimum-required confidence to auto-route):
𝜏 = (𝐶_Error−𝐶_Human) / (𝐶_Error−𝐶_Auto)
Using our example-values:
𝜏 = (5.00–1.00) / (5.00–0.05) = 4.00 / 4.95 = 0.808(≈0.81) → 80.8%(≈81%)
Because the model is calibrated via RLCD, the software/sw can-safely implement a strict-rule:
If 𝑝̂ ≥ 0.81 Then auto-route Else(otherwise), route-to human(triage-queue).
3. Production-Architecture & Implementation-Loop
In code, this mathematical calibration-loop runs-as a decoupled validation-pipeline:
[Incoming Ticket] What decision is made/take when the support ticket arrives?
│
▼
┌───────────┐ Single forward pass outputs JSON schema
│ Jev-Model │ ────► with category and calibrated probability:
└───────────┘ {"category": "Billing", "confidence": 0.85}
│
▼
┌───────────┐ Is confidence/trust >= Threshold(i.e. 𝜏 ≈ 0.81)?
│ Rule Eng. │ ───────────────────────┐
└───────────┘ │
│ Yes │ No
▼ ▼
┌──────────────┐ ┌──────────────┐
│ AUTOmated │ │ HUMAN Triage │
│ Routing API │ │ Queue │
└──────────────┘ └──────────────┘Step A: Runtime Routing Guard
The application-code applies the threshold derived-from the Brier-score alignment:
THRESHOLD = 0.81 # Mathematically optimized based on business costs
def route_ticket(ticket_data):
# Jev evaluates schema probabilities in a single forward pass
prediction = jev_model.predict_schema(ticket_data)
category = prediction["category"]
confidence = prediction["confidence"] # Strictly proper probability
if confidence >= THRESHOLD:
dispatch_to_department(category, ticket_data)
log_routing_metric(action="auto", confident=True)
else:
dispatch_to_human_queue(ticket_data)
log_routing_metric(action="human", confident=False)Step B: Continuous Calibration Monitoring (The ECE-Metric)
In production, data-drift can-occur. To ensure the mathematical formulas remain valid, the software/sw logs every-decision and runs a cron-job to-calculate the Expected-Calibration-Error(𝐸𝐶𝐸).
The software/sw divides the predictions into M equally spaced confidence bins (e.g., 0.80–0.90) and ensures that within each-bin, the Accuracy closely-matches the Confidence(conf):
𝐸𝐶𝐸 = ∑ᴹₘ₌₁( |Bₘ| / N ) ⋅ |acc(Bₘ)−conf(Bₘ)|
If the system detects that the 𝐸𝐶𝐸 drifts above a specific-tolerance (e.g., 𝐸𝐶𝐸 > 0.02), it automatically-flags the system for an RLCD fine-tuning iteration or adjusts the business threshold(𝜏) upward to protect against uncalibrated drift.
Here is a complete, production-ready Python example using NumPy to calculate the Expected-Calibration-Error(𝐸𝐶𝐸).
To make it realistic, the code simulates historical routing data from a model like Jev. It groups the data into confidence-bins/intervals, computes the accuracy versus confidence for each bin, and weights them to-find the total calibration error.
import numpy as np
def calculate_ece(confidences, accuracies, num_bins=10):
"""
Calculates the Expected Calibration Error (ECE) for a set of predictions.
Parameters:
------------
confidences : np.ndarray
The calibrated probability/confidence scores assigned by the model
for the predicted class (values between 0.0 and 1.0).
accuracies : np.ndarray
The actual outcomes. 1 if the model's prediction was correct,
0 if it was incorrect.
num_bins : int
The number of equally spaced confidence intervals to use (default: 10).
Returns:
---------
float : The ECE score (closer to 0.0 means better calibration).
"""
# Ensure inputs are numpy arrays
confidences = np.array(confidences)
accuracies = np.array(accuracies)
total_samples = len(confidences)
ece = 0.0
# Create equally spaced bin boundaries (e.g., [0.0, 0.1, 0.2, ..., 1.0])
bin_boundaries = np.linspace(0, 1, num_bins + 1)
print(f"{'Bin Interval':<15} | {'Samples':<8} | {'Avg Conf':<10} | {'Avg Acc':<10} | {'Abs Diff':<10}")
print("-" * 65)
for i in range(num_bins):
bin_lower = bin_boundaries[i]
bin_upper = bin_boundaries[i + 1]
# Find indices of samples that fall into the current bin
# Includes lower bound, and upper bound (handles the edge case for 1.0)
if i == num_bins - 1:
in_bin = (confidences >= bin_lower) & (confidences <= bin_upper)
else:
in_bin = (confidences >= bin_lower) & (confidences < bin_upper)
bin_size = np.sum(in_bin)
# Skip empty bins to avoid division by zero
if bin_size > 0:
avg_confidence_in_bin = np.mean(confidences[in_bin])
avg_accuracy_in_bin = np.mean(accuracies[in_bin])
# Mathematical absolute difference for this specific bin
abs_difference = np.abs(avg_accuracy_in_bin - avg_confidence_in_bin)
# Print metrics for this bin to inspect calibration visual representation
interval_str = f"[{bin_lower:.1f}, {bin_upper:.1f})"
print(f"{interval_str:<15} | {bin_size:<8} | {avg_confidence_in_bin:.4f} | {avg_accuracy_in_bin:.4f} | {abs_difference:.4f}")
# Weight the bin error by its size relative to total samples and add to ECE
ece += (bin_size / total_samples) * abs_difference
print("-" * 65)
return ece
# ==========================================
# SIMULATION: Testing with Mock Routing Data
# ==========================================
np.random.seed(42) # For reproducible results
n_tickets = 10000
# Simulate a well-calibrated Jev-style model:
# Confidences mostly clustered in higher brackets (0.5 to 1.0)
mock_confidences = np.random.uniform(0.5, 1.0, size=n_tickets)
# Generate true outcomes where probability of being '1' (correct)
# matches the simulated confidence score (true statistical calibration)
mock_accuracies = np.random.binomial(1, mock_confidences)
# Inject a tiny bit of noise/drift to simulate a real-world scenario
drift_indices = np.random.choice(n_tickets, size=200, replace=False)
mock_accuracies[drift_indices] = 0 # Force some errors to create an ECE > 0
# Calculate ECE
ece_score = calculate_ece(mock_confidences, mock_accuracies, num_bins=5)
print(f"\nCalculated Expected Calibration Error (ECE): {ece_score:.4f} ({ece_score * 100:.2f}%)")How-do you interpret the result in production?
- Low 𝐸𝐶𝐸(≤ 2–3%): This means that your RLCD model maintains its mathematical-calibration. If your previously-calculated business-threshold is 81%, you can-be confident/trust that the tickets sent automatically will have a true controlled error rate.
- High 𝐸𝐶𝐸(≥ 5%): This indicates-that the production-data has suffered from data-drift. Even though the model claims to-have 90% confidence in an interval, its actual-accuracy in that bin has decreased(for example to 80%). This is the automatic warning-signal for engineers that the automation-threshold needs to-be artificially-increased or that the Jev-model requires a new RLCD alignment session.
Enjoy!
To-Do list for our later-research(about this actual Jev story’s subject):
— How-do we integrate this above-script with an automated alerting-system?
— How-do we modify the above-code if we want to calculate the Brier-Score(which measures calibration and predictive-quality simultaneously)?
— Also,… take a look at… how to adjust thresholds if your business-costs(𝐶_Error vs. 𝐶_Human) shift-dynamically?
That is All! Bye…Bye!






