
Security News
PolinRider: North Korea-Linked Supply Chain Campaign Expands Across Open Source Ecosystems
PolinRider expands across npm, Packagist, Go modules, and Chrome extensions, using hidden loaders to target developer environments.
AI SDK middleware for oncely idempotency - prevent duplicate LLM calls
AI SDK middleware for idempotent LLM calls. Prevent duplicate API calls and save tokens.
AI API calls are expensive. When networks fail, users retry, or your app has bugs—you pay twice for the same work. Neither OpenAI, Anthropic, nor the AI SDK provide built-in idempotency.
import { wrapLanguageModel, generateText } from 'ai';
import { openai } from '@ai-sdk/openai';
import { idempotencyMiddleware } from '@oncely/ai';
const model = wrapLanguageModel({
model: openai('gpt-4-turbo'),
middleware: idempotencyMiddleware({ ttl: '5m' }),
});
// First call - hits the API, costs tokens
const result1 = await generateText({ model, prompt: 'Explain quantum computing' });
// Retry/duplicate - returns cached response instantly, zero tokens
const result2 = await generateText({ model, prompt: 'Explain quantum computing' });
npm install @oncely/ai @oncely/core ai
For production, add a storage adapter:
npm install @oncely/redis ioredis # Standard Redis
npm install @oncely/upstash # Serverless (Upstash, Vercel KV)
import { wrapLanguageModel } from 'ai';
import { idempotencyMiddleware } from '@oncely/ai';
const model = wrapLanguageModel({
model: yourModel,
middleware: idempotencyMiddleware(),
});
import { wrapLanguageModel } from 'ai';
import { idempotencyMiddleware } from '@oncely/ai';
import { redis } from '@oncely/redis';
const model = wrapLanguageModel({
model: openai('gpt-4-turbo'),
middleware: idempotencyMiddleware({
storage: redis(),
ttl: '5m',
}),
});
import { idempotencyMiddleware } from '@oncely/ai';
import { upstash } from '@oncely/upstash';
const model = wrapLanguageModel({
model: anthropic('claude-3-opus'),
middleware: idempotencyMiddleware({
storage: upstash(),
ttl: '10m',
}),
});
By default, the middleware generates a cache key by hashing:
Same inputs = same key = cached response.
Pass an explicit key via providerOptions:
const result = await generateText({
model,
prompt: 'Hello',
providerOptions: {
oncely: { key: 'user-123-greeting' },
},
});
const model = wrapLanguageModel({
model: yourModel,
middleware: idempotencyMiddleware({
getKey: (params) => {
// Your custom key logic
return `custom:${hashObject(params.prompt)}`;
},
}),
});
| Option | Type | Default | Description |
|---|---|---|---|
storage | StorageAdapter | MemoryStorage | Storage backend |
ttl | string | number | '5m' | Cache duration |
getKey | (params) => string | Auto-hash | Custom key generation |
includeModelInKey | boolean | true | Include model ID in cache key |
onHit | (key, response) => void | — | Callback on cache hit |
onMiss | (key) => void | — | Callback on cache miss |
Override settings per-request via providerOptions.oncely:
const result = await generateText({
model,
prompt: 'Hello',
providerOptions: {
oncely: {
key: 'explicit-key', // Override auto-generated key
ttl: '1h', // Override TTL for this request
skip: true, // Skip idempotency entirely
},
},
});
The middleware works with any AI SDK provider:
@ai-sdk/openai)@ai-sdk/anthropic)@ai-sdk/google)@ai-sdk/mistral)@ai-sdk/cohere)Works with both generateText and streamText:
const result = await streamText({
model,
prompt: 'Write a poem',
});
// Cached streams are replayed from storage
for await (const chunk of result.textStream) {
console.log(chunk);
}
import { wrapLanguageModel } from 'ai';
import { idempotencyMiddleware } from '@oncely/ai';
const model = wrapLanguageModel({
model: openai('gpt-4-turbo'),
middleware: [
idempotencyMiddleware({ storage: redis() }),
loggingMiddleware(),
rateLimitMiddleware(),
],
});
MIT
FAQs
AI SDK middleware for oncely idempotency - prevent duplicate LLM calls
The npm package @oncely/ai receives a total of 0 weekly downloads. As such, @oncely/ai popularity was classified as not popular.
We found that @oncely/ai demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
PolinRider expands across npm, Packagist, Go modules, and Chrome extensions, using hidden loaders to target developer environments.

Security News
Open source attacks are accelerating as AI coding agents pull in dependencies faster, with less human review.

Research
/Security News
Malicious Chrome and Firefox extensions posed as free VPNs while stealing clipboard data through later extension updates.