git clone https://github.com/LvcidPsyche/claude-api-optimization.git
cd claude-api-optimization
npm installexport ANTHROPIC_API_KEY="your-key-here"
export MODEL_PRIMARY="anthropic/claude-haiku-4-5"
export MODEL_FALLBACK="anthropic/claude-sonnet-4-20250514"npm test
node demo.js-
Clone Repository
git clone https://github.com/LvcidPsyche/claude-api-optimization.git cd claude-api-optimization -
Install Dependencies
npm install
-
Configure Environment
Option A: Environment File
cp .env.example .env # Edit .env with your API keyOption B: Environment Variables
export ANTHROPIC_API_KEY="sk-ant-..."
-
Verify Setup
npm test
const ClaudeCostMonitor = require('./cost-monitor');
const monitor = new ClaudeCostMonitor();
monitor.trackUsage('haiku-4-5', 1000, 500);
console.log(monitor.generateReport());const ModelRouter = require('./model-router');
const router = new ModelRouter();
const result = router.selectModel('Classify this email');
console.log(result.model); // "anthropic/claude-haiku-4-5"
console.log(result.estimatedCostSavings);const PromptCache = require('./prompt-cache');
const cache = new PromptCache();
const optimized = cache.optimizeForCaching(messages, systemPrompt);
console.log(optimized);const ResponseCache = require('./response-cache');
const cache = new ResponseCache();
cache.set('Query', response);
const cached = cache.get('Query');
console.log(cache.getStats());const BatchProcessor = require('./batch-processor');
const processor = new BatchProcessor(process.env.ANTHROPIC_API_KEY);
processor.addRequest('Classify this email');
console.log(processor.estimateMetrics());const CostBenchmark = require('./benchmark');
const benchmark = new CostBenchmark();
benchmark.runScenario('My scenario', requests);
console.log(benchmark.getReport());# Email classification
node examples/email-classification.js
# Batch content generation
node examples/batch-content-generation.js# All tests
npm test
# Specific test file
npm test test/cost-monitor.test.js
# With coverage
npm test -- --coverage# Copy config to OpenClaw
cp optimized-config.json ~/.openclaw/openclaw.jsonconst {
ClaudeCostMonitor,
ModelRouter,
PromptCache,
ResponseCache,
BatchProcessor,
CostBenchmark
} = require('claude-api-optimization');
// Use in your app# Run cost monitoring
node cost-monitor.js
# Run model routing
node model-router.js
# Run benchmarks
node benchmark.jsANTHROPIC_API_KEY=your_key_here
MODEL_PRIMARY=anthropic/claude-haiku-4-5
MODEL_FALLBACK=anthropic/claude-sonnet-4-5
CACHE_TTL=3600000
CACHE_MAX_SIZE=1000
BATCH_MAX_SIZE=100000Create config.json:
{
"models": {
"primary": "anthropic/claude-haiku-4-5",
"fallback": "anthropic/claude-sonnet-4-5"
},
"cache": {
"ttl": 3600000,
"maxSize": 1000
},
"batch": {
"maxSize": 100000,
"maxWaitTime": 86400000
}
}Solution:
export ANTHROPIC_API_KEY="your-key-here"Solution:
npm install
npm testSolution:
rm -rf node_modules package-lock.json
npm installSolution:
- Verify cache TTL is set correctly
- Check cache size limits
- Ensure entries haven't expired
Solution:
- Batch processing can take 5-24 hours
- Check batch status via API
- Reduce batch size if needed
High-Volume Classification:
export CACHE_MAX_SIZE=5000 # Larger cache
export CACHE_TTL=86400000 # 24-hour TTLBatch Processing:
export BATCH_MAX_SIZE=100000 # Max batch sizeReal-time Applications:
export CACHE_TTL=300000 # 5-minute TTL
export CACHE_MAX_SIZE=500 # Smaller cacheconst monitor = new ClaudeCostMonitor();
const stats = monitor.generateReport();
console.log(JSON.stringify(stats, null, 2));const cache = new ResponseCache();
const metrics = cache.export();
// Send to monitoring system- Documentation: See README.md
- Issues: GitHub Issues
- Contributing: See CONTRIBUTING.md
- ✅ Complete installation
- 📖 Read README.md
- 🧪 Run tests:
npm test - 📚 Review examples in
examples/ - 🚀 Integrate into your application