AI features

fold.run includes AI-powered capabilities for code generation and error diagnosis. These features are available on the Pro plan.

Code generation

Generate a complete function from a natural language description:

curl -X POST https://api.fold.run/ai/generate \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "description": "An API that returns the current time in multiple timezones"
  }'

Response:

{
  "code": "import { defineHandler, type FoldContext } from '@fold-run/runtime';\n\nexport default defineHandler(async (fold: FoldContext) => {\n  // ...\n});",
  "description": "Returns current time for UTC, EST, PST, and CET timezones"
}

The generated code always uses @fold-run/runtime's defineHandler pattern with no external dependencies.

Tool generation

Generate a function with an input schema, ready for MCP or A2A:

curl -X POST https://api.fold.run/ai/generate-tool \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "description": "Look up the weather for a given city",
    "input_schema": {
      "type": "object",
      "properties": {
        "city": { "type": "string" }
      }
    }
  }'

Response includes code, description, and the finalized input_schema.

Via the CLI

# Generate and deploy in one step
fold create-tool weather-lookup "Look up weather for a city"

Error diagnosis

When a function is throwing errors, AI can analyze the source code and recent error logs to suggest a fix:

curl -X POST https://api.fold.run/functions/fn_abc123/diagnose \
  -H "Authorization: Bearer YOUR_TOKEN"

Response:

{
  "function_id": "fn_abc123",
  "error_count": 10,
  "diagnosis": "The function is failing because `env.API_KEY` is undefined. You need to add an API_KEY secret for this organization before deploying. Run: curl -X POST https://api.fold.run/secrets -H 'Authorization: Bearer <token>' -d '{\"name\": \"API_KEY\", \"value\": \"...\"}'"
}

The diagnosis examines the last 10 error activations and the function's source code to provide actionable suggestions.

Via MCP

AI agents can diagnose errors using the MCP diagnose_function tool:

"Diagnose the errors on my weather-lookup function"

See MCP integration for details.

Model

AI features run close to your deployment for low-latency responses.

Plan requirement

AI features require the Pro plan. On the Free plan, AI endpoints return a 403 with error code PRO_PLAN_REQUIRED.

The Pro plan includes 5,000,000 AI tokens per month. Usage beyond this cap is metered at $10.00 per 1,000,000 tokens (see Billing for details). The Free plan has no AI token allowance.

Upgrade from the billing page or see Billing.

Reliability

AI features are fire-and-forget — they never block deploys or activations. If the AI model is temporarily unavailable, the platform continues operating normally without AI-generated summaries or diagnoses.