Back to Copilotkit

Performance Tip

docs/snippets/shared/performance-tip.mdx

1.57.0647 B
Original Source

Performance tip: throttle streaming re-renders

When streaming long responses, the agent fires a re-render for every token. With many messages in the thread, this can cause jank. Two quick fixes:

  1. Throttle re-renders — add throttleMs to cap update frequency:
tsx
const { agent } = useAgent({ throttleMs: 50 }); // ~20 renders/sec
  1. Memoize message components — wrap your message renderer in React.memo so only the streaming message re-renders:
tsx
const Message = React.memo(({ msg }) => <p>{msg.content}</p>);

See the useAgent performance docs for more details.