xiaohongshu
xiaohongshu
agent-cost-optimizer
Real-time cost tracking, budget enforcement, and ROI measurement for AI agent operations. Track token usage, predict costs, enforce budget caps ($50-70/month typical), optimize model selection, cache results, measure cost-to-value. Use when tracking AI costs, preventing budget overruns, optimizin...
Full skill instructions
agent-cost-optimizer provides comprehensive cost tracking, budget enforcement, and ROI measurement for AI agent operations.
Purpose: Control and optimize AI spending while maximizing value delivered
Pattern: Task-based (7 operations for cost management)
Key Innovation: Real-time cost tracking with automatic budget enforcement and cost-effective fallbacks
Industry Context (2025):
Solution: Comprehensive cost management from tracking to optimization
Use agent-cost-optimizer when:
Purpose: Monitor token consumption per skill invocation
Process:
Initialize Tracking:
{
"tracking_id": "track_20250126_1200",
"skill": "multi-ai-verification",
"started_at": "2025-01-26T12:00:00Z",
"tokens": {
"prompt": 0,
"completion": 0,
"total": 0
},
"cost": {
"amount_usd": 0.00,
"model": "claude-sonnet-4-5"
}
}
Track During Execution:
// After each AI call
trackTokens({
prompt_tokens: response.usage.input_tokens,
completion_tokens: response.usage.output_tokens,
model: 'claude-sonnet-4-5'
});
// Update running totals
Finalize Tracking:
{
"tracking_id": "track_20250126_1200",
"skill": "multi-ai-verification",
"completed_at": "2025-01-26T12:45:00Z",
"duration_minutes": 45,
"tokens": {
"prompt": 15234,
"completion": 8932,
"total": 24166
},
"cost": {
"amount_usd": 0.073,
"model": "claude-sonnet-4-5",
"rate": "$3 per million tokens"
}
}
Save to Cost Log:
# Append to daily cost log
cat tracking.json >> .cost-tracking/$(date +%Y-%m-%d).json
Outputs:
Validation:
Time Estimate: Automatic (integrated into skills)
Purpose: Compute accurate costs based on provider pricing
Pricing (2025 rates):
Anthropic (Claude):
| Model | Input (per MTok) | Output (per MTok) |
|---|---|---|
| Claude Opus 4.5 | $15 | $75 |
| Claude Sonnet 4.5 | $3 | $15 |
| Claude Haiku 4.5 | $0.80 | $4 |
OpenAI (Codex):
| Model | Input | Output |
|---|---|---|
| GPT-5.1-codex | $5 | $15 |
| o3 | $10 | $40 |
| o4-mini | $1.50 | $6 |
Google (Gemini):
| Model | Input | Output |
|---|---|---|
| Gemini 2.5 Pro | $1.25 | $5 |
| Gemini 2.5 Flash | $0.15 | $0.60 |
Process:
function calculateCost(usage, model) {
const pricing = {
'claude-sonnet-4-5': { input: 3, output: 15 },
'claude-haiku-4-5': { input: 0.80, output: 4 },
'claude-opus-4-5': { input: 15, output: 75 },
// ... more models
};
const rates = pricing[model];
const inputCost = (usage.prompt_tokens / 1_000_000) * rates.input;
const outputCost = (usage.completion_tokens / 1_000_000) * rates.output;
return {
input_cost: inputCost,
output_cost: outputCost,
total_cost: inputCost + outputCost,
currency: 'USD'
};
}
Outputs:
Purpose: Prevent exceeding monthly budget limits
Process:
Set Budget:
{
"monthly_budget_usd": 100,
"skill_budgets": {
"multi-ai-verification": 70,
"multi-ai-research": 20,
"multi-ai-testing": 10
},
"alert_thresholds": {
"warning": 0.80,
"critical": 0.95
}
}
Check Before Operation:
async function checkBudget(skill, estimated_cost) {
const usage = getCurrentMonthUsage();
const remaining = budget.monthly_budget_usd - usage.total_cost;
if (estimated_cost > remaining) {
return {
allowed: false,
reason: `Budget exceeded: $${usage.total_cost}/$${budget.monthly_budget_usd}`,
overage: estimated_cost - remaining
};
}
if (usage.total_cost / budget.monthly_budget_usd > 0.80) {
return {
allowed: true,
warning: `80% of monthly budget used ($${usage.total_cost}/$${budget.monthly_budget_usd})`
};
}
return { allowed: true };
}
Enforce:
const budgetCheck = await checkBudget('multi-ai-verification', 0.50);
if (!budgetCheck.allowed) {
console.log(`❌ Budget exceeded: ${budgetCheck.reason}`);
console.log(`Options:`);
console.log(` 1. Use cheaper model (Sonnet → Haiku)`);
console.log(` 2. Skip optional verification layers`);
console.log(` 3. Request budget increase`);
return;
}
if (budgetCheck.warning) {
console.log(`⚠️ ${budgetCheck.warning}`);
}
// Proceed with operation
Outputs:
Purpose: Choose cost-effective model for each task
Decision Matrix:
| Task Type | Recommended Model | Cost | Rationale |
|---|---|---|---|
| Simple verification (Layer 1-2) | Haiku | $ | Rules-based, fast, cheap |
| Code generation | Sonnet | $$ | Balanced quality/cost |
| Complex reasoning (architecture) | Opus | $$$ | Best quality, worth premium |
| LLM-as-judge | Sonnet or external model | $$ | Good judgment, reasonable cost |
| Test generation | Sonnet | $$ | Comprehensive coverage needed |
| Research | Sonnet/Haiku mix | $-$$ | Haiku for search, Sonnet for synthesis |
Auto-Optimization:
function selectModel(task_type, criticality, budget_remaining) {
// Critical + budget OK → Use Opus
if (criticality === 'critical' && budget_remaining > 20) {
return 'claude-opus-4-5';
}
// Standard → Use Sonnet
if (criticality === 'standard') {
return 'claude-sonnet-4-5';
}
// Budget low or simple task → Use Haiku
if (budget_remaining < 5 || task_type === 'simple') {
return 'claude-haiku-4-5';
}
return 'claude-sonnet-4-5'; // Default
}
Outputs:
Purpose: Avoid re-computing identical operations
Process:
Cache Key Generation:
function generateCacheKey(operation, inputs) {
// Hash inputs to create unique key
const content_hash = crypto
.createHash('sha256')
.update(JSON.stringify(inputs))
.digest('hex');
return `${operation}_${content_hash}`;
}
Check Cache Before Operation:
const cacheKey = generateCacheKey('verify_code', {
files: ['src/auth.ts'],
file_hashes: {'src/auth.ts': 'abc123'}
});
const cached = readCache(cacheKey);
if (cached && !isExpired(cached, 24)) {
// Use cached result
console.log('📦 Using cached verification result');
return cached.result;
}
// Cache miss → run verification
const result = await runVerification();
// Save to cache
saveCache(cacheKey, result, ttl: 24 hours);
Cache Structure:
{
"cache_key": "verify_code_abc123def456",
"created_at": "2025-01-26T12:00:00Z",
"expires_at": "2025-01-27T12:00:00Z",
"inputs": {
"files": ["src/auth.ts"],
"file_hashes": {"src/auth.ts": "abc123"}
},
"result": {
"quality_score": 92,
"layers_passed": 5,
"issues": []
},
"cost_saved": 0.073
}
Outputs:
Validation:
Purpose: Calculate return on investment for AI spending
Process:
Track Time Saved:
{
"task": "Implement user authentication",
"without_ai": {
"estimated_hours": 40,
"developer_rate": 100,
"total_cost": 4000
},
"with_ai": {
"actual_hours": 11.3,
"developer_rate": 100,
"developer_cost": 1130,
"ai_cost": 2.50,
"total_cost": 1132.50
},
"roi": {
"time_saved_hours": 28.7,
"cost_saved": 2867.50,
"roi_percentage": 253,
"payback_period_hours": 0.025
}
}
Calculate ROI:
function calculateROI(task) {
const time_saved = task.without_ai.estimated_hours - task.with_ai.actual_hours;
const cost_saved = task.without_ai.total_cost - task.with_ai.total_cost;
const roi_percentage = (cost_saved / task.with_ai.ai_cost) * 100;
return {
time_saved_hours: time_saved,
cost_saved_usd: cost_saved,
roi_percentage: roi_percentage,
payback_period: task.with_ai.ai_cost / (task.without_ai.developer_rate * (time_saved / 40)) // weeks
};
}
Monthly ROI Report:
# Monthly ROI Report - January 2025
## AI Spending
- Total AI costs: $87.50
- Breakdown:
- multi-ai-verification: $62.30 (71%)
- multi-ai-research: $18.40 (21%)
- multi-ai-testing: $6.80 (8%)
## Time Savings
- Tasks completed: 8
- Total hours saved: 156 hours
- Average savings per task: 19.5 hours
## Cost Savings
- Developer cost avoided: $15,600 (156h × $100/h)
- AI costs: $87.50
- Net savings: $15,512.50
## ROI
- ROI: 17,728% ($177 saved per $1 spent)
- Payback period: 0.03 weeks (immediate)
- Value multiplier: 178x
## Recommendations
- ✅ Current spending highly cost-effective
- Continue using AI for all qualifying tasks
- Consider increasing budget (high ROI)
Outputs:
Purpose: Estimate costs before starting expensive operations
Process:
Historical Data:
{
"operation": "multi-ai-verification",
"mode": "all_5_layers",
"historical_costs": [
{"date": "2025-01-15", "tokens": 24166, "cost": 0.073},
{"date": "2025-01-18", "tokens": 21893, "cost": 0.066},
{"date": "2025-01-20", "tokens": 26543, "cost": 0.080}
],
"avg_cost": 0.073,
"std_dev": 0.007
}
Predict Before Operation:
const prediction = predictCost('multi-ai-verification', {
mode: 'all_5_layers',
code_size_lines: 850
});
console.log(`💰 Estimated cost: $${prediction.estimated_cost} ± $${prediction.std_dev}`);
console.log(` Range: $${prediction.min_cost} - $${prediction.max_cost}`);
console.log(` Confidence: ${prediction.confidence}%`);
// Check budget
if (prediction.estimated_cost > budget_remaining) {
console.log(`⚠️ Estimated cost exceeds remaining budget`);
console.log(`Options:`);
console.log(` 1. Use Haiku (estimated: $${prediction.estimated_cost * 0.27})`);
console.log(` 2. Skip Layer 5 (save ~60%: $${prediction.estimated_cost * 0.4})`);
console.log(` 3. Increase budget`);
}
Outputs:
Baseline (All Sonnet): $100/month
Optimized (Smart selection):
Aggressive (Maximum savings):
Trade-off: Some quality reduction at Layers 3-4
Without Caching: Re-verify same code multiple times
With Caching (24-hour TTL):
Implementation:
const cacheKey = hash(files_to_verify);
const cached = getCache(cacheKey);
if (cached && !isExpired(cached, 24)) {
return cached.result; // $0 cost
}
const result = await verify(); // $0.073 cost
saveCache(cacheKey, result);
Baseline (Always 5-agent ensemble):
Optimized (Conditional ensemble):
Decision Logic:
function shouldUseEnsemble(criticality, code_size, budget) {
if (criticality === 'critical') return 5;
if (criticality === 'high' && budget > 20) return 3;
return 1; // Single agent
}
Full Verification (All 5 layers): ~$0.073
Fast-Track (Layers 1-2 only):
Decision:
if (lines_changed < 50 && files_changed.every(f => !isCritical(f))) {
// Fast-track: Layers 1-2 only
mode = 'fast_track';
estimated_cost = 0.015;
} else {
// Full verification
mode = 'standard';
estimated_cost = 0.073;
}
Sample Budget ($100/month):
{
"monthly_budget_usd": 100,
"allocation": {
"multi-ai-verification": {
"budget": 70,
"rationale": "Most expensive (LLM-as-judge)"
},
"multi-ai-research": {
"budget": 20,
"rationale": "Occasional use, tri-AI"
},
"multi-ai-testing": {
"budget": 10,
"rationale": "Mostly automated"
},
"buffer": 10
},
"assumptions": {
"features_per_month": 8,
"verifications_per_feature": 1.5,
"research_per_month": 2
}
}
Daily:
# Check today's spending
cat .cost-tracking/$(date +%Y-%m-%d).json | jq '[.[] | .cost.amount_usd] | add'
# Output: $3.45 today
Monthly:
# Check month-to-date
cat .cost-tracking/2025-01-*.json | jq '[.[] | .cost.amount_usd] | add'
# Output: $67.80 this month (68% of budget)
Projection:
// Project end-of-month
const days_elapsed = 26;
const days_in_month = 31;
const current_spend = 67.80;
const projected = (current_spend / days_elapsed) * days_in_month;
// = $81.14 projected (within budget ✅)
80% Budget (Warning):
⚠️ BUDGET ALERT: 80% Used
**Current**: $80.00 / $100.00 (80%)
**Remaining**: $20.00
**Days left**: 5
**Projected EOMs**: $93.75 (within budget)
**Recommendations**:
- Monitor spending closely
- Use Haiku for simple tasks
- Cache aggressively
- Skip optional layers where safe
95% Budget (Critical):
🚨 CRITICAL: 95% Budget Used
**Current**: $95.00 / $100.00 (95%)
**Remaining**: $5.00
**Days left**: 5
**Projected EOM**: $110 (OVER BUDGET)
**Actions Required**:
1. Pause non-critical verifications
2. Use Haiku exclusively
3. Request budget increase OR
4. Defer work to next month
**Auto-throttling**: Enabled
- Only critical operations allowed
- All optional layers disabled
- Ensemble verification disabled
Budget Exceeded:
❌ BUDGET EXCEEDED
**Current**: $102.50 / $100.00 (102.5%)
**Overage**: $2.50
**Operations BLOCKED** until:
1. Budget increased OR
2. Next month (resets automatically)
**Emergency Override**: Requires approval
Quantifiable Value:
Formula:
ROI = ((Value Delivered - AI Costs) / AI Costs) × 100%
Example:
Feature without AI: 40 hours × $100/hour = $4,000
Feature with AI: 11.3 hours × $100/hour + $2.50 AI = $1,132.50
Value Delivered = $4,000 - $1,130 = $2,870
AI Costs = $2.50
ROI = ($2,870 / $2.50) × 100% = 114,800%
Template:
# AI ROI Report - January 2025
## Summary
- **AI Spending**: $87.50
- **Value Delivered**: $15,600 (156 hours saved × $100/hour)
- **ROI**: 17,728%
- **Payback**: Immediate
## Details
### Features Delivered (8)
1. User authentication - 11.3h (was 40h), ROI: 114,800%
2. Payment integration - 8.5h (was 32h), ROI: 108,235%
[... more ...]
### Cost Breakdown
- Verification: $62.30 (71%) - Highest cost, highest value
- Research: $18.40 (21%) - Occasional, high impact
- Testing: $6.80 (8%) - Mostly automated, low cost
### Savings
- Time: 156 hours saved
- Cost: $15,512.50 net savings
- Quality: 4 bugs prevented (saved ~20 hours)
### Recommendations
- ✅ ROI is excellent (17,728%)
- Consider increasing budget (high returns)
- Current spending optimal
| Operation | Purpose | Time | Automation |
|---|---|---|---|
| Track | Monitor token usage | Automatic | 100% |
| Calculate | Compute costs | Automatic | 100% |
| Enforce | Budget caps | Automatic | 100% |
| Optimize | Model selection | Semi-auto | 70% |
| Cache | Avoid re-compute | Automatic | 100% |
| Measure ROI | Value analysis | Manual | 30% |
| Predict | Cost estimation | Automatic | 90% |
| Strategy | Savings | Trade-off | Recommended For |
|---|---|---|---|
| Model selection | 10-35% | Some quality loss | All features |
| Caching | 90% | Stale results risk | Unchanged code |
| Ensemble optimization | 60% | Lower confidence | Non-critical |
| Layer skipping | 80% | Less thorough | Minor changes |
agent-cost-optimizer ensures cost-effective AI operations through real-time tracking, budget enforcement, model optimization, and ROI measurement - preventing budget overruns while maximizing value delivered.
For cost reports, see examples/. For optimization strategies, see Cost Optimization Strategies section.
xiaohongshu
technical spec
product ux expert
database patterns
Conduct multi-agent task orchestration and workflow coordination.
Initialize project with Conductor artifacts (product definition,
Expert in web animations, transitions, and motion design using Framer Motion and CSS
Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...
PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.
Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...
Display project status, active tracks, and next actions
Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.
Generate, edit, and beat-sync AI video with leading models in one workspace.
The world's fastest calendar for remote work
Transform Your Design with AI Designer by ImgCreator.ai
Revolutionizing Video Production with AI-Powered Creativity
Extend an image past the frame and let AI fill the new aspect ratio.
Discover your celebrity doppelgänger with StarByFace!
ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.
Opus.ai: Revolutionize Your Web Experience