Skip to main content

Overview

In the Upsonic framework, Chat is a comprehensive conversational interface that provides stateful session management for AI agents. It handles the complete lifecycle of chat interactions, including message history, memory integration, cost tracking, and both streaming and non-streaming interfaces. The Chat class serves as a high-level orchestrator that manages:
  • Session lifecycle and state management
  • Memory integration and conversation persistence
  • Cost and token tracking across sessions
  • Both blocking and streaming response modes
  • Error handling and retry mechanisms
  • Concurrent invocation control

Chat Attributes

The Chat class provides comprehensive configuration options to customize conversational behavior and session management.

Core Attributes

Memory Configuration

Advanced Configuration

Creating a Chat Session

Chat sessions are created directly in code using the constructor. Each chat session can be customized with specific memory settings, storage backends, and session management options.

Basic Chat Creation

Chat with Custom Memory Settings

Chat with Debug and Retry Configuration

Advanced Chat Configuration

Custom Storage Backends

Memory Management Strategies

Chat Interface Methods

Basic Messaging

Streaming Responses

Session Management

Session State Management

Session States

The Chat class manages several session states:
  • IDLE: Session is ready to accept new messages
  • AWAITING_RESPONSE: Processing a user message
  • STREAMING: Streaming a response to the user
  • ERROR: Session encountered an error

Session Lifecycle

Memory Integration

Conversation History

Memory Persistence

Cost and Token Tracking

Cost Monitoring

Session Analytics

Error Handling and Retry Logic

Automatic Retry

Error Recovery

Practical Examples

Customer Support Chat

Educational Assistant

Multi-User Chat System

Streaming Chat Interface

Best Practices

Performance Optimization

  1. Choose appropriate storage: Use in-memory for development, persistent storage for production
  2. Configure memory settings: Enable only needed memory features to optimize performance
  3. Set retry limits: Balance between reliability and response time
  4. Use streaming for long responses: Improve user experience with real-time output

Session Management

  1. Use unique session IDs: Ensure proper session isolation
  2. Implement session cleanup: Close unused sessions to free resources
  3. Monitor session state: Handle errors gracefully
  4. Use context managers: Ensure proper resource cleanup

Memory Configuration

  1. Enable summarization for long conversations: Prevent memory overflow
  2. Use user analysis for personalization: Improve user experience
  3. Configure message limits: Balance between context and performance
  4. Choose appropriate memory modes: Update vs replace based on use case

Complete Example