Build AI agents
in Django.

Batteries included: agents, workflows, subagents, oversight, and RAG when you need it. Ready in days.

uv add django-ai-sdk
Go to docs →
Django AI SDK mascot
01 / Setup

Create an AI agent in minutes.

1. Install

Add the package and configure Django.

Terminal
uv add django-ai-sdk
# or
pip install django-ai-sdk
settings.py
INSTALLED_APPS = [
    ...
    "django_ai_sdk",
]
Terminal
python manage.py migrate

2. Define your agent

Create a class with name, model, and instructions.

agents.py
from django_ai_sdk import Agent
from django_ai_sdk.adapters.base import Stream
from django_ai_sdk.generators import openai_responses_chat
from haystack import Pipeline

class HelpDeskAgent(Agent):
    name = "Help Desk"
    model = "gpt-5-mini"
    instructions = "You are a helpful support agent."
    llm = openai_responses_chat

    async def get_pipeline_adapter(self, thread_id=None, user=None):
        storage_adapter = await self.get_storage_adapter(thread_id)
        generator = self.get_llm()
        return Stream(
            pipeline=Pipeline(),
            generator=generator,
            storage_adapter=storage_adapter,
        )

3. Add RAG, when you need it

Optional: override two methods to ground answers in your documents.

agents.py
from django_ai_sdk.rags import (
    BM25QueryExpanderRAG,
    BM25QueryExpanderRAGConfig,
)

async def get_rag_queryset(self, memory_id=None):
    if memory_id:
        return Entry.objects.filter(memory_id=memory_id)
    return Entry.objects.all()

async def get_rag_pipeline(self, memory_id=None):
    documents = await self.get_rag_documents(memory_id)
    if not documents:
        return None
    return BM25QueryExpanderRAG(
        documents=documents,
        config=BM25QueryExpanderRAGConfig(top_k=5),
    )

4. Return streaming response

Use as_view() to return an SSE response.

views.py
from .agents import HelpDeskAgent

agent = HelpDeskAgent()

@router.post("/chat")
async def chat(request, payload: ChatRequest):
    return await agent.as_view(
        payload.messages,
        thread_id=payload.thread_id,
    )
02 / In action

More than chat with your documents.

Stream every token

Answers stream in as they are written. Reasoning, tool calls and citations arrive as their own events, and the Vercel AI SDK and OpenAI clients render them as they come in.

chat > work-agentWhat's the weather in Amsterdam?WWork AgentReasoned for 2sget_weather(city="Amsterdam")It's 7°C and cloudy in Amsterdam, with 40 km/h wind from thewest. Rain is expected after 16:00, so bring a coat ▍1 knmi.nl2 weather.comMessage Work Agent

Workflows that wait for a person

Chain agents into steps that run in a worker. A step can stop for approval and continue on the next worker after a restart.

workflows > refund-customerRunningevent:new-ticket“send-refund” is waiting for approvalRefund €42.00 to order #1048, please verifyRejectApproveStepstriage-agentCompleted in 1.1ssend-refundWaiting for approvalnotify-customerPending

Oversight in one class

Block prompt injection, mask personal data and send risky turns to a person. Lenses ask the questions and rules act on the answers, in one Python class.

oversight > support-policyPrompt injection“Ignore all previous instructions…”BlockedPersonal dataCall me atMaskedIBAN in an answerQueued for the privacy teamQueuedOrder status questionNo rule matchedAllowed

Subagents for the hard parts

A coordinator splits the job between specialists that run in parallel, each with its own tools and instructions.

subagents > swarmCoordinatorplans and delegatesResearcherresearchAnalystcodeWriterwrite3 subagents ran in parallel in 4.2s
03 / What's included

Agents

Run the same agent from a view, a background task or a plain API call.

Workflows

Chain agent calls into steps. Every step is recorded, so you can see what ran and why.

Subagents

Split a hard problem across specialist agents instead of one agent that does everything.

Tool Calling

Give agents MCP servers, memory and your own Python functions as tools.

Streaming Responses

Answers stream as they are generated, in the format the Vercel AI SDK reads.

Conversation Storage

Threads keep their history, so you don't write message storage yourself.

RAG Pipelines

Ground answers in your documents with hybrid search and query expansion.

Reindexing

Refresh embeddings when your documents change, without a rebuild.

File Processing

Answer questions about the spreadsheets, contracts and reports your users upload.

Integrations

Let agents act in the tools your team already uses, such as Zendesk, Linear and GitHub.

Workers

Workflow runs and uploads run in a worker on DBOS, in your own database. If a worker dies, the next one picks up the run where it stopped.

Oversight

Block prompt injection, mask personal data and send risky turns to a person, from one Python class.

04 / Full example
agents.py
from django_ai_sdk import Agent
from django_ai_sdk.adapters.base import Stream
from django_ai_sdk.generators import openai_responses_chat
from django_ai_sdk.rags import (
    BM25QueryExpanderRAG,
    BM25QueryExpanderRAGConfig,
)
from haystack import Pipeline

class HelpDeskAgent(Agent):
    name = "Help Desk"
    model = "gpt-5-mini"
    instructions = "You are a helpful support agent."
    llm = openai_responses_chat

    async def get_rag_queryset(self, memory_id=None):
        if memory_id:
            return Entry.objects.filter(memory_id=memory_id)
        return Entry.objects.all()

    async def get_rag_pipeline(self, memory_id=None):
        documents = await self.get_rag_documents(memory_id)
        if not documents:
            return None
        return BM25QueryExpanderRAG(
            documents=documents,
            config=BM25QueryExpanderRAGConfig(top_k=5),
        )

    async def get_pipeline_adapter(self, thread_id=None, user=None):
        storage_adapter = await self.get_storage_adapter(thread_id)
        generator = self.get_llm()
        return Stream(
            pipeline=Pipeline(),
            generator=generator,
            storage_adapter=storage_adapter,
        )
views.py
from .agents import HelpDeskAgent

agent = HelpDeskAgent()

@router.post("/chat")
async def chat(request, payload: ChatRequest):
    return await agent.as_view(
        payload.messages,
        thread_id=payload.thread_id,
    )
05 / Studio

See it all running in Studio.

Studio is the open-source reference app for the SDK: a Django project that uses every feature, and native apps for macOS, iPhone and iPad that talk to it.

Django app

Chat agents, swarms, deep research, memories and RAG, workflows with a worker, MCP integrations, and django-ninja and DRF APIs. Run it in Docker, against your own SDK checkout or the release on PyPI.

macOS, iPhone and iPad

A native SwiftUI client. Streamed chat with reasoning, tool calls, artifacts, citations and file uploads. Build your own agents, and run a workflow and follow it live.