Agent State Management

Implements Zustand and TanStack Query state management patterns for AI agent chat frontends.

Updated Apr 2, 2026
One-click install
npx skills add https://github.com/khiwniti/carbonscope --skill agent-state-management-khiwniti
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: Agent State Management
Source: https://github.com/khiwniti/carbonscope/tree/main/ai-agent-saas-expert/skills/agent-state-management
Command: npx skills add https://github.com/khiwniti/carbonscope --skill agent-state-management-khiwniti

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires zustand, @tanstack/react-query, immer.

What problem does it solve? Building an AI agent chat frontend requires coordinating transient UI state (active thread, streaming messages, sidebar) with server data (threads, messages, agent configs), and mixing these concerns causes re-render storms, stale caches, and duplicated state. ## Core Features & Use Cases - Store-per-domain Zustand design: Separate threadStore, uiStore, and agentBuilderStore with immer and persist middleware, plus granular selector patterns to prevent unnecessary re-renders during streaming. - TanStack Query server state: Query client configuration, infinite thread history pagination, and structured query key factories for agents and threads. - Optimistic updates and cache invalidation: Optimistic thread creation and message sending with rollback on error, plus a cache invalidation matrix for common events. - Use Case: When building a chat page that streams assistant tokens over SSE, use the useAgentThread composite hook to append tokens to the TanStack Query cache while Zustand tracks only the streaming message ID, so sidebar components never re-render. ## Quick Start Ask the AI to design the client-side state layer for an agent chat frontend using Zustand stores and TanStack Query with optimistic thread creation.

Frequently Asked Questions about Agent State Management

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I manage state in a React AI chat application?▼

Split state by ownership: use TanStack Query for all server data like threads, messages, and agent configs, and Zustand for transient UI state like the active thread ID, streaming message ID, and sidebar visibility. Never duplicate server data in Zustand stores.

Zustand vs TanStack Query for chat state management?▼

They serve different purposes and work together. TanStack Query owns server state with caching, refetching, and invalidation, while Zustand owns synchronous client state like connection status and draft forms. Use both rather than choosing one.

How do I prevent re-renders during streaming in Zustand?▼

Subscribe components to specific slices using selectors like useThreadStore(s => s.activeThreadId) instead of the whole store. Components that do not use streamingMessageId will not re-render as tokens arrive during streaming.

How do I implement optimistic updates with TanStack Query?▼

In the mutation's onMutate callback, cancel in-flight queries, snapshot the previous cache value, and write an optimistic entry with setQueryData. Roll back to the snapshot in onError and replace the placeholder with real server data in onSuccess.

Should messages use staleTime Infinity in TanStack Query?▼

Yes, when messages are updated via SSE push rather than polling. Setting staleTime to Infinity prevents unnecessary refetches since the cache is updated directly through setQueryData as stream tokens arrive.