Skip to main content

Basic Creation

A Chat requires an Agent and session/user identifiers:
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
    )

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


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

With Memory Configuration

Enable conversation memory features:
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,
        user_analysis_memory=True,
        num_last_messages=50
    )

    await chat.invoke("My name is Alice")
    response = await chat.invoke("What is my name?")
    print(response)


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

With Custom Storage

Use persistent storage for session data:
import asyncio
from upsonic import Agent, Chat
from upsonic.storage import SqliteStorage


async def main():
    storage = SqliteStorage(db_file="chat.db")
    agent = Agent("anthropic/claude-sonnet-4-5")

    chat = Chat(
        session_id="session1",
        user_id="user1",
        agent=agent,
        storage=storage
    )

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


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

With Retry Configuration

Configure retry behavior for transient failures:
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,
        retry_attempts=5,
        retry_delay=2.0,
        max_concurrent_invocations=2
    )

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


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

Session and User IDs

  • session_id: Unique per conversation (e.g., "user123_session1")
  • user_id: Unique per user, can have multiple sessions
Use consistent IDs to maintain context across application restarts.