Skip to content

feat: session TTL + turn limit to prevent O(N²) token growth #4

@oct26th

Description

@oct26th

Problem

Every message resumes the full conversation history, causing token usage to grow O(N²) with turn count. There's currently no mechanism to bound session lifetime or notify users of context usage.

Proposed solution

  1. Session TTL — auto-clear session after N hours of idle (env: SESSION_TTL_HOURS, default 24, 0 = disable)
  2. Turn limit — auto-clear session after N turns (env: SESSION_MAX_TURNS, default 50, 0 = disable)
  3. /usage command — show session age, idle time, turn count, and cumulative cost

DB: two new columns (turn_count, total_cost_usd) added via migration — fully backwards compatible.

I've implemented this in my fork: https://github.com/oct26th/claudecode-discord — happy to open a PR if there's interest.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions