Skip to content
AgenticAIWiki
  1. Learn
  2. Part 1
  3. 8. Multimodal and voice
  4. 8.5

Lesson 8.5 · 5 of 9 · 4 min read · Reviewed 24 Nov 2026

Worked example: vision, PDFs and voice front ends

By the end, you can

  • Use Claude's vision and PDF support.
  • Put Claude behind a voice front end.

Images in a message

message = client.messages.create(
    model="<model id>",
    max_tokens=500,
    messages=[{
        "role": "user",
        "content": [
            {"type": "image", "source": {"type": "base64", "media_type": "image/png", "data": b64}},
            {"type": "text", "text": "What is the total on this receipt?"}
        ]
    }]
)

Images can also be given by URL or by a file id from the Files APIFiles API: An API for uploading a file once and referring to it by id in later requests, instead of re-sending its contents.. Several images can go in one message. Check the visionVision: A model’s ability to read and reason about images, such as screenshots, charts and photos. docs for supported formats and size limits.

PDFs

{"type": "document",
 "source": {"type": "base64", "media_type": "application/pdf", "data": "..."}}

Claude processes each page’s text and an image of the page, so tables and charts are understood, not just the words. Long PDFs use many tokens; combine with prompt cachingPrompt caching: Reusing the processed form of a repeated prompt prefix across requests, which cuts cost and latency. if you’ll ask several questions about the same file (70.1). CitationsCitations: References in an answer that point to the exact source passages supporting each claim. can point to passages in documents (7.2).

Voice front ends

The Claude API works with text, images and documents. For a voice assistant:

STT service --> transcript --> Claude (streaming) --> sentences --> TTS service

Stream Claude’s reply and send each finished sentence to TTS so speech starts early. Use a system promptSystem prompt: Instructions passed in the top-level system field that set the model’s role, rules and context for the whole conversation. written for the ear (8.2). Keep tool calls and guardrailsGuardrail: A control that keeps an AI system within safe and allowed behaviour, such as input checks, tool limits or output filters. the same as in your text agentAgent: A system where a model decides its own next steps, calling tools in a loop until the task is done..

Try it

The Playground simulates Claude calls, so you can practise building content blocksContent block: One typed piece of a message, such as text, image, document, tool_use or tool_result. and streamingStreaming: Receiving a response as a series of server-sent events while it is generated, instead of waiting for the whole reply. handlers without a key.

On other providers

OpenAI’s and Gemini’s APIs also accept images and PDFs in the same request as text, and Gemini takes audio and video too. Both offer a files endpoint for content you reuse across requests. TokenToken: The unit a model reads and writes: a word, part of a word, a space or a symbol, mapped to an integer id. costs per image or page differ by provider and resolution, so measure on your own documents.

Key points

  • Claude accepts images and PDFs in messages alongside text, so one request can mix them.
  • Images are sent as base64 data, URLs or uploaded file ids; PDFs are sent as document blocks.
  • For voice, pair Claude with speech-to-text and text-to-speech services, streaming at each step.
Hands-on labRuns in the Playground

Send an image block in a request

Build a message that mixes an image and a question in the right shape, check it in code, and send it to the simulated client.

You will have: A helper that builds and validates an image-plus-text content list, and a completed simulated request.

Steps

  1. Run the starter: it sends only a text block.
  2. Fill in image_block(): a base64 source with media_type and data.
  3. Put the image block before the question in the content list.
  4. Fill in check(): every block has a type, images have a supported media type, text is not empty.
  5. Send the request to the simulated client and print the block types you sent and the stop reason.

Done when

  • The content list is ['image', 'text'] in that order.
  • check() rejects a block with media_type 'image/bmp'.
  • The simulated request completes and prints a stop reason.

Open in the Playground No API key needed.

Starter code (Python)
import base64
import anthropic
client = anthropic.Anthropic()
FAKE_IMAGE = b"not a real png, just test bytes"  # the Playground can't see images; shape is what we practise

def image_block(data, media_type="image/png"):
    # TODO: return {"type": "image", "source": {"type": "base64", "media_type": ..., "data": base64 text}}
    return None

def check(content):
    # TODO: raise ValueError for a block without a type, an unsupported media_type or empty text
    return True

content = [{"type": "text", "text": "What is the total on this receipt?"}]  # TODO: image first
check(content)
reply = client.messages.create(model="claude-sonnet-5-5", max_tokens=200, messages=[{"role": "user", "content": content}])
print("sent:", [b["type"] for b in content])
print("stop_reason:", reply.stop_reason)
Show a solution
import base64
import anthropic
client = anthropic.Anthropic()
FAKE_IMAGE = b"not a real png, just test bytes"
SUPPORTED = {"image/png", "image/jpeg", "image/gif", "image/webp"}

def image_block(data, media_type="image/png"):
    return {"type": "image", "source": {"type": "base64", "media_type": media_type,
                                        "data": base64.b64encode(data).decode("ascii")}}

def check(content):
    for b in content:
        if "type" not in b:
            raise ValueError("block without a type")
        if b["type"] == "image" and b["source"]["media_type"] not in SUPPORTED:
            raise ValueError("unsupported media_type " + b["source"]["media_type"])
        if b["type"] == "text" and not b["text"].strip():
            raise ValueError("empty text block")
    return True

try:
    check([image_block(FAKE_IMAGE, "image/bmp")])
except ValueError as e:
    print("rejected:", e)

content = [image_block(FAKE_IMAGE), {"type": "text", "text": "What is the total on this receipt?"}]
check(content)
reply = client.messages.create(model="claude-sonnet-5-5", max_tokens=200, messages=[{"role": "user", "content": content}])
print("sent:", [b["type"] for b in content])
print("stop_reason:", reply.stop_reason)

Do it Practise streaming in the Playground in the Playground. No API key needed.

Quick checkYou want Claude to answer questions about a 20-page PDF with charts. What should you send?
How sure are you?
Pick an answer and how sure you are