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:Get your Perplexity API Key
Navigate to the API Keys tab in the API Portal and generate a new key.
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:- Search results arrive via
response.reasoning.search_resultsevents — one event per search the model runs — containing URLs, titles, and snippets for each source. - Content chunks arrive incrementally as the model generates text via
response.output_text.deltaevents. - Citation references appear in the text as numbered markers like
[1],[2], mapping to the search resultidfield.
id field.
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.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 moresearch_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.
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
-
Use a search-enabled preset like
fastorlowfor citation-rich responses. Different presets use different citation formats —fastuses[1], whilelowuses[web:1]. -
Accumulate search results from every event or item. A response contains one
search_resultsevent (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. -
Use the
idfield to map citations. Each search result has a numericidthat corresponds to the[N]reference in the text. - Validate URLs before displaying them. Use HEAD requests with timeouts to filter out any unreachable sources.
-
Never generate your own URLs. Use only the
search_resultsfrom the API response. Model-generated URLs can be hallucinated. -
Handle missing references gracefully. If a
[N]reference in the text has no matchingidin your collected search results, display the reference number without a link rather than crashing. - 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.