Skip to content

React Hooks API Reference

The @inngest/use-agent package provides a comprehensive set of React hooks for building AI chat interfaces with AgentKit networks. This reference section documents every hook, component, and utility in detail.

Terminal window
npm install @inngest/use-agent
# Peer dependencies
npm install react @inngest/realtime uuid

These hooks provide the primary functionality for building AI chat applications:

Essential components for connection management and configuration:

Specialized hooks for specific features and UI patterns:

Core Streaming Hooks:

  • useAgent: Multi-thread real-time streaming
  • useChat: Complete chat application functionality
  • useThreads: Thread persistence and management

Specialized Storage:

  • useEphemeralThreads: Client-side storage for demos
  • useConversationBranching: Message editing workflows

UI Utilities:

  • useMessageActions: Copy, like, share, read aloud
  • useEditMessage: Message editing state management
  • useSidebar: Responsive sidebar state
  • useIsMobile: Mobile device detection

Infrastructure:

  • AgentProvider: Shared context and connections
  • Transport layer: Configurable API communication

🎯 Progressive Enhancement: Start simple with useChat, add advanced hooks as needed

🔧 Composable Architecture: Hooks work independently or together based on your needs

⚡ Performance First: Intelligent connection sharing and efficient state management

🛡️ Production Ready: Comprehensive error handling, TypeScript support, and testing

🔄 Backward Compatible: Smooth migration from local implementations to package

All hooks use consistent, well-defined TypeScript interfaces:

import type {
// Message and content types
ConversationMessage,
MessagePart,
TextUIPart,
ToolCallUIPart,
HitlUIPart,
// State and status types
AgentStatus,
Thread,
ThreadState,
// Event and streaming types
NetworkEvent,
AgentMessageChunk,
// Transport and configuration
AgentTransport,
UseAgentOptions,
UseChatConfig,
// Error handling
AgentError,
ErrorClassification,
} from "@inngest/use-agent";

Each hook has a comprehensive return type interface:

import type {
UseAgentReturn,
UseChatReturn,
UseThreadsReturn,
} from "@inngest/use-agent";
// Example: Complete typing for useChat
const chat: UseChatReturn = useChat({
initialThreadId: "thread-123",
});
import { useChat, AgentProvider } from "@inngest/use-agent";
function App() {
return (
<AgentProvider userId="user-123">
<ChatInterface />
</AgentProvider>
);
}
function ChatInterface() {
const {
messages, // ConversationMessage[]
sendMessage, // (message: string) => Promise<void>
status, // AgentStatus
isConnected, // boolean
threads, // Thread[]
switchToThread, // (threadId: string) => Promise<void>
createNewThread // () => string
} = useChat();
return <div>/* Your chat UI */</div>;
}
import {
AgentProvider,
createDefaultAgentTransport,
type DefaultAgentTransportConfig
} from "@inngest/use-agent";
const transport: DefaultAgentTransportConfig = {
api: {
sendMessage: '/api/v2/chat',
fetchThreads: '/api/v2/conversations'
},
headers: {
'Authorization': `Bearer ${token}`
}
};
<AgentProvider transport={transport}>
<ChatApp />
</AgentProvider>
import { useAgent } from "@inngest/use-agent";
function AdvancedChat() {
const {
threads, // Record<string, ThreadState>
currentThreadId, // string
setCurrentThread, // (threadId: string) => void
sendMessageToThread, // (threadId: string, message: string) => Promise<void>
getThread, // (threadId: string) => ThreadState | undefined
createThread, // (threadId: string) => void
removeThread // (threadId: string) => void
} = useAgent({
threadId: 'initial-thread',
userId: 'user-123'
});
return <div>/* Custom multi-thread UI */</div>;
}

✅ Efficient Patterns:

// Stable configuration objects
const transportConfig = useMemo(
() => ({
headers: { Authorization: `Bearer ${token}` },
}),
[token]
);
// Reasonable state capture
state: () => ({
currentTab: activeTab,
formData: getCurrentForm(),
});

❌ Memory Leaks:

// Don't capture massive objects
state: () => ({
entireAppState: store.getState(), // Too large!
allUserHistory: history, // Too large!
});
// Don't recreate config on every render
transport: createDefaultAgentTransport({
// New instance!
headers: { Authorization: `Bearer ${token}` },
});

✅ Efficient:

// Single provider, shared connections
<AgentProvider userId="user-123">
<ChatSidebar /> {/* Shares connection */}
<ChatMain /> {/* Shares connection */}
</AgentProvider>

❌ Inefficient:

// Multiple separate connections
<ChatSidebar /> {/* useThreads creates connection */}
<ChatMain /> {/* useAgent creates connection */}

All hooks provide comprehensive error information:

const { error, connectionError, clearError } = useChat();
if (error) {
console.log({
message: error.message, // Human-readable error
recoverable: error.recoverable, // Can user retry?
timestamp: error.timestamp, // When it occurred
suggestion: error.suggestion, // How to fix
});
}
// Automatic retry for recoverable errors
useEffect(() => {
if (error?.recoverable) {
const timer = setTimeout(() => {
clearError();
// Optionally retry the failed operation
}, 3000);
return () => clearTimeout(timer);
}
}, [error]);

Enable comprehensive debug output:

import { createDebugLogger } from "@inngest/use-agent";
const logger = createDebugLogger("MyComponent", true);
logger.log("Component initialized");
logger.warn("Non-critical issue");
logger.error("Critical error");
<AgentProvider
debug={process.env.NODE_ENV === 'development'}
transport={isDev ? devTransport : prodTransport}
>
<ChatApp />
</AgentProvider>
// Before: Local imports from examples
import { useChat } from "@/hooks/use-chat";
import { AgentProvider } from "@/contexts/AgentContext";
// After: Package imports
import { useChat, AgentProvider } from "@inngest/use-agent";

No API changes required - the package maintains full backward compatibility with local implementations.

Package VersionAgentKit VersionReact VersionFeatures
1.0.0+0.3.0+18.0.0+Full feature set
0.9.0+0.2.0+18.0.0+Beta features

This reference section provides the definitive documentation for every aspect of the @inngest/use-agent package. Whether you’re just getting started or building advanced custom implementations, you’ll find the complete API documentation and guidance you need here.