Skip to main content
This example shows how to protect streaming LLM responses, detecting threats and PII as tokens arrive rather than waiting for the complete response.

Overview

Streaming presents unique security challenges:
  • Responses arrive token-by-token
  • Threats may span multiple chunks
  • Users see partial content before full analysis
PromptGuard handles streaming with real-time scanning.

How Streaming Protection Works

Implementation

With Response Scanning

Enable scan_responses=True to scan the complete response after streaming:

Real-Time Chunk Scanning

For immediate threat detection during streaming:

Node.js Streaming

Auto-Instrumentation

With Vercel AI SDK

Server-Sent Events (SSE)

FastAPI Streaming

Handling Blocked Streams

When a threat is detected mid-stream:

Performance Considerations

Recommendations

  1. For chat interfaces: Use input scanning + end-of-stream response scan
  2. For sensitive data: Use real-time chunk scanning
  3. For low-latency needs: Use input-only scanning with async response analysis

Best Practices

  1. Buffer appropriately - Don’t scan every token, batch into meaningful chunks
  2. Handle interruptions gracefully - Users may see partial content
  3. Log blocked streams - Track for security analysis
  4. Consider UX - Decide if you show partial content before blocking

Next Steps

Streaming Reference

Full streaming documentation

Error Handling

Handle errors gracefully