Route between Claude, GPT, and Gemini through one API with governance.
Multi-Provider AI
The @codmir/ai package provides a single interface for multiple AI providers. Route requests to Claude, GPT, or Gemini based on cost, latency, or quality -- with governance policies for rate limiting and approval gates.
Install
npm install @codmir/aiCreate a CodemirAI Instance
import { createCodemirAI } from '@codmir/ai';
const ai = createCodemirAI({
defaultProvider: 'anthropic',
context: {
projectId: 'my-project',
name: 'My Application',
techStack: ['TypeScript', 'React', 'PostgreSQL'],
description: 'E-commerce platform with AI-powered search',
},
providers: {
anthropic: {
apiKey: process.env.ANTHROPIC_API_KEY,
},
},
});The context object is injected into every request as a system message so the AI understands your project structure.
Provider Selection
Automatic Selection
CodemirAI picks the best available provider when you do not specify one:
const response = await ai.chat('Explain the auth flow');
// Uses the default provider, or falls back to the next available oneExplicit Provider
Force a specific provider per request:
const response = await ai.complete({
messages: [{ role: 'user', content: 'Generate a migration script' }],
provider: 'anthropic',
model: 'claude-sonnet-4-20250514',
maxTokens: 4096,
});Check Provider Availability
// Check a specific provider
const available = ai.isProviderAvailable('anthropic');
// Get status of all providers
const statuses = ai.getProviderStatus();
for (const s of statuses) {
console.log(`${s.name}: ${s.configured ? 'ready' : 'not configured'}`);
}
// Get the best provider with optional preference
const best = ai.getBestProvider('anthropic');Governance Policies
Governance controls what the AI is allowed to do and how often.
Rate Limiting
const ai = createCodemirAI({
governance: {
enabled: true,
rateLimitPerMinute: 60,
},
});
ai.on('rate_limit_reached', (event) => {
console.warn(`Rate limit exceeded: ${event.data.limit}/min`);
});Approval Gates
Require explicit approval for specific actions:
const ai = createCodemirAI({
governance: {
enabled: true,
rateLimitPerMinute: 60,
requireApproval: ['code_generation', 'deployment'],
autoApprove: ['analysis', 'documentation'],
},
});
ai.on('approval_required', (event) => {
console.log(`Approval needed for: ${event.data.action}`);
});
ai.on('governance_decision', (event) => {
const { decision } = event.data;
console.log(`Action: ${decision.action}, Approved: ${decision.approved}`);
if (decision.conditions) {
console.log('Conditions:', decision.conditions);
}
});Project Context
Keep the AI aware of your project by updating context dynamically:
// Set initial context
ai.setContext({
projectId: 'my-project',
name: 'My App',
techStack: ['TypeScript', 'Next.js'],
});
// Add knowledge as you discover it
ai.addKnowledge({
title: 'Database Schema',
content: 'Users table has a one-to-many relationship with Projects...',
});
// Context is automatically injected into all subsequent requests
const response = await ai.chat('How should I structure the API for user projects?');Swarm Voting -- Multi-Model Consensus
For high-stakes decisions, run the same prompt through multiple models and compare results:
import { createSwarm } from '@codmir/ai';
const swarm = createSwarm({
participants: [
{ provider: 'anthropic', model: 'claude-sonnet-4-20250514', weight: 1.0 },
{ provider: 'openai', model: 'gpt-4o', weight: 0.8 },
{ provider: 'gemini', model: 'gemini-2.5-pro', weight: 0.7 },
],
strategy: 'consensus',
});
const result = await swarm.run({
task: 'Review this database migration for data loss risks',
context: migrationSQL,
});
console.log(result.consensus); // The agreed-upon answer
console.log(result.discussion); // Individual model responses
console.log(result.agreement); // Agreement percentageEmbeddings
Generate embeddings for semantic search across any provider:
const embeddings = await ai.embed('How do I deploy?', {
provider: 'anthropic',
});
// Batch embedding
const batchEmbeddings = await ai.embed([
'deployment guide',
'authentication flow',
'database migrations',
]);Event Monitoring
Track every AI operation:
ai.on('request_started', (e) => {
console.log(`Request ${e.data.requestId} started on ${e.data.provider}`);
});
ai.on('request_completed', (e) => {
console.log(`Completed. Tokens: ${e.data.usage.totalTokens}`);
});
ai.on('request_failed', (e) => {
console.error(`Failed on ${e.data.provider}: ${e.data.error}`);
});Next Steps
- Smart Model Routing -- advanced routing with circuit breakers and scoring
- AI Monitoring -- track tokens, cost, and latency in the dashboard
- Swarm Execution -- coordinate thousands of agents on one task