Syncing Anthropic Claude Streaming Events to an Animated Rive Mascot
By the Charmotion team · Updated · How we write guides

Map each Claude streaming event to one mascot state. message_start means the request is loading, the first text block means talking, a thinking or tool block means thinking, and message_stop means happy. Send only changes to a single mood number in the Rive file, so the mascot blends between states instead of flickering on every token.
Key takeaways
- Claude streams its answer as server-sent events, and only a few of them need to change the mascot's state.
- Map each stage (loading, thinking, talking, happy) to one mood number that the Rive file reads.
- Set the mood only when the stage changes, and treat usage counts as cumulative totals.
- Catch errors and ignore stale streams so the mascot always ends in a clear state.
Already have a character or logo? See examples in real app screens: splash, onboarding, loading and empty-state screens, with the animation that plays in each.
What events does the Claude streaming API send?
This guide shows how to sync Anthropic Claude streaming API events to an animated mascot, using a Rive file whose state is driven by one number. The mascot never needs the text itself. It only needs to know what stage the answer is in.
Setting stream to true on a Messages API request makes the response arrive incrementally as server-sent events. A streamed response begins with a message_start event, then content block events, then one or more message_delta events, and ends with message_stop. The official docs are at Anthropic's streaming guide.
| Event | What it carries | Use for the mascot |
|---|---|---|
| message_start | A Message object whose content array is empty; stop_reason is null | The request has begun: show loading |
| content_block_start | A new block begins, either text or another block type such as a tool call | Choose talking for text, thinking for anything else |
| content_block_delta | An incremental piece of the current block | Keep the current state; do not change mood per token |
| message_delta | delta.stop_reason and stop_sequence, plus usage.output_tokens | Read why the response ended |
| message_stop | The stream is complete | Show happy, or attention if the answer was cut off |
event: message_start
data: {"type": "message_start", "message": {"content": [], "stop_reason": null, ...}}
event: content_block_start
data: {"type": "content_block_start", "index": 0, "content_block": {"type": "text", ...}}
event: message_stop
data: {"type": "message_stop"}
Write your handler to ignore event types it does not recognize, so a new event added later does not break the mascot. The docs describe the sequence above, and your code should not depend on every event being present in every response.
Plain text chunks are not enough for this job. The SDK's text stream gives you words, but not whether the model is in a thinking block, has finished, or stopped early. The full event objects carry that information.
Which mascot mood should each stage drive?
Pick one mood per stage of the response, then keep the mapping in one constant file so designers and developers read the same numbers. The table uses the mood numbers from Charmotion character files, which you can reuse or replace with your own.
| Stage | Triggered by | Mood number | Mood name |
|---|---|---|---|
| Request sent | Your code, before the first event | 5 | loading |
| Text is arriving | content_block_start with a text block | 7 | talking |
| Model is reasoning or calling a tool | content_block_start with another block type | 3 | thinking |
| Answer complete | message_stop | 1 | happy |
| Answer cut off | A stop_reason you treat as a warning, read in message_delta | 9 | attention |
| Request failed | A thrown error in your stream loop | 4 | sad |
Two rules keep the mascot readable. First, change the mood only when the stage changes, not on every content_block_delta. Second, decide what a cut-off answer means for your product. Anthropic's stop reasons page explains each value, and you should compare the value you receive against that list rather than guessing from the text.
Keep the mood for a happy finish short. A success pose works best when the user can read the answer, so many products return to idle (0) after a few seconds instead of staying happy.
How do you add a mood number to the Rive file?
The mascot needs one number it can read at runtime. In Rive, data binding lets app code read and write values through a ViewModel. Create a number property, usually named mood, on the view model, then bind it to the state machine so each state responds to its value. Rive's editor page lists data binding as a way to drive any property from live data, and the Rive editor covers the setup.
You can build this yourself or start from a file that already has the input. Charmotion's character files include a CharacterViewModelMachine with a number property named mood and a CharacterStateMachine with a number input named mood. The numbers are fixed: idle 0, happy 1, wave 2, thinking 3, sad 4, loading 5, listening 6, talking 7, sleeping 8, attention 9. Moods blend into each other, so a switch from loading to talking reads as a transition rather than a cut. Characters have no one-shot triggers, so use happy for a success and sad for an error. The boolean accents hides the small accent icons if your design does not want them.
Exporting a .riv file for a product needs a paid Rive plan. Exports from the free plan play a Rive splash screen. For current plan details, see Is Rive free. Rive's pricing page is at rive.app/pricing.
If you use a Charmotion character, the state machine and the number input are already present. If you build your own file, name the input the same way so the code in the next section works unchanged.
How do you connect the Claude stream to the mascot in code?
Install the official SDK, then keep three pieces apart: a pure mapper that turns one event into a mood, a loop that reads the stream and sends changes, and a small setter that writes the number into Rive. Only the setter depends on Rive, so the rest can be tested without a browser.
npm install @anthropic-ai/sdk
The mapper and the loop look like this in TypeScript. Read the model name from your environment and use a model ID you have checked in Anthropic's docs, since model names change over time.
import Anthropic from '@anthropic-ai/sdk';
const MOOD = {
idle: 0,
happy: 1,
thinking: 3,
sad: 4,
loading: 5,
talking: 7,
attention: 9,
} as const;
type Mood = (typeof MOOD)[keyof typeof MOOD];
// Returns the mood for one event, or null when the mood should not change.
export function moodForEvent(event: { type: string; content_block?: { type: string } }): Mood | null {
switch (event.type) {
case 'content_block_start':
return event.content_block?.type === 'text' ? MOOD.talking : MOOD.thinking;
case 'message_stop':
return MOOD.happy;
default:
return null;
}
}
export async function streamToMascot(
client: Anthropic,
model: string,
prompt: string,
setMood: (mood: Mood) => void
): Promise<void> {
let current: Mood = MOOD.loading;
setMood(current);
try {
const stream = client.messages.stream({
model,
max_tokens: 1024,
messages: [{ role: 'user', content: prompt }],
});
for await (const event of stream) {
const next = moodForEvent(event);
if (next !== null && next !== current) {
current = next;
setMood(next);
}
}
} catch (err) {
setMood(MOOD.sad);
}
}
The loop sends a mood only when it changes, so a long text answer produces one talking update rather than hundreds. The message_delta event is read for its stop reason, but its mood is set by your own rule in the warning case, which is why the mapper returns null for it.
On the Rive side, the setter is the only part that touches the file. In the Rive web runtime you read the number from the view model instance and write to its value. The exact call depends on how you obtain the instance, so follow Rive's data binding docs for the web runtime.
// Obtain viewModelInstance from your Rive instance, as shown in Rive's data binding docs.
const moodProperty = viewModelInstance.number('mood');
function setMood(mood) {
moodProperty.value = mood;
}
Pass setMood into streamToMascot. In a React app, the same function can live in a component that owns the Rive instance, and the stream can start from a button or a route handler.
What goes wrong when a stream drives a mascot?
Most problems come from treating every event as a state change. Check these before you ship.
- Flicker. Setting the mood on each content_block_delta makes the mascot jump between states. The loop above sends a value only when the stage changes.
- Cumulative usage. In message_delta, the token counts are cumulative, not per-event increments. If you show progress, use the latest value. Adding each event's count together overstates the total.
- Early stop reasons. stop_reason is null in message_start and arrives in message_delta. Do not branch on it before that event.
- Dropped connections. A failed stream throws, and the catch block sets sad. Show a retry button rather than leaving the mascot in thinking.
- Stale streams. If the user sends a second message while the first is running, the first stream can still write its mood later. Give each request an ID and ignore events from an older one.
- Unmounted components. When the screen closes, stop reading the stream so a finished request cannot change the mascot on another screen.
One mascot should show one mood at a time. If you run more than one assistant request on a page, give each its own mascot, or queue the requests so the last one to start is the only one that writes.
Tool use adds a case. Anthropic's fine-grained tool streaming docs describe an eager_input_streaming flag on tool definitions, which lets tool inputs stream without server-side JSON buffering. While a tool block streams, the mapper above shows thinking, which is the right signal for the user.
How do you test the mapping before you ship?
Because the mapper is a pure function, you can test it with recorded events and no network. Record one real stream to a JSON file during development, then replay it in a unit test. The expected sequence for a plain text answer is loading, talking, then happy.
const events = [
{ type: 'message_start' },
{ type: 'content_block_start', content_block: { type: 'text' } },
{ type: 'content_block_delta' },
{ type: 'content_block_delta' },
{ type: 'message_delta' },
{ type: 'message_stop' },
];
// Expected moods in order: 5 (loading, set before the stream), 7 (talking), 1 (happy)
Add a second recording for a tool call and a third for a request that fails partway through. Each should produce a different sequence, and each assertion catches a different mistake. Run the same sequences against the Rive file with the mood planner to check timing before the app is built. The mascot state planner helps lay out which state follows which.
When the test passes, review the transitions on a device. A state that looks right in the editor can still feel abrupt if the mood changes faster than the blend. If it does, slow down the mapping rather than adding more states. For a broader look at how chat avatars react to streams, read animating a chat avatar from streaming events, and for the state machine itself, see Rive state machines.
How to sync Anthropic Claude streaming API events to an animated Rive mascot
- Read the event order. Confirm the sequence Claude sends: message_start, content block events, message_delta, then message_stop. Write the handler to ignore unknown event types.
- Choose one mood per stage. Map loading (5), talking (7), thinking (3), happy (1), attention (9) and sad (4) to stages of the response, and keep the numbers in one constant file.
- Add a mood number to the Rive file. Create a number property named mood on the ViewModel, bind it to the state machine, and export a .riv file from a paid plan.
- Map events and send changes only. Write moodForEvent to return a mood for each event, and a loop that calls the setter only when the mood differs from the current one.
- Handle errors and stale streams. Catch thrown errors and set sad, give each request an ID, and ignore events from an older request or a closed screen.
Frequently asked questions
Do I need a paid Rive plan to use this?
You can build and preview the file on the free plan, but exports from the free plan play a Rive splash screen. A product that ships the .riv file needs a paid plan to export without it. Check /guides/is-rive-free-pricing for current plan details, since Rive changes its plans over time.
Can the mascot react to tool calls as well as text?
Yes. A content block that is not text can show thinking while the tool input streams, and the mood returns to talking or happy when the next block starts or the message ends. Keep tool results out of the mood logic unless your product needs a separate state for them.
Should the browser call Claude directly?
Keep the API key on a server. A common pattern is a route in your backend that calls Claude with streaming enabled and forwards the events to the browser. The mapper and the mascot code can stay the same either way.
Why not drive the mascot from the text chunks alone?
Text chunks show only the words. They do not tell you when a thinking block starts, when a tool call begins, or when the message ends with a stop reason, so the mascot would miss the stages that matter most.
Sources
- Streaming Messages, Anthropic
- Claude API primer, Anthropic
- Handling stop reasons, Anthropic
- Fine-grained tool streaming, Anthropic
- Rive data binding in the web runtime, Rive
- Rive pricing, Rive