Skip to content

Instantly share code, notes, and snippets.

@orumad
Last active October 17, 2025 12:02
Show Gist options
  • Save orumad/89c3f6d27b09120c36347056f1a5fd11 to your computer and use it in GitHub Desktop.
Save orumad/89c3f6d27b09120c36347056f1a5fd11 to your computer and use it in GitHub Desktop.
Claude Code Model Configuration Tool - Switch between GLM and Claude models (v0.5)
#!/bin/bash
# Claude Code Model Configuration Script
# Usage: cla [c] [claude options and commands]
# Version: 0.7
# Gist: https://gist.github.com/orumad/89c3f6d27b09120c36347056f1a5fd11
set -e
# Default values
VERSION="0.7"
CLAUDE_CODE_CMD="claude"
CONFIG_DIR="$HOME/.claude"
SETTINGS_FILE="$CONFIG_DIR/settings.json"
GLM_CONFIG_DIR="$HOME/.config"
GLM_CONFIG_FILE="$GLM_CONFIG_DIR/claude-glm.config.json"
# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to ensure config directory exists
ensure_config_dir() {
if [[ ! -d "$GLM_CONFIG_DIR" ]]; then
mkdir -p "$GLM_CONFIG_DIR"
fi
}
# Function to save configuration to file
save_config() {
local api_key="$1"
ensure_config_dir
cat > "$GLM_CONFIG_FILE" << EOF
{
"api_key": "$api_key",
"last_updated": "$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
}
EOF
echo -e "${GREEN}Configuration saved to $GLM_CONFIG_FILE${NC}"
}
# Function to load configuration from file
load_config() {
if [[ -f "$GLM_CONFIG_FILE" ]]; then
if command -v jq >/dev/null 2>&1; then
SAVED_API_KEY=$(jq -r '.api_key // ""' "$GLM_CONFIG_FILE" 2>/dev/null)
else
# Fallback parsing without jq
SAVED_API_KEY=$(grep '"api_key"' "$GLM_CONFIG_FILE" | sed 's/.*"api_key": *"\([^"]*\)".*/\1/' 2>/dev/null || echo "")
fi
else
SAVED_API_KEY=""
fi
}
# Function to display help
show_help() {
echo "Claude Code Model Configuration Tool v$VERSION"
echo ""
echo "USAGE:"
echo " cla [c] [claude options and commands]"
echo ""
echo "DESCRIPTION:"
echo " Configures Claude Code to use Z.AI GLM models (GLM-4.6 + GLM-4.5-Air)."
echo " By default, uses GLM-4.6 as main model (Sonnet/Opus) and GLM-4.5-Air as fast model (Haiku)."
echo ""
echo "OPTIONS:"
echo " c Switch back to default Claude models"
echo " --api-key KEY Set Z.AI API key for GLM models (saved to config)"
echo " --show-config Display current configuration"
echo " --version, -v Show version information"
echo " --help, -h Show this help message"
echo ""
echo "EXAMPLES:"
echo " cla --api-key YOUR_API_KEY # Save API key and configure GLM"
echo " cla # Use saved configuration (GLM)"
echo " cla c # Switch to Claude models"
echo " cla c --help # Show Claude help with default models"
echo " cla --show-config # Show current and saved config"
echo " cla --edit # Edit with GLM models"
echo " cla c --edit # Edit with Claude models"
echo ""
}
# Function to show current configuration
show_config() {
load_config
echo -e "${BLUE}Current Configuration:${NC}"
echo ""
# Show environment variables
if [[ -n "$ANTHROPIC_BASE_URL" ]]; then
echo -e "${GREEN}Base URL:${NC} $ANTHROPIC_BASE_URL"
else
echo -e "${YELLOW}Base URL:${NC} Default Anthropic"
fi
if [[ -n "$ANTHROPIC_MODEL" ]]; then
echo -e "${GREEN}Main Model:${NC} $ANTHROPIC_MODEL"
else
echo -e "${YELLOW}Main Model:${NC} Default"
fi
if [[ -n "$ANTHROPIC_SMALL_FAST_MODEL" ]]; then
echo -e "${GREEN}Fast Model:${NC} $ANTHROPIC_SMALL_FAST_MODEL"
else
echo -e "${YELLOW}Fast Model:${NC} Default"
fi
# Show saved configuration
echo -e "${BLUE}Saved Configuration:${NC}"
if [[ -f "$GLM_CONFIG_FILE" ]]; then
echo -e "${GREEN}Config File:${NC} $GLM_CONFIG_FILE"
if [[ -n "$SAVED_API_KEY" ]]; then
echo -e "${GREEN}Saved API Key:${NC} ${SAVED_API_KEY:0:8}...${SAVED_API_KEY: -4}"
else
echo -e "${YELLOW}Saved API Key:${NC} Not set"
fi
if command -v jq >/dev/null 2>&1; then
local last_updated=$(jq -r '.last_updated // "unknown"' "$GLM_CONFIG_FILE" 2>/dev/null)
echo -e "${BLUE}Last Updated:${NC} $last_updated"
fi
else
echo -e "${YELLOW}Config File:${NC} Not found"
fi
# Show Claude settings
if [[ -f "$SETTINGS_FILE" ]]; then
echo -e "${GREEN}Claude Settings:${NC} $SETTINGS_FILE"
if command -v jq >/dev/null 2>&1; then
local current_model=$(jq -r '.model // "not set"' "$SETTINGS_FILE" 2>/dev/null || echo "Unable to read")
local current_small_model=$(jq -r '.smallFastModel // "not set"' "$SETTINGS_FILE" 2>/dev/null || echo "Unable to read")
echo -e "${BLUE}Current Model in Settings:${NC} $current_model"
echo -e "${BLUE}Current Small Fast Model:${NC} $current_small_model"
fi
else
echo -e "${YELLOW}Claude Settings:${NC} Not found"
fi
echo ""
}
# Function to configure for GLM models
configure_glm() {
local api_key="$1"
# Load saved config first
load_config
# Use provided API key, or saved one, or environment variable
local token="${api_key:-${SAVED_API_KEY:-$ANTHROPIC_AUTH_TOKEN}}"
if [[ -z "$token" ]]; then
echo -e "${RED}Error:${NC} API key required for GLM models."
echo "Use --api-key option, save it in config, or set ANTHROPIC_AUTH_TOKEN environment variable."
echo "Get your API key from: https://z.ai/manage-apikey/apikey-list"
exit 1
fi
echo -e "${BLUE}Configuring for GLM models (GLM-4.6 + GLM-4.5-Air)...${NC}"
# Update settings.json with GLM configuration
if [[ -f "$SETTINGS_FILE" ]]; then
if command -v jq >/dev/null 2>&1; then
local temp_file=$(mktemp)
jq --arg token "$token" '.env += {
"ANTHROPIC_AUTH_TOKEN": $token,
"ANTHROPIC_BASE_URL": "https://api.z.ai/api/anthropic",
"API_TIMEOUT_MS": "3000000",
"ANTHROPIC_DEFAULT_HAIKU_MODEL": "glm-4.5-air",
"ANTHROPIC_DEFAULT_SONNET_MODEL": "glm-4.6",
"ANTHROPIC_DEFAULT_OPUS_MODEL": "glm-4.6"
}' "$SETTINGS_FILE" > "$temp_file" && mv "$temp_file" "$SETTINGS_FILE"
echo -e "${GREEN}Updated settings.json with GLM models configuration${NC}"
else
echo -e "${RED}Error:${NC} jq is required to modify settings.json"
exit 1
fi
else
echo -e "${RED}Error:${NC} settings.json not found at $SETTINGS_FILE"
exit 1
fi
# Save configuration if API key was provided
if [[ -n "$api_key" ]]; then
save_config "$api_key"
fi
}
# Function to configure for Claude models
configure_claude() {
echo -e "${BLUE}Configuring for Claude models...${NC}"
# Update settings.json to remove GLM configuration
if [[ -f "$SETTINGS_FILE" ]]; then
if command -v jq >/dev/null 2>&1; then
local temp_file=$(mktemp)
jq 'del(.env["ANTHROPIC_AUTH_TOKEN"]) | del(.env["ANTHROPIC_BASE_URL"]) | del(.env["API_TIMEOUT_MS"]) | del(.env["ANTHROPIC_DEFAULT_HAIKU_MODEL"]) | del(.env["ANTHROPIC_DEFAULT_SONNET_MODEL"]) | del(.env["ANTHROPIC_DEFAULT_OPUS_MODEL"])' "$SETTINGS_FILE" > "$temp_file" && mv "$temp_file" "$SETTINGS_FILE"
echo -e "${GREEN}Removed GLM configuration from settings.json${NC}"
else
echo -e "${RED}Error:${NC} jq is required to modify settings.json"
exit 1
fi
else
echo -e "${RED}Error:${NC} settings.json not found at $SETTINGS_FILE"
exit 1
fi
}
# Function to launch Claude Code
launch_claude() {
echo -e "${GREEN}Launching Claude Code...${NC}"
exec "$CLAUDE_CODE_CMD" "$@"
}
# Parse command line arguments
API_KEY=""
SHOW_CONFIG=false
USE_CLAUDE=false
CLAUDE_ARGS=()
while [[ $# -gt 0 ]]; do
case $1 in
--help|-h)
show_help
exit 0
;;
--version|-v)
echo "cla v$VERSION"
exit 0
;;
--api-key)
API_KEY="$2"
shift 2
;;
--show-config)
SHOW_CONFIG=true
shift
;;
c)
USE_CLAUDE=true
shift
;;
*)
CLAUDE_ARGS+=("$1")
shift
;;
esac
done
# Handle special commands
if [[ "$SHOW_CONFIG" == true ]]; then
show_config
exit 0
fi
# Configure based on command
if [[ "$USE_CLAUDE" == true ]]; then
configure_claude
else
# GLM configuration (default behavior)
if [[ -n "$API_KEY" || -f "$GLM_CONFIG_FILE" || -n "$ANTHROPIC_AUTH_TOKEN" ]]; then
configure_glm "$API_KEY"
else
# No API key provided and no saved config
load_config
if [[ -n "$SAVED_API_KEY" ]]; then
echo -e "${BLUE}Loading saved GLM configuration${NC}"
configure_glm ""
else
echo -e "${YELLOW}No API key found. Please provide one to use GLM models.${NC}"
echo -e "${BLUE}Usage: cla --api-key YOUR_Z_AI_API_KEY${NC}"
echo -e "${BLUE}Get your API key from: https://z.ai/manage-apikey/apikey-list${NC}"
exit 1
fi
fi
fi
# Launch Claude Code with remaining arguments
launch_claude "${CLAUDE_ARGS[@]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment