Package Exports
- usefy
- usefy/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (usefy) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Usefy Node.js SDK
Real-time AI cost control for OpenAI, Anthropic, and other providers.
Installation
npm install usefyQuick Start
import { UsefyClient } from 'usefy';
import OpenAI from 'openai';
// Initialize Usefy
const guard = new UsefyClient({
apiKey: 'us_live_your_key_here',
projectId: 'your_project_id',
});
// Wrap your OpenAI client
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
const client = guard.wrapOpenAI(openai);
// Use normally - budgets enforced automatically
const response = await client.chat.completions.create({
model: 'gpt-4',
messages: [{ role: 'user', content: 'Hello!' }],
});
console.log(response.choices[0].message.content);Configuration
const guard = new UsefyClient({
apiKey: 'us_live_...', // Required: Your Usefy API key
projectId: 'my-project', // Required: Your project identifier
baseUrl: 'https://...', // Optional: Custom API URL
timeoutMs: 50, // Optional: Check timeout (default: 50ms)
userId: 'user_123', // Optional: Track per-user budgets
environment: 'production', // Optional: Environment tag
});Manual Usage
// Check before making a request
const check = await guard.check('openai', 'gpt-4');
if (check.decision === 'block') {
console.log('Request blocked:', check.reason);
return;
}
// Make your AI call...
// Track usage after
await guard.track({
provider: 'openai',
model: 'gpt-4',
inputTokens: 100,
outputTokens: 50,
});Fail-Open Behavior
The SDK uses a fail-open approach:
- If Usefy API is slow or unreachable, requests are allowed
- This ensures your application remains functional even if Usefy has issues
- The default timeout is 50ms to minimize latency impact
Links
License
MIT - USEFY LTD