Budget
Control agent costs with spending limits and usage tracking
Agents can enforce spending limits to prevent runaway costs โ set a max budget and the agent stops automatically.
graph LR
subgraph "Budget Control"
A[๐ค Agent] --> U[๐ฐ Usage]
U --> B{Limit?}
B -->|OK| C[โ
Continue]
B -->|Exceeded| S[๐ Stop]
end
classDef agent fill:#8B0000,stroke:#7C90A0,color:#fff
classDef tool fill:#189AB4,stroke:#7C90A0,color:#fff
classDef usage fill:#F59E0B,stroke:#7C90A0,color:#fff
classDef ok fill:#10B981,stroke:#7C90A0,color:#fff
classDef stop fill:#EF4444,stroke:#7C90A0,color:#fff
class A agent
class U,B usage
class C ok
class S stop
classDef agent fill:#8B0000,color:#fff
classDef tool fill:#189AB4,color:#fffQuick Start
- Simple Usage
typescript 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'); - With Configuration
typescript 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}`); - With Token Limits
typescript 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
sequenceDiagram
participant User
participant Agent
participant Budget as Budget Monitor
User->>Agent: chat("task")
Agent->>Budget: Check remaining budget
Budget-->>Agent: OK ($0.30 remaining)
Agent->>Agent: Generate response
Agent->>Budget: Record usage
Budget-->>Agent: Updated ($0.18 remaining)
Agent-->>User: ResponseConfiguration
| Option | Type | Description |
|---|---|---|
maxCostUsd | number | Maximum cost in USD |
maxTokens | number | Maximum 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
Set limits before production
Always configure maxCostUsd before deploying agents to production. Without limits, a runaway loop or unexpectedly large input can result in significant API costs.
Use token limits for predictability
maxTokens limits output length, making costs more predictable. Combine with maxCostUsd for both per-response and cumulative control.
Monitor usage after each run
Call agent.getUsage() after completion to track cumulative spending. Log this data to build cost forecasts over time.
Match budget to task scope
Simple Q&A tasks need less than $0.01. Detailed research tasks may need $0.10โ$0.50. Set limits to match the expected scope, not just a large safety number.