Building a Pluggable AI Agent System with Django
How HyperSaaS implements a multi-framework agent architecture that supports LangGraph, PydanticAI, and custom handlers.
The Problem with Monolithic AI Integrations
Most SaaS boilerplates hard-wire a single AI framework into their codebase. When you want to switch from LangChain to PydanticAI, or add a custom inference pipeline, you're looking at a full rewrite.
Our Approach: Framework-Agnostic Boundaries
HyperSaaS introduces a simple but powerful pattern:
- AgentMessage — a framework-agnostic dataclass that all handlers produce
- BaseAgentHandler — an abstract base class defining
invoke()andastream() - Plain Python tools — no
@tooldecorators, no framework imports
Each handler converts its native output (LangChain messages, PydanticAI results) into AgentMessage at the boundary. The service layer never imports any AI framework.
Adding a New Framework
To add support for a new AI framework:
- Create a new handler in
chat/handlers/your_framework/ - Implement
BaseAgentHandler.invoke()andastream() - Register it in the handler factory
- Set
agent_frameworkon the chat session
The tools, service layer, and frontend all work without changes.
Why This Matters
Your AI stack will evolve. New frameworks emerge constantly. By keeping a clean boundary between your business logic and your AI runtime, you can adopt new tools without rewriting your application.