Skip to main content
This guide shows how to consume streaming responses from the Agent API, extract citations as they arrive, validate source URLs, and build a fully cited output. Streaming is essential for responsive UIs and long-running searches — you can display text and sources progressively instead of waiting for the full response.
The fast preset is optimized for quick, citation-rich answers. The model inserts numbered references like [1], [2] in the text, and the corresponding source URLs arrive in the search_results output item. See the Agent API Presets docs for all available presets.

Prerequisites

Install the SDKs:
If you don’t have an API key yet:

Get your Perplexity API Key

Navigate to the API Keys tab in the API Portal and generate a new key.
Then export your API key as an environment variable:

How Streaming Citations Work

When you stream an Agent API response with a search-enabled preset, the API sends a sequence of server-sent events (SSE). The flow is:
  1. Search results arrive via response.reasoning.search_results events — one event per search the model runs — containing URLs, titles, and snippets for each source.
  2. Content chunks arrive incrementally as the model generates text via response.output_text.delta events.
  3. Citation references appear in the text as numbered markers like [1], [2], mapping to the search result id field.
Your client accumulates the text, collects search results, then maps the numbered references to source URLs using the id field.
A response can contain more than one batch of search results. Single-step presets like fast typically search once, but multi-step presets (such as deep research) run many searches — one search_results event (streaming) or output item (non-streaming) per search, all sharing a single citation id space. Always collect the results from every event or item. If you keep only the first batch, most [N] references in the text won’t resolve and citations will look hallucinated.

Basic Streaming with Citations

Parsing Citation References from Text

The model inserts numbered references like [1], [2] into the generated text. To build a rich output with clickable links, parse these references and map them to source URLs using the search results.

Validating Citation URLs

In production systems, you should validate that citation URLs are well-formed and reachable before presenting them to users. This avoids broken links and improves trust in the output.
Never ask the model to generate source URLs. Always use the search_results output from the API response. Model-generated URLs can be hallucinated. The search results contain verified URLs from real web searches.

Progressive Display with Live Citation Count

For chat UIs, it’s useful to show a live citation counter as text streams in, then render the full reference list once the stream completes.

Handling Search Results

The Agent API returns one or more search_results output items with rich metadata (id, title, snippet, URL, date) for each source — one item per search the model ran. This is richer than a flat URL list — use it to build source cards, sidebars, or detailed reference sections.
Each search result includes id, title, url, snippet, and date. The id maps directly to the [N] references in the text. Use this to build rich source cards for your UI.

Complete Example: Streaming Research Assistant

A self-contained script that streams an Agent API response, extracts citations, validates URLs, and produces a formatted markdown output.

Tips and Best Practices

  1. Use a search-enabled preset like fast or low for citation-rich responses. Different presets use different citation formats — fast uses [1], while low uses [web:1].
  2. Accumulate search results from every event or item. A response contains one search_results event (streaming) or output item (non-streaming) per search the model ran — multi-step presets run many. Append each batch to a single list; overwriting on each event or reading only the first item silently drops most sources.
  3. Use the id field to map citations. Each search result has a numeric id that corresponds to the [N] reference in the text.
  4. Validate URLs before displaying them. Use HEAD requests with timeouts to filter out any unreachable sources.
  5. Never generate your own URLs. Use only the search_results from the API response. Model-generated URLs can be hallucinated.
  6. Handle missing references gracefully. If a [N] reference in the text has no matching id in your collected search results, display the reference number without a link rather than crashing.
  7. Consider rate limiting for URL validation. If the response includes many sources, validate them with concurrency limits to avoid overwhelming target servers.

Next Steps

Agent API Presets

Explore all presets and their citation formats.

Agent API Quickstart

Get started with the Agent API for multi-provider access and tools.

Agent API Streaming

Streaming patterns and event types for the Agent API.