skills/injective-ai-cost-optimization/SKILL.md
Optimize AI API costs for agentic trading bots. Covers Anthropic token cost tracking, web search cost management, CoinGecko/Massive.com data sourcing, shared caching strategies, and per-user cost caps. Learned from real production data showing web search inflates costs 10-17x.
npx skillsauth add injectivelabs/agent-skills injective-ai-cost-optimizationInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Real-world cost data from production deployment on Injective DEX with Claude Haiku 4.5.
Production data (Claude Haiku 4.5, 15-min intervals):
| Call Type | Input Tokens | Output Tokens | Cost/Call | Daily (96 calls) |
|---|---|---|---|---|
| With web search (web_search_20250305) | 20,000–44,000 | ~1,000 | $0.03–0.05 | $3–5/user |
| Without search (technicals only) | 650–2,600 | ~500 | $0.003 | $0.30/user |
Web search is 10-17x more expensive — not because of the $0.01/search fee, but because Anthropic injects 20K-44K tokens of web page content into the input context.
Market data is the same for all users. Don't fetch it per-user — cache it globally.
CoinGecko (crypto) ──┐
├── Shared Cache (15 min TTL) ──→ All user AI calls
Massive.com (equities)┘
| Asset Class | Source | Cost | What You Get |
|---|---|---|---|
| Crypto (BTC, ETH, etc.) | CoinGecko free tier | Free (no API key) | 200 daily closes, 7d/14d/30d/60d/200d/1y changes, market cap, volume, ATH |
| Equities (NVDA, TSLA, etc.) | Massive.com API | API key required | 200+ daily OHLC bars, volume |
| Forex (EUR, GBP) | Massive.com API | C:EURUSD format | Same as equities |
| Commodities (XAU, XAG) | Massive.com API | C:XAUUSD format | Same as equities |
| Oil | Massive.com API | USO (ETF ticker) | Same as equities |
# Per-coin detailed data (7d/30d/200d changes, ATH, volume)
GET /api/v3/coins/{id}?localization=false&tickers=false&community_data=false&developer_data=false
# 200 daily closes for technical indicators
GET /api/v3/coins/{id}/market_chart?vs_currency=usd&days=200&interval=daily
Rate limits: 5-15 calls/minute (free), 30/minute (demo key). Add 1.5s delay between requests. Use optimistic lock to prevent thundering herd:
let _fetchInProgress = false
if (staleSymbols.length > 0 && !_fetchInProgress) {
_fetchInProgress = true
try { /* fetch */ } finally { _fetchInProgress = false }
}
GET /v2/aggs/ticker/{ticker}/range/1/day/{from}/{to}?apiKey={key}&limit=250&sort=asc&adjusted=true
Tickers: stocks direct (NVDA), forex with C: prefix (C:EURUSD), commodities (C:XAUUSD, USO for oil).
Compute these from 200 daily closes — zero API cost:
| Indicator | Formula | Use | |---|---|---| | EMA(9/21/50/200) | Exponential moving average | Trend direction | | MACD(12,26,9) | EMA(12) - EMA(26), signal = EMA(9) of MACD | Momentum + crossovers | | RSI(14) | Relative strength index | Overbought/oversold | | Bollinger Bands(20,2) | Mean ± 2σ of last 20 closes | Volatility + breakouts |
These replace web search entirely. The AI gets structured data (EMAs, MACD signal, RSI value) instead of 40K tokens of raw web pages. Better signal, 10x cheaper.
Log token counts from every Anthropic response:
const inputTokens = response.usage?.input_tokens ?? 0
const outputTokens = response.usage?.output_tokens ?? 0
const HAIKU_INPUT_PER_M = 0.80
const HAIKU_OUTPUT_PER_M = 4.00
const tokenCost = (inputTokens * HAIKU_INPUT_PER_M + outputTokens * HAIKU_OUTPUT_PER_M) / 1_000_000
logger.info({ inputTokens, outputTokens, tokenCost: `$${tokenCost.toFixed(4)}` }, 'API call cost')
Expose a /api/costs endpoint with rolling 24h summary:
// Protect with admin wallet check
const admins = (process.env.ADMIN_WALLETS ?? '').split(',').map(s => s.trim())
if (!admins.includes(wallet)) return c.json({ error: 'Admin access required' }, 403)
return c.json(getCostSummary())
| Interval | Calls/Day | Token Cost/User | Use Case | |---|---|---|---| | 5 min | 288 | ~$0.86 | Scalping (too expensive for most) | | 15 min | 96 | ~$0.29 | Active trading (recommended default) | | 30 min | 48 | ~$0.14 | Swing trading | | 1 hour | 24 | ~$0.07 | Position trading |
Output tokens scale linearly with symbol count. Cap at 10 symbols per strategy:
max_tokens: Math.max(1024, strategy.symbols.length * 200)
At 9 symbols with 1024 max_tokens, the AI runs out of space and returns truncated (unparseable) JSON.
Cap at 3 strategies per user. Each strategy = 1 AI call per tick. Maximum cost exposure = 3 strategies × 10 symbols × 96 ticks/day = ~$2.90/day.
If re-enabling, use a shared cache:
const NEWS_CACHE_TTL_MS = 60 * 60 * 1000 // 1 hour
// One search for ALL users, cache per symbol
// Reduces 100 users × 96 ticks = 9,600 searches/day to ~24 searches/day
| Model | Input/M | Output/M | Best For | |---|---|---|---| | Haiku 4.5 | $0.80 | $4.00 | Trading signals (default, fast, cheap) | | Sonnet 4 | $3.00 | $15.00 | Complex analysis (4x more expensive) |
Haiku is sufficient for structured signal generation from technical data.
Based on production data:
| Metric | Value | |---|---| | Cost per tick (no search) | ~$0.003 | | Cost per user per day (15 min, 1 strategy) | ~$0.30 | | Cost per user per day (15 min, 3 strategies) | ~$0.90 | | Worst case per user per day | ~$2.90 | | Suggested x402 price per tick | $0.01–0.03 (3-10x markup) | | Suggested monthly subscription | $15–30/user |
max_tokens scaling — fixed 2048 tokens for 2 symbols wastes money. Scale with symbol count.response.usage.development
Detect breaking changes in Injective core between two tagged releases. For use in developer documentation and release notes.
development
Integrate Injective RFQ taker flows into browser apps and operational quote monitors. Use this skill when building, reviewing, or debugging RFQ gateway autosign settlement, Web3Gateway AuthZ setup, manual TakerStream quote collection, RFQ open/close flows, conditional TP/SL intents, quote uptime probes, market-readiness checks, or mainnet RFQ frontend integrations. Covers mainnet parameters, canonical decimals, quote windows, signer slots, market discovery, quote-hit diagnostics, and production RFQ gotchas.
tools
Enables management of Linear issues, teams, projects, comments, or configuration via the `linear` CLI
tools
Mass create, derive, and manage Injective wallets. Generate wallets from mnemonics (BIP-44 HD derivation), create random wallets, convert between ETH/INJ addresses, and batch fund wallets with INJ or USDT. Supports bulk wallet generation and batch funding.