AI Generation
MolnOS AI Generation enables you to describe what you want to build in natural language, and have AI generate the how—complete with resources (Functions and Sites), IAC configuration, and implementation explanations.
Features
Section titled “Features”Intent-Driven Development
Section titled “Intent-Driven Development”Describe your application’s purpose and requirements in natural language. The AI uses your context’s intent and attributes to generate appropriate implementations.
Multiple AI Providers
Section titled “Multiple AI Providers”Choose from industry-leading AI providers or use local models:
- Anthropic Claude
- OpenAI GPT
- Evroc Think - European sovereign cloud AI (OpenAI-compatible)
- Ollama - Run models locally for privacy and cost savings
Context-Aware Generation
Section titled “Context-Aware Generation”The AI has access to:
- Your context’s existing resources (functions, databases, storage)
- MolnOS schemas and API documentation
- Existing function code for context
- Best practices and security guidelines
Iterative Refinement
Section titled “Iterative Refinement”Generate, review, provide feedback, and regenerate until the implementation meets your needs. Each iteration builds on previous attempts with your guidance.
Secure by Design
Section titled “Secure by Design”- No
eval()or unsafe code patterns - Validates inputs and sanitizes outputs
- Follows MolnOS security conventions
- Generated code is checked before presentation
Configuration
Section titled “Configuration”Basic Setup
Section titled “Basic Setup”Add AI configuration to your molnos.config.json:
{ "ai": { "enabled": true, "provider": "anthropic", "apiKey": "${ANTHROPIC_API_KEY}", "model": "claude-sonnet-4-5", "maxTokens": 32000, "temperature": 0.7, "generationTtlHours": 24 }}Configuration Options
Section titled “Configuration Options”| Option | Type | Description | Default |
|---|---|---|---|
enabled | boolean | Enable AI generation features | false |
provider | string | AI provider (anthropic, openai, evroc, ollama) | - |
apiKey | string | API key (supports env var substitution, not needed for Ollama) | - |
model | string | Model to use | Provider default |
baseUrl | string | Custom API endpoint (optional) | Provider default |
endpoint | string | Ollama server URL (Ollama only) | http://localhost:11434 |
maxTokens | number | Maximum tokens for responses | 32000 |
temperature | number | Sampling temperature (0.0-1.0) | 0.7 |
generationTtlHours | number | Hours to keep generations before cleanup | 24 |
Environment Variables
Section titled “Environment Variables”Use environment variable substitution in your config:
{ "ai": { "apiKey": "${ANTHROPIC_API_KEY}" }}Then set the environment variable:
export ANTHROPIC_API_KEY=sk-ant-...Local Models with Ollama
Section titled “Local Models with Ollama”Run AI models locally using Ollama for privacy, cost savings, and offline operation:
{ "ai": { "enabled": true, "provider": "ollama", "endpoint": "http://localhost:11434", "model": "codellama:13b", "maxTokens": 32000, "temperature": 0.7 }}Prerequisites:
-
Install Ollama from ollama.ai
-
Pull a code-capable model:
Terminal window ollama pull codellama:13b# orollama pull deepseek-coder:6.7b# orollama pull qwen2.5-coder:7b -
Ensure Ollama is running (it starts automatically on most systems)
Recommended Models for Code Generation:
codellama:13b- Good balance of quality and speeddeepseek-coder:6.7b- Smaller, faster, good for simple tasksqwen2.5-coder:7b- Strong code understanding
Configuration Options:
endpoint- Ollama server URL (defaults tohttp://localhost:11434)model- Model name fromollama list- No
apiKeyrequired for local Ollama instances
Note: Local models may produce different quality results compared to Claude or GPT-4. For best results with Ollama:
- Use larger models (13b+ parameters)
- Provide clear, detailed intents
- Review generated code carefully
- Iterate with specific feedback
User-Provided API Keys
Section titled “User-Provided API Keys”Users can provide their own API keys when making generation requests, bypassing the server configuration:
curl -X POST http://localhost:3000/contexts/my-app/generate \ -H "Authorization: Bearer $TOKEN" \ -H "Content-Type: application/json" \ -d '{ "intent": "Build a URL shortener", "userApiKey": "sk-ant-..." }'API Workflow
Section titled “API Workflow”1. Create a Context with Intent
Section titled “1. Create a Context with Intent”curl -X POST http://localhost:3000/contexts \ -H "Authorization: Bearer $TOKEN" \ -H "Content-Type: application/json" \ -d '{ "name": "url-shortener", "intent": "Build a URL shortener API that creates short codes and redirects users", "attributes": { "storage": "kv", "auth": "apiKey", "features": ["analytics", "custom-domains"] } }'2. Generate Implementation
Section titled “2. Generate Implementation”curl -X POST http://localhost:3000/contexts/url-shortener/generate \ -H "Authorization: Bearer $TOKEN" \ -H "Content-Type: application/json" \ -d '{}'The AI will:
- Fetch MolnOS schemas to understand available resources
- Query the context for existing resources
- Generate appropriate functions and IAC configuration
- Return a complete implementation with explanation
Response:
{ "success": true, "generation": { "id": "gen_a1b2c3d4", "contextId": "url-shortener", "status": "pending", "functions": [ { "name": "shorten", "path": "functions/shorten.js", "code": "export async function handler(req, context) { ... }" }, { "name": "redirect", "path": "functions/redirect.js", "code": "export async function handler(req, context) { ... }" } ], "iacConfig": { "version": "1", "context": { "name": "url-shortener" }, "resources": { "functions": { ... }, "databases": { ... } } }, "explanation": "# URL Shortener Implementation\n\n..." }}3. Review Generated Code
Section titled “3. Review Generated Code”curl -X GET http://localhost:3000/generations/gen_a1b2c3d4 \ -H "Authorization: Bearer $TOKEN"Review the:
- Functions: Check the generated code for correctness
- IAC Config: Verify resource definitions
- Explanation: Understand the implementation decisions
4. Apply or Discard
Section titled “4. Apply or Discard”Apply (deploy the generated code):
curl -X POST http://localhost:3000/generations/gen_a1b2c3d4/apply \ -H "Authorization: Bearer $TOKEN"Discard (reject the generation):
curl -X POST http://localhost:3000/generations/gen_a1b2c3d4/discard \ -H "Authorization: Bearer $TOKEN"Refine (iterate with feedback):
curl -X POST http://localhost:3000/contexts/url-shortener/generate \ -H "Authorization: Bearer $TOKEN" \ -H "Content-Type: application/json" \ -d '{ "previousAttemptId": "gen_a1b2c3d4", "iterationFeedback": "Add rate limiting to prevent abuse" }'Generation Management
Section titled “Generation Management”List Generations for a Context
Section titled “List Generations for a Context”curl -X GET http://localhost:3000/contexts/url-shortener/generations \ -H "Authorization: Bearer $TOKEN"Returns all generations (pending, applied, discarded) for the context.
Delete a Generation
Section titled “Delete a Generation”curl -X DELETE http://localhost:3000/generations/gen_a1b2c3d4 \ -H "Authorization: Bearer $TOKEN"Permanently removes the generation.
Advanced Usage
Section titled “Advanced Usage”Providing Existing Code
Section titled “Providing Existing Code”Help the AI understand your existing implementation by including current files:
curl -X POST http://localhost:3000/contexts/my-app/generate \ -H "Authorization: Bearer $TOKEN" \ -H "Content-Type: application/json" \ -d '{ "files": [ { "path": "functions/existing.js", "content": "export async function handler(req, context) { ... }" } ] }'Overriding Provider Settings
Section titled “Overriding Provider Settings”Use a different provider or model for specific generation:
curl -X POST http://localhost:3000/contexts/my-app/generate \ -H "Authorization: Bearer $TOKEN" \ -H "Content-Type: application/json" \ -d '{ "providerOverride": "openai", "modelOverride": "gpt-4-turbo" }'Including IAC Configuration
Section titled “Including IAC Configuration”Provide existing IAC to guide generation:
curl -X POST http://localhost:3000/contexts/my-app/generate \ -H "Authorization: Bearer $TOKEN" \ -H "Content-Type: application/json" \ -d '{ "iacConfig": { "version": "1", "context": { "name": "my-app", "intent": "My application" }, "resources": { "databases": { "users": {}, "sessions": {} } } } }'Best Practices
Section titled “Best Practices”Write Clear Intents
Section titled “Write Clear Intents”Be specific about what you want to build:
Good:
{ "intent": "Build a user authentication API with email/password login, JWT tokens, and password reset functionality"}Bad:
{ "intent": "Handle users"}Use Intent Attributes
Section titled “Use Intent Attributes”Provide structured guidance through attributes:
{ "intent": "Build a blog platform", "attributes": { "storage": "kv", "auth": "jwt", "features": ["markdown", "comments", "tags"], "rateLimit": 100 }}Review Before Applying
Section titled “Review Before Applying”Always review generated code for:
- Security: Check input validation, sanitization
- Logic: Verify business logic correctness
- Performance: Consider scalability implications
- Dependencies: Ensure minimal external dependencies
Iterate When Needed
Section titled “Iterate When Needed”Don’t expect perfect code on the first try. Use iteration feedback to refine:
- Generate initial implementation
- Review and identify issues
- Provide specific feedback
- Regenerate with improvements
- Repeat until satisfied
Keep Generations Organized
Section titled “Keep Generations Organized”- Apply or discard generations promptly
- Use descriptive feedback in iterations
- Delete old generations you won’t use
- Monitor generation TTL (default 24 hours)
Security Considerations
Section titled “Security Considerations”Generated Code Review
Section titled “Generated Code Review”MolnOS validates generated code for:
- No
eval()orFunction()constructor - No arbitrary file system access
- No shell command execution
- No
vmmodule usage
But you should also review for:
- Business logic correctness
- Proper error handling
- Input validation
- Authentication/authorization
User-Provided API Keys
Section titled “User-Provided API Keys”When users provide their own API keys:
- Keys are not logged or stored
- Keys are only used for that specific request
- Users pay for their own AI usage
- Reduces operational costs
Permissions
Section titled “Permissions”AI generation endpoints require appropriate permissions:
ai.generation.generate- Generate code implementationsai.generation.read- View and list generationsai.generation.apply- Apply generated code to infrastructureai.generation.discard- Discard a generationai.generation.delete- Delete a generation
Troubleshooting
Section titled “Troubleshooting”AI Generation Not Available
Section titled “AI Generation Not Available”Error: AI generation is not enabled
Solution: Check your molnos.config.json:
{ "ai": { "enabled": true, "provider": "anthropic", "apiKey": "${ANTHROPIC_API_KEY}" }}Verify environment variable is set:
echo $ANTHROPIC_API_KEYAPI Key Authentication Failed
Section titled “API Key Authentication Failed”Error: AI provider authentication failed
Solution:
- Verify API key is correct
- Check API key permissions
- Ensure account has available credits
- Try regenerating the API key
Generation Expired
Section titled “Generation Expired”Error: Generation not found or expired
Solution: Generations expire after generationTtlHours (default 24 hours). Generate again if needed.
Invalid Response Format
Section titled “Invalid Response Format”Error: Invalid response format from AI
Solution: The AI returned malformed JSON. Try:
- Regenerating with the same input
- Simplifying your intent
- Using a different model
- Checking provider status
Rate Limiting
Section titled “Rate Limiting”If you encounter rate limits:
- Wait before retrying
- Use user-provided API keys to distribute load
- Implement client-side retry logic
- Consider upgrading your AI provider tier
Limitations
Section titled “Limitations”As of Version 1.3.0
Section titled “As of Version 1.3.0”- No streaming: Generations complete before returning
- No editing: Can’t edit code in-place (must iterate)
- No multi-context: One context per generation
- No testing: Generated code isn’t automatically tested
- No bundling: External dependencies require manual bundling
Examples
Section titled “Examples”URL Shortener
Section titled “URL Shortener”{ "name": "url-shortener", "intent": "Build a URL shortener that creates short codes, stores mappings, and redirects users. Include analytics for click tracking.", "attributes": { "storage": "kv", "features": ["analytics", "custom-codes"], "auth": "optional" }}Blog Platform
Section titled “Blog Platform”{ "name": "blog", "intent": "Build a blog API with posts, authors, tags, and comments. Support markdown rendering and search.", "attributes": { "storage": "kv", "database": "tables", "features": ["markdown", "search", "comments", "tags"], "auth": "jwt" }}Task Management
Section titled “Task Management”{ "name": "tasks", "intent": "Build a task management API with projects, tasks, assignments, and due dates. Support team collaboration.", "attributes": { "database": "tables", "features": ["assignments", "notifications", "recurring-tasks"], "auth": "jwt", "multitenancy": true }}API Reference
Section titled “API Reference”See the OpenAPI Schema for complete API documentation of all AI generation endpoints.
Related Features
Section titled “Related Features”- Infrastructure as Code - Deploy generated IAC configurations
- Functions - Understand generated function code
- Configuration - Configure MolnOS settings