Back to Home

Claude Code

Claude Code is Anthropic's official terminal-based AI coding assistant. Two environment variables is all it takes to route it through Tokensmart and drive every Claude model (Opus / Sonnet / Haiku) with a single key.

πŸ’‘ One-click config: create a key on the API Keys page, then use the Config Guide β†’ Claude Code menu next to that key β€” you get the full command with your real key already filled in.

Install

npm install -g @anthropic-ai/claude-code

Configure (macOS / Linux)

Append to ~/.bashrc or ~/.zshrc:

export ANTHROPIC_BASE_URL=https://api.tokensmart.ai
export ANTHROPIC_API_KEY=pk_live_xxxxxxxxxxxxxxxx

Reload the shell (source ~/.zshrc) or open a new terminal.

Configure (Windows)

Open PowerShell as Administrator and run:

[System.Environment]::SetEnvironmentVariable("ANTHROPIC_BASE_URL", "https://api.tokensmart.ai", "User")
[System.Environment]::SetEnvironmentVariable("ANTHROPIC_API_KEY", "pk_live_xxxxxxxxxxxxxxxx", "User")

Restart PowerShell / cmd so the new variables take effect.

Verify

claude --version
claude "Hello, introduce yourself"

A normal reply confirms the connection.

Switch models

Default is claude-sonnet-4-6. Inside the session type /model claude-opus-4-8 to jump to Opus. Same key, every model.

Tokensmart tips

  • Turn on prompt caching: Claude Code replays the same long project-context system prompt every turn. Add cache_control: { type: "ephemeral" } to the system block β€” Tokensmart bills cached tokens at roughly 1/10 the normal rate, easily saving 70–90% on heavy use. See Understanding Cache Token Pricing.
  • Dedicated key with daily limit: agent mode loops autonomously and a stuck run can burn dollars fast. Create a Claude-Code-only key on API Keys and set a daily limit you are comfortable with (e.g. $5/day).
  • Restrict the model allowlist: tick only claude-sonnet-4-6 / claude-opus-4-8 under Allowed Models so you cannot accidentally fall through to a non-Claude model.
  • Automatic failover: Tokensmart has built-in redundancy. If a service node is down, it automatically switches to a backup β€” no config change required.
  • Live cost visibility: the API Logs page breaks down each turn's prompt / completion / cache tokens and dollar cost per session.

Troubleshooting

  • 401 Unauthorized β€” key must start with pk_live_, no leading/trailing whitespace
  • 404 / timeout β€” ANTHROPIC_BASE_URL is https://api.tokensmart.ai without a /v1 suffix; Claude Code appends the path itself
  • Insufficient balance β€” top up from the Credits page