Session Management
Persistent conversations with metadata, analytics, and complete serialization support.
Overview
AbstractCore's session management system provides persistent conversations that maintain context across multiple interactions. Sessions can be saved, loaded, and analyzed with built-in analytics.
Key Features
- Persistent Conversations: Maintain context across sessions
- Auto-Compaction: SOTA 2025 algorithm compresses while preserving context
- Complete Serialization: Save/load entire session state
- Advanced Analytics: Generate summaries, assessments, and fact extraction
- Metadata Support: Rich metadata including location, timestamps, and custom fields
- Tool Integration: Full support for tool calling within sessions
Basic Usage
Creating a Session
from abstractcore import create_llm, BasicSession
# Create LLM provider
llm = create_llm("openai", model="gpt-4o-mini")
# Create session with system prompt
session = BasicSession(
llm=llm,
system_prompt="You are a helpful AI assistant specializing in Python development."
)
# Start conversation
response1 = session.generate("Hello, I'm working on a Python project.")
print(response1.content)
# Context is maintained
response2 = session.generate("Can you help me with error handling?")
print(response2.content)
# Auto-compaction with SOTA 2025 algorithm
session.compact(target_tokens=8000) # Compresses while preserving context
Session with Tools
from abstractcore.tools import tool
@tool
def get_current_time() -> str:
"""Get the current time."""
from datetime import datetime
return datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# Create session with tools
session = BasicSession(
llm=llm,
system_prompt="You are a helpful assistant with access to tools.",
tools=[get_current_time]
)
response = session.generate("What time is it?")
print(response.content)
Session Serialization
Saving Sessions
# Basic save
session.save("my_conversation.json")
# Save with analytics
session.save(
"my_conversation.json",
summary=True, # Generate conversation summary
assessment=True, # Generate quality assessment
facts=True # Extract key facts
)
# Save with custom metadata
session.save(
"my_conversation.json",
metadata={
"project": "Python Tutorial",
"user_level": "beginner",
"session_type": "coding_help"
}
)
Loading Sessions
# Load existing session
loaded_session = BasicSession.load("my_conversation.json")
# Continue conversation
response = loaded_session.generate("Let's continue where we left off.")
print(response.content)
# Access session metadata
print(f"Session created: {loaded_session.created_at}")
print(f"Message count: {len(loaded_session.messages)}")
print(f"Provider: {loaded_session.provider_name}")
print(f"Model: {loaded_session.model_name}")
Session Analytics
Generate Summary
# Generate conversation summary with style options
summary = session.generate_summary(style="executive", length="brief")
print("Summary:", summary.summary)
print("Key topics:", summary.key_topics)
print("Participant count:", summary.participant_count)
Quality Assessment
# Generate quality assessment with custom criteria
assessment = session.generate_assessment(criteria=["clarity", "helpfulness"])
print("Overall score:", assessment.overall_score)
print("Helpfulness:", assessment.helpfulness_score)
print("Accuracy:", assessment.accuracy_score)
print("Clarity:", assessment.clarity_score)
print("Feedback:", assessment.feedback)
Fact Extraction
# Extract key facts with entity type filtering
facts = session.extract_facts(entity_types=["person", "organization", "date"])
for fact in facts.facts:
print(f"- {fact.content} (confidence: {fact.confidence})")
print(f" Category: {fact.category}")
print(f" Source: {fact.source_message_index}")
print()
Message Metadata
Adding Metadata to Messages
# Add message with metadata
session.add_message(
role="user",
content="Hello from New York!",
name="Alice",
location="New York, NY",
metadata={
"device": "mobile",
"app_version": "1.2.3",
"user_id": "user_123"
}
)
# Generate response
response = session.generate("How's the weather there?")
# Access message metadata
for message in session.messages:
if message.metadata:
print(f"Message from {message.name} in {message.location}")
print(f"Device: {message.metadata.get('device', 'unknown')}")
print(f"Content: {message.content}")
print()
Location-Aware Conversations
# Set user location for context
session.add_message(
role="user",
content="What's a good restaurant nearby?",
location="San Francisco, CA"
)
response = session.generate()
# Assistant can use location context in response
Advanced Features
Session Configuration
# Advanced session configuration
session = BasicSession(
llm=llm,
system_prompt="You are a helpful assistant.",
max_context_messages=20, # Limit context window
auto_save=True, # Auto-save after each interaction
auto_save_path="auto_save.json",
metadata={
"session_type": "customer_support",
"priority": "high"
}
)
Session Events
from abstractcore.events import EventType, on_global
def log_session_activity(event):
"""Log all session activities."""
print(f"Session event: {event.event_type}")
print(f"Session ID: {event.data.get('session_id')}")
print(f"Message count: {event.data.get('message_count')}")
# Subscribe to session events
on_global(EventType.GENERATION_COMPLETED, log_session_activity)
Batch Processing
# Process multiple sessions
session_files = ["session1.json", "session2.json", "session3.json"]
for file_path in session_files:
session = BasicSession.load(file_path)
# Generate analytics for each session
summary = session.generate_summary()
assessment = session.generate_assessment()
print(f"Session: {file_path}")
print(f"Summary: {summary.summary}")
print(f"Quality Score: {assessment.overall_score}")
print("-" * 50)
Session File Format
Sessions are saved in a structured JSON format with complete state preservation:
{
"format_version": "session-archive/v1",
"created_at": "2025-01-14T10:30:00Z",
"updated_at": "2025-01-14T10:45:00Z",
"provider_name": "OpenAIProvider",
"model_name": "gpt-4o-mini",
"system_prompt": "You are a helpful assistant.",
"parameters": {
"max_tokens": 16384,
"temperature": 0.7
},
"tools": [...],
"messages": [
{
"role": "user",
"content": "Hello!",
"timestamp": "2025-01-14T10:30:00Z",
"metadata": {
"name": "Alice",
"location": "New York, NY"
}
}
],
"analytics": {
"summary": {...},
"assessment": {...},
"facts": {...}
},
"metadata": {
"session_type": "conversation",
"total_messages": 5,
"total_tokens": 1250
}
}