cloudflare-vectorize — independently scanned and version-tracked by SaferSkills.
SaferSkills independently audited cloudflare-vectorize (Agent Skill) and scored it 100/100 (green). The audit ran 55 deterministic rules across Security, Supply Chain, Maintenance, Transparency, and Community; it found 0 high-severity and 0 lower-severity findings. The full rule-by-rule trace and per-finding evidence are below. Free, methodology-open.
Findings & checks · 0 flagged
Every scanned point with the score it earned and what moved between them.
First recorded scan — no prior version to compare against.
The primary manifest — the file an agent reads to learn what this artifact does.
Complete implementation guide for Cloudflare Vectorize - a globally distributed vector database for building semantic search, RAG (Retrieval Augmented Generation), and AI-powered applications with Cloudflare Workers.
Status: Production Ready ✅ Last Updated: 2025-10-21 Dependencies: cloudflare-worker-base (for Worker setup), cloudflare-workers-ai (for embeddings) Latest Versions: [email protected], @cloudflare/[email protected] Token Savings: ~65% Errors Prevented: 8 Dev Time Saved: ~3 hours
# 1. Create the index with FIXED dimensions and metric
npx wrangler vectorize create my-index \
--dimensions=768 \
--metric=cosine
# 2. Create metadata indexes IMMEDIATELY (before inserting vectors!)
npx wrangler vectorize create-metadata-index my-index \
--property-name=category \
--type=string
npx wrangler vectorize create-metadata-index my-index \
--property-name=timestamp \
--type=numberWhy: Metadata indexes MUST exist before vectors are inserted. Vectors added before a metadata index was created won't be filterable on that property.
# Dimensions MUST match your embedding model output:
# - Workers AI @cf/baai/bge-base-en-v1.5: 768 dimensions
# - OpenAI text-embedding-3-small: 1536 dimensions
# - OpenAI text-embedding-3-large: 3072 dimensions
# Metrics determine similarity calculation:
# - cosine: Best for normalized embeddings (most common)
# - euclidean: Absolute distance between vectors
# - dot-product: For non-normalized vectorswrangler.jsonc:
{
"name": "my-vectorize-worker",
"main": "src/index.ts",
"compatibility_date": "2025-10-21",
"vectorize": [
{
"binding": "VECTORIZE_INDEX",
"index_name": "my-index"
}
],
"ai": {
"binding": "AI"
}
}export interface Env {
VECTORIZE_INDEX: VectorizeIndex;
AI: Ai;
}
interface VectorizeVector {
id: string;
values: number[] | Float32Array | Float64Array;
namespace?: string;
metadata?: Record<string, string | number | boolean | string[]>;
}
interface VectorizeMatches {
matches: Array<{
id: string;
score: number;
values?: number[];
metadata?: Record<string, any>;
namespace?: string;
}>;
count: number;
}// INSERT: Keeps first insertion if ID exists
await env.VECTORIZE_INDEX.insert([
{
id: "doc-1",
values: [0.1, 0.2, 0.3, ...],
metadata: { title: "First version" }
}
]);
// UPSERT: Overwrites with latest if ID exists (use this for updates)
await env.VECTORIZE_INDEX.upsert([
{
id: "doc-1",
values: [0.1, 0.2, 0.3, ...],
metadata: { title: "Updated version" }
}
]);// Generate embedding for query
const queryEmbedding = await env.AI.run('@cf/baai/bge-base-en-v1.5', {
text: "What is Cloudflare Workers?"
});
// Search with metadata filtering
const results = await env.VECTORIZE_INDEX.query(
queryEmbedding.data[0],
{
topK: 5,
filter: {
category: "documentation",
timestamp: { $gte: 1704067200 } // After Jan 1, 2024
},
returnMetadata: 'all',
returnValues: false,
namespace: 'prod'
}
);// Equality (implicit $eq)
{ category: "docs" }
// Explicit operators
{ status: { $ne: "archived" } }
// In array
{ category: { $in: ["docs", "tutorials", "guides"] } }
// Not in array
{ category: { $nin: ["deprecated", "draft"] } }
// Range queries (numbers)
{
timestamp: {
$gte: 1704067200, // >= Jan 1, 2024
$lt: 1735689600 // < Jan 1, 2025
}
}
// Range queries (strings) - prefix searching
{
url: {
$gte: "/docs/workers",
$lt: "/docs/workersz" // Matches all /docs/workers/*
}
}
// Nested metadata with dot notation
{ "author.id": "user123" }
// Multiple conditions (implicit AND)
{
category: "docs",
language: "en",
"metadata.published": true
}// Insert with namespace (partition key)
await env.VECTORIZE_INDEX.upsert([
{
id: "1",
values: embedding,
namespace: "customer-123",
metadata: { type: "support_ticket" }
}
]);
// Query only within namespace
const results = await env.VECTORIZE_INDEX.query(queryVector, {
topK: 5,
namespace: "customer-123" // Only search this customer's data
});// List vector IDs (paginated)
const vectors = await env.VECTORIZE_INDEX.listVectors({
cursor: null,
limit: 100
});
// Get specific vectors by ID
const retrieved = await env.VECTORIZE_INDEX.getByIds([
"doc-1", "doc-2", "doc-3"
]);
// Delete vectors
await env.VECTORIZE_INDEX.deleteByIds([
"doc-1", "doc-2"
]);const embeddings = await env.AI.run('@cf/baai/bge-base-en-v1.5', {
text: ["Document 1 content", "Document 2 content"]
});
// embeddings.data is number[][] (array of 768-dim vectors)
const vectors = embeddings.data.map((values, i) => ({
id: `doc-${i}`,
values,
metadata: { source: 'batch-import' }
}));
await env.VECTORIZE_INDEX.upsert(vectors);import OpenAI from 'openai';
const openai = new OpenAI({ apiKey: env.OPENAI_API_KEY });
const response = await openai.embeddings.create({
model: "text-embedding-3-small", // 1536 dimensions
input: "Text to embed"
});
await env.VECTORIZE_INDEX.upsert([{
id: "doc-1",
values: response.data[0].embedding,
metadata: { model: "openai-3-small" }
}]);Low Cardinality (Good for $eq filters):
// Few unique values - efficient filtering
metadata: {
category: "docs", // ~10 categories
language: "en", // ~5 languages
published: true // 2 values (boolean)
}High Cardinality (Avoid in range queries):
// Many unique values - avoid large range scans
metadata: {
user_id: "uuid-v4...", // Millions of unique values
timestamp_ms: 1704067200123 // Use seconds instead
}// ❌ INVALID metadata keys
metadata: {
"": "value", // Empty key
"user.name": "John", // Contains dot (reserved for nesting)
"$admin": true, // Starts with $
"key\"with\"quotes": 1 // Contains quotes
}
// ✅ VALID metadata keys
metadata: {
"user_name": "John",
"isAdmin": true,
"nested": { "allowed": true } // Access as "nested.allowed" in filters
}export default {
async fetch(request: Request, env: Env): Promise<Response> {
const { question } = await request.json();
// 1. Generate embedding for user question
const questionEmbedding = await env.AI.run('@cf/baai/bge-base-en-v1.5', {
text: question
});
// 2. Search vector database for similar content
const results = await env.VECTORIZE_INDEX.query(
questionEmbedding.data[0],
{
topK: 3,
returnMetadata: 'all',
filter: { type: "documentation" }
}
);
// 3. Build context from retrieved documents
const context = results.matches
.map(m => m.metadata.content)
.join('\n\n---\n\n');
// 4. Generate answer with LLM using context
const answer = await env.AI.run('@cf/meta/llama-3-8b-instruct', {
messages: [
{
role: "system",
content: `Answer based on this context:\n\n${context}`
},
{
role: "user",
content: question
}
]
});
return Response.json({
answer: answer.response,
sources: results.matches.map(m => m.metadata.title)
});
}
};function chunkText(text: string, maxChunkSize = 500): string[] {
const sentences = text.match(/[^.!?]+[.!?]+/g) || [text];
const chunks: string[] = [];
let currentChunk = '';
for (const sentence of sentences) {
if ((currentChunk + sentence).length > maxChunkSize && currentChunk) {
chunks.push(currentChunk.trim());
currentChunk = sentence;
} else {
currentChunk += sentence;
}
}
if (currentChunk) chunks.push(currentChunk.trim());
return chunks;
}
// Usage
const chunks = chunkText(longDocument, 500);
const embeddings = await env.AI.run('@cf/baai/bge-base-en-v1.5', {
text: chunks
});
const vectors = embeddings.data.map((values, i) => ({
id: `doc-${docId}-chunk-${i}`,
values,
metadata: {
doc_id: docId,
chunk_index: i,
total_chunks: chunks.length,
content: chunks[i]
}
}));
await env.VECTORIZE_INDEX.upsert(vectors);Problem: Filtering doesn't work on existing vectors
Solution: Delete and re-insert vectors OR create metadata indexes BEFORE insertingProblem: "Vector dimensions do not match index configuration"
Solution: Ensure embedding model output matches index dimensions:
- Workers AI bge-base: 768
- OpenAI small: 1536
- OpenAI large: 3072Problem: "Invalid metadata key"
Solution: Keys cannot:
- Be empty
- Contain . (dot)
- Contain " (quote)
- Start with $ (dollar sign)Problem: "Filter exceeds 2048 bytes"
Solution: Simplify filter or split into multiple queriesProblem: Slow queries or reduced accuracy
Solution: Use lower cardinality fields for range queries, or use seconds instead of milliseconds for timestampsProblem: Updates not reflecting in index
Solution: Use upsert() to overwrite existing vectors, not insert()Problem: "VECTORIZE_INDEX is not defined"
Solution: Add [[vectorize]] binding to wrangler.jsoncProblem: Unclear when to use namespace vs metadata filtering
Solution:
- Namespace: Partition key, applied BEFORE metadata filters
- Metadata: Flexible key-value filtering within namespace# Create index (dimensions and metric cannot be changed later!)
npx wrangler vectorize create <name> \
--dimensions=768 \
--metric=cosine
# List indexes
npx wrangler vectorize list
# Get index details
npx wrangler vectorize get <name>
# Get index info (vector count, mutations)
npx wrangler vectorize info <name>
# Delete index
npx wrangler vectorize delete <name>
# Create metadata index (BEFORE inserting vectors!)
npx wrangler vectorize create-metadata-index <name> \
--property-name=category \
--type=string
# List metadata indexes
npx wrangler vectorize list-metadata-index <name>
# Delete metadata index
npx wrangler vectorize delete-metadata-index <name> \
--property-name=category
# Insert vectors from file
npx wrangler vectorize insert <name> \
--file=vectors.ndjson
# Query vectors
npx wrangler vectorize query <name> \
--vector="[0.1, 0.2, ...]" \
--top-k=5 \
--return-metadata=all
# List vector IDs
npx wrangler vectorize list-vectors <name> \
--count=100
# Get vectors by IDs
npx wrangler vectorize get-vectors <name> \
--ids="id1,id2,id3"
# Delete vectors by IDs
npx wrangler vectorize delete-vectors <name> \
--ids="id1,id2,id3"returnValues: true when needed (saves bandwidth)✅ Use Vectorize when:
❌ Don't use Vectorize for:
All working code examples are in ./templates/:
basic-search.ts - Simple vector search implementationrag-chat.ts - Complete RAG chatbotdocument-ingestion.ts - Document processing pipelinemetadata-filtering.ts - Advanced filtering patternsDetailed guides in ./references/:
wrangler-commands.md - Complete CLI referenceindex-operations.md - Index creation and managementvector-operations.md - Insert, query, delete operationsmetadata-guide.md - Metadata indexes and filteringembedding-models.md - Model configurationsComplete integration guides in ./references/:
integration-workers-ai-bge-base.md - Workers AI integration (@cf/baai/bge-base-en-v1.5)integration-openai-embeddings.md - OpenAI embeddings integrationVersion: 1.0.0 Status: Production Ready ✅ Token Savings: ~65% Errors Prevented: 8 major categories Dev Time Saved: ~2.5 hours per implementation
~30 seconds. Free. No account. Every finding cites a rule and a line of evidence.