{"record":{"id":"5ea33391acde79a4","repo":"tobi/qmd","slug":"failed-to-create-any-embedding-context","errorCode":null,"errorMessage":"Failed to create any embedding context","messagePattern":"Failed to create any embedding context","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"critical","filePath":"src/llm.ts","lineNumber":1218,"sourceCode":"        try {\n          perContextMB = estimateEmbedContextMB({\n            modelBytes: statSync(this.embedModelPath).size,\n            contextSize: LlamaCpp.EMBED_CONTEXT_SIZE,\n          });\n        } catch {\n          // Keep the baseline if the file cannot be stat'd.\n        }\n      }\n      const n = await this.computeParallelism(perContextMB, EMBED_POOL_RERANK_RESERVE_MB);\n      const threads = await this.threadsPerContext(n);\n      for (let i = 0; i < n; i++) {\n        try {\n          this.embedContexts.push(await model.createEmbeddingContext({\n            contextSize: LlamaCpp.EMBED_CONTEXT_SIZE,\n            ...(threads > 0 ? { threads } : {}),\n          }));\n        } catch {\n          if (this.embedContexts.length === 0) throw new Error(\"Failed to create any embedding context\");\n          break;\n        }\n      }\n      this.touchActivity();\n      return this.embedContexts;\n    })();\n\n    try {\n      return await this.embedContextsCreatePromise;\n    } finally {\n      this.embedContextsCreatePromise = null;\n    }\n  }\n\n  /**\n   * Get a single embed context (for single-embed calls). Uses first from pool.\n   */\n  private async ensureEmbedContext(): Promise<LlamaEmbeddingContext> {","sourceCodeStart":1200,"sourceCodeEnd":1236,"githubUrl":"https://github.com/tobi/qmd/blob/dbfd0b4736aeaf761d1a16ca8e424f071df8feb9/src/llm.ts#L1200-L1236","documentation":"LlamaCpp failed to create even a single embedding context via model.createEmbeddingContext(). The code retries context creation and only throws when the first attempt fails (embedContexts is empty), meaning node-llama-cpp could not allocate a context window of LlamaCpp.EMBED_CONTEXT_SIZE for the loaded embedding model.","triggerScenarios":"Calling a method that triggers ensureEmbedContexts()/embed() (e.g. embedBatch, qmd embed) when createEmbeddingContext throws — typically insufficient memory/VRAM for the context size, an incompatible or corrupt GGUF embedding model, or the model was disposed/unloaded mid-operation.","commonSituations":"Low-RAM machines or CI runners where the requested contextSize exceeds available memory; switching to a larger embedding model (embeddinggemma) without enough headroom; GPU layers misconfigured; node-llama-cpp native module build mismatch after upgrade.","solutions":["Free memory or reduce embedding context size / threads configuration and retry","Verify the embedding model file downloads correctly and is a valid GGUF (re-download if corrupt)","Run `qmd doctor` to diagnose model/device issues","Check node-llama-cpp native bindings are built for your platform (rebuild after upgrades)"],"exampleFix":"// before\nconst ctxs = await llamaCpp.getEmbedContexts(); // throws if createEmbeddingContext fails\n// after\ntry {\n  const ctxs = await llamaCpp.getEmbedContexts();\n} catch (e) {\n  console.error('Embedding context creation failed — check RAM/VRAM and model file:', e);\n  // fall back to BM25-only search: qmd search instead of qmd query/vsearch\n}","handlingStrategy":"fallback","validationCode":"const free = process.memory?.available ?? require('os').freemem();\nif (free < 1_000_000_000) { /* skip embeddings, use BM25 only */ }","typeGuard":null,"tryCatchPattern":"try { await llm.embed(text); } catch (e) { if ((e as Error).message.includes('embedding context')) return null; /* degrade to keyword search */ throw e; }","preventionTips":["Run qmd doctor before embedding workflows","Ensure enough free RAM/VRAM for the embedding model's context size","Keep embedding model files valid and version-matched to node-llama-cpp"],"tags":["llm","embedding","memory","node-llama-cpp"],"backgroundTag":"llm-context-allocation-failed","analyzedSha":"dbfd0b4736aeaf761d1a16ca8e424f071df8feb9","analyzedAt":"2026-08-28T18:07:46.628Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}