Stop your AI APIs from draining your wallet. One package. Zero proxies.
Docs β’ Quick Start β’ Why? β’ Issues
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.
pnpm add ai-armorimport { 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.
| 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 |
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!' })import { createArmorHandler } from 'ai-armor/http'
app.use('/api/ai', createArmorHandler(armor))
// 403 = safety blocked, 429 = rate limited, 402 = budget exceededpnpm 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()
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.
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.
| 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 |
| Package | Install |
|---|---|
ai-armor |
pnpm add ai-armor |
@ai-armor/nuxt |
pnpm add @ai-armor/nuxt |
ai-armor saves you money on AI costs. Consider supporting development:
MIT -- Billy Maulana