Course contentsShow
← AI Engineering
Lesson 720 of 1,88618. Conversational AI and ChatbotsFree lesson

Session Timeout and Expiration

Implementing session TTLs, idle timeouts, and cleanup strategies for inactive conversations.

Session Timeout and Expiration

What you'll learn: How to automatically clean up idle conversations and prevent stale sessions from consuming resources in your conversational AI system.

Why Sessions Need to Expire

When you store conversation state (whether in-memory or in a database), inactive sessions can pile up like abandoned shopping carts in a parking lot. Without automatic cleanup, you'll waste memory, database space, and processing time checking sessions that users have long forgotten about.

Core Expiration Strategies

Time-To-Live (TTL)

Set an absolute expiration time when the session is created. For example, "this conversation expires 24 hours after it starts, no matter what." This works like a parking ticket that's valid for one day—after that, it's void.

import time

session = {
    "id": "user123",
    "created_at": time.time(),
    "ttl": 86400,  # 24 hours in seconds
    "messages": []
}

def is_session_expired(session):
    age = time.time() - session["created_at"]
    return age > session["ttl"]

Idle Timeout

Reset the expiration timer every time the user interacts. If 30 minutes pass with no activity, the session ends. This is like a gym locker that stays yours as long as you keep using it, but gets cleared after extended inactivity.

session["last_activity"] = time.time()
session["idle_timeout"] = 1800  # 30 minutes

def check_idle_timeout(session):
    idle_time = time.time() - session["last_activity"]
    return idle_time > session["idle_timeout"]

Cleanup Strategies

Run periodic background jobs to scan and delete expired sessions. Alternatively, check expiration lazily when sessions are accessed—if expired, delete immediately.

Key Takeaway: Implementing session TTLs and idle timeouts prevents resource waste by automatically removing conversations users have abandoned, keeping your system efficient and responsive.