This guide explains the complete workflow for developing AI agents using Agent-as-Code.
- Go 1.21 or later
- Python 3.8 or later
- Docker (optional, for container-based deployment)
- Git
A typical agent project has the following structure:
my-agent/
├── agent.yaml # Agent configuration
├── src/
│ ├── main.py # Main agent code
│ ├── capabilities/ # Agent capabilities
│ ├── models/ # Model integrations
│ └── utils/ # Utility functions
├── tests/ # Test files
├── requirements.txt # Python dependencies
└── README.md # Project documentation
# Create new agent from template
agent init my-agent --template chatbot
# Or create custom agent
agent init my-agent --runtime pythonEdit agent.yaml:
apiVersion: agent.dev/v1
kind: Agent
metadata:
name: my-agent
version: 1.0.0
spec:
runtime: python:3.11
model:
provider: openai
name: gpt-4
capabilities:
- text-generation
- chatExample src/main.py:
from agent_as_code import Agent
class MyAgent(Agent):
async def process(self, input: str) -> str:
# Implement agent logic
response = await self.model.generate(input)
return response
if __name__ == "__main__":
agent = MyAgent()
agent.run()# Build agent
agent build -t my-agent:dev .
# Run locally
agent run my-agent:dev
# View logs
agent logs my-agent:dev
# Test agent
agent test my-agent:devCreate tests in tests/ directory:
# tests/test_agent.py
from my_agent import MyAgent
async def test_process():
agent = MyAgent()
response = await agent.process("Hello")
assert response is not NoneRun tests:
agent test my-agent:dev# Run with debug logging
agent run my-agent:dev --log-level debug
# Inspect agent
agent inspect my-agent:dev
# Check health
agent health my-agent:dev# Initialize git
git init
# Add files
git add agent.yaml src/ tests/
# Commit
git commit -m "Initial agent implementation"# Build production version
agent build -t my-agent:1.0.0 .
# Push to registry
agent push my-agent:1.0.0
# Deploy
agent deploy my-agent:1.0.0- Use version control for
agent.yaml - Keep secrets in environment variables
- Document configuration options
- Follow template structure
- Separate concerns
- Use type hints
- Add documentation
- Write unit tests
- Test capabilities separately
- Use mock models for testing
- Test error handling
from agent_as_code import Agent, AgentError
class MyAgent(Agent):
async def process(self, input: str) -> str:
try:
response = await self.model.generate(input)
return response
except Exception as e:
raise AgentError(f"Processing failed: {e}")import logging
from agent_as_code import Agent
class MyAgent(Agent):
def __init__(self):
self.logger = logging.getLogger(__name__)
async def process(self, input: str) -> str:
self.logger.info(f"Processing input: {input}")
response = await self.model.generate(input)
self.logger.info(f"Generated response: {response}")
return response- Set appropriate resource limits
- Monitor memory usage
- Handle cleanup properly
- Validate inputs
- Sanitize outputs
- Use secure dependencies
- Keep dependencies updated
- Create capability module:
# src/capabilities/summarization.py
class SummarizationCapability:
async def summarize(self, text: str) -> str:
# Implement summarization
pass- Update agent.yaml:
spec:
capabilities:
- summarization- Add model configuration:
spec:
model:
provider: anthropic
name: claude-3- Implement model integration:
from agent_as_code.models import ModelIntegration
class ClaudeIntegration(ModelIntegration):
async def generate(self, prompt: str) -> str:
# Implement Claude integration
passspec:
healthCheck:
command: ["curl", "http://localhost:8080/health"]
interval: 30s
timeout: 10s
retries: 3- Build Failures
# Check build logs
agent build -t my-agent:dev . --verbose- Runtime Errors
# Check agent logs
agent logs my-agent:dev- Model Issues
# Test model connection
agent test my-agent:dev --test-model