> ## Documentation Index
> Fetch the complete documentation index at: https://praison.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Token Management

> Control and monitor token usage

Agents can track and limit token usage for cost control.

```mermaid theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
graph LR
    subgraph "Token Control"
        A[📝 Input] --> B[🔢 Count]
        B --> C{Within Limit?}
        C -->|Yes| D[✅ Process]
        C -->|No| E[⚠️ Limit]
    end
    
    classDef input fill:#6366F1,stroke:#7C90A0,color:#fff
    classDef check fill:#F59E0B,stroke:#7C90A0,color:#fff
    classDef result fill:#10B981,stroke:#7C90A0,color:#fff
    
    class A input
    class B,C check
    class D,E result

    classDef agent fill:#8B0000,stroke:#7C90A0,color:#fff
    classDef tool fill:#189AB4,stroke:#7C90A0,color:#fff

    class Agent agent
    class Token tool
    classDef agent fill:#8B0000,color:#fff
    classDef tool fill:#189AB4,color:#fff
```

## Quick Start

<Steps>
  <Step title="Simple Usage">
    ```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
    import { Agent } from 'praisonai';

    const agent = new Agent({
      instructions: 'You are concise',
      maxTokens: 1000  // Limit output tokens
    });

    await agent.chat('Write a summary');
    ```
  </Step>

  <Step title="With Configuration">
    ```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
    const result = await agent.chat('Hello');
    console.log('Tokens used:', result.usage);
    // { input: 5, output: 20, total: 25 }
    ```
  </Step>
</Steps>

***

## User Interaction Flow

```mermaid theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
sequenceDiagram
    participant User
    participant Agent
    participant Counter
    
    User->>Agent: Request
    Agent->>Counter: Check budget
    Counter-->>Agent: OK
    Agent->>Agent: Generate response
    Agent->>Counter: Update usage
    Agent-->>User: Response + usage
```

***

## Configuration Levels

```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
// 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

| Option       | Description           |
| ------------ | --------------------- |
| `maxTokens`  | Limit output tokens   |
| `maxInput`   | Limit input tokens    |
| `maxTotal`   | Total token budget    |
| `trackUsage` | Enable usage tracking |

***

## API Reference

<Card title="LLM Module" icon="code" href="/docs/sdk/reference/typescript/modules/llm">
  LLM configuration including tokens
</Card>

***

## Best Practices

<AccordionGroup>
  <Accordion title="Set output limits">
    Prevent unexpectedly long responses.
  </Accordion>

  <Accordion title="Track usage">
    Monitor costs with trackUsage.
  </Accordion>

  <Accordion title="Estimate costs">
    Use token counts to estimate API costs.
  </Accordion>
</AccordionGroup>

***

## Related

<CardGroup cols={2}>
  <Card title="Providers" icon="plug" href="/docs/js/providers">
    LLM providers
  </Card>

  <Card title="Execution" icon="play" href="/docs/js/execution">
    Execution settings
  </Card>
</CardGroup>
