Configure Cohere model access
You need to call Cohere models through the AI SDK's unified model interface.
AI Tools · Function
createCohere creates a customized Cohere provider instance from optional settings such as an API key, base URL, headers, and fetch implementation. Use the provider to create models for Cohere's chat API.
createCohere constructs a Cohere provider instance from optional settings. The instance can then create models for the Cohere chat API; the API key defaults to COHERE_API_KEY when not supplied.
createCohere(options?: { baseURL?: string; apiKey?: string; headers?: Record<string, string>; fetch?: (input: RequestInfo, init?: RequestInit) => Promise<Response> })| Name | Type | Description |
|---|---|---|
baseURL | string | URL prefix used for API calls; defaults to https://api.cohere.com/v2. |
apiKey | string | Credential sent in the Authorization header; defaults to COHERE_API_KEY. |
headers | Record<string,string> | Additional request headers. |
fetch | (input: RequestInfo, init?: RequestInit) => Promise<Response> | Custom implementation for making requests. |
A Cohere provider instance that can create models for the Cohere chat API.
import { createCohere } from '@ai-sdk/cohere';
import { generateText } from 'ai';
const cohere = createCohere({
apiKey: process.env.COHERE_API_KEY,
});
const { text } = await generateText({
model: cohere('command-r'),
prompt: 'Explain vector embeddings in one sentence.',
});
console.log(text);Creates a configured Cohere provider and uses it to select a Cohere chat model for text generation.
You need to call Cohere models through the AI SDK's unified model interface.
Your application routes outbound model traffic through an internal compatible proxy.
Tests or infrastructure require custom request behavior or headers.
createCohere is imported from @ai-sdk/cohere and returns a configurable provider instance. That instance can be used to create models for Cohere's chat API.
Follow-up: How would you avoid hard-coding credentials? Omit apiKey and provide COHERE_API_KEY through the environment, or pass it from a secure runtime configuration.
The optional apiKey is sent in the Authorization header. If it is not supplied, the provider uses the COHERE_API_KEY environment variable.
Follow-up: What other provider-level customization is available? baseURL, headers, and fetch can also be supplied.
baseURL changes the URL prefix used for API calls, while headers adds request headers and fetch allows a custom request implementation. The documented default base URL is https://api.cohere.com/v2.
Follow-up: When is baseURL useful? It is useful when requests must pass through a proxy server or compatible alternate endpoint.
cohere@ai-sdk/coheregenerateTextCohere chat API