Skip to main content
Agents can enforce spending limits to prevent runaway costs — set a max budget and the agent stops automatically.

Quick Start

1

Simple Usage

import { Agent } from 'praisonai';

const agent = new Agent({
  instructions: 'You are a helpful assistant',
  maxCostUsd: 0.50  // Stop after $0.50
});

await agent.chat('Summarize this document');
2

With Configuration

import { Agent } from 'praisonai';

const agent = new Agent({
  instructions: 'You are a helpful assistant',
  maxCostUsd: 1.00
});

const response = await agent.chat('Write a report');
const usage = agent.getUsage();

console.log(`Cost: $${usage.estimatedCostUsd.toFixed(4)}`);
console.log(`Tokens: ${usage.totalTokens}`);
3

With Token Limits

import { Agent } from 'praisonai';

const agent = new Agent({
  instructions: 'Be concise',
  maxTokens: 500,    // Limit output tokens
  maxCostUsd: 0.10   // Also limit cost
});

How It Works


Configuration

OptionTypeDescription
maxCostUsdnumberMaximum cost in USD
maxTokensnumberMaximum output tokens

Common Patterns

Per-Session Budget

import { Agent } from 'praisonai';

// Each session gets $0.25 max
const agent = new Agent({
  instructions: 'You are a research assistant',
  maxCostUsd: 0.25
});

// Agent stops if budget is exceeded mid-task
await agent.chat('Research quantum computing and write a detailed summary');

Monitor and Alert

import { Agent } from 'praisonai';

const agent = new Agent({
  instructions: 'You are a helpful assistant',
  maxCostUsd: 1.00
});

await agent.chat('Write a comprehensive analysis');

const usage = agent.getUsage();
if (usage.estimatedCostUsd > 0.80) {
  console.warn('Budget nearly exhausted:', usage);
}

Batch with Shared Budget

import { Agent } from 'praisonai';

const agent = new Agent({
  instructions: 'Summarize the given text concisely',
  maxTokens: 200,   // Keep each response short
  maxCostUsd: 2.00
});

const documents = ['doc1...', 'doc2...', 'doc3...'];
for (const doc of documents) {
  const summary = await agent.chat(`Summarize: ${doc}`);
  console.log(summary);
}

Best Practices

Always configure maxCostUsd before deploying agents to production. Without limits, a runaway loop or unexpectedly large input can result in significant API costs.
maxTokens limits output length, making costs more predictable. Combine with maxCostUsd for both per-response and cumulative control.
Call agent.getUsage() after completion to track cumulative spending. Log this data to build cost forecasts over time.
Simple Q&A tasks need less than 0.01.Detailedresearchtasksmayneed0.01. Detailed research tasks may need 0.10–$0.50. Set limits to match the expected scope, not just a large safety number.

Token Management

Control token usage per request

Execution

Execution settings and timeouts