Skip to content
AgenticAIWiki
  1. Learn
  2. Part 6
  3. 32. Designing AI products
  4. 32.6

Lesson 32.6 · 6 of 6 · 4 min read · Reviewed 24 Nov 2026

Worked example: streaming UX and citations

By the end, you can

  • Apply streaming and citations in a Claude-powered interface.
  • Handle edge cases in the UI.

Streaming in the UI

on text_delta:          append text to the message bubble
on tool_use start:      show a step: "Looking up order A-1042..."
on tool result:         tick the step
on message_delta:       read stop_reason
on error mid-stream:    mark message incomplete, show Retry

Render Markdown incrementally, and avoid layout jumps as text grows.

Citations

With citationsCitations: References in an answer that point to the exact source passages supporting each claim. enabled on document blocks (7.2), the response arrives as text blocks, some with citations that point to the document and location. In the UI:

  • Put a small marker after each cited sentence.
  • On click or hover, show the quoted passage and its title or page.
  • Flag answers with no citations when your product requires them.

Stop reasons in the UI

stop_reason Show
end_turn Normal answer
max_tokens “This answer was cut short.” with Continue
refusal A clear, polite message and an alternative path
tool_use / pause_turn Keep showing progress; the agent isn’t done

Accessibility

Announce new content politely for screen readers rather than reading every tokenToken: The unit a model reads and writes: a word, part of a word, a space or a symbol, mapped to an integer id., and let users pause or stop generation.

Key points

  • Stream Claude's replies and render text as it arrives; show tool steps for agents.
  • Use citations from document blocks to link claims to sources in the UI.
  • Handle stop reasons visibly: show cut-off, refused or failed replies as such, not as finished answers.
  • Keep the UI honest during errors: partial streams are marked incomplete, with a retry.
Hands-on labRuns in the Playground

Label streamed replies by stop reason

Stream a reply into a message bubble and show it honestly, using its stop reason.

You will have: An ask() function that streams text and labels each reply as complete, cut short or otherwise.

Steps

  1. Run the starter: notice the short reply is labelled complete, which is wrong.
  2. Add labels for max_tokens, refusal and tool_use from the lesson's table.
  3. Make unknown stop reasons show as incomplete, never complete.
  4. Run both requests and compare the labels.

Done when

  • The max_tokens=8 reply is labelled cut short.
  • The longer reply ends with stop_reason end_turn and is labelled complete.
  • No reply is labelled complete unless its stop reason is end_turn.

Open in the Playground No API key needed.

Starter code (Python)
# Stream a reply into a message "bubble" and label it honestly from its stop reason.
import anthropic

client = anthropic.Anthropic()
LABELS = {
    "end_turn": "complete",
    # TODO: add labels for max_tokens, refusal and tool_use (see the table in lesson 32.6)
}


def ask(question, max_tokens):
    bubble = ""
    with client.messages.stream(
        model="claude-sonnet-5-5",
        max_tokens=max_tokens,
        messages=[{"role": "user", "content": question}],
    ) as stream:
        for text in stream.text_stream:
            bubble += text  # in a real UI: append to the bubble as it arrives
        final = stream.get_final_message()
    # TODO: never show an unknown stop reason as complete
    status = LABELS.get(final.stop_reason, "complete")
    print(f"[{status}] {bubble.strip()[:60]}")
    print("stop_reason:", final.stop_reason)
    return status


ask("What is a context window?", max_tokens=8)
ask("What is a context window?", max_tokens=400)
Show a solution
# Stream a reply into a message "bubble" and label it honestly from its stop reason.
import anthropic

client = anthropic.Anthropic()
LABELS = {
    "end_turn": "complete",
    "max_tokens": "cut short: offer Continue",
    "refusal": "declined: show another path",
    "tool_use": "working: keep showing progress",
}


def ask(question, max_tokens):
    bubble = ""
    with client.messages.stream(
        model="claude-sonnet-5-5",
        max_tokens=max_tokens,
        messages=[{"role": "user", "content": question}],
    ) as stream:
        for text in stream.text_stream:
            bubble += text  # in a real UI: append to the bubble as it arrives
        final = stream.get_final_message()
    status = LABELS.get(final.stop_reason, "unknown: treat as incomplete")
    print(f"[{status}] {bubble.strip()[:60]}")
    print("stop_reason:", final.stop_reason)
    return status


ask("What is a context window?", max_tokens=8)
ask("What is a context window?", max_tokens=400)
Quick checkA stream fails halfway through an answer. How should the UI present it?
How sure are you?
Pick an answer and how sure you are

Finished the chapter? Take the chapter check.