sysnote
Analysis

Inside Cloudflare's open Clairic runtime: Python adds the receipts, the model spells PO numbers letter by letter

Cloudflare released its two extraction models, Clairic and Clairic-flash, as open weights on November 18, in Hugging Face repositories Cloudflare/clairic (27B, built on Qwen3.8-27B) and Cloudflare/clairic-flash (9B, built on Qwen3.5-9B), both Apache-2.0. The announcement by Alex Reneau, Michelle Chen and Kevin Flansburg reached the Hacker News front page today, the day Cloudflare's promotional price for the 27B model ends. The weights are only part of the release. Each repository also ships a clairic_runtime package: 57 Python modules, 17,125 lines, not counting the vendored Flash Linear Attention kernels. Reading it shows that for some kinds of fields the code decides how the model is asked, and for receipt totals on Clairic-flash it computes the answer itself.

What the blog describes

Clairic takes a document (text, up to four images or PDF pages) and a schema of typed fields, and returns one answer per field with a confidence score, a probability that the answer is null, and character spans pointing back into the text. The blog says the new version uses "parallel slot autoregressive generation": each field generates its own tokens in its own slot, and all the fields of a request advance together in one batch. For estimates, forecasts, durations, dates and "explicit arithmetic", Cloudflare says it "trained special heads".

On quality, the blog says there are no public extraction benchmarks and offers internal evals "as directional guidance since they are self-reported". The most striking figure is that image reading "has improved from 207 errors to 0 on our internal benchmarks". Pricing counts input tokens only: $0.24 per million for Clairic after today, $0.10 for Clairic-flash. The Hugging Face README describes the code as "local Python request helpers".

Receipts: the model votes, Python adds

Ask Clairic-flash for a number on a text-only request, with instructions that begin with "Sum of" or "The sum of" and mention a receipt and a total, and a separate path starts. The trigger is in receipt_totals.py:

    return bool(
        _RECEIPT.search(instructions)
        and _REQUESTED_TOTAL.search(instructions)
        and (not _COMPOUND.search(instructions))
        and (not _ROUND_EACH.search(instructions))
    )

_COMPOUND is a list of words that disqualify the request: average, minus, discount, convert, percent, "net of" and about twenty more words and symbols. If the instructions pass, the runtime cuts the document into blocks with regular expressions, finds each block's total label and printed amount, and sends the model one question per block: is this receipt one of the requested operands? The answer is constrained by a grammar to true, false or null, at most 8 tokens. The prompt carries the whole document every time and includes the sentences "Do not calculate the sum." and "No expected values are supplied." Python then adds the selected amounts as Decimal values and, if the result differs from the model's own answer, replaces it. The replaced answer is labelled source_receipt_block_sum_v1.

This only applies to Clairic-flash: backend.py enables it when type(self) is ClairicFlashModel, which excludes the 27B subclass. On the 27B the model's own number stands.

I read the code in Cloudflare/clairic at its latest commit, 2c161be5 from November 18 (the Clairic-flash repository ships identical runtime files), and ran the request-preparation part locally, with PyTorch and the other model libraries replaced by stubs, so no model was involved. "Sum of the totals of all receipts." takes the receipt path; so does "The sum of the receipt totals." "Total of all receipts combined." doesn't, and neither does "Sum of the receipt totals, minus the coffee." For a three-receipt sample, with every block marked as included, the runtime's validator returned 697.36, which is the correct sum.

Letting the model choose and the code add seems sensible to me: addition in Decimal is exact, and choosing which amounts belong in the sum is the part that needs reading. Two details are less tidy.

The cost grows with the square of the number of receipts. Every block question repeats the full document. Counted with the Clairic-flash tokenizer and its chat template, my synthetic samples came out roughly like this:

Receipts Document tokens Tokens in the block questions
3 96 1,262
10 314 6,402
30 1,011 40,316
60 2,041 142,566

The runtime adds these prompts to usage.input_tokens, and Cloudflare bills input tokens. At $0.10 per million, 142,566 tokens cost about 1.4 cents, so this isn't ruinous. It is still 70 times the document for one field. The runtime shares the common prompt prefix between the questions, so the compute cost should be lower than the token count suggests. The usage figure the runtime reports isn't.

The path gives up after 32 receipts. prepare_task accepts up to 128 blocks (MAX_BLOCKS = 128), but the summing code in source_sums.py has MAX_OPERANDS = 32. With 33 receipts all marked as included, validate_result returned invalid_operand_count. The questions have been asked by then and their tokens counted. An invalid result is skipped, an exception in this path ends in a bare except Exception: pass, and the response keeps the model's own number. As far as I can see in the code, the only trace is that the answer keeps its ordinary decoder label instead of source_receipt_block_sum_v1.

Purchase-order references, letter by letter

The second path runs on both models. A text field gets it when the request includes an image or PDF, the field is named exactly reference or value (case doesn't matter), its instructions contain a word like "exact", "printed" or "read" and the phrase "purchase order reference", and no other kind of reference (invoice, receipt, tracking, account) is asked for. The runtime then appends a role note and puts this in front of the instructions:

Spell the printed purchase order reference with one space between each individual character. Preserve the actual characters; do not normalize letters and digits.

The model is expected to write something like P O - 4 4 1 7, and identifier_spelling.py keeps every other character. On the 27B, for one to three images, the runtime also adds a copy of the page stretched to twice its width (27b-po-all-content-horizontal2-supplement-v2), but only when the model is serving one request at a time, which is the default in the Hugging Face loader.

The trigger is narrow. With the same instruction, "Read the printed purchase order reference exactly as shown.", a field called reference or value gets the spelling path, while po_reference, po_number and purchase_order go down the ordinary path. "Copy the printed PO reference." and "The purchase order number exactly as printed." miss it too, even with the field named reference.

Smaller paths, different per model

The two model classes switch more of these on and off with flags. Clairic-flash, for example, crops and enlarges the first printed lines of a single-image document when a field asks for a seller, merchant, vendor or customer name (image_party_names.py). The 27B has that flag off and LITERAL_IMAGE_PAYMENT_ROWS on instead. The file list continues: receipt_hybrid_inventory.py, row_omission.py, image_financial_lines.py, printed_temporal.py. Triggers this specific look to me like fixes for observed failure cases. Cloudflare doesn't say which cases they came from, or which part of the 207-to-0 improvement comes from the weights and which from this code.

What this means if you use the weights

If you load Cloudflare/clairic through its own load_release_model, you get all of this. If you put the safetensors into a generic serving stack, you get the fine-tuned Qwen weights and none of the routines above. Anyone comparing Clairic with other extraction models should say which of the two they ran. Inside the official runtime, field names and instruction wording choose the pipeline: renaming reference to po_number or writing "total of" instead of "sum of" sends the request elsewhere.

I couldn't check what runs behind @cf/cloudflare/clairic and @cf/cloudflare/clairic-flash on Workers AI. The hosted documentation says every pass includes the full state and is billed, and that answers carry "decoder metadata", which fits the same design, but it doesn't mention receipt questions or the 32-receipt limit. I haven't run either model, so the token counts above come from the runtime's prompt builder and the tokenizer, not from a billed request.

Comments

No comments yet. Be the first.

Write a comment

Comments are published after review.