Recently Written · git

set-autocompact

Set a 500k-token auto-compact threshold for Claude Code and Codex

git clone https://github.com/equwal/set-autocompact

Log | Files | Refs


commit 255ae3a990948db1a59c04e4cc437d689e3696f2
equwal <13551856+equwal@users.noreply.github.com>
2026-10-03 22:16:19 -0700

Replace the Python script with a shell script that uses jq

jq edits the Claude Code settings.json. grep handles the Codex config.toml, because jq cannot parse TOML. The script runs in Git Bash on Windows and in sh on Linux.

 set_autocompact.py | 40 ----------------------------------------
 set_autocompact.sh | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 34 insertions(+), 40 deletions(-)
diff --git a/set_autocompact.py b/set_autocompact.py
deleted file mode 100644
index b763edc..0000000
--- a/set_autocompact.py
+++ /dev/null
@@ -1,40 +0,0 @@
-"""Set a 500k-token auto-compact threshold for Claude Code and Codex on this computer.
-
-Copilot CLI (hardcoded 95%) and Antigravity have no setting for it.
-"""
-
-import json
-import re
-import shutil
-import subprocess
-from pathlib import Path
-
-TOKENS = 500_000
-HOME = Path.home()
-
-
-def save(p: Path, text: str) -> None:
-    """Back up, write, then commit and push the file in its config repo."""
-    p.parent.mkdir(parents=True, exist_ok=True)
-    if p.exists():
-        shutil.copy2(p, f"{p}.bak-autocompact")
-    p.write_text(text, encoding="utf-8")
-    if (p.parent / ".git").exists():
-        git = ["git", "-C", str(p.parent)]
-        subprocess.run([*git, "commit", "-qm", f"Set auto-compact threshold to {TOKENS} tokens", "--", p.name])
-        subprocess.run([*git, "push", "-q"])
-    print(f"set: {p}")
-
-
-claude = HOME / ".claude" / "settings.json"
-settings = json.loads(claude.read_text(encoding="utf-8")) if claude.exists() else {}
-settings["autoCompactWindow"] = TOKENS
-save(claude, json.dumps(settings, indent=2, ensure_ascii=False) + "\n")
-
-# Drop any old value, then put the key first so it stays out of every [table].
-codex = HOME / ".codex" / "config.toml"
-toml = codex.read_text(encoding="utf-8") if codex.exists() else ""
-toml = re.sub(r"(?m)^model_auto_compact_token_limit\s*=.*\n?", "", toml)
-save(codex, f"model_auto_compact_token_limit = {TOKENS}\n{toml}")
-
-print("blocked: Copilot CLI and Antigravity have no setting")
diff --git a/set_autocompact.sh b/set_autocompact.sh
new file mode 100755
index 0000000..82418dd
--- /dev/null
+++ b/set_autocompact.sh
@@ -0,0 +1,34 @@
+#!/bin/sh
+# Set a 500k-token auto-compact threshold for Claude Code and Codex on this computer.
+# Needs jq. Copilot CLI (hardcoded 95%) and Antigravity have no setting for it.
+set -eu
+TOKENS=500000
+
+# Back up $1, replace it with $2, then commit and push it in its config repo.
+save() {
+  if [ -f "$1" ]; then cp "$1" "$1.bak-autocompact"; fi
+  mv "$2" "$1"
+  dir=$(dirname "$1")
+  if [ -d "$dir/.git" ]; then
+    git -C "$dir" commit -qm "Set auto-compact threshold to $TOKENS tokens" -- "$(basename "$1")" || true
+    git -C "$dir" push -q || true
+  fi
+  echo "set: $1"
+}
+
+mkdir -p ~/.claude ~/.codex
+
+f=~/.claude/settings.json
+if [ ! -f "$f" ]; then echo '{}' > "$f"; fi
+jq --indent 2 ".autoCompactWindow = $TOKENS" "$f" > "$f.tmp"
+save "$f" "$f.tmp"
+
+# Drop any old value, then put the key first so it stays out of every [table].
+f=~/.codex/config.toml
+{
+  echo "model_auto_compact_token_limit = $TOKENS"
+  if [ -f "$f" ]; then grep -v '^model_auto_compact_token_limit[[:space:]]*=' "$f" || true; fi
+} > "$f.tmp"
+save "$f" "$f.tmp"
+
+echo "blocked: Copilot CLI and Antigravity have no setting"