State Streaming

Stream partial agent state to your React Native UI while a tool call is still running.

What is this?

By default, agent state only updates between backend checkpoints, so a long tool call (writing a full document, drafting an email) reaches the UI as one burst at the end. In an agent-native app, that feels broken: users expect to watch the output appear.

State streaming copies one tool argument into an agent state key while the model writes the argument. Your screen, subscribed with useAgent, re-renders on every token.

When should I use this?

Use state streaming when a tool's output is long-form text or a growing structured value, and you want the user to see it build up in real time. Common shapes:

  • A writing agent that produces a document
  • A research agent that collects a list of findings
  • A planning agent that builds a step-by-step plan

The backend: one streaming state mapping

Map the tool call to a state snapshot

AGUIStreamOptions.MapCall maps a tool call to AG-UI events. Return a StateSnapshotEvent built from the tool's arguments, and attach the options to the endpoint with WithMetadata:

Program.cs
using System.ComponentModel;
using System.Text.Json;
using AGUI.Abstractions;
using AGUI.Server;
#pragma warning disable OPENAI001
using Microsoft.Extensions.AI;
using Microsoft.Agents.AI;
using Microsoft.Agents.AI.Hosting.AGUI.AspNetCore;
using Microsoft.Extensions.AI;
using OpenAI;
using OpenAI.Chat;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddAGUIServer();
var app = builder.Build();

string openAiApiKey = builder.Configuration["OPENAI_API_KEY"]
    ?? throw new InvalidOperationException("Set OPENAI_API_KEY");

// Define a tool the LLM may call to report its progress
[Description("Report current step progress.")]
static string StepProgress([Description("Steps completed so far")] string[] steps)
    => "Progress received.";

AITool stepProgress = AIFunctionFactory.Create(StepProgress, name: "step_progress");
var agent = new OpenAIClient(openAiApiKey)
    .GetChatClient("gpt-6-luna")
    .AsIChatClient().AsBuilder()
    .ConfigureOptions(options => options.RawRepresentationFactory = _ =>
        new OpenAI.Chat.ChatCompletionOptions { ReasoningEffortLevel = new("none") })
    .Build()
    .AsAIAgent(
        name: "AGUIAssistant",
        instructions: "You are a helpful assistant that may call the 'step_progress' tool to report intermediate steps.",
        tools: [stepProgress]);

// Map the completed tool arguments to a shared-state snapshot
AGUIStreamOptions streamOptions = new AGUIStreamOptions()
    .MapCall("step_progress", call =>
    {
        if (call.Arguments?.TryGetValue("steps", out object? steps) is not true)
        {
            return [];
        }

        JsonElement snapshot = JsonSerializer.SerializeToElement(
            new { observed_steps = steps });
        return [new StateSnapshotEvent { Snapshot = snapshot }];
    });

app.MapAGUIServer("/", agent).WithMetadata(streamOptions);
await app.RunAsync();

Know what streams and what does not

MapCall runs on the completed FunctionCallContent, so the state key updates once, when the tool call finishes. It does not grow token by token while the model writes the arguments. To stream partial arguments into state, register a provider-specific extractor with MapStreamingToolCallArguments.

The backend pattern is always the same: map one streaming tool argument to one shared-state key. Each framework does this in its own way:

  • LangGraph Python: StateStreamingMiddleware with StateItem(...) entries.
  • LangGraph TypeScript: copilotkitCustomizeConfig with an emitIntermediateState mapping.
  • Direct SDK adapters: parse partial tool arguments in the streaming loop, and emit STATE_SNAPSHOT whenever the mapped value changes.
  • LlamaIndex and Pydantic AI: there is no per-token mapping. A backend tool sends a state snapshot after each step or tool call instead (see the setup above).

When the model streams that argument, CopilotKit writes every partial value into shared state before the tool finishes.

A few things to note:

  • The state key must exist in your agent state (document in this example).
  • The tool and argument names must match the exact tool call the model makes (write_document.document here).
  • Streamed values are previews. When the step ends, the state the agent saves replaces them, so save the final value when the tool finishes. On CrewAI, LlamaIndex, and Pydantic AI, write the final value to the agent state yourself, or it is overwritten when the step returns.

The frontend: useAgent and OnStateChanged

The app side is the same as any other shared-state subscription: useAgent with OnStateChanged gives you a live agent.state. Add OnRunStatusChanged for a "writing" indicator.

src/DocumentView.tsx
import { ActivityIndicator, ScrollView, Text, View } from "react-native";
import { useAgent, UseAgentUpdate } from "@copilotkit/react-native/headless";

export function DocumentView() {
  const { agent } = useAgent({
    agentId: "default",
    updates: [UseAgentUpdate.OnStateChanged, UseAgentUpdate.OnRunStatusChanged],
  });
  const document = (agent.state as { document?: string } | undefined)?.document ?? "";

  return (
    <View style={{ padding: 16, gap: 8 }}>
      {agent.isRunning ? <ActivityIndicator /> : null}
      <ScrollView style={{ maxHeight: 320 }}>
        <Text>{document}</Text>
      </ScrollView>
    </View>
  );
}

agent.state.document is a string that grows on every token, and agent.isRunning tells you whether to show the indicator. The ScrollView with a maxHeight keeps a long document from pushing the rest of the screen away.

If a fast stream re-renders a heavy screen too often, pass throttleMs to useAgent to batch the updates.

Try it

Ask the agent to write a short poem. The document text grows on your screen while the agent writes. When the run ends, agent.isRunning is false, so the indicator goes away.

How is this guide?