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
How Streaming Protection Works
Implementation
Auto-Instrumentation (Recommended)
With Response Scanning
Enablescan_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
- For chat interfaces: Use input scanning + end-of-stream response scan
- For sensitive data: Use real-time chunk scanning
- For low-latency needs: Use input-only scanning with async response analysis
Best Practices
- Buffer appropriately - Don’t scan every token, batch into meaningful chunks
- Handle interruptions gracefully - Users may see partial content
- Log blocked streams - Track for security analysis
- Consider UX - Decide if you show partial content before blocking
Next Steps
Streaming Reference
Full streaming documentation
Error Handling
Handle errors gracefully