Official JavaScript/TypeScript SDK for Vectorcache - the intelligent caching layer for LLMs that uses semantic similarity to dramatically reduce API costs and improve response times.
- β Full TypeScript support with comprehensive type definitions
- β Easy integration with any Node.js or browser application
- β Semantic caching - Intelligent cache hits based on meaning, not exact matches
- β Cost optimization - Save up to 90% on LLM API costs
- β Performance boost - Cache hits return in ~50ms vs ~2000ms for API calls
- β Error handling - Robust error handling with custom error types
- β Debug support - Built-in logging and cache workflow visualization
- β Promise-based - Modern async/await API
npm install vectorcacheyarn add vectorcachepnpm add vectorcacheimport { VectorcacheClient } from 'vectorcache';
const client = new VectorcacheClient({
apiKey: 'your-api-key-here'
});
const result = await client.query({
query: "What is machine learning?",
context: "AI tutorial",
model: "gpt-4"
});
console.log(result.response);
console.log('Cache hit:', result.is_cache_hit);
console.log('Response time:', result.response_time_ms + 'ms');- Sign up at vectorcache.com
- Create a project in your dashboard
- Generate an API key
- Use the API key to initialize the client
const client = new VectorcacheClient({
apiKey: process.env.VECTORCACHE_API_KEY
});const client = new VectorcacheClient({
apiKey: 'your-api-key', // Required: Your Vectorcache API key
baseUrl: 'https://api.vectorcache.com', // Optional: API base URL
projectId: 'your-project-id', // Optional: Default project ID
timeout: 30000, // Optional: Request timeout (ms)
logLevel: 'warn' // Optional: 'debug' | 'info' | 'warn' | 'error' | 'none'
});Make a cached query to your LLM.
const result = await client.query({
query: "Explain quantum computing",
context: "For a beginner audience", // Optional
model: "gpt-4", // Optional, defaults to 'gpt-4'
max_tokens: 150, // Optional, defaults to 1000
temperature: 0.7, // Optional, defaults to 0.7
metadata: { userId: "123" } // Optional
});Response:
{
response: string; // The cached or generated response
is_cache_hit: boolean; // Whether this was a cache hit
similarity_score?: number; // Similarity score for cache hits
response_time_ms: number; // Response time in milliseconds
tokens_used?: number; // Tokens used (for cache misses)
estimated_cost?: number; // Estimated cost in USD
cache_entry_id?: string; // Cache entry ID for hits
query_id: string; // Unique query ID for tracking
}Test cache workflow with debugging information.
const result = await client.testCache({
project_id: 'your-project-id',
prompt: "What is AI?",
similarity_threshold: 0.8, // Optional, defaults to 0.85
include_debug: true // Optional, defaults to true
});Get cache statistics for a project.
const stats = await client.getCacheStats('your-project-id');
console.log('Total entries:', stats.total_entries);
console.log('Cache hits:', stats.total_hits);
console.log('Cost saved:', stats.total_cost_saved);Find similar queries in your cache.
const similar = await client.findSimilarQueries(
"What is artificial intelligence?",
'your-project-id'
);
console.log('Found', similar.total_found, 'similar queries');Clear cache entries.
const result = await client.clearCache({
project_id: 'your-project-id', // Optional
expired_only: true, // Optional, only clear expired entries
older_than_hours: 24 // Optional, clear entries older than X hours
});Check API health and connectivity.
const health = await client.health();
console.log('Status:', health.status);The SDK provides custom error types for different scenarios:
import {
VectorcacheAPIError,
VectorcacheAuthenticationError,
VectorcacheRateLimitError,
VectorcacheValidationError,
VectorcacheNetworkError,
VectorcacheTimeoutError
} from 'vectorcache';
try {
const result = await client.query({
query: "What is AI?",
model: "gpt-4"
});
} catch (error) {
if (error instanceof VectorcacheAuthenticationError) {
console.error('Invalid API key:', error.message);
} else if (error instanceof VectorcacheRateLimitError) {
console.error('Rate limited. Retry after:', error.retryAfter);
} else if (error instanceof VectorcacheValidationError) {
console.error('Validation error:', error.message);
console.error('Details:', error.details);
} else {
console.error('Unexpected error:', error.message);
}
}import { VectorcacheClient } from 'vectorcache';
const client = new VectorcacheClient({
apiKey: process.env.VECTORCACHE_API_KEY
});
// Simple query
const result = await client.query({
query: "What is the capital of France?",
model: "gpt-4"
});
console.log(result.response); // "The capital of France is Paris."const queries = [
"What is machine learning?",
"Explain neural networks",
"What is deep learning?"
];
const results = await Promise.all(
queries.map(query => client.query({ query, model: "gpt-4" }))
);
results.forEach((result, index) => {
console.log(`Query ${index + 1}:`);
console.log(` Cache hit: ${result.is_cache_hit}`);
console.log(` Response: ${result.response}\n`);
});// Get cache statistics
const stats = await client.getCacheStats();
console.log(`Cache hit rate: ${(stats.total_hits / stats.total_entries * 100).toFixed(1)}%`);
console.log(`Total cost saved: $${stats.total_cost_saved.toFixed(4)}`);
// Find similar queries
const similar = await client.findSimilarQueries("What is AI?");
console.log(`Found ${similar.total_found} similar queries`);
// Clear old cache entries
await client.clearCache({
expired_only: true,
older_than_hours: 168 // 1 week
});You can set these environment variables for convenience:
VECTORCACHE_API_KEY=your-api-key-here
VECTORCACHE_PROJECT_ID=your-default-project-idThis SDK works in both Node.js and modern browsers. For browser usage:
<script type="module">
import { VectorcacheClient } from 'https://cdn.skypack.dev/vectorcache';
const client = new VectorcacheClient({
apiKey: 'your-api-key'
});
</script>The SDK is written in TypeScript and includes comprehensive type definitions:
import {
VectorcacheClient,
SemanticQueryRequest,
SemanticQueryResponse,
CacheStatsResponse
} from 'vectorcache';
const client = new VectorcacheClient({ apiKey: 'key' });
// Full type safety
const request: SemanticQueryRequest = {
query: "What is AI?",
model: "gpt-4",
max_tokens: 100
};
const response: SemanticQueryResponse = await client.query(request);# Clone the repository
git clone https://github.com/vectorcache/vectorcache-js.git
# Install dependencies
npm install
# Build the project
npm run build
# Run tests
npm test
# Run examples
node examples/basic-usage.jsWe welcome contributions! Please see our Contributing Guide for details.
MIT License - see LICENSE file for details.
- π Website
- π Documentation
- π Issues
- π¬ Discord Community
- π§ Email: support@vectorcache.com
- π¬ Discord: Join our community
- π Docs: docs.vectorcache.com