Skip to main content
This is currently only supported for the chat completion endpoint.
A ready-to-run example is available here!
Enable real-time display of LLM responses as they’re generated, token by token. This guide demonstrates how to use streaming callbacks to process and display tokens as they arrive from the language model.

How It Works

Streaming allows you to display LLM responses progressively as the model generates them, rather than waiting for the complete response. This creates a more responsive user experience, especially for long-form content generation.
1

Enable Streaming on LLM

Configure the LLM with streaming enabled:
2

Define Token Callback

Create a callback function that processes streaming chunks as they arrive:
The callback receives a ModelResponseStream object containing:
  • choices: List of response choices from the model
  • delta: Incremental content changes for each choice
  • content: The actual text tokens being streamed
3

Register Callback with Conversation

Pass your token callback to the conversation:
The token_callbacks parameter accepts a list of callbacks, allowing you to register multiple handlers if needed (e.g., one for display, another for logging).

Ready-to-run Example

This example is available on GitHub: examples/01_standalone_sdk/29_llm_streaming.py
examples/01_standalone_sdk/29_llm_streaming.py
You can run the example code as-is.
The model name should follow the LiteLLM convention: provider/model_name (e.g., anthropic/claude-sonnet-4-5-20250929, openai/gpt-4o). The LLM_API_KEY should be the API key for your chosen provider.
ChatGPT Plus/Pro subscribers: You can use LLM.subscription_login() to authenticate with your ChatGPT account and access Codex models without consuming API credits. See the LLM Subscriptions guide for details.

Next Steps