Multimodal AI and Real-Time Systems
A multimodal system handles more than text: images, scanned pages, PDFs, audio, and video. Each modality has its own failure modes, costs, and privacy issues. Multimodal input is valuable when the information truly lives in the image or the recording. It adds complexity when a text source would have done the job.
The same principle applies throughout this handbook. Use the cheapest reliable path first, and add a model step only where it earns its place.
Modalities and capabilities
A modality is a type of content: text, image, document, audio, or video. A capability is something a system does with a modality. Keep the two apart when you design:
| Capability | Input | Output | Example |
|---|---|---|---|
| Visual understanding | Image or PDF page | Text, structured data | Reading a nameplate, answering a question about a chart |
| OCR | Scanned image | Text | Digitizing a paper form |
| Speech recognition (STT) | Audio | Text | Transcribing a meeting |
| Speech synthesis (TTS) | Text | Audio | Reading an answer aloud |
| Video understanding | Frames, audio, timestamps | Text, events | Summarizing a training recording |
| Image generation | Text | Image | Creating a diagram |
Check each capability against the current documentation of the provider you use. Support differs by model and changes over time. Do not assume that one model covers every row.
Claude and multimodal input
Claude models accept images and PDFs in the Messages API. Images and PDF documents are sent as content blocks alongside text. The structure below follows the Messages API. Check the current field names for the file types and sizes you use.
import base64
from pathlib import Path
import anthropic
client = anthropic.Anthropic()
MODEL = "claude-sonnet-5-5" # check the models page before shipping
def image_block(path: Path, media_type: str) -> dict:
data = base64.standard_b64encode(path.read_bytes()).decode("ascii")
return {
"type": "image",
"source": {"type": "base64", "media_type": media_type, "data": data},
}
def ask_about_image(path: Path, question: str) -> str:
response = client.messages.create(
model=MODEL,
max_tokens=600,
messages=[{
"role": "user",
"content": [
image_block(path, "image/png"),
{"type": "text", "text": question},
],
}],
)
if response.stop_reason == "max_tokens":
raise RuntimeError("answer truncated")
return "".join(b.text for b in response.content if b.type == "text")Put the image before the question, and refer to it precisely in the question. "Which value is in the top-right box of the table?" gets a better answer than "What does this say?"
Limits to plan for:
- Image size and count per request, and how resolution affects cost and detail. Check the current limits.
- Visual reasoning errors on small text, rotated scans, dense tables, charts, counting, and spatial relations.
- The model can describe something plausible that is not actually visible.
Treat every image and every PDF as untrusted input. A screenshot or a scanned page can contain text that tries to instruct the model (Chapter 13).
Documents, citations, and code execution
Three API features turn a document from a file you read into an input the model can use: PDF support, citations, and code execution. Each one has its own limits and costs. Check the current values before you design around them.
PDFs as document blocks
Send a PDF as a document content block. The API converts each page to an image and extracts its text, then gives the model both. This is why charts, diagrams, and tables are visible to the model. It is also why a PDF costs more than its text.
import base64
from pathlib import Path
import anthropic
client = anthropic.Anthropic()
MODEL = "claude-sonnet-5-5" # check the models page before shipping
def pdf_document(path: Path, title: str) -> dict:
data = base64.standard_b64encode(path.read_bytes()).decode("ascii")
return {
"type": "document",
"title": title,
"source": {"type": "base64", "media_type": "application/pdf", "data": data},
}
response = client.messages.create(
model=MODEL,
max_tokens=800,
messages=[{
"role": "user",
"content": [
pdf_document(Path("q3-report.pdf"), "Q3 report"),
{"type": "text", "text": "Which region had the largest revenue decline? Give the figure."},
],
}],
)Limits to plan for (verify before you ship):
- Request size: 128 MB on the first-party API. The limit varies by platform.
- Pages: 600 per request, or 100 when the context window is under 1M tokens.
- Format: a standard PDF. Password-protected or encrypted files are not supported.
- Density: pages with small fonts, many tables, or heavy graphics can fill the context window before you reach the page limit.
Each page typically uses 1,500 to 3,000 text tokens, plus the image tokens for the page. Use token counting to estimate a specific file before you process a batch. For large or reused files, upload through the Files API and reference the file_id. That keeps request payloads small and lets later requests reuse the file.
Citations
Enable citations on a document block, and the response carries pointers back to the source. For plain text, the pointer is a character range (char_location). For PDFs, it is a page range (page_location), with a 1-indexed start page and an exclusive end page.
document = {
"type": "document",
"title": "Q3 report",
"source": {"type": "base64", "media_type": "application/pdf", "data": data},
"citations": {"enabled": True},
}
for block in response.content:
if block.type == "text" and block.citations:
for c in block.citations:
print(c.cited_text, c.document_title, getattr(c, "start_page_number", None))Things to know:
cited_textdoes not count toward output tokens. When you send it back in later turns, it does not count toward input tokens either.- Citations cannot be combined with structured outputs. A request with both returns a 400 error. Choose one per request, or split the work into two calls (Chapter 4).
- A citation is a pointer to a real passage. It is not proof that the passage supports the claim. Check the claim with an evaluation (Chapter 16).
Code execution
The code execution tool lets Claude run Python, and Bash for file operations, in a sandbox. Use it when the answer needs computation, not only reading: statistics, tables, charts, or format conversion.
response = client.messages.create(
model=MODEL,
max_tokens=4096,
tools=[{"type": "code_execution_20250825", "name": "code_execution"}],
messages=[{
"role": "user",
"content": "Compute the mean and standard deviation of 1 to 10 with the code tool.",
}],
)
for block in response.content:
if block.type == "text":
print(block.text)
elif block.type == "server_tool_use":
print("ran:", block.name) # for example, bash_code_executionSandbox facts (verify before you ship):
- No internet access and no package installation at runtime. Only the pre-installed libraries are available, for example pandas, numpy, matplotlib, openpyxl, and pypdf.
- Resources: 5 GiB of RAM, 5 GiB of disk, 1 CPU, and Python 3.11.
- Files: access is limited to the workspace. Files written to the output directory come back with file IDs. Download them through the Files API.
- Containers: each request gets a new container unless you pass a container ID back. Containers expire 30 days after creation, and an expired container cannot be reused.
- Data: container data is retained for up to 30 days. The feature is not eligible for Zero Data Retention or HIPAA. Check with compliance before you use it on regulated data.
- Billing: if the request includes the newer web search or web fetch tools, there is no charge beyond token costs. Otherwise, execution time is billed per container, with a 5-minute minimum per execution, 1,550 free hours per organization per month, and a price per hour after that. Check the price page before you estimate cost.
The sandbox protects your systems. It does not check the answer. Validate the numbers that matter, as you would any model output.
| Feature | Use it when | Avoid it when |
|---|---|---|
| PDF document blocks | The PDF has charts, tables, scans, or a visual layout | The PDF has a clean text layer and the text is enough. Extract the text instead |
| Citations | Users must verify where an answer came from | The output must follow a strict schema (structured outputs conflict with citations) |
| Code execution | The answer needs computation or a generated file | The task is a simple lookup, or the data cannot be retained by a third party |
Every document and every file can carry instructions. Apply the rules from Chapter 13 before you pass any content to these features.
Choosing the extraction path
Vision is not always the best tool. A document pipeline usually follows this order:
- Native text layer. If a PDF has selectable text, extract it directly. It is exact and cheap.
- OCR for scanned pages with no text layer. Verify numbers, codes, and dates, because OCR errors concentrate there.
- Layout and table parsing to keep rows, columns, and headers together.
- Vision model only where visual context matters: a diagram, a stamp, a handwritten annotation, a chart whose meaning depends on its layout.
A routing function makes the choice explicit:
from dataclasses import dataclass
@dataclass(frozen=True)
class PageInfo:
has_text_layer: bool
text_chars: int
has_images: bool
contains_table: bool
visual_content_matters: bool
def extraction_path(page: PageInfo) -> str:
if page.has_text_layer and page.text_chars > 50:
if page.contains_table:
return "parse_table" # keep column headers with values
return "native_text"
if page.visual_content_matters:
return "vision_model"
return "ocr"The thresholds are illustrative. Tune them on your own documents. The important part is that the route is recorded with each extracted fact, so a reviewer can see how it was produced.
Structured extraction with provenance
A value extracted from a document is a claim. Store its page, its region when available, its original unit, and the source ID, so a reviewer can verify it.
from pydantic import BaseModel, Field
class ExtractedValue(BaseModel):
name: str
value: str | None # None when the value is not legible or not present
unit: str | None
page: int = Field(ge=1)
extraction_path: str # native_text, parse_table, ocr, or vision_model
confidence_note: str | None = None # for example "digit partly obscured"Rules for uncertain values:
- If a number is unreadable, return
Noneand a note. Do not invent a value. - Keep units exactly as written. A value in "kg" and a value in "lb" are different facts.
- Check each extracted value against an authoritative source when one exists, such as a catalog or a database.
For exact measurements and calculations, use the extracted data with ordinary code. The model's reading of a chart is a visual interpretation, and it should be labeled as one.
Speech: cascaded and end-to-end
A cascaded voice system runs speech-to-text, then a text model, then text-to-speech:
microphone -> STT -> text model (with retrieval, validation) -> TTS -> speakerIts advantage is inspectability. You can read the transcript, apply the same text validation as elsewhere, and replace one component at a time. Its weakness is compounded error: a transcription mistake in a model number changes the answer.
An end-to-end speech model processes audio directly and can produce audio. It can preserve timing and tone, and it is harder to inspect and control. Whether a provider offers such a model, and what it supports, changes over time. Check current documentation.
For many products, a text interface with speech recognition for input is enough, and it avoids the cost and complexity of spoken output.
Real-time sessions
Real-time voice needs more than a model call:
- Voice activity detection and endpointing: deciding when the user has finished speaking.
- Interruptions: stopping playback when the user speaks.
- Partial and final transcripts: partial text is provisional.
- Connection handling: reconnecting without losing the conversation state.
- Timeouts: saying something sensible when the model is slow.
If a spoken command triggers an action, require server-side authorization and confirmation before the action runs. A voice command is not proof of identity, and speaker identification is not authentication.
Accuracy in speech
Measure word error rate (WER) or character error rate (CER) on human-reviewed recordings from your real environment: accents, background noise, and your domain's vocabulary. Then measure what matters more: whether transcription errors change the downstream result. A small error in a serial number can matter more than many errors in punctuation.
Confirm critical values with the user. Show the transcript, let the user correct it, and read back names, numbers, and dates.
Voice data is sensitive. Set rules for consent, retention, and deletion before you record anything.
Video
Video adds time. A useful pipeline samples frames, reads subtitles or the audio transcript, keeps timestamps, and combines visual and spoken evidence.
Sampling trades cost for coverage. Sampling every few seconds is cheap and can miss short events. Denser sampling catches them and costs more. Choose the rate by the events you must not miss, and test it on recordings where those events happen quickly.
For a question about one moment, retrieve the segment around it and analyze that segment. Analyzing a whole long recording for a narrow question wastes cost and dilutes attention.
Keep timestamps on every claim. "The installation step is at 04:12" can be checked. "The installation is shown in the video" cannot.
Multimodal retrieval
A user may ask a question with a photo, a voice message, or a PDF, and expect an answer from your documents. The application converts or embeds the input, retrieves relevant text, pages, or segments, and answers with references. Two approaches:
- Convert to text. Captions, transcripts, and extracted text are searchable with the methods from Chapters 16 and 17. This is simpler and easier to inspect, but it loses visual detail.
- Cross-modal embeddings. Images and text share one vector space. This can match visual content directly. It needs its own evaluation, and it is harder to debug.
In both cases, filter by tenant and document permissions before retrieved content reaches the model.
Architecture
Interactive path:
UI (text, microphone, or file input)
-> API / BFF: authenticate, validate media type and size
-> preprocessing: OCR, transcription, frame selection, resizing
-> retrieval (authorized)
-> model call
-> validation (contract, evidence, page or timestamp references)
-> response with citations or accessible captionsBackground path:
upload
-> object storage (original file, checksum, retention policy)
-> queue
-> worker: text extraction, OCR, transcription, frame sampling
-> derived artifacts (text, page images, transcript) with versions
-> index and statusKeep the original file, the derived artifacts, and the processing versions. When the OCR engine or the sampling rate changes, you can reprocess and compare.
Process large media outside the interactive request. Keep provider credentials on the backend.
Security and privacy
Images, recordings, and video can contain faces, voices, addresses, location metadata, and background conversations. Apply these controls:
- Minimize. Upload only what the task needs. Crop images to the relevant region when possible.
- Strip metadata that is not needed, such as location data in photos.
- Restrict access to the original media, and set retention limits for each stored copy.
- Get consent before recording people or generating a voice that resembles someone.
- Treat content as untrusted. A PDF or a recording can carry a prompt injection. Policy in the application controls what the model may do, not what the media says.
Generated and transformed media need provenance. Track the source asset, the model, and the version. When you show AI-generated or AI-modified content, label it. Do not present a synthetic voice or image as verified evidence. A model's description of a photo cannot authenticate the photo, identify a person, or prove that footage is genuine.
Cost, latency, and user experience
Media costs scale with size, resolution, duration, and frame count. Reduce them carefully:
- Crop images to the region that matters.
- Lower the frame rate when the events are slow.
- Process uploads asynchronously and show progress.
- Do not compress so much that small labels, chart scales, or spoken model numbers become unreadable.
Show the user what the system is doing: uploading, extracting, transcribing, searching, generating. Let users review and correct extracted fields and transcripts. Provide a text alternative for every voice interaction and keyboard access for every control.
Evaluation
Test with the media your users actually produce, not with clean demo files. Include blurry photos, rotated scans, small print, noisy rooms, accents, long recordings, missing audio, and misleading text inside images.
Evaluate each stage and the whole task:
- Extraction: field accuracy, missing-value handling, and correctness of page or timestamp references.
- Transcription: WER or CER, and downstream intent accuracy.
- Video: event recall and timestamp accuracy.
- Answers: groundedness, unsupported claims, and refusal behavior when evidence is absent.
- Operations: p50 and p95 latency, time to first useful feedback, cost per successful task, and privacy incidents.
Example: a field technician assistant
A technician asks a question aloud and photographs a machine's nameplate. A reference design:
- Speech is transcribed. The transcript is shown and editable.
- The photo is processed: visible text is read, and the model identifier is extracted with its region.
- If the identifier is uncertain, the system asks the technician to confirm it. It does not guess.
- The identifier is matched against authorized specification documents.
- The answer appears as short text with the document title and page. The system states when evidence is missing, and it does not invent a rated limit.
The text path, with typed questions and retrieval, is built and tested first. Voice input and photos are added in separate iterations, each with its own evaluation. This keeps the reliable path working when a sensor or a microphone fails.
Use it when
- The information is genuinely in the image, the recording, or the video, and cannot be read reliably as text.
- A text-only workflow would miss something the user needs.
Avoid it when
- A native text layer, OCR, or a transcript alone solves the task.
- You add a vision or speech model to a text workflow without a measured gain.
Trade-offs
Multimodal input expands what the product can do and adds cost, latency, privacy obligations, and new failure modes. Cascaded speech is inspectable and adds latency. End-to-end speech feels more natural and is harder to control. Sampled video is cheap and can miss events.
Common mistakes
- Using vision for text that is already in a PDF layer. Extract the text.
- Accepting unreadable numbers. Return null and a note.
- Analyzing whole recordings for narrow questions. Retrieve the segment.
- Treating media as trusted. Images and audio carry instructions.
- Keeping raw media indefinitely. Set retention for each copy.
- Presenting AI descriptions as verification. They are not.
Key takeaway
Multimodal features are worth their complexity only where the information lives in the media. Start with the cheapest reliable path, record provenance for every extracted fact, and treat every image, document, and recording as untrusted input.
Exercises
- Build a
extraction_pathrouter for your documents. Run it on 50 pages and count how many pages take each path. Decide whether the expensive paths are justified. - Extract five values from scanned documents with provenance. Check each against the source by hand, and report the error rate by field type.
- Measure WER on 20 recordings from your real environment. Then measure the rate of downstream errors caused by transcription, and compare the two numbers.
- Write a privacy checklist for a photo upload feature: what is stored, for how long, what metadata is removed, and who can access the original.