Dynamic agent instantiation using the Factory-of-Factories pattern with Claude Agent SDK.
Built for the Austin AI MUG Lightning Talk. ๐ค
Lightning Agents demonstrates a pattern for dynamically loading, instantiating, and even generating AI agents from declarative JSON definitions. Instead of hardcoding agent configurations, you define blueprints that get transformed into factory functions at runtime.
Building AI agents typically involves:
- ๐ Hardcoded system prompts scattered across files
- ๐ Tightly coupled agent definitions and execution logic
- ๐ซ No standardized way to add new agents without code changes
- ๐ Manual configuration duplication when agents share patterns
Factory-of-Factories: A registry that loads agent definitions from JSON, builds factory functions for each, and provides a unified interface for instantiation with runtime context injection.
โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ
โ Definition โ โโโถ โ Factory โ โโโถ โ Registry โ โโโถ โ Instance โ
โ (JSON) โ โ (Callable) โ โ (Unified) โ โ (Ready) โ
โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ
# Clone and setup
cd lightning-agents
uv sync
# Copy env template and configure
cp .env.example .env
# Edit .env with your SEARXNG_URL
# List available agents
lightning list
# Run an agent
lightning run basic_helper "What is the factory pattern?"
# Create a new agent with the architect
lightning architect "code reviewer for Python security"lightning listShows all available agents with descriptions and model info.
lightning run <agent_id> "<prompt>"Examples:
lightning run basic_helper "Explain dependency injection"
lightning run aimug_researcher "What RAG tutorials does AIMUG have?"
lightning run lab_finder "Find labs about LangGraph"lightning architect "<task description>"The architect agent generates a new agent definition, saves it to agents.json, and makes it immediately available.
lightning architect "meeting notes summarizer for action items"
# New agent created: meeting_summarizer
lightning run meeting_summarizer "Summarize: [transcript]"| Agent | Description | Model | Tools |
|---|---|---|---|
basic_helper |
General Q&A assistant | haiku | - |
research_assistant |
Structured research summaries | sonnet | web_search |
python_doc_writer |
Python function documentation | haiku | - |
architect |
๐๏ธ Designs new agent definitions (Voyager-style) | sonnet | db_agents (CRUD) |
tool_architect |
๐ง Designs new custom tools | sonnet | db_tools (CRUD) |
aimug_researcher |
Searches AIMUG content (GitHub, docs, YouTube) | sonnet | web_search, url_read |
lab_finder |
Finds AIMUG labs by topic | haiku | web_search |
git_commit_writer |
โ๏ธ Writes conventional commit messages | haiku | - |
presentation_slide_writer |
๐จ Creates and manages PPTX presentations | sonnet | slides (CRUD), generate_pptx, Read, Bash |
paper_researcher |
๐ Researches papers, downloads PDFs | sonnet | web_search, url_read, download_pdf |
def create_agent(config):
return Agent(config)# Definition โ Factory โ Registry โ Instance
registry = AgentRegistry.from_json("agents.json") # Builds ALL factories
agent = registry.create("researcher", {"topic": "AI"}) # Gets instanceThe registry is the factory-of-factories โ it produces factory functions from definitions, then uses those factories to produce instances.
The "Architect Agent" pattern: an agent that generates new agent definitions.
# Before: 6 agents
lightning list
# Use architect to create a new one
lightning architect "code reviewer for security vulnerabilities"
# After: 7 agents - new one saved to agents.json
lightning list
# Use it immediately
lightning run security_reviewer "Review this auth code..."This enables self-expanding agent systems where the AI itself designs specialized agents for new tasks. ๐คฏ
This project draws inspiration from Voyager, an AI agent that plays Minecraft by building a skill library that grows over time. Instead of hardcoded behaviors, Voyager learns new skills and stores them for reuse.
Lightning Agents applies this concept to agent systems:
- Skill Library โ Agent Registry: Agents stored as reusable definitions
- Learning New Skills โ Architect Agent: Creates new agents on demand
- Tool Acquisition โ Tool Architect: Creates new tools when needed
The result: a system that grows organically based on actual needs, not pre-planned capabilities.
JSON definitions separate what an agent is from how it runs. Easier to version, diff, and review.
The same definition can serve multiple contexts by injecting different runtime parameters.
Instead of manually writing every agent definition, let Claude design agents for new tasks. The system grows based on actual needs.
lightning-agents/
โโโ .env.example # Environment template
โโโ .env # Your config (gitignored)
โโโ pyproject.toml
โโโ README.md
โโโ CLAUDE.md # Developer notes
โโโ db/ # Data (decoupled from source)
โ โโโ agents.json # Agent blueprints
โ โโโ tools.json # Custom tool definitions
โโโ src/lightning_agents/ # Main package
โ โโโ __init__.py
โ โโโ cli.py # CLI entry point
โ โโโ runner.py # Agent execution with MCP
โ โโโ registry.py # Factory-of-Factories pattern
โ โโโ agent_factory.py # Definition โ Instance
โ โโโ mcp_config.py # MCP server configs
โ โโโ tools/ # Custom MCP tools
โ โโโ download_pdf.py # PDF download tool
โ โโโ db_agents.py # Agent CRUD operations
โ โโโ db_tools.py # Tool CRUD operations
โ โโโ presentation.py # Slide manipulation tools
โโโ presentation/ # PPTX slide generator
โโโ generate_slides.py
โโโ slide_content.py
โโโ styles.py
โโโ output/
โโโ lightning-agents.pptx
Use the presentation_slide_writer agent to manage slides:
# List current slides
lightning run presentation_slide_writer "List the slides"
# Add a new slide
lightning run presentation_slide_writer "Add a bullets slide about MCP integration"
# Generate PPTX and PDF
lightning run presentation_slide_writer "Generate the presentation"
# Output: presentation/output/lightning-agents.pptx + .pdfEdit presentation/slide_content.py directly for bulk changes. Supports **bold** and `code` markup.
Copy .env.example to .env and configure:
# SearXNG MCP server URL (for web search agents)
SEARXNG_URL=http://localhost:8888Agents can use MCP tools by declaring them in their tools array:
SearXNG (Web Search):
mcp__searxng__searxng_web_search- Web searchmcp__searxng__web_url_read- Read web page content
Custom Tools (Built-in MCP Server):
mcp__custom-tools__download_pdf- Download PDFs from URLsmcp__custom-tools__db_list_agents/db_get_agent/db_create_agent/db_update_agent/db_delete_agent- Agent CRUDmcp__custom-tools__db_list_tools/db_get_tool/db_create_tool/db_update_tool/db_delete_tool- Tool CRUDmcp__custom-tools__list_slides/add_slide/update_slide/delete_slide/generate_pptx- Presentation management
SDK Primitives:
Agents can also use built-in Claude SDK tools: Read, Write, Edit, Bash, Grep, Glob, WebFetch, WebSearch
- Python 3.13+
uvpackage manager- SearXNG instance (for search-enabled agents)
MIT
โก Built for the Austin AI MUG lightning talk on dynamic agent instantiation patterns. โก
Agents creating agents creating agents... ๐คโก๏ธ๐คโก๏ธ๐ค