Skip to content

billymaulana/ai-armor

Repository files navigation

AI ARMOR

Stop your AI APIs from draining your wallet. One package. Zero proxies.

npm MIT CI 96.6% stars

Docs β€’ Quick Start β€’ Why? β€’ Issues


Why ai-armor?

You ship an AI feature. It works great. Then:

  • A user discovers your endpoint and loops it 10,000 times -- $800 gone
  • Your OpenAI key hits rate limits during a demo -- no fallback
  • Someone pastes their SSN into your chatbot -- PII leak
  • You have no idea which model costs the most -- zero visibility

Every team rebuilds these guardrails from scratch. ai-armor gives you all of them in one npm install.

LiteLLM solved this for Python (39K+ stars). ai-armor is the TypeScript equivalent -- but embeddable, not a proxy.


Quick Start

pnpm add ai-armor
import { createArmor } from 'ai-armor'

const armor = createArmor({
  // Limit users to 20 requests/minute
  rateLimit: {
    strategy: 'sliding-window',
    rules: [{ key: 'user', limit: 20, window: '1m' }],
  },

  // Cap spending at $50/day, auto-downgrade expensive models
  budget: {
    daily: 50,
    monthly: 500,
    onExceeded: 'downgrade-model',
    downgradeMap: { 'gpt-4o': 'gpt-4o-mini' },
  },

  // Block prompt injection and PII before it reaches the API
  safety: {
    promptInjection: true,
    piiDetection: true,
    maxTokensPerRequest: 4096,
  },

  // Cache identical requests (save money on repeated questions)
  cache: { enabled: true, strategy: 'exact', ttl: 300, maxSize: 500 },
})

That's it. Every AI request through this instance is now rate-limited, budget-controlled, safety-scanned, and cached.


What You Get

Feature What it does
πŸ›‘οΈ Rate Limiting Sliding-window per-user, per-IP, per-API-key
πŸ’° Cost Tracking Auto-pricing for 69 models across 17 providers, daily/monthly/per-user budgets
πŸ” Fallback Chains If OpenAI fails, try Anthropic, then Gemini -- with circuit breaker
⚑ Response Caching Exact-match + semantic (embedding) cache with LRU eviction
πŸ”’ Safety Guardrails 26 injection patterns, 6 PII detectors, token limits, custom regex
πŸ”„ Model Routing fast -> gpt-4o-mini, smart -> claude-sonnet-4-6
πŸ“Š Observability Per-request structured logs with cost, latency, tokens
🐘 Redis Adapter Distributed rate limiting for multi-server deployments

Works With Everything

Vercel AI SDK

import { aiArmorMiddleware } from 'ai-armor/ai-sdk'

const model = wrapLanguageModel({
  model: openai('gpt-4o'),
  middleware: aiArmorMiddleware(armor, { userId: 'user-123' }),
})

// generateText and streamText -- both protected automatically
const { text } = await generateText({ model, prompt: 'Hello!' })

Express / h3 / Connect

import { createArmorHandler } from 'ai-armor/http'

app.use('/api/ai', createArmorHandler(armor))
// 403 = safety blocked, 429 = rate limited, 402 = budget exceeded

Nuxt 3/4

pnpm add @ai-armor/nuxt
// nuxt.config.ts
export default defineNuxtConfig({
  modules: ['@ai-armor/nuxt'],
  aiArmor: {
    rateLimit: { strategy: 'sliding-window', rules: [{ key: 'ip', limit: 30, window: '1m' }] },
    budget: { daily: 50, monthly: 500, onExceeded: 'warn' },
    safety: { promptInjection: true, piiDetection: true },
  },
})

Auto-imported composables: useArmorCost(), useArmorStatus(), useArmorSafety()


Redis for Production

Single server? In-memory works fine. Multiple servers or serverless? Use Redis:

import { createRedisAdapter } from 'ai-armor/redis'

const adapter = createRedisAdapter(new Redis(), { prefix: 'myapp:' })
const armor = createArmor({
  rateLimit: { strategy: 'sliding-window', rules: [/* ... */], store: adapter },
  budget: { daily: 50, onExceeded: 'block', store: adapter },
})

Works with ioredis, @upstash/redis, or any Redis-compatible client.


Semantic Caching

Not just exact matches -- cache similar questions too:

const armor = createArmor({
  cache: {
    enabled: true,
    strategy: 'semantic',
    ttl: 3600,
    similarityThreshold: 0.92,
    embeddingFn: async (text) => {
      // Use any embedding provider
      return await getEmbedding(text)
    },
  },
})

"What is TypeScript?" and "Explain TypeScript to me" hit the same cached response.


How It Compares

ai-armor LiteLLM Portkey Vercel AI SDK
Embeddable (npm) βœ… ❌ proxy ❌ SaaS βœ…
TypeScript native βœ… ❌ Python βœ… βœ…
Rate limiting βœ… βœ… ❌ ❌
Cost + budgets βœ… βœ… βœ… SaaS ❌
Safety guardrails βœ… ❌ ❌ ❌
Semantic cache βœ… ❌ ❌ ❌
Redis adapter βœ… βœ… N/A ❌
Nuxt module βœ… ❌ ❌ ❌
Dependencies 1 Many Many Few

Packages

Package Install
ai-armor pnpm add ai-armor
@ai-armor/nuxt pnpm add @ai-armor/nuxt

Sponsors

ai-armor saves you money on AI costs. Consider supporting development:

Sponsor


License

MIT -- Billy Maulana

About

Production AI toolkit for TypeScript -- rate limiting, cost tracking, fallback, caching, safety guardrails

Topics

Resources

License

Code of conduct

Contributing

Stars

2 stars

Watchers

0 watching

Forks

Packages

 
 
 

Contributors