| name | cohere-reference-architecture |
| description | Implement Cohere reference architecture with layered project layout for RAG and agents.
Use when designing new Cohere integrations, reviewing project structure,
or establishing architecture standards for Cohere API v2 applications.
Trigger with phrases like "cohere architecture", "cohere project structure",
"cohere layout", "organize cohere app", "cohere design pattern".
|
| allowed-tools | Read, Grep |
| version | 1.5.0 |
| license | MIT |
| author | Jeremy Longshore <jeremy@intentsolutions.io> |
| tags | ["saas","ai","nlp","cohere"] |
| compatibility | Designed for Claude Code |
Cohere Reference Architecture
Overview
Production-ready architecture for Cohere API v2 applications covering RAG pipelines, tool-use agents, and multi-model orchestration.
Prerequisites
- Understanding of layered architecture
cohere-ai SDK v7+
- TypeScript project with vitest
Project Structure
my-cohere-app/
โโโ src/
โ โโโ cohere/
โ โ โโโ client.ts # CohereClientV2 singleton
โ โ โโโ models.ts # Model selection logic
โ โ โโโ types.ts # Cohere-specific types
โ โ โโโ errors.ts # Error classification
โ โโโ services/
โ โ โโโ chat.ts # Chat completions + streaming
โ โ โโโ rag.ts # RAG pipeline (embed โ rerank โ chat)
โ โ โโโ agents.ts # Tool-use agent loops
โ โ โโโ embed.ts # Batch embedding + caching
โ โ โโโ rerank.ts # Document reranking
โ โ โโโ classify.ts # Few-shot classification
โ โโโ tools/ # Tool definitions for agents
โ โ โโโ registry.ts # Tool name โ executor mapping
โ โ โโโ search.ts
โ โ โโโ calculator.ts
โ โโโ api/
โ โ โโโ chat.ts # POST /api/chat (streaming)
โ โ โโโ embed.ts # POST /api/embed
โ โ โโโ health.ts # GET /api/health
โ โโโ cache/
โ โโโ embeddings.ts # LRU cache for embeddings
โโโ tests/
โ โโโ unit/
โ โ โโโ chat.test.ts
โ โ โโโ rag.test.ts
โ โ โโโ fixtures/ # Mocked API responses
โ โโโ integration/
โ โโโ cohere.test.ts # Real API tests (gated)
โโโ config/
โ โโโ models.json # Model selection per environment
โ โโโ tools.json # Tool definitions
โโโ package.json
Layer Architecture
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ API Layer โ
โ (Express/Next.js routes, SSE stream) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Service Layer โ
โ (RAG pipeline, agent loop, classify) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Cohere Layer โ
โ (CohereClientV2, retry, model select) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Infrastructure Layer โ
โ (Embed cache, tool registry, queue) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Core Components
Client Layer
import { CohereClientV2, CohereError, CohereTimeoutError } from 'cohere-ai';
: | = ;
(): {
(!instance) {
instance = ({
: process..,
});
}
instance;
}
= {
: {
: ,
: ,
: ,
},
: {
: ,
: ,
: ,
},
: {
: ,
},
} ;