Token Usage & Optimization
Understand how Mesrai optimizes token usage to provide cost-effective AI-powered code reviews.
Overview
Mesrai uses Large Language Models (LLMs) to analyze your code. Token usage directly impacts:
- Review Cost: Tokens consumed per review
- Review Speed: Less context = faster reviews
- Review Quality: More context = better insights
Token Basics
What are Tokens?
Tokens are pieces of text that LLMs process:
- 1 token ≈ 4 characters
- 1 token ≈ 0.75 words
- 100 tokens ≈ 75 words
Example Token Counts
// ~50 tokens
function calculateTotal(items) {
return items.reduce((sum, item) => sum + item.price, 0);
}# ~80 tokens
def process_data(data: List[Dict]) -> pd.DataFrame:
"""Transform raw data into processable DataFrame."""
return pd.DataFrame(data).fillna(0)Token Usage by Review Type
| Review Type | Avg Tokens | Cost (GPT-4) | Speed |
|---|---|---|---|
| Quick | 2,000 | $0.02 | 10s |
| Standard | 8,000 | $0.08 | 30s |
| Deep | 25,000 | $0.25 | 90s |
| Full Context | 100,000+ | $1.00+ | 3min+ |
How Mesrai Optimizes Tokens
1. Smart Context Selection
Mesrai only includes relevant files:
# .mesrai.yml
context:
# Only include files related to changes
smart_selection: true
# Maximum files to include
max_files: 50
# Include dependencies
include_imports: true2. Diff-Based Analysis
Instead of sending entire files, Mesrai focuses on:
- Changed lines (± 3 lines context)
- Function signatures
- Related code blocks
- Import statements
3. Caching
Frequently reviewed code is cached:
- File-level cache: Common utilities, config files
- AST cache: Parsed code structures
- Dependency cache: External libraries
Caching reduces token usage by 40-60% on average.
4. Incremental Reviews
For large PRs, Mesrai reviews incrementally:
- First commit: Full context review
- Subsequent commits: Only new changes
- Force full review:
@mesrai review --full
Token Allocation
Token Budget Per Review
Mesrai allocates tokens strategically:
Total: 10,000 tokens
├─ PR Changes: 4,000 tokens (40%)
├─ Context Files: 3,000 tokens (30%)
├─ Dependencies: 2,000 tokens (20%)
└─ Metadata: 1,000 tokens (10%)Priority Ranking
Files are prioritized by relevance:
-
High Priority (always included):
- Modified files
- Direct imports
- Test files for changed code
-
Medium Priority (included if budget allows):
- Related utilities
- Config files
- Type definitions
-
Low Priority (excluded unless needed):
- Vendor code
- Generated files
- Documentation
Configuration
Context Depth
Control how much context is included:
# .mesrai.yml
reviews:
context_depth: medium # quick | medium | deep
quick:
max_tokens: 2000
max_files: 10
medium:
max_tokens: 8000
max_files: 30
deep:
max_tokens: 25000
max_files: 100File Exclusions
Exclude files to save tokens:
exclude:
# Exclude by pattern
patterns:
- "**/*.test.js"
- "dist/**"
- "node_modules/**"
# Exclude by size
max_file_size: 100000 # bytes
# Exclude generated files
generated: trueSmart Limits
Set automatic limits:
limits:
# Max tokens per review
max_tokens: 10000
# Max tokens per file
max_tokens_per_file: 2000
# Warn when approaching limit
warn_at: 8000Monitoring Usage
Dashboard Metrics
View token usage in your Mesrai Dashboard:
- Tokens Used: Total consumed this month
- Cost: Estimated monthly cost
- Avg per Review: Typical token consumption
- Efficiency Score: How optimized your reviews are
Per-Review Breakdown
Each review shows token usage:
Mesrai Review Summary
📊 Tokens Used: 8,234 / 10,000
├─ Changed files: 3,200
├─ Context files: 2,800
├─ Dependencies: 1,500
└─ Metadata: 734
💰 Estimated Cost: $0.08
⚡ Review Time: 28sUsage Alerts
Get notified when approaching limits:
alerts:
# Email when 80% of quota used
quota_warning: 0.8
# Slack notification for high usage
high_usage_threshold: 50000 # tokensCost Optimization Strategies
1. Reduce Context Depth
Use quick reviews for minor changes:
@mesrai review --quick2. Exclude Non-Critical Files
Focus on important code:
exclude:
- "docs/**"
- "examples/**"
- "*.md"3. Use Targeted Reviews
Review specific aspects only:
@mesrai review security
@mesrai review performance4. Batch Smaller PRs
Combine small changes to reduce overhead:
- Multiple small PRs: 5 × 2,000 tokens = 10,000 tokens
- One combined PR: 1 × 6,000 tokens = 6,000 tokens
5. Enable Caching
Let Mesrai cache common patterns:
caching:
enabled: true
ttl: 86400 # 24 hoursPricing Models
Token-Based Pricing
Pay per token consumed:
| Tier | Tokens/Month | Price/Month | Cost per Token |
|---|---|---|---|
| Starter | 500K | $49 | $0.000098 |
| Pro | 2M | $149 | $0.0000745 |
| Enterprise | Unlimited | Custom | $0.00005 |
Review-Based Pricing
Fixed cost per review:
| Plan | Reviews/Month | Price/Month | Cost per Review |
|---|---|---|---|
| Basic | 100 | $29 | $0.29 |
| Team | 500 | $99 | $0.20 |
| Business | 2000 | $299 | $0.15 |
Advanced Optimization
Custom Context Rules
Define which files to include:
context_rules:
- name: "Always include types"
pattern: "**/*.d.ts"
priority: high
- name: "Skip test fixtures"
pattern: "**/fixtures/**"
exclude: true
- name: "Limit large files"
pattern: "**/*.json"
max_size: 50000Dependency Resolution
Control how dependencies are included:
dependencies:
# Include only direct dependencies
depth: 1
# Exclude external libraries
exclude_external: true
# Include type definitions
include_types: trueMulti-Stage Reviews
Split large reviews:
multi_stage:
enabled: true
stages:
- name: "Security Scan"
max_tokens: 5000
focus: security
- name: "Code Quality"
max_tokens: 5000
focus: qualityAPI Integration
Monitor usage programmatically:
curl -H "Authorization: Bearer $TOKEN" \
https://api.mesrai.com/v1/usage
{
"tokens_used": 125000,
"tokens_limit": 500000,
"reviews_count": 42,
"avg_tokens_per_review": 2976,
"cost_usd": 12.25
}Best Practices
- Start with Quick Reviews: Test with minimal context
- Monitor Dashboard: Track usage patterns
- Optimize Gradually: Adjust based on needs
- Use Exclusions: Remove unnecessary files
- Enable Caching: Reuse common context
- Set Budgets: Prevent unexpected costs
Troubleshooting
High Token Usage
Problem: Reviews consuming too many tokens
Solutions:
- Reduce
context_depth - Add more file exclusions
- Use
--quickflag - Split large PRs
Low Quality Reviews
Problem: Reviews missing important issues
Solutions:
- Increase
max_files - Use
--deepflag - Include more context
- Review
excludepatterns
Quota Exceeded
Problem: Monthly token limit reached
Solutions:
- Upgrade plan
- Optimize context selection
- Pause auto-reviews temporarily
- Contact sales for custom limits
FAQs
Q: How many tokens does an average PR use? A: 5,000-10,000 tokens for typical PRs (50-200 lines changed)
Q: Are tokens refunded for failed reviews? A: Yes, failed reviews don’t count toward quota
Q: Can I set hard limits?
A: Yes, configure max_tokens in .mesrai.yml
Q: What happens when I exceed quota? A: Reviews pause until next billing cycle or upgrade
Q: Do cached reviews use tokens? A: No, cached context doesn’t count toward quota