config/bin/claude-provider
Bastien Chanot 6d341f2c9f feat(bin): add dt, dtach-router, claude-provider CLI scripts
- dt: dtach session manager for claude-in-dtach sessions
- dtach-router: SSH-login dashboard to resume sessions (sourced from bashrc)
- claude-provider: switch Claude Code between Anthropic and OpenRouter;
  the OpenRouter API key is read from $OPENROUTER_API_KEY at runtime and
  is never stored in the repo

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-27 18:51:04 +02:00

64 lines
2.0 KiB
Bash
Executable File

#!/bin/bash
# Switch Claude Code between Anthropic (default) and OpenRouter.
# Usage: claude-provider [anthropic|openrouter|status]
# After switching, re-source your shell or run: cpsource
#
# OpenRouter mode reads the API key from $OPENROUTER_API_KEY at source time —
# the key is NOT stored in this script or in the repo. Export it from a private,
# untracked location (e.g. ~/.bashrc.local):
# export OPENROUTER_API_KEY="<your-openrouter-key>"
PROVIDER_FILE="$HOME/.claude-provider-env"
write_anthropic() {
cat > "$PROVIDER_FILE" << 'EOF'
# Claude provider: Anthropic (default)
unset ANTHROPIC_BASE_URL
unset ANTHROPIC_API_KEY
unset ANTHROPIC_DEFAULT_SONNET_MODEL
export CLAUDE_PROVIDER="anthropic"
EOF
echo "Switched to Anthropic (default)."
echo "Run: cpsource (or restart your shell)"
}
write_openrouter() {
cat > "$PROVIDER_FILE" << 'EOF'
# Claude provider: OpenRouter
# Key comes from $OPENROUTER_API_KEY in your environment (never hardcoded here).
export ANTHROPIC_BASE_URL="https://openrouter.ai/api"
export ANTHROPIC_API_KEY="${OPENROUTER_API_KEY:?Set OPENROUTER_API_KEY in your environment to use OpenRouter}"
export CLAUDE_PROVIDER="openrouter"
export ANTHROPIC_DEFAULT_SONNET_MODEL="google/gemma-4-31b-it:free"
EOF
echo "Switched to OpenRouter."
echo "Run: cpsource (or restart your shell)"
}
show_status() {
local provider="${CLAUDE_PROVIDER:-anthropic}"
echo "Current provider: $provider"
if [ -f "$PROVIDER_FILE" ]; then
echo "Config file: $PROVIDER_FILE"
fi
}
case "${1:-status}" in
openrouter|or)
write_openrouter
;;
anthropic|an)
write_anthropic
;;
status|st)
show_status
;;
*)
echo "Usage: claude-provider [anthropic|openrouter|status]"
echo " anthropic (an) — Use Anthropic directly (default)"
echo " openrouter (or) — Use OpenRouter proxy"
echo " status (st) — Show current provider"
exit 1
;;
esac