Problem
Setting up the api integration for the LLM Gateway streaming endpoint. The CHUNK_SIZE_PARAMETER and ENCODING_FORMAT configurations look correct in the request body, but the event loop locks up when throughput spikes. Token accumulation logic breaks down around the twelfth chunk. Stateful buffering just keeps growing until the process crashes. We’ve been trying to validate stream integrity using checksum verification and sequence tracking to catch any packet loss, but it’s not stopping the memory leak. Honestly, the whole buffering setup feels clunky. Metrics are flying everywhere.
Code
const stream = await fetch('https://api.genesys.cloud/v2/ai/llm/stream', {
method: 'POST',
headers: { 'Authorization': `Bearer ${token}`, 'Content-Type': 'application/json' },
body: JSON.stringify({
model: 'genesys-llm-v1',
CHUNK_SIZE_PARAMETER: 1024,
ENCODING_FORMAT: 'utf-8',
sequenceTracking: true
})
});
const reader = stream.body.getReader();
let buffer = '';
let lastSeq = 0;
while (true) {
const { done, value } = await reader.read();
if (done) break;
const chunk = new TextDecoder().decode(value);
const parsed = JSON.parse(chunk);
if (parsed.sequence !== lastSeq + 1) throw new Error('Packet loss detected');
buffer += parsed.token;
lastSeq = parsed.sequence;
}
Error
The stream hangs at high volume. Node throws a FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory. Checksum verification passes fine. Backpressure management isn’t triggering anywhere. Downstream analytics webhook notifications never fire because the completion event gets swallowed. Latency tracking shows token throughput dropping to zero right before the crash. AUDIT_LOG_FORMAT settings are all set correctly, but the compliance debugging logs show nothing after chunk fourteen.
Question
How do I properly handle backpressure in the fetch stream so the event loop doesn’t starve? Need to expose a streaming client for real-time AI integration without blowing the heap. Pacing the read calls manually feels hacky. The LLM Gateway docs don’t mention a maxTokensPerSecond config either.