# .env
OPENAI_API_KEY=sk-...
GOOGLE_GENERATIVE_AI_API_KEY=...
```

## Custom Providers

You can use any OpenAI-compatible embedding endpoint with a custom URL:

```typescript
import { ModelRouterEmbeddingModel } from "@mastra/core";

const embedder = new ModelRouterEmbeddingModel({
  providerId: "ollama",
  modelId: "nomic-embed-text",
  url: "http://localhost:11434/v1",
  apiKey: "not-needed", // Some providers don't require API keys
});
```

## Usage with Memory

The embedding model router integrates seamlessly with Mastra's memory system:

```typescript
import { Memory } from "@mastra/memory";
import { Agent } from "@mastra/core";

const agent = new Agent({
  name: "my-agent",
  instructions: "You are a helpful assistant",
  model: "openai/gpt-4o",
  memory: new Memory({
    embedder: "openai/text-embedding-3-small", // String with autocomplete
  }),
});
```

:::info

The `embedder` field accepts:

- `EmbeddingModelId` (string with autocomplete)
- `EmbeddingModel<string>` (AI SDK v1)
- `EmbeddingModelV2<string>` (AI SDK v2)

:::

## Usage with RAG

Use embedding models for document chunking and retrieval:

```typescript
import { ModelRouterEmbeddingModel } from "@mastra/core";
import { embedMany } from "ai";

const embedder = new ModelRouterEmbeddingModel("openai/text-embedding-3-small");

// Embed document chunks
const { embeddings } = await embedMany({
  model: embedder,
  values: chunks.map((chunk) => chunk.text),
});

// Store embeddings in your vector database
await vectorStore.upsert(
  chunks.map((chunk, i) => ({
    id: chunk.id,
    vector: embeddings[i],
    metadata: chunk.metadata,
  })),
);
```

## TypeScript Support

The model router provides full TypeScript autocomplete for embedding model IDs:

```typescript
import type { EmbeddingModelId } from "@mastra/core";

// Type-safe embedding model selection
const modelId: EmbeddingModelId = "openai/text-embedding-3-small";
//                                  ^ Autocomplete shows all supported models

const embedder = new ModelRouterEmbeddingModel(modelId);
```

## Error Handling

The model router validates provider and model IDs at construction time:

```typescript
try {
  const embedder = new ModelRouterEmbeddingModel("invalid/model");
} catch (error) {
  console.error(error.message);
  // "Unknown provider: invalid. Available providers: openai, google"
}
```

Missing API keys are also caught early:

```typescript
try {
  const embedder = new ModelRouterEmbeddingModel(
    "openai/text-embedding-3-small",
  );
  // Throws if OPENAI_API_KEY is not set
} catch (error) {
  console.error(error.message);
  // "API key not found for provider openai. Set OPENAI_API_KEY environment variable."
}
```

## Next Steps

- [Memory & Semantic Recall](/docs/memory/semantic-recall) - Use embeddings for agent memory
- [RAG & Chunking](/docs/rag/chunking-and-embedding) - Build retrieval-augmented generation systems
- [Vector Databases](/docs/rag/vector-databases) - Store and query embeddings


---
title: "Gateways"
description: "Access AI models through gateway providers with caching, rate limiting, and analytics."
---

{/* This file is auto-generated by generate-model-docs.ts - DO NOT EDIT MANUALLY */}

import { CardGrid, CardGridItem } from "@site/src/components/cards/card-grid";
import { NetlifyLogo } from "@site/src/components/logos/NetlifyLogo";