Skip to main content

Session manager for AI applications

Project description

CHUK AI Session Manager

A powerful session management system for AI applications

Python 3.11+ License: MIT

Automatic conversation tracking, token usage monitoring, tool call logging, infinite context support with automatic summarization, and hierarchical session relationships. Perfect for AI applications that need reliable session management.

🚀 Quick Start

Installation Options

# Basic installation (memory storage only)
pip install chuk-ai-session-manager

# With Redis support for production
pip install chuk-ai-session-manager[redis]

# With enhanced token counting
pip install chuk-ai-session-manager[tiktoken]

# Full installation with all optional features
pip install chuk-ai-session-manager[all]

# Development installation
pip install chuk-ai-session-manager[dev]

Quick Example

from chuk_ai_session_manager import track_conversation

# Track any conversation automatically
session_id = await track_conversation(
    user_message="What's the weather like?",
    ai_response="I don't have access to real-time weather data.",
    model="gpt-3.5-turbo",
    provider="openai"
)

print(f"Conversation tracked in session: {session_id}")

That's it! Zero configuration required.

⚡ Major Features

🎯 Zero-Configuration Tracking

from chuk_ai_session_manager import SessionManager

# Just start using it
sm = SessionManager()
await sm.user_says("Hello!")
await sm.ai_responds("Hi there!", model="gpt-4")

# Get stats instantly
stats = await sm.get_stats()
print(f"Tokens: {stats['total_tokens']}, Cost: ${stats['estimated_cost']:.4f}")

🔄 Infinite Context

# Automatically handles conversations longer than token limits
sm = SessionManager(infinite_context=True, token_threshold=4000)
await sm.user_says("Tell me about the history of computing...")
await sm.ai_responds("Computing history begins with...", model="gpt-4")
# Session will auto-segment when limits are reached

⚙️ Storage Backends

Installation Storage Use Case Performance
pip install chuk-ai-session-manager Memory Development, testing 1.8M ops/sec
pip install chuk-ai-session-manager[redis] Redis Production, persistence 20K ops/sec

🛠️ Tool Integration

# Automatic tool call tracking
await sm.tool_used(
    tool_name="calculator",
    arguments={"operation": "add", "a": 5, "b": 3},
    result={"result": 8}
)

💡 Common Use Cases

Web App Conversation Tracking

from chuk_ai_session_manager import track_conversation

# In your chat endpoint
session_id = await track_conversation(
    user_message=request.message,
    ai_response=ai_response,
    model="gpt-4",
    provider="openai",
    session_id=request.session_id  # Continue existing conversation
)

LLM Wrapper with Automatic Tracking

from chuk_ai_session_manager import track_llm_call
import openai

async def my_openai_call(prompt):
    response = await openai.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=[{"role": "user", "content": prompt}]
    )
    return response.choices[0].message.content

# Automatically tracked
response, session_id = await track_llm_call(
    user_input="Explain machine learning",
    llm_function=my_openai_call,
    model="gpt-3.5-turbo",
    provider="openai"
)

Long Conversations with Auto-Segmentation

from chuk_ai_session_manager import track_infinite_conversation

# Start a conversation
session_id = await track_infinite_conversation(
    user_message="Tell me about the history of computing",
    ai_response="Computing history begins with ancient calculating devices...",
    model="gpt-4",
    token_threshold=4000  # Auto-segment after 4000 tokens
)

# Continue the conversation - will auto-segment if needed
session_id = await track_infinite_conversation(
    user_message="What about quantum computers?",
    ai_response="Quantum computing represents a fundamental shift...",
    session_id=session_id,
    model="gpt-4"
)

🔧 Configuration

Storage Configuration

# Memory provider (default) - fast, no persistence
export SESSION_PROVIDER=memory

# Redis provider - persistent, production-ready (requires redis extra)
export SESSION_PROVIDER=redis
export SESSION_REDIS_URL=redis://localhost:6379/0

Installation Matrix

Command Memory Redis Token Counting Use Case
pip install chuk-ai-session-manager Basic Development
pip install chuk-ai-session-manager[redis] Basic Production
pip install chuk-ai-session-manager[tiktoken] Enhanced Better accuracy
pip install chuk-ai-session-manager[all] Enhanced Full features

📊 Monitoring & Analytics

# Get comprehensive session analytics
stats = await sm.get_stats(include_all_segments=True)

print(f"""
🚀 Session Analytics Dashboard
============================
Session ID: {stats['session_id']}
Total Messages: {stats['total_messages']}
User Messages: {stats['user_messages']}
AI Messages: {stats['ai_messages']}
Tool Calls: {stats['tool_calls']}
Total Tokens: {stats['total_tokens']}
Total Cost: ${stats['estimated_cost']:.6f}
Session Segments: {stats.get('session_segments', 1)}
""")

🏗️ Why CHUK AI Session Manager?

  • Zero Configuration: Start tracking conversations in 3 lines of code
  • Infinite Context: Never worry about token limits again
  • Universal: Works with any LLM provider (OpenAI, Anthropic, etc.)
  • Production Ready: Built-in persistence, monitoring, and error handling
  • Token Aware: Automatic cost tracking across all providers
  • Tool Friendly: Seamless tool call logging and retry mechanisms

🛡️ Error Handling

from chuk_ai_session_manager import (
    SessionManagerError,
    SessionNotFound,
    TokenLimitExceeded
)

try:
    session_id = await track_conversation("Hello", "Hi there")
except SessionNotFound as e:
    print(f"Session not found: {e}")
except TokenLimitExceeded as e:
    print(f"Token limit exceeded: {e}")
except SessionManagerError as e:
    print(f"General session error: {e}")

🔄 Dependencies

  • Required: chuk-sessions (session storage), pydantic (data models), chuk-tool-processor (tool integration)
  • Optional: redis (Redis storage), tiktoken (accurate token counting)

📄 License

MIT License - build amazing AI applications with confidence!


Ready to build better AI applications?

pip install chuk-ai-session-manager

Start tracking conversations in 30 seconds!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

chuk_ai_session_manager-0.7.tar.gz (75.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

chuk_ai_session_manager-0.7-py3-none-any.whl (43.3 kB view details)

Uploaded Python 3

File details

Details for the file chuk_ai_session_manager-0.7.tar.gz.

File metadata

  • Download URL: chuk_ai_session_manager-0.7.tar.gz
  • Upload date:
  • Size: 75.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.2

File hashes

Hashes for chuk_ai_session_manager-0.7.tar.gz
Algorithm Hash digest
SHA256 7d587177a4898e59a224e6c462f0a76e5262c68aa7bac9fcf667492d55e94b1e
MD5 a37172a7c916a42363e28fdee7a5d2ef
BLAKE2b-256 662401b8bd5e772fb578ec16ba3ae3b5553d656f6c1801030520d8a4bb54aa54

See more details on using hashes here.

File details

Details for the file chuk_ai_session_manager-0.7-py3-none-any.whl.

File metadata

File hashes

Hashes for chuk_ai_session_manager-0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 8c49bb7f4194b34d50eca3604e9aa28166bcca1e6b89548456dccbd2d0b1dee2
MD5 e88abc0e18f57444141d518c9b02531a
BLAKE2b-256 b6fde7ef3fa16a116302083d0bf30fc3bd439ab540edbdb0008aa9ae7d9d73f4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page