Overview
Argument Cartographer performs multiple API calls and AI operations per analysis. This guide helps you optimize performance and reduce costs based on the actual implementation.Analysis Performance
Current Performance Characteristics
A typical argument analysis involves:Search Query Generation
1 AI call to generate optimized search query
- Model: Gemini 1.5 Pro
- Tokens: ~100 input, ~20 output
- Duration: 1-2 seconds
Web Search
1-2 Firecrawl search calls for diverse sources
- Results: Up to 20 URLs per search
- Duration: 2-3 seconds per search
- Cost: ~$0.01 per search (varies by plan)
Web Scraping
8 parallel Firecrawl scrape calls for article content
- Duration: 5-10 seconds (parallel execution)
- Data: 20,000 chars per source (160KB total)
- Cost: ~$0.08 per analysis
Main AI Analysis
1 large AI call with full context
- Model: Gemini 1.5 Pro
- Input: 8 sources × 12,000 chars = ~96,000 chars (~24,000 tokens)
- Output: Structured JSON blueprint (~2,000 tokens)
- Duration: 10-20 seconds
- Cost: ~$0.01 per analysis
Twitter Search
1 Twitter API call for social pulse (optional)
- Results: 20 tweets
- Duration: 1-2 seconds
- Cost: Free (within rate limits)
- Duration: 20-35 seconds
- API calls: 3 AI + 10 Firecrawl + 1 Twitter
- Estimated cost: $0.10-0.15 per analysis
Optimization Strategies
Reduce Number of Sources
Current: 8 diverse sources scraped per analysis Optimization: Reduce to 5 sources for faster, cheaper analyses:- ⚡ 30% faster scraping
- 💰 40% lower Firecrawl costs
- ⚠️ Slightly less comprehensive analysis
Reduce Content Length Per Source
Current: 12,000 chars per source in context- ⚡ 20% faster AI processing
- 💰 30% lower AI token costs
- ⚠️ May miss details in longer articles
Implement Caching
Cache Search Results
Cache Search Results
Cache Firecrawl search results by query:Savings: Eliminate repeated searches for popular topics.
Cache Scraped Content
Cache Scraped Content
Cache scraped article content by URL:Savings: Significant for frequently analyzed sources.
Cache Complete Analyses
Cache Complete Analyses
Cache full analysis results for identical queries:Savings: Instant response for duplicate queries.
Caching requires a key-value store. Vercel KV (Redis) is recommended for production deployments.
Parallel Processing Optimization
The application already parallelizes scraping:Firestore Optimization
Data Structure Efficiency
The current security model is already optimized:- ✅ User-scoped queries avoid full collection scans
- ✅ No joins or lookups required
- ✅ Security rules use path-based auth (no database reads)
Indexing Strategy
Create Composite Index for Queries
If you query argument maps by date:Index fields:
userId(Ascending)createdAt(Descending)
Read Optimization
Use Real-time Listeners Sparingly
Use Real-time Listeners Sparingly
Expensive:Optimized:Cost: Listeners count as 1 read on attach + 1 read per update.
Batch Reads
Batch Reads
Client-Side Caching
Client-Side Caching
Enable Firestore persistence:Impact: Reduces reads for frequently accessed documents.
API Cost Optimization
Firecrawl Cost Management
Pricing (varies by plan):- Search: ~$0.001 per result
- Scrape: ~$0.01 per page
- 20 search results: $0.02
- 8 scrapes: $0.08
- Total: ~$0.10
Google Gemini Cost Management
Pricing (Gemini 1.5 Pro):- Input: $3.50 per 1M tokens
- Output: $10.50 per 1M tokens
- Input: ~25,000 tokens = $0.09
- Output: ~2,000 tokens = $0.02
- Total: ~$0.11
Reduce Context Size
Reduce Context Size
Trim scraped content more aggressively:Savings: 50% reduction in input tokens.
Use Gemini Flash for Non-Critical Tasks
Use Gemini Flash for Non-Critical Tasks
For search query generation (simpler task):Savings: 0.01 per call.
Optimize Prompt Engineering
Optimize Prompt Engineering
Shorter, more direct prompts:
Twitter API Cost Management
Pricing:- Basic tier: $100/month (500K tweets)
- Free tier: Deprecated for new apps
- Development/testing environments
- Cost-sensitive deployments
- Topics unlikely to have social media discussion
Performance Monitoring
Add Timing Metrics
Monitor API Usage
Frontend Performance
Optimize Argument Visualization
The visualization component may struggle with large argument trees. Optimization:Progressive Loading
Show results as they become available:Production Recommendations
For Low-Traffic Apps (<100 analyses/day)
For Low-Traffic Apps (<100 analyses/day)
Configuration:
- Use default settings (8 sources, full content)
- Enable Twitter for social pulse
- Implement basic caching (1 hour TTL)
For Medium-Traffic Apps (100-1000 analyses/day)
For Medium-Traffic Apps (100-1000 analyses/day)
Optimizations:
- Reduce to 5 sources per analysis
- Implement aggressive caching (24 hour TTL)
- Use Gemini Flash for search query generation
- Optional Twitter (enable for premium users only)
For High-Traffic Apps (>1000 analyses/day)
For High-Traffic Apps (>1000 analyses/day)
Advanced optimizations:
- Reduce to 3 sources per analysis
- Implement multi-tier caching (Redis + CDN)
- Pre-analyze popular topics (cron jobs)
- Disable Twitter or use separate quota
- Implement user quotas and rate limiting
- Consider self-hosted scraping for frequently accessed domains
Next Steps
Common Issues
Troubleshoot performance problems
API Errors
Debug API integration issues
Firebase Setup
Optimize Firebase configuration
Environment Variables
Configure API keys and limits
