AI Integration Patterns
12 production-ready patterns for robust AI integration. All patterns implement AiClientand compose via the decorator pattern.
Install: pnpm add @cognivo/core — all patterns included.
Guardrails
GuardedClientValidate AI inputs and outputs against configurable rules. Prevent prompt injection, enforce length limits, check confidence ranges, and block PII.
import { GuardedClient, maxLengthRule, noPromptInjectionRule, noPiiRule } from '@cognivo/core';
const client = new GuardedClient(baseClient, {
rules: [maxLengthRule(500), noPromptInjectionRule, noPiiRule],
});Resilience (Fallback)
FallbackClientAutomatically fall back to alternate AI providers when the primary fails. Configure multiple providers in priority order.
import { FallbackClient } from '@cognivo/core';
const client = new FallbackClient({
clients: [openAiClient, anthropicClient],
maxRetries: 2,
});Caching
CachedClientLRU cache for AI responses. Avoid redundant API calls for identical requests.
import { CachedClient, InMemoryLruCache } from '@cognivo/core';
const client = new CachedClient(baseClient, {
cache: new InMemoryLruCache({ maxSize: 100 }),
});Semantic Caching
SemanticCachedClientCache based on semantic similarity rather than exact match. Uses embeddings to find similar previous queries.
import { SemanticCachedClient, SemanticCacheStore } from '@cognivo/core';
const client = new SemanticCachedClient(baseClient, {
store: new SemanticCacheStore({ threshold: 0.9 }),
});Model Routing
ModelRouterClientRoute requests to different AI models based on intent, complexity, or custom logic. Optimize cost and quality.
import { ModelRouterClient } from '@cognivo/core';
const client = new ModelRouterClient({
routes: [
{ match: { intent: AiIntent.Summarize }, client: fastClient },
{ match: { intent: AiIntent.Forecast }, client: advancedClient },
],
default: baseClient,
});Conversation
ConversationalClientManage multi-turn conversations with automatic history tracking and context window management.
import { ConversationalClient } from '@cognivo/core';
const client = new ConversationalClient(baseClient, {
maxTurns: 20,
systemPrompt: 'You are a data analyst.',
});Circuit Breaker
CircuitBreakerClientStop calling a failing provider after repeated errors. Auto-recover after a cooldown period.
import { CircuitBreakerClient } from '@cognivo/core';
const client = new CircuitBreakerClient(baseClient, {
failureThreshold: 5,
cooldownMs: 30_000,
});Self-Refine
SelfRefineClientIteratively improve AI responses by feeding the output back as input. Useful for complex analysis.
import { SelfRefineClient } from '@cognivo/core';
const client = new SelfRefineClient(baseClient, {
maxIterations: 3,
judge: (result) => result.confidence > 0.9,
});Extended Thinking
ExtendedThinkingClientChain-of-thought reasoning with structured steps. The AI shows its work before producing a final answer.
import { ExtendedThinkingClient } from '@cognivo/core';
const client = new ExtendedThinkingClient(baseClient, {
steps: ['analyze', 'hypothesize', 'verify', 'conclude'],
});Prompt Optimization
PromptOptimizerAutomatically improve prompts based on evaluation metrics. A/B test different prompt strategies.
import { PromptOptimizer, PromptEvaluator } from '@cognivo/core';
const optimizer = new PromptOptimizer({
evaluator: new PromptEvaluator({ metrics: ['relevance', 'coherence'] }),
variants: [promptA, promptB],
});Agent Coordination
AgentCoordinatorOrchestrate multiple AI agents with a planner. Each agent has a role and the coordinator manages the workflow.
import { AgentCoordinator } from '@cognivo/core';
const coordinator = new AgentCoordinator({
agents: { analyst: analyticsClient, writer: contentClient },
planner: plannerClient,
});
const result = await coordinator.execute('Analyze Q4 sales and write a report');Observability
ObservableClientTrace AI requests with spans, timing, and metadata. Integrates with any tracing backend via the AiTracer interface.
import { ObservableClient, InMemoryTracer } from '@cognivo/core';
const tracer = new InMemoryTracer();
const client = new ObservableClient(baseClient, { tracer });
await client.run(context);
console.log(tracer.spans); // [{ name, duration, metadata, ... }]AiClient, so you can stack them:ObservableClient(GuardedClient(CachedClient(OpenAiClient))). See the Core API page for a full example.