A Python-based session manager for Claude Pro that automates the creation and tracking of Claude sessions to optimize usage within 5-hour windows. The system provides intelligent session lifecycle management through automated scheduling and comprehensive status tracking.
- π Automated Session Management: Seamlessly create and track Claude Pro sessions
- β° 5-Hour Window Optimization: Intelligent tracking of session expiration times
- π Real-time Status Monitoring: Check current session status and remaining time
- π§ Clean Architecture: Well-structured codebase with clear separation of concerns
- π‘οΈ Error Handling: Comprehensive error management and user-friendly messages
- π¨ Rich Terminal Output: Beautiful formatted output using Rich library
- π Force Session Override: Option to force new sessions when needed
The application follows clean architecture principles with distinct layers:
src/
βββ claude_code_session_manager/
βββ domain/ # Core business models and exceptions
βββ infrastructure/ # External dependencies (Claude CLI, file storage)
βββ services/ # Business logic (SessionManager)
βββ config/ # Application settings and configuration
βββ utils/ # Formatting utilities
- Python 3.10 or higher
- Claude CLI installed and configured
- macOS (primary platform, with plans for cross-platform support)
# Install pipx if not already installed
brew install pipx
pipx ensurepath # apoi deschide un shell nou
# Clone and install
git clone <repository-url>
cd claude-code-sessions-manager
pipx install .
# Verify installation
claude-sessions status-
Clone the repository:
git clone <repository-url> cd claude-code-sessions-manager
-
Install dependencies:
pip install -r requirements.txt
-
Verify Claude CLI installation:
claude --help
With pipx installation (recommended):
# Start a new session immediately
claude-sessions start-now
# Check current session status
claude-sessions status
# Force start a new session (override active session)
claude-sessions start-now -fWith development setup:
# Start a new session immediately
python -m claude_code_session_manager start-now
# Check current session status
python -m claude_code_session_manager status
# Force start a new session (override active session)
python -m claude_code_session_manager start-now -f| Command | Description | Options |
|---|---|---|
start-now |
Creates and activates a new Claude session | -f, --force: Override existing active session |
status |
Displays current session information and status | None |
$ claude-sessions start-now
β
Sesiune activatΔ cu succes!
$ claude-sessions status
π Sesiune Claude Pro
βββ π
CreatΔ: 2024-01-15 14:30:25
βββ β° ExpirΔ: 2024-01-15 19:30:25
βββ β
Status: ActivΔ
βββ π Timp rΔmas: 4h 23mThe application uses environment variables for configuration with sensible defaults:
| Variable | Default | Description |
|---|---|---|
SESSION_DURATION_HOURS |
5 |
Session window duration in hours |
SESSION_FILE_PATH |
"session.json" |
Path to session storage file |
CLAUDE_TIMEOUT_SECONDS |
10 |
Claude CLI command timeout |
CLAUDE_MAX_TURNS |
1 |
Maximum conversation turns |
CLAUDE_OUTPUT_FORMAT |
"json" |
Claude CLI output format |
# Temporary (current session)
export SESSION_DURATION_HOURS=6
export SESSION_FILE_PATH="/path/to/custom/session.json"
# Permanent (add to ~/.bashrc or ~/.zshrc)
echo 'export SESSION_DURATION_HOURS=6' >> ~/.zshrc# Install development dependencies
pip install -r requirements.txt
# Install pre-commit hooks (if using)
pre-commit install# Linting
ruff check .
# Code formatting
ruff format .
# Type checking
basedpyright# Run all tests
python run_tests.py
# Run with different verbosity levels
python run_tests.py -v 1 # Less verbose
python run_tests.py -q # Minimal output
# Run specific test module
python run_tests.py -m test_modelsclaude-code-sessions-manager/
βββ src/
β βββ claude_code_session_manager/ # Main package directory
β βββ __init__.py
β βββ __main__.py # Module entry point (python -m)
β βββ main.py # CLI entry point
β βββ config/
β β βββ __init__.py
β β βββ settings.py # Configuration management
β βββ domain/
β β βββ __init__.py
β β βββ models.py # Session model
β β βββ exceptions.py # Custom exceptions
β βββ infrastructure/
β β βββ __init__.py
β β βββ claude_client.py # Claude CLI integration
β β βββ storage.py # File-based session storage
β βββ services/
β β βββ __init__.py
β β βββ session_manager.py # Core business logic
β βββ utils/
β βββ __init__.py
β βββ formatters.py # Output formatting utilities
βββ tests/ # Comprehensive test suite
β βββ __init__.py
β βββ config/
β β βββ __init__.py
β β βββ test_settings.py
β βββ domain/
β β βββ __init__.py
β β βββ test_models.py
β β βββ test_exceptions.py
β βββ infrastructure/
β β βββ __init__.py
β β βββ test_claude_client.py
β β βββ test_storage.py
β βββ services/
β β βββ __init__.py
β β βββ test_session_manager.py
β βββ utils/
β βββ __init__.py
β βββ test_helpers.py
βββ pyproject.toml # Modern Python packaging configuration
βββ requirements.txt # Python dependencies
βββ run_tests.py # Test runner script
βββ CLAUDE.md # Development instructions
βββ README.md # This file
- Session Activation: Tests Claude CLI availability and creates a new session
- Data Persistence: Stores session metadata in JSON format for reliability
- Status Tracking: Monitors session expiration and provides real-time status
- CLI Integration: Seamless integration with Claude CLI for actual AI interactions
graph LR
A[Start Session] --> B[Test Claude CLI]
B --> C[Create Session]
C --> D[Store Data]
D --> E[Active Session]
E --> F{5 Hours Passed?}
F -->|No| G[Session Active]
F -->|Yes| H[Session Expired]
G --> F
H --> A
Claude CLI not found:
# Verify Claude CLI installation
which claude
# If not found, install from: https://github.com/anthropics/claude-cliPermission denied on session file:
# Check file permissions
ls -la session.json
# Fix permissions if needed
chmod 644 session.jsonSession not activating:
# Check Claude CLI status
claude --help
# Verify network connectivity
ping anthropic.com- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Run tests and linting (
python run_tests.py && ruff check .) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
The application includes LaunchAgent integration for automated session management that runs every 5 minutes.
# Install LaunchAgent for automated scheduling
./install-scheduler.sh
# Uninstall LaunchAgent
./uninstall-scheduler.shIf you prefer manual setup:
-
Copy the template:
cp com.claude-sessions.scheduler.plist.template com.claude-sessions.scheduler.plist
-
Replace placeholders:
{{USER_HOME}}: Your home directory (e.g.,/Users/yourusername){{VENV_PATH}}: Path to your Python virtual environment
-
Install the LaunchAgent:
cp com.claude-sessions.scheduler.plist ~/Library/LaunchAgents/ launchctl load ~/Library/LaunchAgents/com.claude-sessions.scheduler.plist
# Check LaunchAgent status
launchctl list | grep claude-sessions
# Monitor logs
tail -f ~/.local/share/claude-sessions/scheduler.out
# View errors
tail -f ~/.local/share/claude-sessions/scheduler.errImportant: When using LaunchAgent automation on macOS, you may encounter permission prompts that prevent the scheduler from running automatically. This happens because Python needs permission to execute the Claude CLI.
- Open System Settings > Privacy & Security > Full Disk Access
- Click the + button to add applications
- Navigate to your Python executable (usually
/usr/bin/python3or your virtual environment's Python) - Add Python to the Full Disk Access list
- Optionally, also add Terminal for additional compatibility
If Full Disk Access seems too broad, you can try the more specific approach:
- Open System Settings > Privacy & Security > Automation
- Look for Python or Terminal in the list
- Enable permissions for Claude CLI or other relevant applications
After granting permissions, test the LaunchAgent:
# Manually trigger the LaunchAgent to test
launchctl start com.claude-sessions.scheduler
# Check if it ran successfully
tail ~/.local/share/claude-sessions/scheduler.outNote: This permission setup is required only once. The LaunchAgent will then run automatically every 5 minutes without further prompts.
- π Cross-platform Support: Windows and Linux compatibility
- π Usage Analytics: Session usage statistics and insights
- π Notifications: Alert system for session expiration
- π― Smart Scheduling: Intelligent session timing based on usage patterns
This project is licensed under the MIT License - see the LICENSE file for details.
For issues and questions:
- π Bug Reports: Open an issue on GitHub
- π‘ Feature Requests: Create a feature request issue
- π§ General Questions: Use GitHub Discussions
Made with β€οΈ for optimizing Claude Pro usage