# Voyage Code 2

Voyage Code 2 is Voyage AI by MongoDB's code-specialized embedding model. It improves code retrieval by 14.52% over OpenAI text-embedding-3-large across 11 datasets, with a context window of 0 tokens covering Python, C++, Java, and major ML frameworks.

- **Model ID:** `voyage/voyage-code-2`
- **Type:** embedding
- **Providers:** voyage
- **Context window:** Not applicable
- **Maximum output tokens:** 0
- **Pricing:** $0.12/1M input tokens
- **Canonical page:** https://vercel.com/ai-gateway/models/voyage-code-2

## Supported parameters

Detailed capability metadata has not been reported for this model.

## Example

```ts
import { embed } from 'ai';

const result = await embed({
  model: 'voyage/voyage-code-2',
  value: 'Sunny day at the beach',
})
```

## About

Voyage Code 2 is Voyage AI by MongoDB's code-specialized embedding model, released January 1, 2024. It features a context window of 0 tokens and targets code retrieval, code completion, and code assistant applications. On code retrieval tasks across 11 datasets derived from HumanEval, APPS, MBPP, DS-1000, CodeChef, and LeetCode, Voyage Code 2 achieves a 14.52% improvement in recall@5 over OpenAI text-embedding-3-large.

Voyage Code 2 also performs well on general-purpose text retrieval, exceeding OpenAI text-embedding-3-large by 3.03% and Cohere Embed v3 by 4.93%. You can use a single embedding model for both code and documentation retrieval rather than maintaining separate indices with different models.

Voyage AI by MongoDB evaluates it on Python, C++, and Java, plus documentation and usage patterns for Matplotlib, NumPy, Pandas, PyTorch, SciPy, scikit-learn, and TensorFlow. The model handles both natural language queries searching for code (text-to-code) and code snippets searching for similar code (code-to-code).

## What to consider

Voyage Code 2 targets code search. If you're embedding source code, function signatures, and documentation for retrieval, it outperforms general-purpose embedding models by a wide margin.

Voyage AI by MongoDB released voyage-code-3, which supports 300+ programming languages, a 32K context window, and Matryoshka dimensionality. Use voyage-code-3 for new deployments unless you need compatibility with existing Voyage Code 2 indices.

Despite its code focus, Voyage Code 2 outperforms several general-purpose models on standard text retrieval. Use it for mixed code-and-documentation corpora without a second model.

## When to use

### Best For

- **Code search engines** that retrieve relevant functions, classes, or modules from natural language queries
- **Code completion pipelines** that use retrieval-augmented generation to find similar code patterns
- **Developer documentation search** over API references, library docs, and code examples
- **Mixed code and text retrieval** where a single model handles both source code and natural language documentation
- **ML framework documentation** retrieval for Python-centric data science and machine learning workflows

### Consider Alternatives When

- **You need broader language coverage** beyond Python, C++, and Java; voyage-code-3 supports 300+ programming languages
- **You need a longer context window**; voyage-code-3 offers 32K tokens versus Voyage Code 2's 0 tokens
- **Your workload is general-purpose text** with no code; a general-purpose embedding model like voyage-3.5 fits better
- **You need Matryoshka dimensionality** for flexible embedding sizes; voyage-code-3 supports 2048/1024/512/256 dimensions

## Best for

- **Code search engines:** Retrieve relevant functions, classes, or modules from natural language queries
- **Code completion pipelines:** Retrieval-augmented generation finds similar code patterns
- **Developer documentation search:** API references, library docs, and code examples
- **Mixed code and text retrieval:** A single model handles both source code and natural language documentation
- **ML framework documentation:** Retrieval for Python-centric data science and machine learning workflows

## Consider alternatives

- **You need broader language coverage:** voyage-code-3 supports 300+ programming languages beyond Python, C++, and Java
- **You need a longer context window:** voyage-code-3 offers 32K tokens versus Voyage Code 2's 0 tokens
- **Your workload is general-purpose text with no code:** A general-purpose embedding model like voyage-3.5 fits better
- **You need Matryoshka dimensionality:** voyage-code-3 supports 2048/1024/512/256 dimensions for flexible sizing

## Frequently asked questions

### What programming languages does Voyage Code 2 support?

Voyage Code 2 is evaluated on Python, C++, and Java, along with documentation for major ML libraries (NumPy, Pandas, PyTorch, scikit-learn, TensorFlow, Matplotlib, SciPy). It handles both text-to-code and code-to-code retrieval.

### How does Voyage Code 2 compare to OpenAI text-embedding-3-large on code?

Voyage Code 2 achieves a 14.52% improvement in recall@5 over OpenAI text-embedding-3-large across 11 code retrieval datasets derived from HumanEval, APPS, MBPP, DS-1000, CodeChef, and LeetCode.

### Should I use Voyage Code 2 or voyage-code-3?

For new deployments, voyage-code-3 is the recommended choice. It supports 300+ programming languages (vs. Python, C++, Java), a 32K context window (vs. 16K), and flexible Matryoshka dimensions. Use Voyage Code 2 if you have existing indices and want to avoid re-embedding.

### Can Voyage Code 2 handle general text retrieval too?

Yes. Voyage Code 2 exceeds OpenAI text-embedding-3-large by 3.03% on general-purpose text retrieval, making it viable for mixed code-and-documentation corpora.

### What is the context window for Voyage Code 2?

0 tokens. This handles most individual source files and documentation pages. For larger code contexts, use voyage-code-3 with its 32K-token window.

### How do I authenticate Voyage Code 2 through Vercel AI Gateway?

Add your Voyage AI by MongoDB API key in AI Gateway settings, then send embedding requests through AI Gateway. AI Gateway authenticates requests across providers.

### What retrieval tasks is Voyage Code 2 evaluated on?

11 code retrieval datasets totaling 43,909 query-document pairs, derived from HumanEval, APPS, MBPP, DS-1000, CodeChef, and LeetCode. The evaluation metric is `recall@5`.

## Links

- [Model page](https://vercel.com/ai-gateway/models/voyage-code-2)
- [AI Gateway documentation](https://vercel.com/docs/ai-gateway)
- [Provider model documentation](https://docs.voyageai.com/docs/embeddings)
- [Provider pricing](https://docs.voyageai.com/docs/pricing)
