By Sagar Shankaran, Founder of CallSphere
Learn how to build secure session management for AI agent conversations. Covers session token design, server-side storage, expiration, concurrent session handling, and forced invalidation with FastAPI.
Key takeaways
AI agent conversations are inherently stateful. Each interaction builds on previous messages, tool calls, and context. Unlike a simple REST API where each request is independent, an agent conversation requires maintaining state across multiple exchanges — the conversation history, tool execution results, user preferences, and security context.
While JWTs handle authentication (who is this user), sessions handle the conversation state (what has this user been doing with this agent). Combining both gives you stateless auth verification with stateful conversation tracking.
A conversation session for an AI agent needs more than a traditional web session. It must track the agent state, conversation history reference, and security metadata:
flowchart LR
CLIENT(["Client SDK"])
GW["API Gateway<br/>auth plus rate limit"]
APP["FastAPI app<br/>handlers and DI"]
VAL["Pydantic validation"]
SVC["Service layer<br/>business logic"]
DB[(Database)]
QUEUE[(Background queue)]
OBS[(Tracing)]
CLIENT --> GW --> APP --> VAL --> SVC
SVC --> DB
SVC --> QUEUE
SVC --> OBS
SVC --> CLIENT
style GW fill:#4f46e5,stroke:#4338ca,color:#fff
style APP fill:#f59e0b,stroke:#d97706,color:#1f2937
style DB fill:#ede9fe,stroke:#7c3aed,color:#1e1b4b
from pydantic import BaseModel
from datetime import datetime
from typing import Optional
class AgentSession(BaseModel):
session_id: str
user_id: str
org_id: str
agent_id: str
started_at: datetime
last_activity: datetime
expires_at: datetime
message_count: int = 0
tool_calls_count: int = 0
ip_address: str
user_agent: str
is_active: bool = True
metadata: dict = {}
Use cryptographically random session tokens stored in Redis for fast lookups. Redis provides natural TTL support, making session expiration automatic:
Hear it before you finish reading
Talk to a live CallSphere AI voice agent in your browser — 60 seconds, no signup.
import secrets
import json
from datetime import datetime, timezone, timedelta
import redis.asyncio as redis
redis_client = redis.from_url("redis://localhost:6379/0")
SESSION_TTL_HOURS = 4
SESSION_PREFIX = "agent_session:"
async def create_session(
user_id: str, org_id: str, agent_id: str,
ip_address: str, user_agent: str,
) -> tuple[str, AgentSession]:
session_id = secrets.token_urlsafe(32)
now = datetime.now(timezone.utc)
session = AgentSession(
session_id=session_id,
user_id=user_id,
org_id=org_id,
agent_id=agent_id,
started_at=now,
last_activity=now,
expires_at=now + timedelta(hours=SESSION_TTL_HOURS),
ip_address=ip_address,
user_agent=user_agent,
)
await redis_client.setex(
f"{SESSION_PREFIX}{session_id}",
SESSION_TTL_HOURS * 3600,
session.model_dump_json(),
)
# Track user's active sessions for concurrent session management
await redis_client.sadd(f"user_sessions:{user_id}", session_id)
return session_id, session
async def get_session(session_id: str) -> Optional[AgentSession]:
data = await redis_client.get(f"{SESSION_PREFIX}{session_id}")
if not data:
return None
return AgentSession.model_validate_json(data)
async def update_session_activity(session: AgentSession):
session.last_activity = datetime.now(timezone.utc)
session.message_count += 1
ttl = await redis_client.ttl(f"{SESSION_PREFIX}{session.session_id}")
if ttl > 0:
await redis_client.setex(
f"{SESSION_PREFIX}{session.session_id}",
ttl,
session.model_dump_json(),
)
Create a dependency that validates both the JWT (authentication) and the session (conversation state):
from fastapi import Depends, HTTPException, Header, Request
async def get_agent_session(
request: Request,
x_session_id: str = Header(...),
user: TokenPayload = Depends(get_current_user),
) -> AgentSession:
session = await get_session(x_session_id)
if not session or not session.is_active:
raise HTTPException(status_code=440, detail="Session expired or invalid")
# Verify session belongs to this user
if session.user_id != user.sub:
raise HTTPException(status_code=403, detail="Session does not belong to user")
# Verify IP consistency (optional — strict mode)
client_ip = request.client.host
if session.ip_address != client_ip:
raise HTTPException(
status_code=403,
detail="Session IP mismatch — possible session hijacking",
)
await update_session_activity(session)
return session
Limit the number of active agent sessions per user to prevent abuse and resource exhaustion:
MAX_CONCURRENT_SESSIONS = 5
async def enforce_session_limit(user_id: str):
session_ids = await redis_client.smembers(f"user_sessions:{user_id}")
active_sessions = []
for sid in session_ids:
sid_str = sid.decode() if isinstance(sid, bytes) else sid
session = await get_session(sid_str)
if session and session.is_active:
active_sessions.append(session)
else:
# Clean up expired session references
await redis_client.srem(f"user_sessions:{user_id}", sid_str)
if len(active_sessions) >= MAX_CONCURRENT_SESSIONS:
# Terminate the oldest session
oldest = min(active_sessions, key=lambda s: s.started_at)
await invalidate_session(oldest.session_id)
Support both single-session and all-session invalidation. All-session invalidation is critical for password changes and security incidents:
async def invalidate_session(session_id: str):
session = await get_session(session_id)
if session:
session.is_active = False
await redis_client.setex(
f"{SESSION_PREFIX}{session_id}",
60, # Keep briefly for graceful cleanup
session.model_dump_json(),
)
await redis_client.srem(
f"user_sessions:{session.user_id}", session_id
)
async def invalidate_all_sessions(user_id: str):
"""Nuclear option — invalidate all sessions for a user."""
session_ids = await redis_client.smembers(f"user_sessions:{user_id}")
for sid in session_ids:
sid_str = sid.decode() if isinstance(sid, bytes) else sid
await redis_client.delete(f"{SESSION_PREFIX}{sid_str}")
await redis_client.delete(f"user_sessions:{user_id}")
The conversation endpoint uses both authentication and session management:
Still reading? Stop comparing — try CallSphere live.
CallSphere ships complete AI voice agents per industry — 14 tools for healthcare, 10 agents for real estate, 4 specialists for salons. See how it actually handles a call before you book a demo.
@router.post("/agents/{agent_id}/chat")
async def chat_with_agent(
agent_id: str,
message: str,
session: AgentSession = Depends(get_agent_session),
user: TokenPayload = Depends(get_current_user),
):
# Session already validated — agent_id matches, user verified
response = await run_agent(agent_id, message, session.session_id)
return {"response": response, "message_count": session.message_count}
JWTs are great for authentication but poorly suited for session state. You cannot invalidate a JWT before it expires without maintaining a server-side revocation list — which defeats the purpose of stateless tokens. Sessions stored in Redis give you instant invalidation, concurrent session tracking, and the ability to store conversation metadata that would bloat a JWT.
For conversation sessions, losing them on a Redis restart is usually acceptable — the user starts a new conversation. If persistence matters, configure Redis with AOF (Append Only File) persistence or use Redis Cluster with replication. For critical session data like tool execution state, persist checkpoints to PostgreSQL alongside the Redis session.
It depends on the use case. For interactive chat agents, 30 minutes to 4 hours of inactivity is reasonable. For long-running autonomous agents executing multi-step tasks, sessions may need to last hours or days — use a sliding window that extends the TTL on each activity. Always provide an explicit "end session" action so users can terminate sessions voluntarily.
#SessionManagement #FastAPI #AIAgents #Redis #Security #Stateful #AgenticAI #LearnAI #AIEngineering

