Skip to main content

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:
Parameters:
  • message: The message content to send
  • threadId: Optional thread ID for separate conversations (defaults to “default”)
Returns: Flow<BaseEvent> - Stream of AG-UI protocol events Example:

getHistory

Access the conversation history for a thread:
Example:

clearHistory

Clear conversation history for one or all threads:
Example:

Automatic History Management

How It Works

The StatefulAgUiAgent automatically:
  1. Captures your messages: When you call chat(), your message is added to the thread’s history
  2. Includes system prompt: On first message, system prompt is automatically prepended
  3. Sends full context: Each request includes the complete conversation history for that thread
  4. Captures responses: Agent responses are automatically parsed and added to history
  5. Manages state: Agent state updates are captured and maintained
  6. Trims old messages: When maxHistoryLength is 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 the getHistory() 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

Use AgUiAgent when:
  • Agent manages state server-side
  • Single interactions
  • Memory usage is critical
  • Server has conversation context
Use StatefulAgUiAgent when:
  • Client needs conversation control
  • Complex multi-turn conversations
  • Agent doesn’t maintain server-side state
  • Full conversation history needed