ThisCalculator

Everyday

Set OpenCode Spending Limits & Avoid Unexpected Charges

Updated · Standard formula, shown below

This guide shows you how to set spending limits in OpenCode and configure OpenRouter to prevent surprise bills. You’ll learn the exact steps to cap your usage, set up notifications, and what to do if you’re still charged more than expected.

What You’ll Need

  • An OpenCode installation (CLI or IDE extension) with an active OpenRouter API key
  • Access to your OpenRouter account dashboard (openrouter.ai)
  • Basic familiarity with your operating system’s terminal or command prompt

Step-by-Step

  1. Check your current OpenRouter usage. Log in to openrouter.ai and go to the “Activity” or “Usage” page. Note your current spend and the billing period. This tells you your baseline before setting limits.
  2. Set a hard credit limit on OpenRouter. In your OpenRouter dashboard, navigate to “Settings” → “Billing” or “Limits.” Look for “Monthly credit limit” or “Spending limit.” Enter an amount you’re comfortable with (e.g., $20). Save the change. OpenRouter will block requests once you hit this limit.
  3. Enable email notifications. In the same Billing section, find “Notification preferences.” Turn on alerts at 50%, 80%, and 100% of your limit. This gives you time to react before you’re cut off.
  4. Set a per-request max cost in OpenCode. Open your OpenCode configuration file. On most systems this is ~/.config/opencode/config.json (Linux/macOS) or %APPDATA%\opencode\config.json (Windows). If the file doesn’t exist, create it.
  5. Add a budget field. Inside the JSON, add a key like "budget": { "max_cost_per_request": 0.10 }. Replace 0.10 with your desired maximum cost per single request (in USD). Save the file.
  6. Restart OpenCode. Close and reopen OpenCode so the new config loads. Verify the setting took effect by running a simple prompt and checking the cost reported.
  7. Use a cheaper model for routine tasks. In OpenCode, you can specify a model per session or per prompt. For everyday questions, choose a low-cost model like openrouter/auto or a small model (e.g., meta-llama/llama-3.1-8b-instruct). Reserve expensive models for complex tasks.
  8. Monitor your usage in real time. While using OpenCode, run opencode status or check the sidebar (if using the IDE extension) to see your current session cost. This helps you catch runaway prompts early.
  9. Set a monthly budget in OpenCode (optional). Some versions support a "monthly_budget" field in the same config file. Add "budget": { "monthly_limit": 50 } to cap total spend per calendar month. If your version doesn’t support it, rely on OpenRouter’s limit.
  10. Test the limit. Temporarily set your OpenRouter limit to $1, then make a few requests. Confirm you get an error when you exceed it. Reset to your real limit afterward.

Common Mistakes

  • Setting only an OpenCode budget, not OpenRouter. OpenCode’s budget is per-request only; it doesn’t cap total daily or monthly spend. Always set a hard limit on OpenRouter.
  • Forgetting to include the currency. OpenRouter limits are in USD. If you enter “50” thinking it’s euros, you might spend more than intended.
  • Using a model with a high per-token cost without checking. Some models cost $5 per million tokens; others cost $0.10. A long conversation can rack up charges quickly.
  • Not enabling notifications. Without alerts, you won’t know you’re close to the limit until you’re blocked.
  • Editing the wrong config file. OpenCode may have multiple config files (user, project, global). Make sure you edit the one that’s actually loaded.

Tips

  • Set your OpenRouter limit to a round number that’s slightly below what you’re comfortable spending—this gives you a mental buffer.
  • Use the --model flag in OpenCode to force a cheap model for one-off tasks, e.g., opencode --model openrouter/auto.
  • Review your OpenRouter activity weekly. Look for unusually long conversations or repeated requests that suggest a bug in your automation.
  • If you’re using OpenCode in a CI/CD pipeline, set a very low per-request limit (e.g., $0.01) to prevent infinite loops from costing you.
  • Consider using OpenRouter’s “credits” system: prepay a small amount (e.g., $10) and set your limit to that amount. Once it’s gone, you’re done for the month.

Troubleshooting

Limit not working? If you’re still getting charged after setting a limit, check that you saved the OpenRouter settings. Sometimes the page requires a “Update” button. Also, verify that you’re using the same API key in OpenCode that’s linked to the limit.

OpenCode config not loading? Run opencode config list to see which files are active. If your budget field isn’t recognized, update OpenCode to the latest version—older versions may ignore unknown fields.

Unexpected charges appeared anyway. First, check your OpenRouter activity log for requests after the limit timestamp. If you see them, it’s likely a delay in the limit taking effect. Contact OpenRouter support with the request IDs. If the charges are from before the limit, you’ll need to pay them—limits don’t retroactively apply.

Can’t find the billing page? OpenRouter’s UI changes occasionally. Look under your profile avatar → “Credits” or “Billing.” If you can’t find it, use the help center or contact support.

Limit is set but OpenCode still sends requests? OpenCode may cache the config. Restart it completely, or kill any background processes. Also check that you haven’t set a different config for a specific project that overrides the global one.

Frequently Asked Questions

Does OpenRouter’s limit stop all requests instantly?

Yes, once you hit the limit, OpenRouter returns a 402 error and no further requests are processed. However, there may be a few seconds of latency before the limit is enforced.

Can I set a daily limit instead of monthly?

OpenRouter currently only supports monthly limits. For daily caps, you’d need to track usage yourself or use OpenCode’s per-request budget as a proxy.

What happens if I exceed my OpenRouter limit mid-request?

That request is allowed to complete, but the next one will be blocked. You won’t be charged for a partial request beyond what was already consumed.

Are there any fees for setting a limit?

No, setting a spending limit is free. You only pay for the tokens you actually use.

Can I set a limit in OpenCode without using OpenRouter?

OpenCode’s budget fields work with any provider, but the per-request limit only applies to a single API call. For total spend caps, you need provider-level controls like OpenRouter’s.

Will my OpenCode sessions stop automatically when the limit is hit?

No, OpenCode won’t automatically stop your session. You’ll see an error message, and you’ll need to manually stop the session or wait for the next request to fail.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.