Cloudflare Vectorize vector database for semantic search and RAG. Use for vector indexes, embeddings, similarity search, or encountering dimension mismatches, filter errors.
npx skills add https://github.com/secondsky/claude-skills --skill cloudflare-vectorize
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-11-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
bunx wrangler vectorize create my-index \
--dimensions=768 \
--metric=cosine
# 2. Create metadata indexes IMMEDIATELY (before inserting vectors!)
bunx wrangler vectorize create-metadata-index my-index \
--property-name=category \
--type=string
bunx wrangler vectorize create-metadata-index my-index \
--property-name=timestamp \
--type=number
Why: 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 vectors
wrangler.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;
}
| Operation | Method | Key Point |
|-----------|--------|-----------|
| Insert | insert([...]) | Keeps first if ID exists |
| Upsert | upsert([...]) | Overwrites if ID exists (use for updates) |
| Query | query(vector, { topK, filter }) | Returns similar vectors |
| Delete | deleteByIds([...]) | Remove by ID array |
| Get | getByIds([...]) | Retrieve specific vectors |
| Operator | Example | Description |
|----------|---------|-------------|
| $eq | { category: "docs" } | Equality (implicit) |
| $ne | { status: { $ne: "archived" } } | Not equal |
| $in | { category: { $in: ["a", "b"] } } | In array |
| $nin | { category: { $nin: ["x"] } } | Not in array |
| $gte/$lt | { timestamp: { $gte: 123 } } | Range queries |
📄 Full operations guide: Load references/vector-operations.md for complete insert/upsert/query/delete examples with code.
| Model | Provider | Dimensions | Best For |
|-------|----------|------------|----------|
| @cf/baai/bge-base-en-v1.5 | Workers AI | 768 | Free, general purpose |
| text-embedding-3-small | OpenAI | 1536 | Balance quality/cost |
| text-embedding-3-large | OpenAI | 3072 | Highest quality |
📄 Integration guides:
references/integration-workers-ai-bge-base.md for Workers AI setupreferences/integration-openai-embeddings.md for OpenAI integration| Limit | Value |
|-------|-------|
| Max metadata indexes | 10 per index |
| Max metadata size | 10 KiB per vector |
| String index | First 64 bytes (UTF-8) |
| Filter size | Max 2048 bytes |
Keys cannot: be empty, contain . (reserved for nesting), contain ", or start with $.
📄 Complete metadata guide: Load references/metadata-guide.md for cardinality best practices, nested metadata, and advanced filtering patterns.
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)
});
}
};
Recommended chunk sizes: 300-500 characters for semantic coherence.
Key metadata for chunks:
doc_id: Parent document IDchunk_index: Position in documentcontent: Text for retrieval display📄 Full chunking implementation: See templates/document-ingestion.ts for complete chunking pipeline.
Problem: Filtering doesn't work on existing vectors
Solution: Delete and re-insert vectors OR create metadata indexes BEFORE inserting
Problem: "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: 3072
Problem: "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 queries
Problem: Slow queries or reduced accuracy
Solution: Use lower cardinality fields for range queries, or use seconds instead of milliseconds for timestamps
Problem: 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.jsonc
Problem: Unclear when to use namespace vs metadata filtering
Solution:
- Namespace: Partition key, applied BEFORE metadata filters
- Metadata: Flexible key-value filtering within namespace
Essential commands:
# Create index (dimensions/metric are PERMANENT)
bunx wrangler vectorize create <name> --dimensions=768 --metric=cosine
# Create metadata index (MUST be before inserting vectors!)
bunx wrangler vectorize create-metadata-index <name> --property-name=category --type=string
# Get index info
bunx wrangler vectorize info <name>
📄 Full CLI reference: Load references/wrangler-commands.md for all vectorize commands.
returnValues: true when needed (saves bandwidth)✅ Use Vectorize when:
❌ Don't use Vectorize for:
| Reference File | Load When... |
|----------------|--------------|
| references/vector-operations.md | Need full insert/upsert/query/delete code examples |
| references/metadata-guide.md | Setting up metadata indexes, filtering best practices |
| references/wrangler-commands.md | Using Vectorize CLI commands |
| references/integration-workers-ai-bge-base.md | Integrating Workers AI embeddings |
| references/integration-openai-embeddings.md | Integrating OpenAI embeddings |
| references/embedding-models.md | Comparing embedding model options |
| references/index-operations.md | Index lifecycle management |
| Template | Purpose |
|----------|---------|
| templates/basic-search.ts | Simple vector search |
| templates/rag-chat.ts | Complete RAG chatbot |
| templates/document-ingestion.ts | Document chunking pipeline |
| templates/metadata-filtering.ts | Advanced filtering |
When installing vector database packages, follow supply chain security best practices:
npm config set ignore-scripts true (or Bun: disabled by default)socket package score npm <pkg> or use socket npm install <pkg> to check packagesLoad the dependency-upgrade skill for full security configuration including Socket CLI integration, cooldown setup, lockfile validation, and CI enforcement.
Version: 1.0.0
Status: Production Ready ✅
Token Savings: ~65%
Errors Prevented: 8 major categories
Dev Time Saved: ~2.5 hours per implementation
Create new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an existing skill, run evals to test a skill, benchmark skill performance with variance analysis, or optimize a skill's description for better triggering accuracy.
Access NCBI GEO for gene expression/genomics data. Search/download microarray and RNA-seq datasets (GSE, GSM, GPL), retrieve SOFT/Matrix files, for transcriptomics and expression analysis.
Bayesian modeling with PyMC. Build hierarchical models, MCMC (NUTS), variational inference, LOO/WAIC comparison, posterior checks, for probabilistic programming and inference.
Multi-objective optimization framework. NSGA-II, NSGA-III, MOEA/D, Pareto fronts, constraint handling, benchmarks (ZDT, DTLZ), for engineering design and optimization problems.
Statistical modeling toolkit. OLS, GLM, logistic, ARIMA, time series, hypothesis tests, diagnostics, AIC/BIC, for rigorous statistical inference and econometric analysis.
Add unsigned integer (uint) type support to PyTorch operators by updating AT_DISPATCH macros. Use when adding support for uint16, uint32, uint64 types to operators, kernels, or when user mentions enabling unsigned types, barebones unsigned types, or uint support.
Convert PyTorch AT_DISPATCH macros to AT_DISPATCH_V2 format in ATen C++ code. Use when porting AT_DISPATCH_ALL_TYPES_AND*, AT_DISPATCH_FLOATING_TYPES*, or other dispatch macros to the new v2 API. For ATen kernel files, CUDA kernels, and native operator implementations.
Write docstrings for PyTorch functions and methods following PyTorch conventions. Use when writing or updating docstrings in PyTorch code.
Take secondsky/cloudflare-vectorize from the repository into ~/.claude/skills for personal
use, or into .claude/skills inside a project.
The agent identifies a skill by the name field in its header. Two skills with the
same name cannot sit side by side — one of them will be ignored.
The instructions reference npm.
Without those the skill loads but fails at the first command.