StatefulAgUiAgent
StatefulAgUiAgent is a stateful client implementation that automatically maintains conversation history and sends the complete message history with each request. This provides seamless conversational experiences where the agent has full context of previous interactions.
Key Features
- Automatic History Management: Conversation history is maintained automatically per thread
- Simple API: Just use
chat()- no manual state management required - Thread Support: Multiple conversation threads with separate histories
- Automatic Trimming: Configurable limits with automatic old message removal
- State Tracking: Automatically captures and maintains agent state updates
Usage
Basic Conversation
Multiple Conversation Threads
Configuration
Convenience Builders
The easiest way to create StatefulAgUiAgent instances:Basic Setup
Advanced Configuration
Methods
chat
Send a message in a conversational context with automatic history:message: The message content to sendthreadId: Optional thread ID for separate conversations (defaults to “default”)
Flow<BaseEvent> - Stream of AG-UI protocol events
Example:
getHistory
Access the conversation history for a thread:clearHistory
Clear conversation history for one or all threads:Automatic History Management
How It Works
The StatefulAgUiAgent automatically:- Captures your messages: When you call
chat(), your message is added to the thread’s history - Includes system prompt: On first message, system prompt is automatically prepended
- Sends full context: Each request includes the complete conversation history for that thread
- Captures responses: Agent responses are automatically parsed and added to history
- Manages state: Agent state updates are captured and maintained
- Trims old messages: When
maxHistoryLengthis exceeded, oldest messages are automatically removed (system message is preserved)
Message Flow Example
Automatic History Trimming
Thread Management
Separate Conversations
Thread Lifecycle
Error Handling
Network Errors
Best Practices
Memory Management
Threading Strategy
Important Notes
Memory-Only Storage
Conversation history is stored in memory only. When your application restarts, all conversation history is lost. If you need persistence, you must implement your own storage solution using thegetHistory() method to retrieve conversations and save them to a database or file.
Thread Safety
The StatefulAgUiAgent is designed for single-threaded use per instance. For concurrent access, create separate agent instances or implement your own synchronization.Comparison with AgUiAgent
| Feature | AgUiAgent (Stateless) | StatefulAgUiAgent |
|---|---|---|
| History Management | Manual/Server-side | Automatic |
| Memory Usage | Minimal | Higher (stores history) |
| Context Preservation | Agent/Server handles | Client handles |
| Multi-turn Conversations | Requires server support | Built-in |
| History Trimming | Not applicable | Automatic |
- Agent manages state server-side
- Single interactions
- Memory usage is critical
- Server has conversation context
- Client needs conversation control
- Complex multi-turn conversations
- Agent doesn’t maintain server-side state
- Full conversation history needed