Multi-modal AI in Web Apps — AI Web

A multi-modal model processes or produces more than one kind of data: text, images, audio, PDFs. Photos go through your server as base64. At OpenAI, the shape…

A customer walks up to the Bookshop till and holds out a phone: "Do you have this one?" The screen shows a photo of a book cover. The cashier looks at the photo, recognises the book, then checks the stock on the computer. The Bookshop assistant can do the same with a multi-modal model: a model that can process or produce more than one kind of data. That means text, images and audio, even documents such as PDFs. In this story, the vision model (a model that can read images) is the cashier's eyes: the model only recognises the book. Stock and prices still come from the database: the cashier's computer.

What goes in and what comes out isn't always the same kind of data:

Images in: requests to OpenAI and Claude

The flow is the same as for a text question. The browser sends the photo to your server (POST /api/assistant/photo). Then the server calls the provider, because the API key lives only on the server. The photo travels as base64: the file's bytes written as text, about 4/3 of the original size. The request shape differs per provider (checked in each one's documentation, 11 October 2026). The code below sends the same question to OpenAI, then to Claude. b64 holds the photo as base64:

// OpenAI, Responses API: input_image with a full data URL
const r = await openai.responses.create({
  model: "gpt-6-luna",
  input: [{ role: "user", content: [
    { type: "input_text", text: "What is the title on this cover? Answer with the title only." },
    { type: "input_image", image_url: `data:image/jpeg;base64,${b64}`, detail: "auto" },
  ] }],
});

// Claude, Messages API: an image block, base64 WITHOUT the "data:" prefix
const m = await anthropic.messages.create({
  model: "claude-haiku-5-5",
  max_tokens: 100,
  messages: [{ role: "user", content: [
    { type: "image", source: { type: "base64", media_type: "image/jpeg", data: b64 } },
    { type: "text", text: "What is the title on this cover? Answer with the title only." },
  ] }],
});

Images become tokens: count first

Images are billed as input tokens. How to count them is written in the documentation:

Before you look at the table, guess. A customer's phone photo of a cover is 1200 × 1600 pixels, and the Bookshop assistant's whole text prompt is about 1,200 tokens. On Claude, how many tokens is the photo: about 300, about 2,500, or about 25,000?

Photo size OpenAI (patches × 1.2) Claude
1200 × 1600 (straight from the phone) 38 × 50 = 1,900 patches → 2,280 tokens 43 × 58 = 2,494 tokens
768 × 1024 24 × 32 = 768 patches → 922 tokens 28 × 37 = 1,036 tokens
576 × 768 18 × 24 = 432 patches → 519 tokens 21 × 28 = 588 tokens

The original photo on Claude is 2,494 tokens, twice the whole text prompt. claude-haiku-5-5 and gpt-6-luna have the same input price, $0.10 per 1 million tokens, so 1,000 original photos on Claude cost $0.25, and only $0.06 once shrunk to 576 × 768. On gpt-6-astra ($10 per 1 million tokens), using the token counts in the OpenAI column, 1,000 original photos cost $22.80, and shrunk ones $5.19. To read the title on a cover, a long side of 768 pixels is plenty.

Shrink in the browser before uploading

In the shrinkPhoto function below, longSide is the longest side you want (768 by default):

async function shrinkPhoto(file, longSide = 768) {
  const bitmap = await createImageBitmap(file); // the EXIF orientation is already applied
  const scale = Math.min(1, longSide / Math.max(bitmap.width, bitmap.height));
  const canvas = document.createElement("canvas");
  canvas.width = Math.round(bitmap.width * scale);  // 1200 -> 576
  canvas.height = Math.round(bitmap.height * scale); // 1600 -> 768
  canvas.getContext("2d").drawImage(bitmap, 0, 0, canvas.width, canvas.height);
  return new Promise((done) => canvas.toBlob(done, "image/jpeg", 0.85));
}

Photos carry a location: strip EXIF

EXIF is a metadata block (data about the photo) inside a JPEG file. A JPEG file is made of several segments, and each segment starts with a two-byte marker. EXIF sits in the APP1 segment, with the byte marker FF E1. It holds the camera make, the time, and often the GPS coordinates where the photo was taken. So a cover photographed at home carries the location of the customer's home. The Lab builds a real JPEG byte by byte: 362 bytes, with a 182-byte EXIF block. That block holds GPS 6°12'37.2" S, 106°50'56.4" E (-6.210333, 106.849) and the time 2026:10:11 08:12:07.

The removeExif function below reads the file one segment at a time and copies every segment except APP1:

function removeExif(bytes) {
  const parts = [bytes.subarray(0, 2)];        // SOI: the start of a JPEG file
  let i = 2;
  while (i < bytes.length) {
    const marker = bytes[i + 1];
    if (marker === 0xda) { parts.push(bytes.subarray(i)); break; } // SOS: image data to the end
    const length = (bytes[i + 2] << 8) | bytes[i + 3];
    if (marker !== 0xe1) parts.push(bytes.subarray(i, i + 2 + length)); // APP1 (EXIF) is skipped
    i += 2 + length;
  }
  const result = new Uint8Array(parts.reduce((n, p) => n + p.length, 0));
  let at = 0;
  for (const p of parts) { result.set(p, at); at += p.length; }
  return result;
}

