DOCS
v0.4

AI Integration Patterns

12 production-ready patterns for robust AI integration. All patterns implement AiClient and compose via the decorator pattern.

Install: pnpm add @cognivo/core — all patterns included.

01

Guardrails

GuardedClient

Validate 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],
});
02

Resilience (Fallback)

FallbackClient

Automatically 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,
});
03

Caching

CachedClient

LRU 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 }),
});
04

Semantic Caching

SemanticCachedClient

Cache 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 }),
});
05

Model Routing

ModelRouterClient

Route 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,
});
06

Conversation

ConversationalClient

Manage 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.',
});
07

Circuit Breaker

CircuitBreakerClient

Stop 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,
});
08

Self-Refine

SelfRefineClient

Iteratively 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,
});
09

Extended Thinking

ExtendedThinkingClient

Chain-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'],
});
10

Prompt Optimization

PromptOptimizer

Automatically 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],
});
11

Agent Coordination

AgentCoordinator

Orchestrate 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');
12

Observability

ObservableClient

Trace 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, ... }]
All patterns implement AiClient, so you can stack them: ObservableClient(GuardedClient(CachedClient(OpenAiClient))). See the Core API page for a full example.