mem0ai/mem0 · error · Error

Together API key is required

Error message

Together API key is required

What it means

TogetherEmbedder (which extends OpenAIEmbedder) requires an API key at construction time. It checks config.apiKey first, then the TOGETHER_API_KEY environment variable; if neither is set it throws immediately, before any network call. This is a fail-fast configuration error, not a runtime API error.

Source

Thrown at mem0-ts/src/oss/src/embeddings/together.ts:14

import { OpenAIEmbedder } from "./openai";
import { EmbeddingConfig } from "../types";

const DEFAULT_BASE_URL = "https://api.together.ai/v1";
const DEFAULT_MODEL = "intfloat/multilingual-e5-large-instruct";

export class TogetherEmbedder extends OpenAIEmbedder {
  constructor(config: EmbeddingConfig) {
    const openAICompatibleConfig = { ...config };
    delete openAICompatibleConfig.embeddingDims;

    const apiKey = config.apiKey || process.env.TOGETHER_API_KEY;
    if (!apiKey) {
      throw new Error("Together API key is required");
    }

    super({
      ...openAICompatibleConfig,
      apiKey,
      // Honor TOGETHER_API_BASE like the Together LLM does, so a user behind a
      // gateway who sets it gets it for embeddings too instead of silently
      // hitting api.together.ai.
      baseURL:
        config.baseURL ||
        config.url ||
        process.env.TOGETHER_API_BASE ||
        DEFAULT_BASE_URL,
      model: config.model || DEFAULT_MODEL,
    });
  }
}

View on GitHub (pinned to 001c235229)

Solutions

  1. Set TOGETHER_API_KEY in the environment or pass apiKey in the embedder config
  2. If using dotenv, ensure import 'dotenv/config' (or config()) runs before the Memory/TogetherEmbedder is constructed
  3. Check the exact spelling TOGETHER_API_KEY and that it is exported in the deploy environment
  4. Get a valid key from api.together.ai if you do not have one

Example fix

// before
const memory = new Memory({ embedder: { provider: "together" } }); // no key anywhere

// after
const memory = new Memory({
  embedder: { provider: "together", config: { apiKey: process.env.TOGETHER_API_KEY! } },
});
Defensive patterns

Strategy: validation

Validate before calling

const apiKey = config.apiKey ?? process.env.TOGETHER_API_KEY;
if (!apiKey) throw new Error("TOGETHER_API_KEY missing - set it before constructing Memory");

Type guard

function isMissingApiKeyError(err: unknown): boolean {
  return err instanceof Error && err.message === "Together API key is required";
}

Try / catch

try {
  new TogetherEmbedder(config);
} catch (err) {
  if (err instanceof Error && err.message === "Together API key is required") {
    throw new Error("Configuration error: provide config.apiKey or set TOGETHER_API_KEY");
  }
  throw err;
}

Prevention

When it happens

Trigger: Constructing Memory or TogetherEmbedder with provider 'together' while passing no apiKey and having no TOGETHER_API_KEY in the environment; env var name typo (TOGETHER_APIKEY, TOGETHER_KEY); .env file loaded after module initialization or not loaded at all.

Common situations: CI environment missing the secret; dotenv imported after Memory construction; key set in one shell but the process runs elsewhere (Docker, serverless); confusing the Together key with the OpenAI key.

Related errors


AI-assisted analysis of mem0ai/mem0@001c235229 (2026-08-15). Data as JSON: /api/errors/0925718071b66546. Report an issue: GitHub.