STREAMING_OPTIMIZATION

A guide and set of code patterns for displaying live agent output smoothly in a web interface. It groups incoming text, tool steps, and status changes before applying them during browser animation frames.

In plain words
What is it for?
Use it when building live chat or agent panels that stream responses, update progress by step, and automatically keep the latest content visible.
Why use it?
It reduces excessive screen updates and layout work that can make a streaming interface stutter or stop responding.

Agent

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add agents/homenshum/nodebenchai/streaming_optimization
Clone the repo
git clone --depth 1 https://github.com/HomenShum/NodeBenchAI
Per session 0 Only the description is in the session, so the agent can decide to use it. The body loads when it is invoked.
When invoked 1,685 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00000 $0.01685
Opus 5 $0.00000 $0.00843
Sonnet 5 $0.00000 $0.00337
Haiku 4.5 $0.00000 $0.00169

Measured 2d ago against content hash 918de79d6398, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

STREAMING_OPTIMIZATION scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 2d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

docs/agents/STREAMING_OPTIMIZATION.md · 242 lines

How it starts

The opening of the file, as written. The whole thing — 242 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Streaming UI Optimization Guide

Overview

This document describes the streaming UI optimization patterns implemented in FastAgentPanel to ensure smooth, animated streaming with per-step updates at 30-60fps without layout thrashing.

Architecture

1. Frame-Aligned Streaming Buffer (useStreamingBuffer)

Purpose: Accumulate streaming updates and flush them on animation frames to prevent excessive re-renders.

How it works:

  • Collects tokens, tool steps, and status updates in a ring buffer
  • Flushes updates on requestAnimationFrame or at configurable intervals (default: 33ms ≈ 30fps)
  • Automatically flushes when buffer exceeds max size (default: 50 updates)
  • Prevents UI blocking by batching updates

Usage:

const { addUpdate, flush, start, stop, isActive, bufferSize } = useStreamingBuffer(
  (updates) => {
    // Apply batched updates to state
    setMessages(applyUpdates(messages, updates));
  },
  { maxBufferSize: 50, flushIntervalMs: 33, enableLogging: false }
);

// Start streaming
start();

// Add updates as they arrive
addUpdate({ type: 'token', data: 'hello', timestamp: Date.now() });

// Stop and flush remaining
stop();

2. Smart Auto-Scroll (useSmartAutoScroll)

Purpose: Intelligent auto-scroll that respects user scroll position and prevents scroll fighting.

Features:

  • Only auto-scrolls when user is near bottom (configurable threshold, default: 80px)
  • Pauses auto-scroll when user scrolls up
  • Resumes when user scrolls back to bottom
  • Uses scroll anchoring to prevent layout thrash
  • Respects prefers-reduced-motion for accessibility

Usage:

const { autoScroll, isNearBottom, scrollToBottom, reset, userHasScrolledUp } = useSmartAutoScroll(
  scrollRef,
  { nearBottomThreshold: 80, enableLogging: false }
);

// Auto-scroll when new messages arrive
useEffect(() => {
  autoScroll();
}, [messages.length, autoScroll]);

// Reset when starting new conversation
useEffect(() => {
  reset();
}, [reset]);

Read the full file on GitHub · 242 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 2d ago First seen · 242 lines · 0 tokens per session scan A 918de79d6398

Subscribe to this mod's changes

STREAMING_OPTIMIZATION is an agent published in the GitHub repository HomenShum/NodeBenchAI (14 stars, last pushed 18d ago), licensed MIT. It costs nothing until one of its globs matches a file; then it loads 1,685 tokens. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.