I was building a "search what was said in my videos" feature on Supabase: split each transcript into 30-second chunks, embed them with the built-in gte-small model, store the vectors in pgvector, and let someone ask a question and jump to the moment that answers it.
The embedding step kept killing my Edge Function:
546: WORKER_RESOURCE_LIMIT
It happened on an 18-minute video. I was looping over all 35 chunks and embedding them in one function call, and it died before the tenth.
Why it happens
gte-small runs inside the Edge Function, on CPU. One embedding is about 160 ms of CPU time. Thirty-five of them back to back blows past the per-request CPU budget, and Supabase kills the worker with 546. It is not a timeout you can raise - it is a resource ceiling, so "give it more time" does nothing.
The fix: embed a small batch, then call yourself
Instead of embedding everything in one call, embed a few chunks, and if a full batch came back, have the function invoke itself for the next one. Five per call stays well inside the limit.
// embed one small batch of not-yet-embedded chunks
export const embedPendingChunks = async (supabase, batch = 5) => {
const { data: pending } = await supabase
.from('transcript_chunks')
.select('id, content')
.is('embedding', null)
.order('id')
.limit(batch)
for (const chunk of pending ?? []) {
const embedding = await embed(chunk.content) // gte-small
await supabase
.from('transcript_chunks')
.update({ embedding: JSON.stringify(embedding) })
.eq('id', chunk.id)
}
return pending?.length ?? 0
}
The function that drives it chains to itself while a full batch keeps coming back:
const BATCH = 5
const SELF = `${Deno.env.get('SUPABASE_URL')}/functions/v1/index-transcripts`
Deno.serve(async (req) => {
const embedded = await embedPendingChunks(supabase, BATCH)
// a full batch means there is probably more waiting - run again
if (embedded === BATCH) {
EdgeRuntime.waitUntil(
fetch(SELF, {
method: 'POST',
headers: { Authorization: req.headers.get('Authorization') ?? '' },
}),
)
}
return Response.json({ embedded })
})
EdgeRuntime.waitUntil lets the current call return while the next one starts, so you get a chain of short calls instead of one long one that dies. Each call does a little work and hands off.
For the same 18-minute video, this embedded all 35 chunks about 15 seconds after the first call, over seven calls. No 546.
Two things that mattered:
-
Claim work before you do it. Mark a transcript as being processed (I set a
chunked_attimestamp) the moment you start on it, so if two calls overlap they do not chunk the same video twice. -
Batch size is a dial. Five was comfortable with
gte-smallat ~160 ms each. A heavier embedding model means a smaller batch.
Where the transcript comes from
This assumes you already have the transcript with a start time on every line. I get those from Importly: send it a video or audio URL, get the transcript back with timestamps, which is what makes "jump to the moment that answers the question" work. The full recipe - the pgvector table, the match_transcript_chunks function and the search endpoint - is in the Supabase transcript search guide.
The pattern generalizes: anything CPU-heavy in an Edge Function - embeddings, image work, big parses - is better as small self-chaining batches than one big loop.
Top comments (0)