Build your own AI Agent: Integrating OpenAI API with Python Django
AlgoBiz · 2026
June 27, 2026 · 10 min read
Build your own AI Agent: Integrating OpenAI API with Python Django
Learn how to build and host custom AI agents on your backend. Abhijith P A guides you through connecting OpenAI assistant endpoints, session history, and server-sent streaming events in Django.
Artificial Intelligence is shifting from basic query-and-response interfaces to agentic systems—applications that can invoke tools, reason about paths, and execute background tasks autonomously. In this guide, I will show you how to build a stateful AI agent on a Django backend, connect it to OpenAI's API, and stream responses to your React client in real time.
Architecture of an AI Agent
An AI agent requires three core blocks: (1) System Persona/Instructions (defining behavior), (2) Thread/Memory (preserving conversational state), and (3) Tools (giving the model capabilities like querying database tables or sending emails). In our setup, Django serves as the orchestrator, storing conversation logs in PostgreSQL and communicating with OpenAI's API via a secure backend gateway.
Step 1: Installing Dependencies
Start by installing the official OpenAI Python package. Ensure your environment variables are configured securely with your API key.
pip install openaiStep 2: Managing Chat Threads and Sessions
To support ongoing conversations, we store thread IDs in our database. When a user submits a message, we fetch their active thread or initialize a new one on OpenAI's servers.
from openai import OpenAI
import os
client = OpenAI(api_key=os.environ.get('OPENAI_API_KEY'))
def get_or_create_thread(session_id: str) -> str:
# Lookup in database
chat_session = ChatSession.objects.filter(session_id=session_id).first()
if chat_session and chat_session.openai_thread_id:
return chat_session.openai_thread_id
# If new, create on OpenAI
thread = client.beta.threads.create()
ChatSession.objects.create(session_id=session_id, openai_thread_id=thread.id)
return thread.idStep 3: Streaming Agent Responses (Server-Sent Events)
Static JSON responses make the UI feel slow and laggy. Instead, we stream text tokens as they are generated using Django's StreamingHttpResponse. This provides an instant 'typing' effect to the user.
from django.http import StreamingHttpResponse
from django.views.decorators.csrf import csrf_exempt
import json
@csrf_exempt
def stream_agent_chat(request):
body = json.loads(request.body)
thread_id = get_or_create_thread(body['session_id'])
# Submit message
client.beta.threads.messages.create(
thread_id=thread_id,
role="user",
content=body['message']
)
# Helper generator to stream tokens
def generate_tokens():
run = client.beta.threads.runs.create_and_poll( # Or use Assistant Stream APIs
thread_id=thread_id,
assistant_id=os.environ.get('OPENAI_ASSISTANT_ID')
)
messages = client.beta.threads.messages.list(thread_id=thread_id)
# Stream the last message content parts...
yield f"data: {messages.data[0].content[0].text.value}\n\n"
return StreamingHttpResponse(generate_tokens(), content_type="text/event-stream")Best Practices for Production AI backends
When deploying AI features at scale, keep these security guidelines in mind:
- Limit token usage: enforce max output lengths to control API expenses.
- Validate tools input: never pass raw AI inputs directly into database executors.
- Use Redis for session cache: store session threads in memory to avoid query delays.
- Graceful fallback: provide an alternative response if the OpenAI API encounters limits.