Quickstart with SDK
Add PromptCacheAI in a Node app
The TypeScript SDK is the fastest way to add PromptCacheAI. It checks the cache before your model call, calls your model on a miss, and saves the response when saving is recommended.
1. Create an API key
Sign in, open Settings - API Keys, and create a key. Keep it server-side.
Do not expose your PromptCacheAI API key in browser code, mobile apps, or public client bundles.
2. Create a test namespace
Create one namespace for the workflow you want to test first, such as support-faq. Set it to test mode so PromptCacheAI records what would have been reused while your app still calls its model.
Learn more in How PromptCacheAI Works.
3. Install the SDK
npm install @promptcacheai/sdk4. Wrap your model call
Use withCache around your existing provider call. The SDK stays provider agnostic: you still own model keys, model selection, retries, streaming, and safety checks.
import OpenAI from "openai";
import { PromptCacheAI } from "@promptcacheai/sdk";
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY!,
});
const pcai = new PromptCacheAI({
apiKey: process.env.PROMPTCACHEAI_API_KEY!,
});
export async function answerSupportQuestion(prompt: string) {
const result = await pcai.withCache({
prompt,
namespace: "support-faq",
provider: "openai",
model: "gpt-4o-mini",
callModel: async () => {
const completion = await openai.chat.completions.create({
model: "gpt-4o-mini",
messages: [{ role: "user", content: prompt }],
});
return completion.choices[0]?.message?.content ?? "";
},
});
return result.response;
}5. Review and switch live
In test mode, your application keeps calling the model, but the dashboard shows exact would-hits, semantic would-hits, cached responses, and prompt variants. Approve variants that should reuse an answer and reject variants that should not.
When the cache behavior looks trustworthy, switch the namespace to live. At that point, approved exact and semantic hits can return cached responses before another model call.
Production behavior
Fails open
If the cache check fails, withCache calls your model unless you enable strict mode.
Saves after misses
On a miss, the SDK calls your model and saves the final response when PromptCacheAI recommends saving.
Provider agnostic
PromptCacheAI does not need your model provider key. Your app remains in control of the provider call.
Result fields
{
response: "You have 30 days to return items with a receipt.",
cached: false,
source: "model",
promptHash: "82d2bf...",
cacheMode: "test",
saveRecommended: true,
cacheDecision: "miss_no_candidate",
canonicalPromptHash: undefined,
saved: true
}Need manual control?
Use the REST API Integration guide if you want to call /chat and /cache/save directly.