Openrouter Cost Controls
Overview
This skill demonstrates setting up spending limits, per-request budgets, and automated cost monitoring to prevent unexpected charges.
Prerequisites
- OpenRouter account with credit management access
- Understanding of model pricing tiers
Instructions
- Set account-level limits: Configure spending limits in the OpenRouter dashboard under Settings > Limits to cap total monthly spend
- Use per-request max_tokens: Always set
max_tokensin every request to cap completion length and prevent runaway costs - Track costs per request: Read the
usagefield in each response and multiply by model pricing to track cumulative spend in your application - Implement budget middleware: Create a middleware that checks cumulative spend before each request and rejects requests that would exceed the budget
- Set up alerts: Query
/api/v1/auth/keyperiodically to check remaining credits and alert when balance drops below a threshold
Output
- Per-request cost tracking integrated into your application
- Budget enforcement middleware that blocks requests over the limit
- Automated alerts when spending approaches configured thresholds
Error Handling
| Error | Cause | Fix |
|---|---|---|
| 402 Payment Required | Account credits exhausted | Top up credits or switch to free models |
| Budget exceeded locally | Middleware blocked the request | Increase budget limit or optimize prompts to reduce token usage |
| Inaccurate cost tracking | Using stale pricing data | Refresh model pricing from /models endpoint daily |
See ${CLAUDE_SKILL_DIR}/references/errors.md for full error reference.
Examples
See ${CLAUDE_SKILL_DIR}/references/examples.md for runnable code samples.