Session
The Session manages conversation state across multiple agent interactions, providing memory and context persistence. Sessions are automatically created and managed.
Overview
What is a Session?
A Session:
- Tracks conversation history across interactions
- Persists state between agent invocations
- Manages thread context
- Supports multiple storage backends
Creating Sessions
In CLI
Sessions are automatically managed:
from agentkernel.cli import CLI
# CLI automatically creates unique session per user
# Session ID is generated automatically
CLI.main()
In API
Sessions are created per 'session_id' and hence indirectly controlled by the API user:
POST /run
{
"agent": "assistant",
"message": "Hello!",
"session_id": "user-123-conversation-1"
}
Programmatically [Advanced usage]
from agentkernel.core import Session
# Create a new session
session = Session(id="custom-session-id")
# Use with runner
result = await runner.run(agent, session, prompt)
Storage Backends
In-Memory Storage (Default)
Fast but volatile:
export AK_SESSION__TYPE=in_memory
# Sessions lost when process restarts
# Good for development and testing
Redis Storage
Persistent and scalable:
export AK_SESSION__TYPE=redis
export AK_SESSION__REDIS__URL=redis://localhost:6379
# Sessions persist across restarts
# Good for production
# Supports distributed deployments
DynamoDB Storage
Serverless and fully managed:
export AK_SESSION__TYPE=dynamodb
export AK_SESSION__DYNAMODB__TABLE_NAME=agent-kernel-sessions
# Serverless, auto-scaling storage
# Good for AWS serverless deployments
# No infrastructure to manage
# Pay-per-use pricing
Session Lifecycle
Session Data
Sessions store framework-specific data:
# Each framework stores its own session data
session.set("openai_assistant_session", openai_session_obj)
session.set("langgraph_state", graph_state)
session.set("crewai_context", crew_context)
# Access later
openai_session = session.get("openai_assistant_session")
Multi-Agent Sessions
Sessions can track multiple agents:
# Same session, different agents
session = Session(id="user-123")
# Agent 1 execution
result1 = await agent1.runner.run(agent1, session, prompt1)
# session.set("agent1_session", ...)
# Agent 2 execution (same session)
result2 = await agent2.runner.run(agent2, session, prompt2)
# session.set("agent2_session", ...)
# Both agents share the same session
# But maintain separate framework-specific state
Thread Management
Sessions support conversation threads:
# Each user conversation gets a unique session
session_id = f"user-{user_id}-thread-{thread_id}"
session = Session(id=session_id)
# Conversation history maintained in session
# Agents have full context of previous interactions