In the Lab, the 362-byte JPEG becomes 180 bytes after removeExif: the GPS is gone, and the browser still shows it. The model doesn't read metadata (OpenAI's and Claude's docs both say so). But the file passes through your server, sits in storage, and can be shown again with its GPS, in a review for instance. A new JPEG from toBlob is made only from the canvas pixels, so the original file's EXIF isn't copied. Still, not every upload goes through a canvas, so the server strips it too, for example by saving the image again as a new JPEG file.

Speech: from and to text

The code below shows a record button in a browser that has no SpeechRecognition (under either name), then reads the stock aloud:

const Recognition = globalThis.SpeechRecognition ?? globalThis.webkitSpeechRecognition;
if (!Recognition) showRecordButton(); // record with MediaRecorder, send to /api/transcribe
speechSynthesis.speak(Object.assign(new SpeechSynthesisUtterance("The Rainbow Troops: 12 in stock."), { lang: "en-US" }));

PDF documents

A supplier sends a 12-page price list as a PDF:

Ask for the PDF's content as JSON with Structured Outputs (the AI-Powered Forms & Content lesson). Then the Bookshop's admin checks it before the catalogue is updated.

Alt text: a draft from the model, a decision by a person

A vision model can draft alt text from a product photo: "An orange book cover with a dark title band". The model can misread the title, doesn't know the page's context, and doesn't know which images are only decoration (decoration gets alt=""). The admin reads the draft, corrects it to "Cover of the novel The Rainbow Troops by Andrea Hirata", and only then saves it. The same goes for speech transcripts (the text that speech-to-text produces): show them as text that can still be corrected.

⚠️ Raw photos, mixed-up base64 and GPS sent with the photo

TL;DR: A multi-modal model processes or produces more than one kind of data: text, images, audio, PDFs. Photos go through your server as base64. At OpenAI, the shape is input_image with a data URL (data:image/jpeg;base64,...) in image_url (detail level: low, high, original, auto). At Claude, it's an image block with a base64 source without the data: prefix, plus media_type (the file type). Images are counted in patches (squares of pixels). OpenAI: the number of 32 px patches times 1.2 (an estimate for gpt-6-luna, check usage). Claude: ⌈width/28⌉ × ⌈height/28⌉ (rounded up). A 1200 × 1600 photo is 2,280 tokens on OpenAI and 2,494 on Claude. Shrunk to 576 × 768, it's 519 and 588. Shrink in the browser with a canvas (a drawing area) before uploading. Strip EXIF (the photo's metadata in the APP1 segment, often with GPS) on the client and the server. Speech: gpt-transcribe turns speech into text ($0.0045 per minute, language hint languages: ["en"]), gpt-4o-mini-tts turns text into speech (shuts down 6 January 2027; tell listeners the voice is AI). The browser's Web Speech API is free: speechSynthesis (reading aloud) is everywhere, SpeechRecognition (listening) isn't in every browser yet. PDFs: input_file at OpenAI or a document block at Claude, text plus an image of every page. The model reads, the server decides: stock from SQL, and a person checks alt text and extracted data.

⚡ Multi-modal: more than one kind of data

Meaning: A multi-modal model can process or produce more than one kind of data: text, images, audio, even PDFs. Picture a cashier who looks at a cover photo on a customer's phone, then checks the stock on the computer. The vision model (a model that reads images) is the cashier's eyes: it only reads the title on the cover. Stock and price still come from the database: the cashier's computer.

⚡ Images count as tokens too

Write:

const scale = Math.min(1, longSide / Math.max(bitmap.width, bitmap.height));
Guess first: Claude counts images in patches (squares) of 28 × 28 pixels. Tokens = ⌈width / 28⌉ × ⌈height / 28⌉, each side rounded up. A 1200 × 1600 phone photo is 43 × 58 = 2,494 tokens. Shrunk until its long side is 768, how many tokens is it?

588 tokens. The photo becomes 576 × 768, or 21 × 28 patches.

Meaning: bitmap is the photo the browser has read, and longSide the longest side you want (768). scale (what the photo's size is multiplied by) is at most 1, so small photos aren't enlarged. The original photo costs twice the Bookshop assistant's whole text prompt (about 1,200 tokens), and 768 pixels is plenty to read a title.

⚡ Photos carry a location

Meaning: EXIF is a metadata block (data about the photo) in the APP1 segment (part) of a JPEG file. It often includes the GPS coordinates where the photo was taken, such as the customer's home. The removeExif function drops the APP1 segment: the Lab's file goes down from 362 to 180 bytes, the GPS is gone, and the browser still shows it.

What you will learn