Skip to main content
The Swarms framework provides powerful real-time streaming capabilities for agents, allowing you to see responses being generated token by token as they’re produced by the language model. This creates a more engaging and interactive experience, especially useful for long-form content generation, debugging, or when you want to provide immediate feedback to users.

Installation

Install the swarms package using pip:

Basic Setup

  1. First, set up your environment variables:

Step by Step

  • Install and put your keys in .env
  • Turn on streaming in Agent() with streaming_on=True
  • print_on=True (the default) renders the tokens in a live panel; print_on=False streams silently and only returns the final text

Code

Send tokens to your own function

streaming_callback receives each token as a string. It only fires on streamed calls, so keep streaming_on=True:

Iterate over tokens

run_stream() yields tokens as a generator, and arun_stream() is its async version. Both turn streaming on for the call, so you do not need streaming_on=True:
run_stream() and arun_stream() run the agent through run() on a background thread. With max_loops="auto" they enter the autonomous loop, and tokens from every LLM turn, including turns that follow tool calls, come through the same stream. Exceptions raised in the run are re-raised once the stream ends. See Agent Configuration for arun_stream() and the detailed stream=True mode, and Token usage for what agent.usage reports.

Connect With Us

If you’d like technical support, join our Discord below and stay updated on our Twitter for new updates!