Meta Agent: AI-Powered Agent Code Generator
Overview
The Meta Agent is an AI-powered code generator that creates Kagura agents from natural language descriptions. Simply describe what you want your agent to do, and Meta Agent will generate complete, production-ready Python code.
Quick Start
Interactive Mode
You'll be prompted to describe your agent:
🤖 Kagura Agent Builder
Describe your agent in natural language and I'll generate the code.
What should your agent do? Translate English to Japanese
🔍 Parsing agent specification...
📋 Agent Specification
Name: translator
Description: Translate English to Japanese
Input: str
Output: str
Tools: None
Memory: No
⚙️ Generating agent code...
🔒 Validating code security...
✅ Code validated
✅ Agent created: agents/translator.py
Non-Interactive Mode
kagura build agent \
--description "Translate English to Japanese" \
--output translator.py \
--no-interactive
Generated Code Example
For the description "Translate English to Japanese", Meta Agent generates:
"""Translate English to Japanese
Auto-generated by Kagura Meta Agent
Created: 2025-10-13 12:00:00
Kagura Version: 2.5.0
"""
from kagura import agent
@agent(
model="gpt-4o-mini",
temperature=0.7,
)
async def translator(input_data: str) -> str:
"""Translate English to Japanese
Args:
input_data: str - Input data
Returns:
str - Generated result
"""
# System prompt for this agent
system_prompt = """You are a professional translator. Translate text from English to Japanese."""
# Template variable for LLM (will be rendered at runtime)
return f"{system_prompt}\n\nInput: {input_data}"
Advanced Features
Agent with Tools
If your description mentions code execution, web search, or other tools, Meta Agent will automatically include them:
Generated code will include:
from kagura import agent
from kagura.core.executor import CodeExecutor
@agent(
model="gpt-4o-mini",
temperature=0.7,
tools=[CodeExecutor()],
)
async def math_solver(input_data: str) -> str:
"""Solve math problems using Python"""
# ...
Agent with Memory
For conversational agents:
Generated code will include:
from kagura import agent
from kagura.core.memory import MemoryManager
@agent(
model="gpt-4o-mini",
temperature=0.7,
enable_memory=True,
max_messages=100,
)
async def chatbot(input_data: str, memory: MemoryManager) -> str:
"""Conversational chatbot with memory"""
memory.add_message("user", str(input_data))
# ...
CLI Options
kagura build agent
Options:
-d, --description TEXT: Natural language agent description-o, --output PATH: Output file path (default:agents/<name>.py)--model TEXT: LLM model for code generation (default:gpt-4o-mini)--interactive / --no-interactive: Interactive mode (default:True)--no-validate: Skip code validation
Examples
# Interactive mode (default)
kagura build agent
# Direct generation
kagura build agent -d "Summarize text in 3 bullet points" -o summarizer.py
# Use GPT-4
kagura build agent -d "Complex reasoning task" --model gpt-4o
# Skip validation (not recommended)
kagura build agent -d "Test agent" --no-validate
How It Works
Meta Agent uses a multi-stage pipeline:
- Natural Language Parsing: Uses LLM to extract structured specification from your description
- Code Generation: Uses Jinja2 templates to generate clean Python code
- Security Validation: AST analysis ensures generated code is safe
- File Creation: Saves the generated agent to a file
Architecture
Natural Language → NLSpecParser → AgentSpec → CodeGenerator → Python Code
↓
CodeValidator (Security)
Security
All generated code is validated for security:
- ✅ Syntax checking
- ✅ Forbidden import detection (subprocess, eval, etc.)
- ✅ Dangerous function call detection
- ✅ Required @agent decorator verification
Dangerous code will be rejected before saving.
Best Practices
Writing Good Descriptions
Good descriptions are specific:
✅ "Translate English to Japanese" ✅ "Summarize articles in 3 bullet points" ✅ "Execute Python code to solve math problems"
Avoid vague descriptions:
❌ "Do something with text" ❌ "Help me" ❌ "Agent"
Tool Detection
Meta Agent automatically detects when tools are needed:
- Code Execution: "execute code", "run python", "calculate"
- Web Search: "search web", "google", "find online"
- Memory: "remember", "conversation history", "recall"
- File Operations: "read file", "write file"
Be explicit if you need specific tools.
Programmatic Usage
You can also use Meta Agent from Python:
from kagura.meta import MetaAgent
# Initialize
meta = MetaAgent(model="gpt-4o-mini")
# Generate from description
code = await meta.generate("Translate English to Japanese")
print(code)
# Generate from spec
from kagura.meta.spec import AgentSpec
spec = AgentSpec(
name="translator",
description="Translate text",
input_type="str",
output_type="str",
system_prompt="You are a professional translator."
)
code = await meta.generate_from_spec(spec)
Troubleshooting
"Validation failed: Missing @agent decorator"
The generated code doesn't include the @agent decorator. This is a bug - please report it.
"Validation failed: Disallowed import: subprocess"
The LLM generated code with dangerous imports. Try a more specific description or use --no-validate (not recommended).
Generated code doesn't work
The generated code is a starting point. You may need to:
- Adjust the system prompt
- Add more specific logic
- Test and iterate
Examples
See examples/meta_agent/ for complete examples:
translator.py: Simple translation agentcode_solver.py: Agent with code executionchatbot.py: Agent with memory
Next Steps
- Learn about Agent Builder for more complex configurations
- Explore Testing Framework for testing generated agents
- Read the Meta Agent API Reference