openclaw-performance-profiler
v1.0.0
Published
Performance profiling and cost optimization tool for OpenClaw workflows
Maintainers
Readme
📊 OpenClaw Performance Profiler
Optimize costs and performance for OpenClaw workflows - Track token usage, identify bottlenecks, and reduce API costs.
🎯 Why This Matters
OpenClaw workflows can get expensive. This tool helps you:
- 💰 Track Costs - Monitor API usage and expenses
- ⚡ Find Bottlenecks - Identify slow operations
- 🎯 Optimize Tokens - Reduce unnecessary token consumption
- 📈 Visualize Metrics - Charts and reports
- 🔔 Set Budgets - Alert when costs exceed thresholds
🚀 Quick Start
Install
npm install -g openclaw-performance-profilerProfile a Workflow
openclaw-profile start
# ... run your workflow ...
openclaw-profile stop
openclaw-profile reportExample Output
📊 OpenClaw Performance Profile
⏱️ Execution Time: 12.3s
💰 Total Cost: $0.45
🎯 Token Usage: 15,234 tokens
📈 Breakdown by Tool:
Tool Calls Time Tokens Cost
─────────────────────────────────────────────
web_fetch 12 2.4s 1,200 $0.02
web_search 3 1.8s 4,500 $0.09
exec 45 0.8s 450 $0.01
message 5 0.2s 9,084 $0.33
─────────────────────────────────────────────
Total 65 5.2s 15,234 $0.45
🔍 Optimization Suggestions:
⚠️ web_fetch is called 12 times - consider batching
💡 message uses 60% of tokens - simplify prompts
✅ exec is well-optimized📖 Commands
openclaw-profile start [name]
Start profiling a session:
openclaw-profile start my-workflow
# Options:
# --watch Live monitoring
# --threshold <n> Alert when cost > thresholdopenclaw-profile stop
Stop current profiling session:
openclaw-profile stopopenclaw-profile report [session]
Generate performance report:
openclaw-profile report
# Options:
# --format <json|html|csv>
# --output <file>
# --compare <session> Compare with another sessionopenclaw-profile analyze [file]
Analyze execution logs:
openclaw-profile analyze openclaw.log
# Detects:
# - Redundant API calls
# - Token waste
# - Slow operations
# - Cost spikesopenclaw-profile budget set <amount>
Set cost budget:
openclaw-profile budget set 10.00 # $10/day
# Options:
# --period <day|week|month>
# --alert <email|slack|webhook>📊 Metrics Tracked
Token Usage
- Input tokens - Prompt/context sent to model
- Output tokens - Model response
- Total tokens - Sum of input + output
- Cost - Based on model pricing
Performance
- Execution time - Total duration
- Tool latency - Individual tool call times
- Queue time - Wait time for rate limits
- Throughput - Operations per second
Resource Usage
- API calls - Number of external requests
- Retries - Failed attempts that retried
- Cache hits/misses - If caching is enabled
- Memory - Peak memory usage
🎓 Examples
Example 1: Compare Two Sessions
openclaw-profile report session-1 --compare session-2Output:
Comparing: session-1 vs session-2
Metric Session 1 Session 2 Δ
─────────────────────────────────────────────────
Execution Time 12.3s 8.7s -29% ✅
Total Cost $0.45 $0.32 -29% ✅
Token Usage 15,234 10,891 -28% ✅
API Calls 65 48 -26% ✅
🎉 session-2 is 29% cheaper and 29% faster!Example 2: Identify Token Waste
openclaw-profile analyze --focus tokensOutput:
🎯 Token Usage Analysis
Top Token Consumers:
1. message → 9,084 tokens (60%)
- Prompt is 3.2KB per call
- 💡 Suggestion: Reduce context window
2. web_search → 4,500 tokens (30%)
- Returns full web pages
- 💡 Suggestion: Use extractMode: 'text'
3. web_fetch → 1,200 tokens (8%)
- ✅ Optimal
Potential Savings: 40% reduction → $0.18 savedExample 3: Real-Time Monitoring
openclaw-profile start --watch📡 Live Performance Monitor
Time Tool Tokens Cost Status
────────────────────────────────────────────────
12:01 web_fetch 120 $0.002 ✅
12:01 message 1,850 $0.037 ✅
12:02 web_search 1,500 $0.030 ✅
12:02 message 1,920 $0.038 ⚠️ High tokens
12:03 exec 50 $0.001 ✅
Current Total: $0.108 | Budget: $10.00 | 1% used🔧 Integration
As Middleware
import { Profiler } from 'openclaw-performance-profiler';
const profiler = new Profiler({
enabled: true,
logLevel: 'info',
});
// Wrap your workflow
profiler.wrap(async () => {
await myWorkflow();
});
const report = profiler.getReport();
console.log(report);Programmatic API
import { Profiler } from 'openclaw-performance-profiler';
const profiler = new Profiler();
profiler.startSession('my-workflow');
// Track individual operations
profiler.track('web_fetch', async () => {
return await tools.web_fetch({ url: 'https://example.com' });
});
profiler.endSession();
// Get insights
const insights = profiler.analyze();
console.log(insights.suggestions);📈 Visualization
Generate Charts
openclaw-profile chart --type line --metric cost --output cost-over-time.pngChart types:
- line - Trends over time
- bar - Comparison between tools/sessions
- pie - Proportion breakdown
- heatmap - Time-based activity
Dashboard (Bonus Feature)
Run local web dashboard:
openclaw-profile dashboard --port 8080Features:
- Real-time metrics
- Historical trends
- Cost projections
- Alerts management
🎯 Optimization Strategies
1. Reduce Token Usage
Before:
await tools.message({
message: `Here is a very long explanation with lots of unnecessary words
that could be simplified and made more concise...` // 50 tokens
});After:
await tools.message({
message: `Concise explanation.` // 5 tokens
});
// Saved: 45 tokens × $0.002/token = $0.092. Batch API Calls
Before:
for (const url of urls) {
await tools.web_fetch({ url }); // 10 separate calls
}After:
await Promise.all(
urls.map(url => tools.web_fetch({ url }))
); // Parallel execution, 60% faster3. Cache Results
Before:
// Fetch same data multiple times
const data1 = await tools.web_fetch({ url });
const data2 = await tools.web_fetch({ url }); // Duplicate!After:
const cache = new Map();
const cached = cache.get(url) || await tools.web_fetch({ url });
cache.set(url, cached);
// Saved: 1 API call4. Use Cheaper Models
Some tasks don't need the most powerful model:
// For simple classification
profiler.suggest('use-cheaper-model', {
current: 'claude-sonnet-4',
recommended: 'claude-haiku-3',
savings: '80%',
});💰 Cost Models (Built-in)
| Model | Input ($/1K tokens) | Output ($/1K tokens) | |-------|---------------------|----------------------| | Claude Sonnet 4 | $0.003 | $0.015 | | Claude Haiku 3 | $0.00025 | $0.00125 | | GPT-4 Turbo | $0.010 | $0.030 | | GPT-3.5 Turbo | $0.0005 | $0.0015 |
Prices are estimates. Configure actual pricing in config.
🤝 Contributing
Contributions welcome! Ideas:
- [ ] More visualization types
- [ ] Integration with billing APIs
- [ ] ML-powered optimization suggestions
- [ ] Slack/Discord notifications
- [ ] Multi-tenant cost tracking
📄 License
MIT © Alex - Built for cost-effective OpenClaw
🔗 Links
Optimize performance. Reduce costs. Build smarter. 📊
