Skip to Content

AI: models and OCR

Ask a hosted language model and read text in images from a backend, with the project's key, limits and usage handled by Floor.

This page shows how a backend asks a language model and reads text in an image, and how a project's key, monthly limits and usage work. You need a backend (see Your first backend).

Both calls go through Floor, not straight to a provider. Floor picks the key (the project's own, or Floor's), counts the request against the project's limits, and records it with its tokens, time and caller. Your backend needs no provider key and no outbound entry for it.

1. Ask a model

A guard asks why a gate refused a car. The backend gathers the facts and asks for a one-line answer:

// api/visits/[id]/explain.rs  →  GET /api/visits/{id}/explain
use floor::prelude::*;

#[derive(Serialize, Type)]
pub struct Explanation {
    text: String,
}

#[get(can = "visits:read", timeout = "2m")]
pub async fn get(cx: Cx, Path(id): Path<i64>) -> Result<Json<Explanation>> {
    let (plate, action): (String, String) = sqlx::query_as(
        "select v.plate, e.action from visits v join edge_events e on e.visit_id = v.id
          where v.id = $1 and v.organization = $2",
    )
    .bind(id)
    .bind(cx.org()?)
    .fetch_optional(cx.db())
    .await?
    .or_not_found()?;

    let answer = cx
        .chat(
            Chat::new(format!("Plate {plate} was {action} at the gate. Why, in one sentence?"))
                .system("You explain gate decisions to security guards. Be brief and plain.")
                .max_tokens(200),
        )
        .await?;
    Ok(Json(Explanation { text: answer.text }))
}

A model can take longer than a route's default 60 seconds, so give such routes a timeout, or ask from a job.

Chat::
new(prompt)starts with one user message
.user(s), .assistant(s)more turns, in order
.system(s)instructions
.model(m)the model; Floor's default when left out
.max_tokens(n)the answer's length limit; default 4096, at most 32000

The answer (ChatAnswer) has text, model (the model that answered) and stop_reason.

Effort. Claude models take an effort, low, medium, high, xhigh or max, which trades depth for tokens. Chat has no builder for it; set the field:

let mut chat = Chat::new("Summarise yesterday's gate faults for the site manager.");
chat.effort = Some("low".into());
let answer = cx.chat(chat).await?;

2. Read text in an image

let ocr = cx.ocr(jpeg_bytes).await?;
// ocr.text: lines joined with newlines, e.g. "LEB\n1234"
for line in &ocr.lines {
    // line.text, line.confidence (0 to 1), line.bbox: [x0, y0, x1, y1] in pixels
}
  • Images. PNG, JPEG, WebP or BMP, up to 16 MB.
  • What it reads. Latin letters, digits and punctuation: plates, labels, meters and notices photographed straight on. It doesn't straighten rotated text and doesn't read Urdu.
  • Confidence. Lines below 0.5 are dropped.
  • Speed. About a tenth of a second for a plate in Floor's tests.

OCR runs in Floor's own OCR service (PaddleOCR models), not at a model provider.

Models

  • claude-opus-5-5 is Floor's default. Any claude-… model goes to Anthropic.
  • openai/<model> goes to the OpenAI-compatible endpoint Floor is set up with.
  • Fallbacks. When a Claude model declines a request, Anthropic may answer with another model; answer.model names the one that did.

Floor answers in one piece: streaming replies, tools and structured output aren't built yet.

The project's key

By default, requests use Floor's key. A project with its own provider account sets it as a secret, and its requests then use and bill that key:

floor secrets set sac ANTHROPIC_API_KEY          # reads the value from stdin
floor secrets set sac OPENAI_API_KEY

With neither key, the request is refused before anything is sent.

Limits and usage

Operators set monthly limits per project, in tokens and in images:

floor ai limits sac --tokens 2000000 --images 20000
floor ai usage sac                       # the last 30 days by model, and this month against the limits
floor ai status                          # providers, the default model, the OCR service, the last day
  • Months are calendar months in UTC.
  • At a limit, Floor refuses further requests until the month turns.
  • What counts. Every request that reached a provider or the OCR service is recorded, with its tokens, images, time, outcome and the backend that asked. Requests refused earlier (a bad model, no key) aren't counted.

When a call fails

cx.chat and cx.ocr return an error when Floor refuses or the provider fails: a limit reached, the provider rate limiting, a bad request, or the provider unreachable. Returned from a route with ?, it answers 502. In a job, an Err fails the job and its queue retries it, which suits work like summaries that can wait.

From a terminal

floor ai ask sac "Summarise yesterday's gate faults" --effort low
floor ai ocr sac gate2-frame.jpg

Testing

floor::testing::TestApp's fake Floor answers chat and ocr with what you set: t.answer_chat(text) and t.set_ocr_text(text). See Testing.

On this page