Real World Example

A Banking-style Support Chat

Bank and fintech chatbots (HDFC EVA, ICICI iPal, PhonePe and Paytm help) combine everything in this module: per-session memory, streaming answers, strict error handling, and a hand-over to a human. The ShopEase chat architecture:

Next.js chat UI ──POST /api/chat (SSE)──► FastAPI
                                           │  load history + summary (Redis, key chat:c-77, TTL 24h)
                                           │  retrieve policy chunks (RAG)  +  tools (orders)
                                           ▼
                                     Claude API (stream)
                                           │
                     events ◄──────────────┘   save history; summarize when > 20 turns
                     "Talk to a human" button → creates a Freshdesk/Zendesk ticket with the summary

Session Storage with Redis

import json
import redis.asyncio as redis

r = redis.from_url("redis://localhost:6379")
TTL = 24 * 3600

async def load_history(cid: str) -> list:
    raw = await r.get(f"chat:{cid}")
    return json.loads(raw) if raw else []

async def save_history(cid: str, history: list) -> None:
    serializable = [
        {"role": m["role"],
         "content": m["content"] if isinstance(m["content"], str)
                    else [b.model_dump() if hasattr(b, "model_dump") else b for b in m["content"]]}
        for m in history
    ]
    await r.set(f"chat:{cid}", json.dumps(serializable), ex=TTL)

Tip: Store the full content blocks the API returned (not just the text) so thinking and tool blocks can be sent back correctly on the next turn. Keep chat logs only as long as your privacy policy allows.