content/providers/01-ai-sdk-providers/25-cohere.mdx
The Cohere provider contains language and embedding model support for the Cohere chat API.
The Cohere provider is available in the @ai-sdk/cohere module. You can install it with
<Tabs items={['pnpm', 'npm', 'yarn', 'bun']}> <Tab> <Snippet text="pnpm add @ai-sdk/cohere" dark /> </Tab> <Tab> <Snippet text="npm install @ai-sdk/cohere" dark /> </Tab> <Tab> <Snippet text="yarn add @ai-sdk/cohere" dark /> </Tab>
<Tab> <Snippet text="bun add @ai-sdk/cohere" dark /> </Tab> </Tabs>You can import the default provider instance cohere from @ai-sdk/cohere:
import { cohere } from '@ai-sdk/cohere';
If you need a customized setup, you can import createCohere from @ai-sdk/cohere
and create a provider instance with your settings:
import { createCohere } from '@ai-sdk/cohere';
const cohere = createCohere({
// custom settings
});
You can use the following optional settings to customize the Cohere provider instance:
baseURL string
Use a different URL prefix for API calls, e.g. to use proxy servers.
The default prefix is https://api.cohere.com/v2.
apiKey string
API key that is being sent using the Authorization header.
It defaults to the COHERE_API_KEY environment variable.
headers Record<string,string>
Custom headers to include in the requests.
fetch (input: RequestInfo, init?: RequestInit) => Promise<Response>
Custom fetch implementation.
Defaults to the global fetch function.
You can use it as a middleware to intercept requests,
or to provide a custom fetch implementation for e.g. testing.
generateId () => string
Optional function to generate unique IDs for each request. Defaults to the SDK's built-in ID generator.
You can create models that call the Cohere chat API using a provider instance.
The first argument is the model id, e.g. command-r-plus.
Some Cohere chat models support tool calls.
const model = cohere('command-r-plus');
You can use Cohere language models to generate text with the generateText function:
import { cohere } from '@ai-sdk/cohere';
import { generateText } from 'ai';
const { text } = await generateText({
model: cohere('command-r-plus'),
prompt: 'Write a vegetarian lasagna recipe for 4 people.',
});
Cohere language models can also be used in the streamText function
and support structured data generation with Output
(see AI SDK Core).
| Model | Image Input | Object Generation | Tool Usage | Tool Streaming |
|---|---|---|---|---|
command-a-03-2025 | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
command-a-reasoning-08-2025 | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
command-r7b-12-2024 | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
command-r-plus-04-2024 | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
command-r-plus | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
command-r-08-2024 | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
command-r-03-2024 | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
command-r | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
command | <Cross size={18} /> | <Cross size={18} /> | <Cross size={18} /> | <Cross size={18} /> |
command-nightly | <Cross size={18} /> | <Cross size={18} /> | <Cross size={18} /> | <Cross size={18} /> |
command-light | <Cross size={18} /> | <Cross size={18} /> | <Cross size={18} /> | <Cross size={18} /> |
command-light-nightly | <Cross size={18} /> | <Cross size={18} /> | <Cross size={18} /> | <Cross size={18} /> |
Cohere has introduced reasoning with the command-a-reasoning-08-2025 model. You can learn more at https://docs.cohere.com/docs/reasoning.
import { cohere, type CohereLanguageModelOptions } from '@ai-sdk/cohere';
import { generateText } from 'ai';
async function main() {
const { text, reasoning } = await generateText({
model: cohere('command-a-reasoning-08-2025'),
prompt:
"Alice has 3 brothers and she also has 2 sisters. How many sisters does Alice's brother have?",
// optional: reasoning options
providerOptions: {
cohere: {
thinking: {
type: 'enabled',
tokenBudget: 100,
},
} satisfies CohereLanguageModelOptions,
},
});
console.log(reasoning);
console.log(text);
}
main().catch(console.error);
You can create models that call the Cohere embed API
using the .embedding() factory method.
const model = cohere.embedding('embed-english-v3.0');
You can use Cohere embedding models to generate embeddings with the embed function:
import { cohere, type CohereEmbeddingModelOptions } from '@ai-sdk/cohere';
import { embed } from 'ai';
const { embedding } = await embed({
model: cohere.embedding('embed-english-v3.0'),
value: 'sunny day at the beach',
providerOptions: {
cohere: {
inputType: 'search_document',
} satisfies CohereEmbeddingModelOptions,
},
});
Cohere embedding models support additional provider options that can be passed via providerOptions.cohere:
import { cohere, type CohereEmbeddingModelOptions } from '@ai-sdk/cohere';
import { embed } from 'ai';
const { embedding } = await embed({
model: cohere.embedding('embed-english-v3.0'),
value: 'sunny day at the beach',
providerOptions: {
cohere: {
inputType: 'search_document',
truncate: 'END',
} satisfies CohereEmbeddingModelOptions,
},
});
The following provider options are available:
inputType 'search_document' | 'search_query' | 'classification' | 'clustering'
Specifies the type of input passed to the model. Default is search_query.
search_document: Used for embeddings stored in a vector database for search use-cases.search_query: Used for embeddings of search queries run against a vector DB to find relevant documents.classification: Used for embeddings passed through a text classifier.clustering: Used for embeddings run through a clustering algorithm.truncate 'NONE' | 'START' | 'END'
Specifies how the API will handle inputs longer than the maximum token length.
Default is END.
NONE: If selected, when the input exceeds the maximum input token length will return an error.START: Will discard the start of the input until the remaining input is exactly the maximum input token length for the model.END: Will discard the end of the input until the remaining input is exactly the maximum input token length for the model.| Model | Embedding Dimensions |
|---|---|
embed-english-v3.0 | 1024 |
embed-multilingual-v3.0 | 1024 |
embed-english-light-v3.0 | 384 |
embed-multilingual-light-v3.0 | 384 |
embed-english-v2.0 | 4096 |
embed-english-light-v2.0 | 1024 |
embed-multilingual-v2.0 | 768 |
You can create models that call the Cohere rerank API
using the .reranking() factory method.
const model = cohere.reranking('rerank-v3.5');
You can use Cohere reranking models to rerank documents with the rerank function:
import { cohere } from '@ai-sdk/cohere';
import { rerank } from 'ai';
const documents = [
'sunny day at the beach',
'rainy afternoon in the city',
'snowy night in the mountains',
];
const { ranking } = await rerank({
model: cohere.reranking('rerank-v3.5'),
documents,
query: 'talk about rain',
topN: 2,
});
console.log(ranking);
// [
// { originalIndex: 1, score: 0.9, document: 'rainy afternoon in the city' },
// { originalIndex: 0, score: 0.3, document: 'sunny day at the beach' }
// ]
Cohere reranking models support additional provider options that can be passed via providerOptions.cohere:
import { cohere, type CohereRerankingModelOptions } from '@ai-sdk/cohere';
import { rerank } from 'ai';
const { ranking } = await rerank({
model: cohere.reranking('rerank-v3.5'),
documents: ['sunny day at the beach', 'rainy afternoon in the city'],
query: 'talk about rain',
providerOptions: {
cohere: {
maxTokensPerDoc: 1000,
priority: 1,
} satisfies CohereRerankingModelOptions,
},
});
The following provider options are available:
maxTokensPerDoc number
Maximum number of tokens per document. Default is 4096.
priority number
Priority of the request. Default is 0.
| Model |
|---|
rerank-v3.5 |
rerank-english-v3.0 |
rerank-multilingual-v3.0 |