-
Notifications
You must be signed in to change notification settings - Fork 0
Token Management
Amayyas edited this page Dec 5, 2025
·
3 revisions
Understanding and managing tokens in Flutter AI SDK.
Tokens are pieces of words that AI models process. Roughly:
- 1 token ≈ 4 characters in English
- 100 tokens ≈ 75 words
final response = await ai.chat('Hello!');
if (response.usage != null) {
print('Prompt tokens: ${response.usage!.promptTokens}');
print('Completion tokens: ${response.usage!.completionTokens}');
print('Total tokens: ${response.usage!.totalTokens}');
}class Usage {
final int promptTokens; // Tokens in your input
final int completionTokens; // Tokens in the response
final int totalTokens; // Sum of both
}| Model | Context Window | Max Output |
|---|---|---|
| gpt-4-turbo | 128,000 | 4,096 |
| gpt-4 | 8,192 | 8,192 |
| gpt-4-32k | 32,768 | 32,768 |
| gpt-3.5-turbo | 16,385 | 4,096 |
| gpt-4o | 128,000 | 4,096 |
| Model | Context Window | Max Output |
|---|---|---|
| claude-3-opus | 200,000 | 4,096 |
| claude-3-sonnet | 200,000 | 4,096 |
| claude-3-haiku | 200,000 | 4,096 |
| claude-3.5-sonnet | 200,000 | 8,192 |
| Model | Context Window | Max Output |
|---|---|---|
| gemini-1.5-pro | 2,000,000 | 8,192 |
| gemini-1.5-flash | 1,000,000 | 8,192 |
| gemini-1.0-pro | 32,768 | 8,192 |
// Access token counter
final tokenCounter = ai.context.estimatedTokens;
print('Current context tokens: $tokenCounter');
// Check available tokens
final available = ai.context.availableTokens;
print('Available for response: $available');int estimateTokens(String text) {
// Rough estimate: 1 token ≈ 4 characters
return (text.length / 4).ceil();
}
// Usage
final estimate = estimateTokens('Hello, how are you today?');
print('Estimated tokens: $estimate'); // ~7 tokensfinal ai = FlutterAI(
provider: AIProvider.openai,
config: AIConfig(
apiKey: 'your-key',
maxTokens: 500, // Limit response length
),
);final contextManager = ContextManager(
maxTokens: 8000, // Maximum for context
reservedTokens: 1000, // Reserved for response
);// Before making requests
if (ai.context.estimatedTokens > 6000) {
print('Warning: Context is getting large');
}
if (ai.context.availableTokens < 500) {
print('Warning: Limited room for response');
ai.clearContext();
}try {
await ai.chat(message);
} on AIContextLengthError catch (e) {
print('Context too long!');
print('Max: ${e.maxTokens}');
print('Requested: ${e.requestedTokens}');
// Clear and retry
ai.clearContext();
await ai.chat(message);
}class TokenTracker {
int totalInputTokens = 0;
int totalOutputTokens = 0;
void track(Usage? usage) {
if (usage != null) {
totalInputTokens += usage.promptTokens;
totalOutputTokens += usage.completionTokens;
}
}
double estimateCost(double inputPrice, double outputPrice) {
return (totalInputTokens / 1000 * inputPrice) +
(totalOutputTokens / 1000 * outputPrice);
}
}
// Usage
final tracker = TokenTracker();
final response = await ai.chat('Hello');
tracker.track(response.usage);
// GPT-4-turbo pricing example
final cost = tracker.estimateCost(0.01, 0.03); // $/1K tokens
print('Estimated cost: \$${cost.toStringAsFixed(4)}');await for (final chunk in ai.streamChat(message)) {
if (chunk.isDone && chunk.usage != null) {
print('Stream used ${chunk.usage!.totalTokens} tokens');
}
}// For concise responses
final config = AIConfig(
apiKey: 'key',
maxTokens: 300,
);
// For detailed responses
final config = AIConfig(
apiKey: 'key',
maxTokens: 2000,
);class AICostMonitor {
int sessionTokens = 0;
final int budget = 10000; // Token budget
Future<AIResponse> chat(FlutterAI ai, String message) async {
if (sessionTokens >= budget) {
throw Exception('Token budget exceeded');
}
final response = await ai.chat(message);
sessionTokens += response.usage?.totalTokens ?? 0;
print('Session tokens: $sessionTokens / $budget');
return response;
}
}// Verbose - uses more tokens
final response = await ai.chat('''
Hello! I hope you are doing well today.
I have a question for you that I would like you to answer.
Can you please tell me what the capital of France is?
Thank you very much in advance for your help!
''');
// Concise - uses fewer tokens
final response = await ai.chat('What is the capital of France?');// After completing a task
ai.clearContext();
// Or periodically
if (ai.conversation.length > 20) {
// Summarize first
final summary = await ai.chat(
'Summarize our conversation briefly',
addToContext: false,
);
ai.clearContext();
ai.context.addUserMessage('Previous context: ${summary.text}');
}- Context Management - Managing conversation history
- Configuration - Setting maxTokens
- Error Handling - Handling context length errors
Getting Started
Core Concepts
Advanced Features
API Reference
Examples
Other