Skip to content

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.

Describe your application’s purpose and requirements in natural language. The AI uses your context’s intent and attributes to generate appropriate implementations.

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

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

Generate, review, provide feedback, and regenerate until the implementation meets your needs. Each iteration builds on previous attempts with your guidance.

  • No eval() or unsafe code patterns
  • Validates inputs and sanitizes outputs
  • Follows MolnOS security conventions
  • Generated code is checked before presentation

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
}
}
OptionTypeDescriptionDefault
enabledbooleanEnable AI generation featuresfalse
providerstringAI provider (anthropic, openai, evroc, ollama)-
apiKeystringAPI key (supports env var substitution, not needed for Ollama)-
modelstringModel to useProvider default
baseUrlstringCustom API endpoint (optional)Provider default
endpointstringOllama server URL (Ollama only)http://localhost:11434
maxTokensnumberMaximum tokens for responses32000
temperaturenumberSampling temperature (0.0-1.0)0.7
generationTtlHoursnumberHours to keep generations before cleanup24

Use environment variable substitution in your config:

{
"ai": {
"apiKey": "${ANTHROPIC_API_KEY}"
}
}

Then set the environment variable:

Terminal window
export ANTHROPIC_API_KEY=sk-ant-...

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:

  1. Install Ollama from ollama.ai

  2. Pull a code-capable model:

    Terminal window
    ollama pull codellama:13b
    # or
    ollama pull deepseek-coder:6.7b
    # or
    ollama pull qwen2.5-coder:7b
  3. Ensure Ollama is running (it starts automatically on most systems)

Recommended Models for Code Generation:

  • codellama:13b - Good balance of quality and speed
  • deepseek-coder:6.7b - Smaller, faster, good for simple tasks
  • qwen2.5-coder:7b - Strong code understanding

Configuration Options:

  • endpoint - Ollama server URL (defaults to http://localhost:11434)
  • model - Model name from ollama list
  • No apiKey required 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

Users can provide their own API keys when making generation requests, bypassing the server configuration:

Terminal window
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-..."
}'
Terminal window
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"]
}
}'
Terminal window
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..."
}
}
Terminal window
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

Apply (deploy the generated code):

Terminal window
curl -X POST http://localhost:3000/generations/gen_a1b2c3d4/apply \
-H "Authorization: Bearer $TOKEN"

Discard (reject the generation):

Terminal window
curl -X POST http://localhost:3000/generations/gen_a1b2c3d4/discard \
-H "Authorization: Bearer $TOKEN"

Refine (iterate with feedback):

Terminal window
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"
}'
Terminal window
curl -X GET http://localhost:3000/contexts/url-shortener/generations \
-H "Authorization: Bearer $TOKEN"

Returns all generations (pending, applied, discarded) for the context.

Terminal window
curl -X DELETE http://localhost:3000/generations/gen_a1b2c3d4 \
-H "Authorization: Bearer $TOKEN"

Permanently removes the generation.

Help the AI understand your existing implementation by including current files:

Terminal window
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) { ... }"
}
]
}'

Use a different provider or model for specific generation:

Terminal window
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"
}'

Provide existing IAC to guide generation:

Terminal window
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": {}
}
}
}
}'

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"
}

Provide structured guidance through attributes:

{
"intent": "Build a blog platform",
"attributes": {
"storage": "kv",
"auth": "jwt",
"features": ["markdown", "comments", "tags"],
"rateLimit": 100
}
}

Always review generated code for:

  • Security: Check input validation, sanitization
  • Logic: Verify business logic correctness
  • Performance: Consider scalability implications
  • Dependencies: Ensure minimal external dependencies

Don’t expect perfect code on the first try. Use iteration feedback to refine:

  1. Generate initial implementation
  2. Review and identify issues
  3. Provide specific feedback
  4. Regenerate with improvements
  5. Repeat until satisfied
  • Apply or discard generations promptly
  • Use descriptive feedback in iterations
  • Delete old generations you won’t use
  • Monitor generation TTL (default 24 hours)

MolnOS validates generated code for:

  • No eval() or Function() constructor
  • No arbitrary file system access
  • No shell command execution
  • No vm module usage

But you should also review for:

  • Business logic correctness
  • Proper error handling
  • Input validation
  • Authentication/authorization

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

AI generation endpoints require appropriate permissions:

  • ai.generation.generate - Generate code implementations
  • ai.generation.read - View and list generations
  • ai.generation.apply - Apply generated code to infrastructure
  • ai.generation.discard - Discard a generation
  • ai.generation.delete - Delete a generation

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:

Terminal window
echo $ANTHROPIC_API_KEY

Error: AI provider authentication failed

Solution:

  1. Verify API key is correct
  2. Check API key permissions
  3. Ensure account has available credits
  4. Try regenerating the API key

Error: Generation not found or expired

Solution: Generations expire after generationTtlHours (default 24 hours). Generate again if needed.

Error: Invalid response format from AI

Solution: The AI returned malformed JSON. Try:

  1. Regenerating with the same input
  2. Simplifying your intent
  3. Using a different model
  4. Checking provider status

If you encounter rate limits:

  1. Wait before retrying
  2. Use user-provided API keys to distribute load
  3. Implement client-side retry logic
  4. Consider upgrading your AI provider tier
  • 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
{
"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"
}
}
{
"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"
}
}
{
"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
}
}

See the OpenAPI Schema for complete API documentation of all AI generation endpoints.