| name | cohere-sdk-patterns |
| description | Apply production-ready Cohere SDK patterns for TypeScript and Python.
Use when implementing Cohere integrations, refactoring SDK usage,
or establishing team coding standards for Cohere API v2.
Trigger with phrases like "cohere SDK patterns", "cohere best practices",
"cohere code patterns", "idiomatic cohere", "cohere wrapper".
|
| allowed-tools | Read, Write, Edit |
| version | 1.5.0 |
| license | MIT |
| author | Jeremy Longshore <jeremy@intentsolutions.io> |
| tags | ["saas","ai","nlp","cohere"] |
| compatibility | Designed for Claude Code |
Cohere SDK Patterns
Overview
Production-ready patterns for the cohere-ai TypeScript SDK (CohereClientV2) and Python cohere package. Real model names, real API shapes, real error types.
Prerequisites
cohere-ai v7+ installed (TypeScript) or cohere v5+ (Python)
- Familiarity with async/await patterns
- Understanding of Cohere API v2 endpoints
Instructions
Pattern 1: Singleton Client with Retry
import { CohereClientV2, CohereError, CohereTimeoutError } from 'cohere-ai';
let instance: CohereClientV2 | null = null;
export function getCohere(): CohereClientV2 {
if (!instance) {
if (!process.env.CO_API_KEY) {
throw new Error('CO_API_KEY environment variable is required');
}
instance = new CohereClientV2({
token: process.env.CO_API_KEY,
});
}
return instance;
}
export async function withRetry<T>(
operation: () => Promise<T>,
maxRetries = 3,
baseDelayMs = 1000
): <T> {
( attempt = ; attempt <= maxRetries; attempt++) {
{
();
} (err) {
(attempt === maxRetries) err;
(err ) {
status = err.;
(status && status !== && status < ) err;
} (!(err )) {
err;
}
delay = baseDelayMs * .(, attempt - );
jitter = .() * ;
( (r, delay + jitter));
}
}
();
}