Written by
Sagar Shankaran· Founder, CallSphere
LinkedInSagar Shankaran is the founder of CallSphere, where he builds production AI voice and chat agents deployed across healthcare, hospitality, real estate, and home services. He writes about agentic AI, LLM engineering, and shipping voice agents that handle real calls in production.
See how AI voice agents work for your industry. Live demo available -- no signup required.
A founder's guide to the personal AI assistant market: best AI assistant apps, business-grade options, and how CallSphere's voice agent fits in.
A founder's guide to free AI agents, low-code AI agent builders, and how to know when you should pay for a real platform like CallSphere.
Graphiti is the open-source temporal knowledge graph for AI agents in 2026. Learn how bi-temporal memory beats vector RAG for voice agents and long-running LLMs.
Chatbot app vs ChatGPT in 2026: a founder's clear take on the difference, when to use which, and how a real AI chatbot app development works.
How we built a fault-tolerant HVAC emergency triage and tech-dispatch platform on Kubernetes — three-tier CQRS, 11 micro-agents on the OpenAI Agents SDK + LangGraph, NATS JetStream, DTMF/SMS/WebSocket acceptance, circuit breakers, and an evaluation pipeline that catches regressions before they wake a tech at 3 AM.
Head-to-head: OpenAI Frontier and Anthropic's managed agent stack — strengths, fit, and what each means for enterprise AI voice and chat deployment.
© 2026 CallSphere Inc. All rights reserved.
Made within San Francisco
Watch how CallSphere handles real customer calls, schedules appointments, and processes payments — live.
Try Live DemoBook a DemoCalculate Your ROI