Documentation

KV / Vectorize / AI

ready

Provision and bind Cloudflare KV namespaces, Vectorize indexes (with metadata indexes), D1 databases, and Workers AI + AI Gateway directly in nextdeploy.yml.

Source reference: cli/internal/serverless/cloudflare/bindings.go, cli/internal/serverless/cloudflare/vectorize.go, cli/internal/serverless/cloudflare/aigateway.go


Cloudflare KV

Use KV for edge rate limiting, session metadata, and cross-isolate revalidateTag() invalidation (NEXTCOMPILE_CACHE):

version: "1.0"
target_type: serverless

app:
  name: my-app
  domain: app.example.com
  environment: production

serverless:
  provider: cloudflare
  cloudflare:
    compatibility_date: "2026-05-01"
    compatibility_flags:
      - nodejs_compat
    resources:
      kv:
        - name: rate-limit-kv
          binding: RATE_LIMIT
        - name: nextcompile-cache-kv
          binding: NEXTCOMPILE_CACHE

When NEXTCOMPILE_CACHE is bound, NextDeploy's CloudflareCacheHandler automatically propagates revalidateTag() timestamps across every Cloudflare edge isolate worldwide.

[!NOTE] KV Write Limits & Edge Rate Limiting: Cloudflare KV limits updates to 1 PUT per second per key (and 1,000 writes/day on Free plans). NextDeploy's edge guard (guard.mjs) coalesces rate-limit kv.put calls to once per 1,100ms per key per isolate via ctx.waitUntil(...), catches KV 429 contention errors, and also supports native Cloudflare ratelimit bindings (protection.rate_limit.binding) for high-throughput endpoints.

Workers AI, AI Gateway & Vectorize

Provision a Vectorize index (including filterable metadata indexes) and an AI Gateway, and bind Workers AI + Vectorize in nextdeploy.yml:

version: "1.0"
target_type: serverless

app:
  name: my-ai-app
  domain: ai.example.com
  environment: production

serverless:
  provider: cloudflare
  cloudflare:
    compatibility_date: "2026-05-01"
    compatibility_flags:
      - nodejs_compat
    resources:
      vectorize:
        - name: docs-embeddings
          binding: DOCS_INDEX
          dimensions: 768
          metric: cosine
          metadata_indexes:
            - property_name: category
              type: string
      ai_gateway:
        - slug: main-gateway
          cache_ttl: 300
          collect_logs: true
    bindings:
      ai:
        - name: AI
          gateway:
            id: main-gateway

Access AI and DOCS_INDEX through globalThis.__nextdeployEnv:

const env = (globalThis as { __nextdeployEnv?: CloudflareEnv }).__nextdeployEnv
const result = await env?.AI.run('@cf/meta/llama-3.1-8b-instruct', {
  prompt: 'Summarize NextDeploy in one sentence.',
})

Related