feat(copilot): implement token efficiency strategy (#364)#365
feat(copilot): implement token efficiency strategy (#364)#365nanotaboada merged 4 commits intomasterfrom
Conversation
WalkthroughUpdated Copilot instruction content to split base vs on-demand guidance and clarified token budgets; adjusted AGENTS.md loading notes; added a new Changes
Sequence Diagram(s)mermaid Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Possibly related issues
Possibly related PRs
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 3
🤖 Fix all issues with AI agents
In `@scripts/count-tokens.sh`:
- Around line 94-95: The savings calculation can divide by zero when TOTAL is 0;
update the script around the SAVINGS calculation to guard against this by
checking if TOTAL equals 0 before doing SAVINGS=$((AGENTS_TOKENS * 100 / TOTAL))
and handle that case (e.g., set SAVINGS=0 or print "0% saved"/a clear message)
so AGENTS_TOKENS and TOTAL are not used in a division-by-zero operation; ensure
the change is made where SAVINGS, AGENTS_TOKENS and TOTAL are defined/used so
downstream echo "💡 Savings: ${SAVINGS}% saved when AGENTS.md not needed" always
prints a safe value.
- Around line 64-73: The script assumes files exist; add existence checks before
calling the Python counter so failures are explicit: for each target (the calls
that set COPILOT_TOKENS and AGENTS_TOKENS using "python3 /tmp/count_tokens.py
<file>") test the file exists (e.g. using a shell test like [ -f "<file>" ]) and
if missing, print a clear warning and set the corresponding variable to a safe
default (e.g. "0" or "N/A") or skip counting, otherwise run the python script as
before; ensure messages mirror the current echo lines so behavior is consistent.
- Around line 23-42: The interactive read -p block will hang in CI; modify the
script to detect non-interactive environments (e.g. using [ -t 0 ] or checking
CI/CI_CD env vars) and skip the prompt automatically: if non-interactive, set
USE_APPROX=1 (or respect an env flag like AUTO_INSTALL_TIKTOKEN=1 to
auto-install) and bypass the read -p/install flow; otherwise keep the existing
prompt flow that runs pip3 install tiktoken and reexecs via exec "$0" "$@".
Ensure you change only the prompt surrounding code that references read -p and
USE_APPROX so CI runs don't block.
🧹 Nitpick comments (2)
scripts/count-tokens.sh (2)
49-62: Consider usingmktempfor the temporary Python script.Using a fixed path
/tmp/count_tokens.pycould cause conflicts if multiple instances run concurrently (e.g., parallel CI jobs on the same runner).♻️ Proposed improvement
- cat > /tmp/count_tokens.py << 'PYTHON' + TEMP_SCRIPT=$(mktemp /tmp/count_tokens.XXXXXX.py) + cat > "$TEMP_SCRIPT" << 'PYTHON'Then update all references from
/tmp/count_tokens.pyto"$TEMP_SCRIPT".
27-27: Consider--userflag for pip install.Global pip install may require elevated permissions. Using
--userinstalls to the user's local directory without needing sudo.♻️ Proposed fix
- if pip3 install tiktoken --quiet; then + if pip3 install tiktoken --user --quiet; then
- Optimize copilot-instructions.md to 622 tokens (600-650 target) - Update AGENTS.md with accurate token counts (~2,550 tokens) - Add scripts/count-tokens.sh with auto-install tiktoken - Achieve 80% token savings when AGENTS.md not loaded
7a8c4bd to
96238c9
Compare
Coverage summary from CodacySee diff coverage on Codacy
Coverage variation details
Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch: Diff coverage details
Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified: See your quality gate settings Change summary preferences |
- Add CI/CD detection to skip interactive prompts - Check file existence before counting tokens - Guard against division by zero in savings calculation - Suppress blake2b/blake2s errors for clean output
…a/Dotnet.Samples.AspNetCore.WebApi into feat/copilot-token-efficiency
|



Summary by CodeRabbit
Documentation
Chores
✏️ Tip: You can customize this high-level summary in your review settings.