Build a production chatbot with FastAPI, Redis conversation history, and the Anthropic Claude API.
Production Chatbot Architecture
# FastAPI chatbot with Redis session history
from fastapi import FastAPI
from pydantic import BaseModel
import anthropic, redis, json
app = FastAPI()
client = anthropic.Anthropic()
r = redis.Redis(decode_responses=True)
class ChatRequest(BaseModel):
session_id: str
message: str
@app.post('/chat')
async def chat(req: ChatRequest):
# Load history
key = 'chat:' + req.session_id
history = json.loads(r.get(key) or '[]')
# Add user message
history.append({'role':'user','content':req.message})
if len(history) > 20:
history = history[-20:] # sliding window
# Call LLM
response = client.messages.create(
model='claude-opus-4-5', max_tokens=500,
system='You are a helpful customer support agent for EzyCoders.',
messages=history
)
reply = response.content[0].text
# Persist updated history
history.append({'role':'assistant','content':reply})
r.setex(key, 3600, json.dumps(history)) # 1hr TTL
return {'reply': reply, 'session_id': req.session_id}