Skip to main content

Overview

Chat enables you to build stateful conversational sessions with automatic memory management, cost tracking, and session analytics. It integrates seamlessly with Agent and Task to provide persistent conversation context across multiple interactions.

Key Features

  • Session Management: Unique session and user identification with state tracking
  • Memory Integration: Automatic conversation history, summarization, and user profile management
  • Cost Tracking: Real-time token usage and cost monitoring per session
  • Streaming Support: Both blocking and streaming response modes
  • Error Handling: Built-in retry mechanisms and error recovery
  • Storage Backends: Flexible storage options for persistence

Example

Create a Chat session and interact with an Agent:
from upsonic import Agent, Task, Chat

# Create agent
agent = Agent("openai/gpt-4o")

# Create chat session
chat = Chat(
    session_id="user123_session1",
    user_id="user123",
    agent=agent
)

# Send a message
response = await chat.invoke("Hello, how are you?")
print(response)

# Access metrics
print(f"Total cost: ${chat.total_cost}")
print(f"Messages: {len(chat.all_messages)}")