Skip to content

Sessions

A Session represents a conversation. It stores messages, context, and links to associated tasks.

Creating Sessions

from redis_agent_kit import SessionManager
import redis.asyncio as redis

client = redis.from_url("redis://localhost:6379", decode_responses=True)
session_manager = SessionManager(client)

# Create a session
session = await session_manager.create_session()
print(session.session_id)  # "01HXYZ..."

# Create with context
session = await session_manager.create_session(context={
    "user_id": "user_123",
    "session_type": "support",
})

Adding Messages

# Add user message
await session_manager.add_message(
    session.session_id,
    role="user",
    content="What is Redis?",
)

# Add assistant response
await session_manager.add_message(
    session.session_id,
    role="assistant",
    content="Redis is an in-memory data store...",
)

Retrieving Messages

messages = await session_manager.get_messages(session.session_id)
for msg in messages:
    print(f"{msg.role}: {msg.content}")

Context

Store arbitrary data with the session:

# Set context at creation
session = await session_manager.create_session(context={"user_id": "123"})

# Update context later
await session_manager.update_context(session.session_id, {
    "user_id": "123",
    "premium": True,
    "last_topic": "databases",
})

# Get session with context
session = await session_manager.get_session(session.session_id)
print(session.context)  # {"user_id": "123", "premium": True, ...}

Session and Tasks

Sessions contain multiple tasks (one per agent turn):

from redis_agent_kit import TaskManager

task_manager = TaskManager(client)

# Create tasks in a session
task1 = await task_manager.create_task(session_id=session.session_id, message="What is Redis?")
task2 = await task_manager.create_task(session_id=session.session_id, message="How do I install it?")

# Get all tasks for a session
tasks = await task_manager.list_tasks(session_id=session.session_id)

Deleting Sessions

await session_manager.delete_session(session.session_id)

Custom Prefix

session_manager = SessionManager(client, prefix="myapp")
# Keys: myapp:session:{id}

Session Fields

Field Type Description
session_id str Unique ID (ULID)
context dict Arbitrary context data
created_at datetime Creation time
updated_at datetime Last update time