Token Management

Track and limit token usage to control costs in Rust agents

Set token limits so your Rust agents stay within budget and never generate unexpectedly long responses.

Diagram

Token Control

Yes

No

📥 Input

🔢 Count Tokens

Within Limit?

🤖 Process

✂️ Truncate / Stop

📊 Update Usage

Quick Start

  1. Set Output Token Limit
    rust
    use praisonai::Agent;
    
    let agent = Agent::new()
        .name("Assistant")
        .instructions("You are concise")
        .max_tokens(500)  // Limit output to 500 tokens
        .build()?;
    
    let response = agent.chat("Explain quantum computing").await?;
    println!("{}", response);
  2. Track Token Usage
    rust
    use praisonai::Agent;
    
    let agent = Agent::new()
        .name("Assistant")
        .instructions("You are helpful")
        .build()?;
    
    let response = agent.chat("Hello").await?;
    
    // Check usage after the call
    let usage = agent.token_usage();
    println!("Input tokens:  {}", usage.input_tokens);
    println!("Output tokens: {}", usage.output_tokens);
    println!("Total tokens:  {}", usage.total_tokens);
  3. Set Context Window Limit
    rust
    use praisonai::Agent;
    
    let agent = Agent::new()
        .name("Assistant")
        .instructions("You summarize long documents")
        .max_tokens(1000)      // Output limit
        .max_context(8192)     // Context window limit
        .build()?;

How It Works

Diagram
LLMAgentUserLLMAgentUsertoken_usage() updatedchat(“question”)Count input tokensRequest (max_tokens=500)Response + usage statsRecord usageResponse

Configuration Options

OptionTypeDefaultDescription
max_tokensu32provider defaultMaximum output tokens per request
max_contextu32provider defaultMaximum context window size

Common Patterns

Budget-Aware Loops

Stop processing when cumulative cost gets too high:

rust
use praisonai::Agent;

let agent = Agent::new()
    .name("Analyst")
    .instructions("Analyze text")
    .max_tokens(200)
    .build()?;

let questions = vec!["Q1", "Q2", "Q3"];
let token_limit = 1000u32;

for question in &questions {
    let usage = agent.token_usage();
    if usage.total_tokens >= token_limit {
        println!("Token budget exhausted after {} tokens", usage.total_tokens);
        break;
    }
    let answer = agent.chat(question).await?;
    println!("{}", answer);
}

Verbose Usage Reporting

rust
use praisonai::Agent;

let agent = Agent::new()
    .name("Reporter")
    .instructions("Generate reports")
    .max_tokens(800)
    .verbose(true)  // Logs token usage after each request
    .build()?;

agent.chat("Write a weekly summary").await?;

Best Practices

Set max_tokens for predictable output

Always set max_tokens when you know the expected output length. For short answers use 200, for summaries 500–1000, for detailed analysis 2000+. This prevents runaway generation and unexpected costs.

Monitor cumulative usage

Call agent.token_usage() after each request to track total spend. In loops or multi-turn conversations, check the running total and stop when approaching your budget.

Respect context window limits

Set max_context slightly below the model’s documented limit to leave headroom. Running at 100% of the context window can cause errors with some providers.

Use verbose mode in development

Enable .verbose(true) during development to see token counts logged automatically after each request. Disable in production to reduce noise.


Related