| name | create-agent |
| description | Creates a new Agent subclass with correct mixin composition. Invoke when the user says "create an agent", "make a new agent class", "write an agent for [task]", "add a [name]Agent", "build a [tool-calling / conversational / reasoning] agent", or "I need an agent that does [X]". Also triggers on "implement an agent" or "scaffold a new Agent subclass".
|
| triggers | ["create an agent","create a new agent","make a new agent class","write an agent for","add a agent","build a agent","implement an agent","scaffold an agent subclass","new Agent subclass","I need an agent"] |
Create a New Agent Subclass
You are creating a new Agent subclass for the django-ai-sdk project.
Step 1 — Gather Requirements
Ask or infer the following before writing code:
- What does this agent do? (summarization, code review, customer support, data analysis...)
- Does it need tools? → set
tools = [fn1, fn2] and register @tool functions
- Does it need structured output? → set
output_schema = MyPydanticModel
- Is it conversational? → set
enable_conversation = True for ORM persistence
- Does it use a reasoning model? → set
reasoning = ReasoningConfig(...)
- Which provider and model? → or omit to use project defaults from
AI_SDK settings
Step 2 — Choose the Right Capability Set
Agent already inherits all mixins. Activate capabilities via class attributes only:
| Capability | Class attribute to set |
|---|
| Tool calling | tools = [my_tool_fn] |
| Structured output | output_schema = MyPydanticModel |
| Conversation persistence | enable_conversation = True |
| Reasoning model | reasoning = ReasoningConfig(effort="high") |
| Prompt caching | enable_cache = True (default) |
Never subclass the mixins directly — subclass Agent only.
Step 3 — Tool Registration
Define tools with the @tool decorator, then assign to the class:
from djangosdk.tools.decorator import tool
@tool
def get_weather(city: str, unit: Literal["celsius", "fahrenheit"] = "celsius") -> str:
"""Return current weather for a city."""
...
class WeatherAgent(Agent):
model = "gpt-4.1"
system_prompt = "You are a helpful weather assistant."
tools = [get_weather]
The @tool decorator automatically generates the JSON schema. The dispatch loop in Agent.handle() calls tools and loops until the model stops requesting them (capped at max_tool_iterations=10).
Step 4 — Structured Output
For agents that must return a validated Pydantic model:
from pydantic import BaseModel
from djangosdk.agents.base import Agent
class SentimentResult(BaseModel):
sentiment: Literal["positive", "negative", "neutral"]
confidence: float
reasoning: str
class SentimentAgent(Agent):
model = "gpt-4.1"
system_prompt = "Classify the sentiment of the provided text."
output_schema = SentimentResult
response = agent.handle("I love this product!")
print(response.structured)
Step 5 — Reasoning Agents
For agents backed by a reasoning model (o3, Claude 3.7, DeepSeek R1):
from djangosdk.agents.base import Agent
from djangosdk.providers.schemas import ReasoningConfig
class DeepAnalysisAgent(Agent):
provider = "openai"
model = "o3"
reasoning = ReasoningConfig(effort="high")
class ThinkingAgent(Agent):
provider = "anthropic"
model = "claude-3-7-sonnet-20250219"
reasoning = ReasoningConfig(extended_thinking=True, thinking_budget=16000)
class DeepSeekAgent(Agent):
provider = "deepseek"
model = "deepseek/deepseek-r1"
reasoning = ReasoningConfig(budget_tokens=8000)
Access thinking blocks: response.thinking → List[ThinkingBlock]
Step 6 — Conversational Agent (Persistent History)
class SupportAgent(Agent):
model = "gpt-4.1"
system_prompt = "You are a helpful support agent."
enable_conversation = True
response = agent.handle("My order hasn't arrived.", conversation_id="conv-abc123")
Step 7 — Agent File Template
Place new agents in the consuming project (e.g. myapp/agents.py), not in the SDK itself:
"""[AgentName] — [one-line description]."""
from __future__ import annotations
from typing import Literal
from djangosdk.agents.base import Agent
from djangosdk.providers.schemas import ReasoningConfig
from djangosdk.tools.decorator import tool
class [Name]Agent(Agent):
provider = "[provider]"
model = "[model]"
system_prompt = "[system prompt]"
temperature = 0.7
max_tokens = 2048
tools = []
output_schema = None
reasoning = None
enable_cache = True
enable_conversation = False
Step 8 — Write Tests Immediately
Always write tests alongside the agent. Use the write-tests skill or follow this pattern:
from djangosdk.testing.fakes import FakeProvider
from djangosdk.testing.assertions import assert_prompt_sent
def test_[name]_agent_returns_text(fake_provider):
fake_provider.set_response("[expected output]")
agent = [Name]Agent()
agent._provider = fake_provider
response = agent.handle("[example prompt]")
assert response.text == "[expected output]"
assert_prompt_sent(fake_provider, "[example prompt]")