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

Quick Start

1

Set Output Token Limit

2

Track Token Usage

3

Set Context Window Limit


How It Works


Configuration Options


Common Patterns

Budget-Aware Loops

Stop processing when cumulative cost gets too high:

Verbose Usage Reporting


Best Practices

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.
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.
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.
Enable .verbose(true) during development to see token counts logged automatically after each request. Disable in production to reduce noise.

Budget

Dollar-based spending limits

Context Management

Context window strategies