Skip to content

Field-level confidence scores for structured AI outputs

How Chowder turns token log probabilities into field-level confidence scores

A cheerful fluffy silver tabby kitten holding a card with three field-level confidence scores.

When AI inference produces structured outputs, we can get more than just a valid JSON response. We can also measure how confident the model was in producing each field value. A predefined schema enforces field names and types during inference, while the model’s token log probabilities let us derive confidence scores for each field value.

Chowder computes these scores as part of structured inference. Its SQL infer function returns typed output alongside a matching set of confidence scores. We call this a confidence twin: it follows the structure of the output, with a confidence score in place of each scalar value.

For example, when classifying customer service conversations, Chowder returns both the structured output and its confidence twin.

Structured output
{
  "has_issue": true,
  "issue_type": "online payments",
  "tags": ["billing", "urgent"]
}
Confidence twin
{
  "has_issue": 0.90,
  "issue_type": 0.85,
  "tags": [0.94, 0.81]
}

Illustrative scores. Each value and its confidence share the same path through the JSON.

These scores give us a signal of model confidence, derived from the generated tokens for each field value. They measure token likelihood, not the probability that a value is correct. We can calibrate them for domain-specific tasks using representative labeled data.

From tokens to confidence

Language models generate text one token at a time. When token log probabilities are available, they tell us how likely the model considered each generated token, given the prompt and preceding tokens.

For example, a model’s response to “What is the capital of France?” is represented as a sequence of tokens and their log probabilities:

# Illustrative tokens and log probabilities
output = "Paris is the capital of France."

tokens = [
    "Paris", " is", " the",
    " capital", " of", " France", ".",
]

logprobs = [
    -0.126739,  # Paris
    -0.042571,  # is
    -0.013923,  # the
    -0.209305,  # capital
    -0.020642,  # of
    -0.156193,  # France
    -0.050016,  # .
]

Why field-level confidence?

Now suppose the model returns that answer inside JSON:

{
  "answer": "Paris is the capital of France."
}

The response contains both the answer and the structure around it: braces, quotes, punctuation, and the field name answer. Those structural tokens are often highly predictable, even when the answer itself is wrong. Averaging over the entire response mixes confidence in the content with confidence in its formatting.

For structured outputs, what matters is the field value. An uncertain invoice total should not look more trustworthy because the model confidently generated the surrounding JSON.

Field-level scoring aligns the calculation with the tokens that represent each value.

The token alignment problem

JSON organizes values into fields, objects, and arrays. Token boundaries do not necessarily follow that structure. A string can span several tokens, and a single token can contain both part of a value and surrounding punctuation.

For STRUCT and ARRAY outputs, Chowder parses the original JSON and identifies the exact text occupied by each scalar value, including strings, numbers, Booleans, and nulls. It then finds the smallest sequence of whole tokens needed to represent that text and uses their log probabilities to calculate the value’s confidence score.

Illustrative Paris token alignment: JSON key and outer syntax tokens are faded and excluded. The eight tokens that represent the quoted answer string are repeated in the aggregate panel, each pointing to its own log probability. Only these eight log probabilities are averaged and exponentiated to produce the field score 0.924.
Illustrative tokenization and log probabilities. Faded tokens are excluded. The aggregate uses only the eight tokens that represent the quoted answer value, each paired with its log probability. This example shows a string inside a JSON object. The opening-quote token includes a leading space; its contribution cannot be separated. The symbol ␣ marks a space.

Chowder tracks each value by its position in the JSON structure. This works for nested objects and arrays, even when the same value appears more than once.

If Chowder cannot safely align the tokens with their values, it preserves valid output and returns null confidence with an error explaining the failure.

Calculating field confidence

Chowder combines the probabilities of the tokens that represent each value using their geometric mean. We can calculate this by exponentiating the mean of their log probabilities:

Field confidence

From token log probabilities

exp(1n∑i=1nlogpi)

Equivalent geometric mean

(∏i=1npi)1n

piProbability of token i

nNumber of tokens in the value

We use the geometric mean for three reasons:

  1. Probabilities multiply, not add. The probability of a token sequence is the product of its conditional token probabilities. The geometric mean preserves this multiplicative structure.
  2. Penalizes low-confidence tokens. The arithmetic mean can hide very low probabilities behind high ones. The geometric mean is more sensitive to weak links.
  3. Normalizes for sequence length. Taking the nth root makes scores more comparable across sequences of different lengths than the raw product would.

Querying field confidence in Chowder

To use field-level confidence in Chowder, we declare the output type in an infer function call. Chowder returns the inferred values in output and their matching scores in confidence.

For example, we can classify a customer service message and query the predicted issue type alongside its confidence score:

WITH messages AS (
    SELECT 'My online payment was charged twice. Please fix this urgently.' AS content
), classified AS (
    SELECT infer(
        content,
        'Identify whether the customer has an issue, its type, and short descriptive tags.',
        STRUCT<
            has_issue BOOLEAN,
            issue_type VARCHAR,
            tags ARRAY<VARCHAR>
        >
    ) AS inference
    FROM messages
)
SELECT
    inference.output.issue_type AS issue_type,
    inference.confidence.issue_type AS issue_type_confidence
FROM classified
WHERE inference.output.has_issue
  AND inference.confidence.issue_type >= 0.7;

The query returns the predicted issue type and its confidence score. It keeps rows where has_issue is true and the confidence score for issue_type is at least 0.7. Chowder also returns confidence scores for has_issue and each element in tags.

The 0.7 threshold is illustrative. The example can return no rows if the predicted issue type scores below that threshold.

We can use the same approach when querying document pages or customer service conversations. The confidence scores are available directly in SQL, so we can select them alongside the inferred values, filter on individual fields, or group results to identify patterns in confidence.

Chowder supports confidence scores for every output type accepted by infer. If Chowder produces a valid output but can’t compute its confidence score, it keeps the output, sets confidence to NULL, and explains why in error.

For example, we can classify the same message using an ENUM:

SELECT infer(
    'My online payment was charged twice. Please fix this urgently.',
    'Classify the customer issue.',
    ENUM('billing', 'technical', 'other')
) AS inference;

Here, inference.output contains the predicted label and inference.confidence contains one score. The labels in a standalone ENUM must be non-empty strings.

For a standalone ENUM, Chowder uses the probabilities of the tokens that contain the label itself. Tokens containing only surrounding quotes or whitespace are excluded. If a token contains both part of the label and a quote, it still contributes its whole probability. Whitespace and escaped quotes inside the label count as part of the label.

Numeric outputs and Boolean inference over images also return a single score from their generated value tokens. For Boolean inference over text, Chowder uses its specialized classifier and returns the predicted label’s probability as confidence.

With infer(..., ..., TEXT), the output is free-form text. Chowder returns one confidence score for the whole answer: the geometric mean of its token probabilities. There is no surrounding JSON structure to separate from the answer. A correct answer can still have a relatively low score, especially when there are many reasonable ways to phrase it.

Turning scores into decisions

Field-level confidence lets us decide what to do with each inferred value. We can accept a predicted category from one field while sending an uncertain amount from another field for review. Chowder returns the values and their scores as typed data, so we can inspect both and filter by confidence in the same SQL query. We can use representative labeled data to calibrate the scores and choose thresholds for the task.

Get started with Chowder in the docs and run your first multimodal SQL query.

Glancelot page routing