[GH-ISSUE #6708] [Issue]: Memory re-vectorization fails on long chats because all chunks go out in a single embedding request #3757

Closed
opened 2026-10-21 06:06:53 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @boricha417 on GitHub (Sep 26, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/6708

Originally assigned to: @SpicyMarinara on GitHub.

Summary

Re-vectorizing memories for a long chat fails every time because Marinara sends all memory chunks of the chat to the /embeddings endpoint in one request. NanoGPT (and OpenAI-compatible providers in general) cap a single embedding request at 300,000 tokens, so a chat estimated at ~543k tokens is rejected with HTTP 400 no matter how many times the rebuild is retried. Lorebook vectorization already batches its requests (BATCH_SIZE = 50 in lorebooks.routes.ts, reduced to 10 for the sequential path in #5768), but memory recall does not.

This is different from #3317: that issue was about a single input being longer than the model's context (fixed by splitting texts at 18,000 chars). Here every individual input is fine; it is the sum of all inputs in one request that exceeds the provider's batch limit.

Why this happens in normal use

This is not an exotic edge case; it is the normal outcome of an unbounded request size:

  • The request grows linearly with chat length, so every chat eventually crosses the limit. The 300k-token cap is not NanoGPT-specific: it is OpenAI's documented per-request limit for the embeddings API, and most OpenAI-compatible providers mirror it.

  • Rebuilding memories is a normal workflow, not a one-off. Marinara itself requires it when the embedding model changes ("Rebuild memories before mixing embedding models"). In my case the bundled local embedder (all-MiniLM-L6-v2) turned out not to understand Korean at all, so I switched to a Korean-capable remote model and had to re-vectorize every chat. That is exactly when the whole history of a long chat is embedded in one go.

  • Korean (and CJK text in general) tokenizes 2–3× heavier than English, so the limit is reached much sooner. Numbers from my Korean roleplay chats:

    • 227 messages → 167,637 tokens, succeeded (NanoGPT usage page)
    • 643 messages → 543,297 tokens estimated, rejected
    • 809 messages → 643,917 tokens estimated, rejected

    That is roughly 750–850 tokens per message, so the cap sits at around 350–400 messages for a Korean chat. Chats of that length are common in roleplay, and any chat that keeps growing will fail on its next rebuild.

Expected behavior

Re-vectorization should split the chunks into multiple embedding requests (for example 20–50 texts per request, or a token-estimated budget per request) so that a chat of any length can be vectorized. Ideally the batch size would be configurable or derived from the provider's error, and partial progress would be kept between batches.

Actual behavior

The rebuild fails with a 400 from the provider and no memory chunks are written. Retrying produces the exact same estimated token count and the same rejection. Once, the same request timed out with a 504 from the provider instead, which is likely the same oversized request hitting the provider's time limit.

Steps to reproduce

  1. Create a NanoGPT connection and set its embedding model to qwen/qwen3-embedding-8b (max input 32k tokens, max batch 300k tokens).
  2. On the chat connection, point the "embedding connection" dropdown at that NanoGPT connection.
  3. Open a long chat (enough messages that the total text is well over 300k tokens; Korean text reaches this faster than English).
  4. Run "rebuild / re-vectorize memories".
  5. Observe the 400 error in the server log and no memory chunks being created.

Environment

  • Marinara Engine version: 2.4.6 (Docker image ghcr.io/pasta-devs/marinara-engine:latest, pulled 2026-09-16)
  • Install type: docker
  • OS + version: Ubuntu (Vultr VPS), Docker Compose
  • Browser or app shell: Chrome (does not matter, server-side failure)
  • Embedding provider: NanoGPT, model qwen/qwen3-embedding-8b

Logs, screenshots, or video

Four identical failures within two minutes (retries on the 643-message chat), preceded by one 504, then the 809-message chat about twenty minutes later:

{"level":40,"time":1790432860278,"err":{"type":"LLMHttpError","message":"Embedding request failed (504): Embedding generation timed out. Please try again later.","status":504},"msg":"[memory-recall] Configured embedding source [N] Qwen 8b (qwen/qwen3-embedding-8b) failed"}
{"level":40,"time":1790433398424,"err":{"type":"LLMHttpError","message":"Embedding request failed (400): Estimated input tokens (543297) exceeds maximum batch size 300000 for model qwen/qwen3-embedding-8b.","status":400},"msg":"[memory-recall] Configured embedding source [N] Qwen 8b (qwen/qwen3-embedding-8b) failed"}
{"level":40,"time":1790434681662,"err":{"type":"LLMHttpError","message":"Embedding request failed (400): Estimated input tokens (643917) exceeds maximum batch size 300000 for model qwen/qwen3-embedding-8b.","status":400},"msg":"[memory-recall] Configured embedding source [N] Qwen 8b (qwen/qwen3-embedding-8b) failed"}

Stack (identical for every 400):

LLMHttpError: Embedding request failed (400): Estimated input tokens (543297) exceeds maximum batch size 300000 for model qwen/qwen3-embedding-8b.
    at llmHttpErrorFromResponse (file:///app/packages/server/dist/services/llm/base-provider.js:86:12)
    at OpenAIProvider.embed (file:///app/packages/server/dist/services/llm/base-provider.js:608:19)
    at async withConnectionAdmission (file:///app/packages/server/dist/services/generation/connection-admission.js:148:24)
    at async RateLimitAwareProvider.embed (file:///app/packages/server/dist/services/llm/rate-limit-aware-provider.js:138:24)
    at async Object.embed (file:///app/packages/server/dist/services/memory-recall-embedding.js:165:24)
    at async embedMemoryRecallTexts (file:///app/packages/server/dist/services/memory-recall.js:81:38)
    at async chunkAndEmbedMessagesUnlocked (file:///app/packages/server/dist/services/memory-recall.js:274:24)
    at async file:///app/packages/server/dist/services/memory-recall.js:320:9
    at async serializeMemoryMutation (file:///app/packages/server/dist/services/memory-recall.js:33:16)
    at async Object.<anonymous> (file:///app/packages/server/dist/routes/chats.routes.js:1842:25)

Additional context

Where the single request is built (main, packages/server/src/services/memory-recall.ts, chunkAndEmbedMessagesUnlocked):

// Embed all chunks using local model
const texts = embeddableChunks.map((c) => c.content);
const embeddings = await embedMemoryRecallTexts(texts, { ...options, inputType: "document" });

embedMemoryRecallTexts passes the whole array to provider.embed, and OpenAIProvider.embed in base-provider.ts posts { input: texts, model } as-is, so the request size grows linearly with chat length. The lorebook path in lorebooks.routes.ts already loops for (let i = 0; i < texts.length; i += BATCH_SIZE); the same approach in chunkAndEmbedMessagesUnlocked (concatenating the per-batch vectors before the dimension check and insert) would fix this.

Nice-to-have: when the configured embedding source fails, the current log line does not include the chat id, so it is hard to tell which chat failed from the server log alone.

  • I read this template and provided the requested details.
Originally created by @boricha417 on GitHub (Sep 26, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/6708 Originally assigned to: @SpicyMarinara on GitHub. ## Summary Re-vectorizing memories for a long chat fails every time because Marinara sends all memory chunks of the chat to the `/embeddings` endpoint in **one** request. NanoGPT (and OpenAI-compatible providers in general) cap a single embedding request at 300,000 tokens, so a chat estimated at ~543k tokens is rejected with HTTP 400 no matter how many times the rebuild is retried. Lorebook vectorization already batches its requests (`BATCH_SIZE = 50` in `lorebooks.routes.ts`, reduced to 10 for the sequential path in #5768), but memory recall does not. This is different from #3317: that issue was about a single *input* being longer than the model's context (fixed by splitting texts at 18,000 chars). Here every individual input is fine; it is the *sum* of all inputs in one request that exceeds the provider's batch limit. ## Why this happens in normal use This is not an exotic edge case; it is the normal outcome of an unbounded request size: - The request grows linearly with chat length, so **every** chat eventually crosses the limit. The 300k-token cap is not NanoGPT-specific: it is OpenAI's documented per-request limit for the embeddings API, and most OpenAI-compatible providers mirror it. - Rebuilding memories is a normal workflow, not a one-off. Marinara itself requires it when the embedding model changes ("Rebuild memories before mixing embedding models"). In my case the bundled local embedder (all-MiniLM-L6-v2) turned out not to understand Korean at all, so I switched to a Korean-capable remote model and had to re-vectorize every chat. That is exactly when the whole history of a long chat is embedded in one go. - Korean (and CJK text in general) tokenizes 2–3× heavier than English, so the limit is reached much sooner. Numbers from my Korean roleplay chats: - 227 messages → 167,637 tokens, succeeded (NanoGPT usage page) - 643 messages → 543,297 tokens estimated, rejected - 809 messages → 643,917 tokens estimated, rejected That is roughly 750–850 tokens per message, so the cap sits at around 350–400 messages for a Korean chat. Chats of that length are common in roleplay, and any chat that keeps growing will fail on its next rebuild. ## Expected behavior Re-vectorization should split the chunks into multiple embedding requests (for example 20–50 texts per request, or a token-estimated budget per request) so that a chat of any length can be vectorized. Ideally the batch size would be configurable or derived from the provider's error, and partial progress would be kept between batches. ## Actual behavior The rebuild fails with a 400 from the provider and no memory chunks are written. Retrying produces the exact same estimated token count and the same rejection. Once, the same request timed out with a 504 from the provider instead, which is likely the same oversized request hitting the provider's time limit. ## Steps to reproduce 1. Create a NanoGPT connection and set its embedding model to `qwen/qwen3-embedding-8b` (max input 32k tokens, max batch 300k tokens). 2. On the chat connection, point the "embedding connection" dropdown at that NanoGPT connection. 3. Open a long chat (enough messages that the total text is well over 300k tokens; Korean text reaches this faster than English). 4. Run "rebuild / re-vectorize memories". 5. Observe the 400 error in the server log and no memory chunks being created. ## Environment - Marinara Engine version: 2.4.6 (Docker image `ghcr.io/pasta-devs/marinara-engine:latest`, pulled 2026-09-16) - Install type: docker - OS + version: Ubuntu (Vultr VPS), Docker Compose - Browser or app shell: Chrome (does not matter, server-side failure) - Embedding provider: NanoGPT, model `qwen/qwen3-embedding-8b` ## Logs, screenshots, or video Four identical failures within two minutes (retries on the 643-message chat), preceded by one 504, then the 809-message chat about twenty minutes later: ``` {"level":40,"time":1790432860278,"err":{"type":"LLMHttpError","message":"Embedding request failed (504): Embedding generation timed out. Please try again later.","status":504},"msg":"[memory-recall] Configured embedding source [N] Qwen 8b (qwen/qwen3-embedding-8b) failed"} {"level":40,"time":1790433398424,"err":{"type":"LLMHttpError","message":"Embedding request failed (400): Estimated input tokens (543297) exceeds maximum batch size 300000 for model qwen/qwen3-embedding-8b.","status":400},"msg":"[memory-recall] Configured embedding source [N] Qwen 8b (qwen/qwen3-embedding-8b) failed"} {"level":40,"time":1790434681662,"err":{"type":"LLMHttpError","message":"Embedding request failed (400): Estimated input tokens (643917) exceeds maximum batch size 300000 for model qwen/qwen3-embedding-8b.","status":400},"msg":"[memory-recall] Configured embedding source [N] Qwen 8b (qwen/qwen3-embedding-8b) failed"} ``` Stack (identical for every 400): ``` LLMHttpError: Embedding request failed (400): Estimated input tokens (543297) exceeds maximum batch size 300000 for model qwen/qwen3-embedding-8b. at llmHttpErrorFromResponse (file:///app/packages/server/dist/services/llm/base-provider.js:86:12) at OpenAIProvider.embed (file:///app/packages/server/dist/services/llm/base-provider.js:608:19) at async withConnectionAdmission (file:///app/packages/server/dist/services/generation/connection-admission.js:148:24) at async RateLimitAwareProvider.embed (file:///app/packages/server/dist/services/llm/rate-limit-aware-provider.js:138:24) at async Object.embed (file:///app/packages/server/dist/services/memory-recall-embedding.js:165:24) at async embedMemoryRecallTexts (file:///app/packages/server/dist/services/memory-recall.js:81:38) at async chunkAndEmbedMessagesUnlocked (file:///app/packages/server/dist/services/memory-recall.js:274:24) at async file:///app/packages/server/dist/services/memory-recall.js:320:9 at async serializeMemoryMutation (file:///app/packages/server/dist/services/memory-recall.js:33:16) at async Object.<anonymous> (file:///app/packages/server/dist/routes/chats.routes.js:1842:25) ``` ## Additional context Where the single request is built (main, `packages/server/src/services/memory-recall.ts`, `chunkAndEmbedMessagesUnlocked`): ```ts // Embed all chunks using local model const texts = embeddableChunks.map((c) => c.content); const embeddings = await embedMemoryRecallTexts(texts, { ...options, inputType: "document" }); ``` `embedMemoryRecallTexts` passes the whole array to `provider.embed`, and `OpenAIProvider.embed` in `base-provider.ts` posts `{ input: texts, model }` as-is, so the request size grows linearly with chat length. The lorebook path in `lorebooks.routes.ts` already loops `for (let i = 0; i < texts.length; i += BATCH_SIZE)`; the same approach in `chunkAndEmbedMessagesUnlocked` (concatenating the per-batch vectors before the dimension check and insert) would fix this. Nice-to-have: when the configured embedding source fails, the current log line does not include the chat id, so it is hard to tell which chat failed from the server log alone. - [x] I read this template and provided the requested details.
bsakaguchi 2026-10-21 06:06:53 +07:00
Author
Owner

@SirAdoni commented on GitHub (Sep 26, 2026):

I'm working on this in a clean sandbox based on current staging. I'll reproduce the oversized embedding request, add bounded batching with regression coverage, and keep this fix in a focused PR.

<!-- gh-comment-id:5847618231 --> @SirAdoni commented on GitHub (Sep 26, 2026): I'm working on this in a clean sandbox based on current staging. I'll reproduce the oversized embedding request, add bounded batching with regression coverage, and keep this fix in a focused PR.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#3757
No description provided.