Skip to content
GitHubDiscord

AI Session (lexigram-ai-session)

AI session management for the Lexigram Framework — branching, checkpointing, multi-agent sessions


Stateful conversation session management for the Lexigram AI framework. Provides full session lifecycle (create → turn → checkpoint → restore → close), pluggable persistence backends, timeline branching, and multi-agent group sessions. Zero-config usage starts with sensible defaults.

Terminal window
uv add lexigram-ai-session
from lexigram import Application
from lexigram.di.module import Module, module
from lexigram.ai.session import SessionModule
from lexigram.ai.session.config import SessionConfig
@module(imports=[
SessionModule.configure(
SessionConfig(backend="in_memory"),
enable_cleanup_scheduler=True,
)
])
class AppModule(Module):
pass
app = Application(modules=[AppModule])
if __name__ == "__main__":
app.run()

Zero-config usage: Call SessionModule.configure() with no arguments to use defaults.

application.yaml
ai_session:
backend: "cache"
session_ttl: 86400
max_turns_per_session: 1000
auto_checkpoint_interval: 10
consolidate_on_close: true
Section titled “Option 2 — Profiles + Environment Variables (recommended)”
Terminal window
export LEX_AI_SESSION__BACKEND=cache
# Environment variables for each field
from lexigram.ai.session.config import SessionConfig
from lexigram.ai.session import SessionModule
config = SessionConfig(
backend="cache",
session_ttl=86400,
max_turns_per_session=1000,
auto_checkpoint_interval=10,
)
SessionModule.configure(config)
FieldDefaultEnv varDescription
enabledTrueLEX_AI_SESSION__ENABLEDEnable the AI session subsystem
backend"in_memory"LEX_AI_SESSION__BACKENDPersistence backend: in_memory, cache, database
session_ttl86400LEX_AI_SESSION__SESSION_TTLSession max age in seconds (0 = no expiry)
cleanup_interval_s600LEX_AI_SESSION__CLEANUP_INTERVAL_SHow often expired sessions are swept
max_turns_per_session1000LEX_AI_SESSION__MAX_TURNS_PER_SESSIONHard turn cap before session is closed
max_sessions_per_user100LEX_AI_SESSION__MAX_SESSIONS_PER_USERConcurrent session limit per user
auto_checkpoint_interval10LEX_AI_SESSION__AUTO_CHECKPOINT_INTERVALCheckpoint every N turns
max_checkpoints_per_session50LEX_AI_SESSION__MAX_CHECKPOINTS_PER_SESSIONRetained checkpoints per session
max_branches_per_session10LEX_AI_SESSION__MAX_BRANCHES_PER_SESSIONMax forked branches per session
max_agents_per_group10LEX_AI_SESSION__MAX_AGENTS_PER_GROUPMax agents in a group session
default_turn_strategy"round_robin"LEX_AI_SESSION__DEFAULT_TURN_STRATEGYMulti-agent turn strategy
consolidate_on_closeTrueLEX_AI_SESSION__CONSOLIDATE_ON_CLOSETrigger memory consolidation on close
MethodDescription
SessionModule.configure(config, enable_cleanup_scheduler)Configure with explicit config
SessionModule.stub(config)Minimal config for testing
  • Session lifecycle: Create, turn, checkpoint, restore, suspend, close with FSM-enforced status transitions
  • Timeline branching: BranchManager forks sessions into independent timelines that can be merged
  • Multi-agent groups: GroupSession coordinates multiple agents with configurable turn-taking strategies
  • Persistence backends: In-memory, Redis (cache), and database stores
  • Web middleware: SessionMiddleware resolves session ID from cookie or HTTP header
  • Analytics: Session analytics with turn counts, token totals, and cost tracking
async with Application.boot(modules=[SessionModule.stub()]) as app:
# your test code
...
FileWhat it contains
src/lexigram/ai/session/module.pySessionModule.configure(), .stub()
src/lexigram/ai/session/config.pySessionConfig
src/lexigram/ai/session/manager/core.pySessionManagerImpl
src/lexigram/ai/session/branching/branch_manager.pyBranchManager
src/lexigram/ai/session/multi_agent/group_session.pyGroupSession
src/lexigram/ai/session/middleware/session_middleware.pySessionMiddleware
src/lexigram/ai/session/stores/Persistence backends
src/lexigram/ai/session/di/provider.pySessionProvider