Skip to content
prune.

How to reduce Claude Code cost with your own API key

The subscription stays with Anthropic. On a token bill, the cap and the base URL are the levers you actually control.

Quick answer

If Claude Code is charging an API key while you pay for Pro or Max, unset ANTHROPIC_API_KEY. Claude Code prefers that variable over the subscription. When you do mean to pay per token, set ANTHROPIC_BASE_URL to the host only (https://api.withprune.com, no /v1), put a prune_ key in ANTHROPIC_API_KEY, and vault the Anthropic key. A daily cap then stops a stuck session before it spends the month. Most coding turns are unique, so expect the cap to do the work. Cache only helps when a call repeats.

1. If you are on Pro or Max, unset the API key

Claude Code prefers ANTHROPIC_API_KEY over a Pro or Max login. A key left in the shell, or in ~/.claude/settings.json, is billed for every turn in that session, including sub-agents. The subscription sits unused. Unset the variable, delete it from the settings file, fully quit Claude Code, then run /logout and /login. Stay on this page only if you mean to pay per token.

2. See which bill you are on

Open the Anthropic console and look at usage. A Claude Pro or Team seat shows up as a subscription. API usage shows up as tokens against a key. Only the second one can be pointed at a gateway. The first one never sends you a base URL to change.

3. Put a spend limit on the workspace

In the Anthropic console, set a monthly spend limit on the workspace. That limit is the backstop. It can still overshoot slightly, and it does not stop one afternoon from eating most of the month. Add a tighter daily cap in front of it.

4. Pick the model for the job

Set ANTHROPIC_MODEL to claude-sonnet-4-6 for the session you are driving, and to claude-haiku-4-5-20251001 when the task is small. Check the id against the model list in your account, because ids change. Leaving Opus on for every file read is the usual reason a coding week costs more than the work.

5. Keep the provider key out of the agent

A sk-ant- key in a shell profile is one laptop away from a leaked-key bill. The agent should hold a gateway key. The provider key stays in a vault. If a key does leak, revoke it the same day. The OpenAI and Claude leak checklist is the order to follow.

Doing this with Prune

Vault the Anthropic key in the dashboard, copy a prune_ key, then close Claude Code. Put this in ~/.claude/settings.json (on Windows, %USERPROFILE%\.claude\settings.json). Do not put the key in a project .claude/settings.json that gets committed.

~/.claude/settings.json
{
  "env": {
    "ANTHROPIC_BASE_URL": "https://api.withprune.com",
    "ANTHROPIC_API_KEY": "prune_your_key",
    "ANTHROPIC_MODEL": "claude-sonnet-4-6"
  }
}

For one terminal session, the same values as environment variables. The host has no /v1. Claude Code adds /v1/messages itself.

session
$env:ANTHROPIC_BASE_URL = "https://api.withprune.com"
$env:ANTHROPIC_API_KEY = "prune_your_key"
$env:ANTHROPIC_MODEL = "claude-sonnet-4-6"
claude

Open Claude Code and run /status. Anthropic base URL must read https://api.withprune.com. If it ends in /v1, Claude Code calls /v1/v1/messages and tells you the model does not exist. That message is the URL, not the model id.

  • In Shield, set a daily dollar cap you can afford to lose. Accounts created on or after 10 October 2026 start on Balanced, which is $200 a day and 300 requests a minute. Older accounts stay uncapped until you pick a mode.
  • In Optimize, set model routing to Off for the account Claude Code uses. A long coding transcript usually stays on the model you picked. A short call can move to a cheaper model when routing is Balanced, and Claude Code will not expect that. The response header X-Prune-Routed-Model shows what ran.
  • Cache still runs. A repeated call can come back without a provider charge. A normal edit loop will miss. Read the receipt on your own traffic before you tell anyone a percentage.

With a single line of code, Prune cuts your AI bill and protects your team from runaway spend, leaked keys and dangerous calls.

Related

Have an account? Sign in · PyPI.