npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

ai-patterns

v1.3.3

Published

Production-ready TypeScript patterns to build solid and robust AI applications. Retry logic, circuit breakers, rate limiting, human-in-the-loop escalation, prompt versioning, response validation, context window management, and more—all with complete type

Downloads

310

Readme

ai-patterns

npm version Downloads License: MIT TypeScript

Battle-tested TypeScript patterns for building rock-solid AI applications.

We provide developers with battle-tested tools for resilient AI workflows: retry logic, circuit breakers, rate limiting, human-in-the-loop escalation, and more — all with complete type safety and composability. Inspired by Vercel AI SDK's developer experience.

Features

Battle-Tested Patterns - Retry, Circuit Breaker, Timeout, Rate Limiter, Fallback, Cache, Debounce, Throttle, Bulkhead, A/B Testing, Cost Tracking, Prompt Versioning, Response Validation, Context Window Management, Reflection Loop, and more 🎨 Elegant Composition - Compose patterns together for complex workflows 🔒 Type-Safe - Full TypeScript support with generics and strict mode 🧩 Composable - Patterns work together seamlessly for robust workflows 📊 Observable - Built-in lifecycle callbacks for monitoring and debugging 🪶 Lightweight - Zero dependencies, minimal overhead ⚡ Production-Ready - Build solid AI applications with confidence 🎯 Developer-Friendly - Inspired by Vercel AI SDK's excellent DX 💰 Cost Control - Track and control AI spending in real-time 🧪 Experimentation - A/B test prompts and models to optimize performance

Installation

npm install ai-patterns
# or
yarn add ai-patterns
# or
pnpm add ai-patterns

Quick Start

Simple Retry

import { retry } from 'ai-patterns';

// Retry any async function
const result = await retry({
  execute: () => fetch('https://api.example.com/data'),
  maxAttempts: 3
});

console.log(result.value);

With Vercel AI SDK

import { retry } from 'ai-patterns';
import { generateText } from 'ai';
import { openai } from '@ai-sdk/openai';

const result = await retry({
  execute: async () => {
    const { text } = await generateText({
      model: openai('gpt-4-turbo'),
      prompt: 'Explain quantum computing',
      maxRetries: 0 // Disable Vercel's built-in retry
    });
    return text;
  },
  maxAttempts: 3
});

console.log(result.value);

💡 Note: While Vercel AI SDK has built-in retry (maxRetries: 2), ai-patterns gives you more flexibility:

  • 🎛️ Custom backoff strategies (exponential, linear, fixed)
  • 📊 Detailed observability (attempts, delays, errors)
  • 🔄 Cross-provider fallback (OpenAI → Claude → Gemini)
  • 🎯 Advanced retry logic (conditional, circuit breakers)

Why ai-patterns?

Building AI applications? You're probably facing these challenges:

Copy-pasting retry logic across every API call ❌ No circuit breakers — one API failure brings down your entire app ❌ Constantly hitting rate limits with no systematic handling ❌ No human oversight for edge cases that need review

With ai-patterns:

Battle-tested patterns ready to use out of the box ✅ Compose like Lego blocks — combine patterns seamlessly ✅ Full type safety — catch errors at compile time ✅ Zero dependencies — lightweight and production-ready

Before ai-patterns:

// 50+ lines of retry logic with exponential backoff,
// jitter, error classification, timeout handling...
let attempt = 0;
const maxAttempts = 3;
while (attempt < maxAttempts) {
  try {
    // ... complex retry logic
  } catch (error) {
    // ... backoff calculation
    // ... error handling
  }
}

After ai-patterns:

const result = await retry({
  execute: () => callAPI(),
  maxAttempts: 3
});

That's it. Simple, reliable, production-ready.

Advanced Usage

Stateful Patterns

Use defineCircuitBreaker and defineRateLimiter for patterns that maintain state:

const breaker = defineCircuitBreaker({
  execute: (prompt: string) => callAPI(prompt),
  failureThreshold: 5,
  resetTimeout: 60000
});

// Reuse the same instance across calls
await breaker('First call');
await breaker('Second call');
console.log(breaker.getState()); // Check circuit state

Pattern Composition

Compose patterns together for robust workflows using the compose() function:

import { compose, withRetry, withTimeout, withFallback } from 'ai-patterns';

// Create a reusable composed function
const robustAI = compose<string, string>([
  withFallback({ fallback: () => "Sorry, service unavailable" }),
  withTimeout({ duration: 10000 }),
  withRetry({
    maxAttempts: 3,
    backoffStrategy: "exponential",
  })
]);

// Use it anywhere
const result = await robustAI(callAI, "Explain quantum computing");

Tip: You can also nest patterns directly if you prefer explicit control flow.

For advanced composition strategies:


Patterns

Core Patterns

| Pattern | Description | Use Case | Docs | |---------|-------------|----------|------| | compose | Functional pattern composition | Complex AI pipelines | 📖 | | retry | Automatic retry with exponential backoff | Unstable APIs, network issues | 📖 | | timeout | Time limits with AbortSignal support | Long-running operations | 📖 | | fallback | Execute alternatives on failure | Multi-provider failover | 📖 | | defineCircuitBreaker | Protect against failing services | External API calls | 📖 | | defineRateLimiter | Control request throughput | API rate limiting | 📖 |

