-
Notifications
You must be signed in to change notification settings - Fork 2.5k
src backend features metered_services
Puter supports metered services through the CostService infrastructure. This allows services to check available funds, record costs, and track usage in a standardized way.
The CostService (src/backend/src/services/drivers/CostService.js
) provides core functionality for managing costs and funds:
// Check if user has sufficient funds
const usageAllowed = await svc_cost.get_funding_allowed({
minimum: cost_in_microcents,
});
// Record a cost
await svc_cost.record_cost({
cost: cost_in_microcents,
});
// Record funding updates
await svc_cost.record_funding_update({
old_amount: previous_amount,
new_amount: updated_amount,
});
Costs are tracked in microcents (1/1,000,000th of a USD cent) to allow for precise metering of very small costs. For example:
- 1 USD = 100 cents = 100,000,000 microcents
- 0.1 cents = 100,000 microcents
- 0.001 cents = 1,000 microcents
AI services are a prime example of metered services in Puter. Each AI service defines its own cost structure based on usage:
{
currency: 'usd-cents',
tokens: 1_000_000, // per million tokens
input: 200, // cost for input tokens
output: 600 // cost for output tokens
}
const microcents_per_character = 400;
const exact_cost = microcents_per_character * text.length;
const min_cost =
(150 * // cents per 1000 pages
Math.pow(10, 6)) / // microcents per cent
1000; // pages // 150,000 microcents per page
Services typically follow this pattern for metered operations:
- Calculate the exact cost or minimum cost for the operation
- Check if the user has sufficient funds using
get_funding_allowed()
- If funds are available:
- For fixed-cost operations: Record the cost immediately
- For variable-cost operations: Record the cost after completion
- If funds are insufficient, throw an
insufficient_funds
error
To add metering to a new service:
- Get the CostService instance:
const svc_cost = this.services.get("cost");
- Define your cost structure:
- Use microcents as the base unit
- Consider both fixed and variable costs
- Document the cost calculation logic
- Implement the usage check:
const usageAllowed = await svc_cost.get_funding_allowed({
minimum: calculated_cost,
});
if (!usageAllowed) {
throw APIError.create("insufficient_funds");
}
- Record costs appropriately:
await svc_cost.record_cost({
cost: final_cost,
});
- For AI-specific metering, see the PuterAI documentation
- For implementation details of CostService, see the service documentation
This wiki is generated from the repository. Do not edit files the wiki.
You are reading documentation for Puter, an open-source high-level operating system.
Getting started with Puter on localhost is as simple as:
git clone https://github.com/HeyPuter/puter.git
npm install
npm run start
- Index (README.md)
- api drivers
- Group Endpoints
- Notification Endpoints
- Share Endpoints
- Type-Tagged Objects
- Comment Prefixes
- contributors vscode
- Local Email Testing
- Puter Extensions
- Repository Structure and Tooling
- Configuring Domains for Self-Hosted Puter
- Configuring Puter
- First Run Issues
- self_hosters config_values
- self_hosters support
- Self-Hosting Puter
- Backend Style
- Puter Backend - Directory Structure
- Puter Backend Boot Sequence
- Puter Kernel Moduels and Services
- Batch and Symlinks
- Metered Services and Cost Management
- Protected Apps and Subdomains
- Service Scripts
- Index (README.md)
- Configuring AI Services
- PuterAI API Request Examples
- src backend src modules puterai config
####### For Contributors