Create an account at agentcache.ai/signup, finish onboarding, then open your workspace dashboard to generate an API key and copy your default namespace.
npm install agentcache-client
import { AgentCache } from 'agentcache-client';
const cache = new AgentCache({
apiKey: 'ac_live_your_key',
baseUrl: 'https://agentcache.ai',
namespace: 'default'
});
curl -X POST https://agentcache.ai/api/cache/set \
-H "Content-Type: application/json" \
-H "X-API-Key: YOUR_API_KEY" \
-H "X-Cache-Namespace: default" \
-d '{
"provider": "openai",
"model": "gpt-4o-mini",
"messages": [{"role":"user","content":"What is 2+2?"}],
"response": "4"
}'
const cached = await cache.get({
provider: 'openai',
model: 'gpt-4o-mini',
messages: [{ role: 'user', content: 'What is 2+2?' }]
});
if (cached.hit) {
console.log('Cache hit!', cached.value);
} else {
const response = await openai.chat.completions.create({
model: 'gpt-4o-mini',
messages: [{ role: 'user', content: 'What is 2+2?' }]
});
await cache.set(
{
provider: 'openai',
model: 'gpt-4o-mini',
messages: [{ role: 'user', content: 'What is 2+2?' }]
},
response.choices[0].message.content
);
}
const toolInput = JSON.stringify({ city: 'San Francisco' });
const weatherCache = await cache.get({
provider: 'tool',
model: 'get_weather',
messages: [{ role: 'user', content: toolInput }]
});
if (!weatherCache.hit) {
const weather = await fetchWeather('San Francisco');
await cache.set(
{
provider: 'tool',
model: 'get_weather',
messages: [{ role: 'user', content: toolInput }]
},
weather,
{ ttl: 1800 }
);
}
const stats = await cache.stats();
console.log(`Cache hit rate: ${stats.metrics.hit_rate}%`);
console.log(`Money saved: ${stats.metrics.cost_saved}`);
console.log(`Requests cached: ${stats.metrics.total_requests}`);
You're now caching AI responses and saving money. Check out the full documentation for advanced features.