Advanced Patterns

| Pattern | Description | Use Case | Docs | |---------|-------------|----------|------| | memoize | Cache function results with TTL | Response caching | 📖 | | defineDebounce | Delay execution until silence period | User input handling | 📖 | | defineThrottle | Limit execution frequency | API call throttling | 📖 | | defineBulkhead | Isolate resources with concurrency limits | Resource isolation | 📖 | | deadLetterQueue | Handle failed operations | Error recovery | 📖 |

Orchestration Patterns

| Pattern | Description | Use Case | Docs | |---------|-------------|----------|------| | fanOut | Parallel processing with concurrency control | Batch operations | 📖 | | saga | Distributed transactions with compensation | Multi-step workflows | 📖 | | conditionalBranch | Route based on conditions | Dynamic workflow routing | 📖 |

AI-Specific Patterns

| Pattern | Description | Use Case | Docs | |---------|-------------|----------|------| | humanInTheLoop | AI → Human escalation | Content moderation | 📖 | | smartContextWindow | Manage context token limits automatically | Long conversations, chat apps | 📖 | | reflectionLoop | AI self-critique and iterative improvement | High-quality content generation | 📖 | | idempotency | Prevent duplicate operations | Payment processing | 📖 |

Experimentation & Monitoring

| Pattern | Description | Use Case | Docs | |---------|-------------|----------|------| | abTest | Test multiple variants simultaneously | Prompt optimization, model selection | 📖 | | costTracking | Monitor and control AI spending | Budget management, cost optimization | 📖 | | versionedPrompt | Manage prompt versions with rollback | Prompt experimentation, gradual rollout | 📖 | | validateResponse | Validate AI responses with auto-retry | Quality assurance, business rules | 📖 |


Pattern Examples

Robust API Call

import { retry, timeout } from 'ai-patterns';

const result = await retry({
  execute: async () => {
    return await timeout({
      execute: () => fetch('https://api.example.com/data'),
      timeoutMs: 5000
    });
  },
  maxAttempts: 3
});

AI Agent with Fallback

import { fallback } from 'ai-patterns';
import { generateText } from 'ai';
import { openai, anthropic } from '@ai-sdk/openai';

const result = await fallback({
  execute: async () => {
    const { text } = await generateText({
      model: openai('gpt-4-turbo'),
      prompt: 'Explain quantum computing'
    });
    return text;
  },
  fallback: async () => {
    const { text} = await generateText({
      model: anthropic('claude-3-5-sonnet-20241022'),
      prompt: 'Explain quantum computing'
    });
    return text;
  }
});

Data Processing Pipeline

import { fanOut } from 'ai-patterns';
import { embed } from 'ai';

const chunks = [
  { id: '1', text: 'Introduction to ML' },
  { id: '2', text: 'Deep learning basics' },
  // ... more chunks
];

const result = await fanOut({
  items: chunks,
  execute: async (chunk) => {
    const { embedding } = await embed({
      model: openai.embedding('text-embedding-3-small'),
      value: chunk.text
    });
    return { id: chunk.id, embedding };
  },
  concurrency: 5
});

Composing Patterns with Middleware

import { compose, retryMiddleware, timeoutMiddleware } from 'ai-patterns/composition';
import { generateText } from 'ai';
import { openai } from '@ai-sdk/openai';

// Compose multiple patterns functionally
const robustAI = compose([
  timeoutMiddleware({ duration: 10000 }),
  retryMiddleware({ maxAttempts: 3, backoffStrategy: 'exponential' })
]);

// Use the composed function
const result = await robustAI(
  async (prompt: string) => {
    const { text } = await generateText({
      model: openai('gpt-4-turbo'),
      prompt
    });
    return text;
  },
  'Explain quantum computing'
);

For detailed pattern documentation:

Runnable examples:


Examples

Basic Examples

Each pattern has a simple runnable example:

Advanced Examples

Real-World Examples

Coming soon:

  • E-commerce - Order processing with saga, retry, and idempotency
  • AI Agent - Chatbot with human escalation and circuit breakers
  • Microservices - API gateway with rate limiting and retries

Documentation

Pattern Documentation

Guides


API Reference

All patterns follow a consistent API design:

const result = await pattern({
  execute: () => yourFunction(),
  // pattern-specific options...
});

See the API Reference for complete details.


Type Safety

Built with TypeScript strict mode for maximum type safety:

// Full type inference with generics
interface User {
  id: string;
  name: string;
  email: string;
}

const result = await retry<User>({
  execute: async () => {
    return await fetchUser();
  }
});

// result.value is typed as User
const user: User = result.value;
console.log(user.email); // ✅ Full autocomplete

Contributing

Contributions are welcome! Please read our Contributing Guide.


License

MIT © Serge KOKOUA


Acknowledgments

Inspired by:


Built with ❤️ by Serge KOKOUA

Empowering developers to build solid and robust AI applications.