> ## Documentation Index
> Fetch the complete documentation index at: https://docs.chowder.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Confidence scores

> See how Chowder returns field-level confidence scores for structured AI responses.

When `infer` returns a typed AI response, such as a `STRUCT`, `ARRAY`, or `ENUM`, Chowder also measures how confident the model was in each value. The output schema fixes the field names and types. The model's token log probabilities give a confidence score for each field value.

Chowder returns these field-level confidence scores in the AI envelope's `confidence` field, as a **confidence twin**. The twin has the same structure as `output`, with a score in place of each scalar value.

For example, when you classify a customer service message, Chowder returns both the structured AI response and its confidence twin:

```json Structured output theme={"theme":"css-variables","languages":{"custom":["/languages/chowder-sql.json"]}}
{
  "has_issue": true,
  "issue_type": "online payments",
  "tags": ["billing", "urgent"]
}
```

```json Confidence twin theme={"theme":"css-variables","languages":{"custom":["/languages/chowder-sql.json"]}}
{
  "has_issue": 0.90,
  "issue_type": 0.85,
  "tags": [0.94, 0.81]
}
```

These scores are illustrative. Each value and its score share the same path through the structure. For example, `confidence.issue_type` scores `output.issue_type`.

Each score is a `FLOAT` between 0 and 1. Like predicted probabilities in traditional ML classification, scores are a signal of model confidence, not a guarantee of correctness.

## Why field-level confidence?

A structured response contains values and the structure around them: braces, quotes, punctuation, and field names. The model usually predicts these structural tokens with high confidence, even when a value is wrong. A single score for the whole response mixes confidence in the content with confidence in its formatting.

What matters is each value. An uncertain invoice total should not look more trustworthy because the model confidently generated the surrounding JSON. Field-level scores use only the tokens that represent each value.

## How Chowder calculates it

Language models generate text one token at a time. Each token has a log probability: how likely the model considered that token, given the prompt and the tokens before it.

Token boundaries don't follow the JSON structure. A string can span several tokens, and one token can contain part of a value and the punctuation around it. To score a value, Chowder:

1. Parses the generated JSON and finds the exact text of each scalar value: strings (including their quotes), numbers, Booleans, and nulls.
2. Finds the smallest run of whole tokens that contains that text.
3. Combines those tokens' probabilities with their geometric mean.

The geometric mean is the exponential of the mean log probability:

```text theme={"theme":"css-variables","languages":{"custom":["/languages/chowder-sql.json"]}}
confidence = exp((log p₁ + log p₂ + … + log pₙ) / n)
```

Here, `pᵢ` is the probability of token `i`, and `n` is the number of tokens in the value. Chowder uses the geometric mean because:

* **Probabilities multiply.** The probability of a token sequence is the product of its token probabilities. The geometric mean keeps that structure.
* **It penalizes weak tokens.** An arithmetic mean can hide a very low probability behind high ones. The geometric mean is more sensitive to it.
* **It normalizes for length.** Taking the `n`th root makes scores comparable across values with different numbers of tokens.

Chowder tracks each value by its position in the structure, so nested objects, arrays, and repeated values each get their own score.

## Query confidence in SQL

Declare the output schema in `infer`. Chowder returns the values in `output` and their scores in `confidence`:

```chowder-sql theme={"theme":"css-variables","languages":{"custom":["/languages/chowder-sql.json"]}}
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 AS output,
    inference.confidence AS confidence
FROM classified;
```

| output | confidence |
| - | - |
| \{&#xA;    "has\_issue": true,&#xA;    "issue\_type": "duplicate charge",&#xA;    "tags": \[&#xA;        "payment",&#xA;        "urgent",&#xA;        "refund"&#xA;    ]&#xA;} | \{&#xA;    "has\_issue": 0.9999303817749023,&#xA;    "issue\_type": 0.7293025851249695,&#xA;    "tags": \[&#xA;        0.8518438339233398,&#xA;        0.6845661401748657,&#xA;        0.6117520332336426&#xA;    ]&#xA;} |

Scores are regular SQL values. Select them alongside the output, filter on individual fields, or group by them to find patterns. For example, replace the final `SELECT` to keep only rows where the model is confident in the issue type:

```chowder-sql theme={"theme":"css-variables","languages":{"custom":["/languages/chowder-sql.json"]}}
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 `0.7` threshold is illustrative. The example can return no rows if the predicted issue type scores below that threshold. See [Choose thresholds](#choose-thresholds).

## Confidence by output type

How Chowder computes confidence depends on the input and output schema:

| Input and output schema | `confidence` |
| - | - |
| `STRUCT` or `ARRAY` | A confidence twin with the same structure as `output` |
| `ENUM` | One score for the selected label. It uses the label's tokens, not the quotes around it. |
| `INT`, `BIGINT`, `FLOAT`, or `DOUBLE` | One field-level score |
| Image input with `BOOL` | One field-level score |
| Text input with `BOOL` | The classifier's probability for the predicted label. See [Boolean classification](https://docs.chowder.ai/reference/column-functions/infer.md#boolean-classification). |
| `TEXT` | One score for the whole response, from all of its tokens |

A `TEXT` score measures how likely the model found its exact wording, not whether the response is correct. Free-form responses can be phrased many ways, so a good response can still score low. For values you want to filter or threshold, use a typed schema such as `STRUCT`, `ARRAY`, or `ENUM`.

## When confidence is null

`confidence` is null when:

* The row failed, so `output` is null and `error` explains why.
* Chowder can't safely align tokens with values. It keeps the valid `output` and explains the failure in `error`. Chowder doesn't cache the result, so a later query retries it.

## Choose thresholds

Different fields can lead to different decisions. You can accept a predicted category while sending an uncertain invoice amount for review. Field-level scores let you make those decisions separately, in the same SQL query as the outputs.

Scores depend on the output schema. The same value can score differently as a standalone value and as a `STRUCT` field, so choose thresholds for each schema. Labels that share words, such as `'very positive'` and `'very negative'`, can score higher, because the model is confident in the shared tokens.

A model can still give an incorrect AI response with high confidence. Choose thresholds using representative labeled data, and revisit them when the model, prompt, or output schema changes.

For the full walkthrough, read [Field-level confidence scores for structured AI outputs](https://chowder.ai/blog/field-level-confidence-scoring) on the Chowder blog.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.