Skip to content
For the complete documentation index, see llms.txt. This page is also available as Markdown.

Streaming Responses, Tool Events, and Completion

Streaming displays updates while a response is being produced. Handle progress, tool activity, errors, and completion separately so a partial answer is not shown as finished.

Keep progress separate from the completed answer

Illustrative client dispatch logic. Map these helpers to the chosen endpoint’s documented event shape and snapshot/delta behavior. A connection closing without a completion signal is not proof of success.

Copy the example
for event in decoded_stream:
    if is_answer_update(event):
        update_answer_using_endpoint_contract(event)
    elif is_tool_event(event):
        update_tool_activity(event)
    elif is_error(event):
        show_error(event)
    elif is_confirmed_completion(event):
        mark_answer_complete()

Before you begin

A streaming-capable interface or documented streaming API endpoint.

Key concepts

Term
Definition

Streaming

Delivery of incremental events or text while work is running. Updates are not the same as a completed answer.

Tool result

The returned data or error from a tool operation. The agent should base its report on this result rather than assuming the call succeeded.

Finish reason

Information indicating why generation ended, when the provider supplies it. Distinguish normal completion from a length limit, interruption, or unreported reason.

Terminal state

A state indicating that a run has ended, such as succeeded, failed, or cancelled. Stop normal completion polling only after a final state is known.

Run

One execution of a project. Use its run ID to retrieve that execution’s state, input, output, and errors.

Handle a streaming response

  1. Start a request using the endpoint’s required streaming format.

  2. Read events as they arrive and update the appropriate part of the interface.

  3. Wait for completion or an explicit failure before marking the answer complete.

The user sees progress and can distinguish a complete response from an interrupted one.

Behavior and limitations

Not every event is answer text. A disconnected stream does not prove that execution stopped. Preserve the run ID and retrieve status where supported. Tool events can contain structured data that should not be appended directly to the answer. Streaming contracts differ between API versions.

Interface Inputs and Output Mapping

Was this helpful?