Overview
RAPTOR provides comprehensive cost tracking and budget enforcement for LLM API usage across multiple providers. All costs are tracked in real-time and enforced before budget limits are exceeded.Real-Time Tracking
Track costs as requests are made, not after the fact
Budget Enforcement
Hard limits prevent runaway costs from expensive operations
Multi-Provider Support
Unified cost tracking for Anthropic, OpenAI, Gemini, Mistral, and Ollama
Cost Callbacks
LiteLLM integration provides automatic token counting and cost calculation
Cost Configuration
Setting Budget Limits
$10.00 per scan
Per-Model Pricing
RAPTOR automatically configures per-model costs based on current provider pricing:Pricing is configured at initialization time. Update
config.py if provider pricing changes.Budget Enforcement
Pre-Request Checks
Before making any LLM request, RAPTOR checks if the estimated cost would exceed the budget:- Within budget: Request proceeds
- Would exceed budget: Request blocked with clear error message
Hard Budget Limit
When budget is exceeded, RAPTOR raisesRuntimeError with guidance:
Real-Time Cost Tracking
Token-Based Calculation
Costs are calculated based on actual token usage reported by LiteLLM:Per-Provider Tracking
Each provider maintains its own cost counter:Global Cost Aggregation
The client aggregates costs across all providers:LiteLLM Integration
Automatic Token Counting
LiteLLM provides automatic token counting for all providers:- Anthropic (Claude)
- OpenAI (GPT)
- Google (Gemini, PaLM)
- Mistral
- Ollama (reports tokens even though free)
Cost Callbacks
RAPTOR registers a LiteLLM callback for detailed cost visibility:- Atomic logging of every LLM call
- Token usage from LiteLLM’s perspective (not just our calculation)
- Duration tracking for performance analysis
- Automatic error capture
Cost Optimization Strategies
1. Response Caching
Avoid re-computing identical requests:sha256(model + system_prompt + user_prompt)
Cache format:
2. Model Selection
Use cheaper models for simpler tasks:3. Prompt Optimization
Reduce token usage with shorter prompts:4. Quota Detection and Fallback
Automatic fallback when quota exceeded:5. Local Model Usage
Use Ollama for free inference:- Pros
- Cons
- Zero cost: No API fees
- Privacy: Data never leaves your machine
- No rate limits: Run as many requests as hardware allows
- Offline capable: Works without internet
Cost Reporting
Real-Time Statistics
Get current cost statistics during scan:Post-Scan Summary
After scan completion, RAPTOR reports total costs:Budget Exhaustion Warning
When budget is nearly exhausted:Advanced Configuration
Per-Scan Budget Override
Cost Reset
Reset cost tracking between scans:Disable Cost Tracking
Cost Analysis Tools
Cost Breakdown by Task
Cost Prediction
Best Practices
Set Realistic Budgets
Start with 25-50 for production. Monitor first scan to calibrate.
Enable Caching
Cache responses to avoid re-computing identical requests (can save 30-50%).
Use Task-Specific Models
Route simple tasks to cheaper models (Gemini for classification, Sonnet for analysis).
Monitor in Real-Time
Check
client.get_stats() during scan to detect runaway costs early.Don't Disable Cost Tracking in Production
Don't Disable Cost Tracking in Production
Always enforce budgets in production to prevent accidentally expensive scans.
Quota Errors are Not Budget Errors
Quota Errors are Not Budget Errors
Quota exceeded = provider rate limit (need to wait or switch provider)Budget exceeded = RAPTOR cost limit (need to increase max_cost_per_scan)
Local Models Have Zero Cost but Lower Quality
Local Models Have Zero Cost but Lower Quality
Use Ollama for development/testing, cloud models for production security research.
Troubleshooting
error
Cause: Scan hit cost limit before processing all findingsFix:
warning
Possible causes:
- Using expensive model (Opus) for all tasks
- Long prompts with unnecessary context
- Cache disabled or not working
info
Check:
- Is
enable_caching=True? - Are prompts identical (including whitespace)?
- Does
out/llm_cache/directory exist?
Further Reading
LiteLLM Cost Tracking
Official LiteLLM documentation on cost tracking features
Provider Pricing
Up-to-date pricing for all major LLM providers
Client Configuration
Full LLM client configuration reference
Optimization Guide
Comprehensive guide to optimizing LLM costs