Token Management

Control and monitor token usage

Agents can track and limit token usage for cost control.

Diagram

Token Control

Yes

No

📝 Input

🔢 Count

Within Limit?

✅ Process

⚠️ Limit

Quick Start

  1. Simple Usage
    typescript
    import { Agent } from 'praisonai';
    
    const agent = new Agent({
      instructions: 'You are concise',
      maxTokens: 1000  // Limit output tokens
    });
    
    await agent.chat('Write a summary');
  2. With Configuration
    typescript
    const result = await agent.chat('Hello');
    console.log('Tokens used:', result.usage);
    // { input: 5, output: 20, total: 25 }

User Interaction Flow

Diagram
CounterAgentUserCounterAgentUserRequestCheck budgetOKGenerate responseUpdate usageResponse + usage

Configuration Levels

typescript
// Level 1: Number - Simple limit
const agent = new Agent({
  maxTokens: 1000
});

// Level 2: Dict - Multiple limits
const agent = new Agent({
  tokens: {
    maxInput: 4000,
    maxOutput: 1000
  }
});

// Level 3: Instance - Full control
const agent = new Agent({
  tokens: {
    maxInput: 4000,
    maxOutput: 2000,
    maxTotal: 8000,
    trackUsage: true,
    onLimit: (usage) => console.warn('Limit reached')
  }
});

Token Options

OptionDescription
maxTokensLimit output tokens
maxInputLimit input tokens
maxTotalTotal token budget
trackUsageEnable usage tracking

API Reference

LLM Module

LLM configuration including tokens


Best Practices

Set output limits

Prevent unexpectedly long responses.

Track usage

Monitor costs with trackUsage.

Estimate costs

Use token counts to estimate API costs.


Related