Skip to main content

Constructor Parameters

ParameterTypeDefaultDescription
session_idstr(required)Unique identifier for this chat session
user_idstr(required)Unique identifier for the user
agentAgent(required)The Agent instance to handle conversations
storageStorageNoneStorage backend (defaults to InMemoryStorage)
full_session_memoryboolTrueStore full conversation history
summary_memoryboolFalseEnable conversation summarization
user_analysis_memoryboolFalseEnable user profile analysis
user_profile_schematypeNoneCustom Pydantic schema for user profiles
dynamic_user_profileboolFalseAuto-generate profile schema from conversations
num_last_messagesintNoneLimit history to last N messages
feed_tool_call_resultsboolFalseInclude tool calls in memory
user_memory_mode'update' | 'replace''update'How to update user profiles
debugboolFalseEnable debug logging
debug_levelint1Debug verbosity (1=standard, 2=detailed)
max_concurrent_invocationsint1Maximum concurrent invoke calls
retry_attemptsint3Number of retry attempts for failed calls
retry_delayfloat1.0Delay between retry attempts (seconds)

Instance Properties

PropertyTypeDescription
stateSessionStateCurrent session state (IDLE, AWAITING_RESPONSE, STREAMING, ERROR)
all_messagesList[ChatMessage]All messages in the session
input_tokensintTotal input tokens used
output_tokensintTotal output tokens used
total_tokensintTotal tokens (input + output)
total_costfloatTotal cost in USD
total_requestsintTotal API requests made
total_tool_callsintTotal tool calls made
run_durationfloatTotal run duration in seconds
time_to_first_tokenfloatTime to first token in seconds
start_timefloatSession start time (Unix timestamp)
end_timefloatSession end time (None if active)
durationfloatSession duration in seconds
last_activityfloatTime since last activity in seconds
is_closedboolWhether the session is closed

Example

import asyncio
from upsonic import Agent, Chat


async def main():
    agent = Agent("anthropic/claude-sonnet-4-5")

    chat = Chat(
        session_id="session1",
        user_id="user1",
        agent=agent,
        full_session_memory=True,
        summary_memory=True,
        num_last_messages=50,
        retry_attempts=3
    )

    response = await chat.invoke("Hello!")
    print(response)

    print(f"State: {chat.state.value}")
    print(f"Cost: ${chat.total_cost:.4f}")
    print(f"Tokens: {chat.total_tokens}")


if __name__ == "__main__":
    asyncio.run(main())