Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🪨 Token-Saver Skill for Antigravity

An Antigravity skill that helps agents optimize Gemini token consumption and reduce costs on GCP / Agent Platform.

What It Does

When activated, this skill guides agents to proactively reduce token usage through 9 complementary strategies:

# Strategy Impact
1 Architectural & Tool Optimization Reduce static "token tax" from unused MCP servers and verbose tool outputs
2 Multi-Model Routing Route routine tasks to Flash, reserve Pro for complex reasoning
3 Vertex AI Context Caching Leverage GCP's lower-cost cached token pricing
4 Workspace Hygiene Filter out node_modules, builds, and media via .antigravityignore
5 Session Hygiene Prune history, fork conversations, reuse subagents
6 "Caveman Mode" Compress agent output — strip filler, keep code exact
7 Dynamic Tool & Prompt Filtering Load only relevant instructions, navigate by symbol not by file
8 AGENTS.md Budget Rule Keep always-loaded config lean, refactor bloat into on-demand skills
9 Pre-Invocation Hook Automatically scan, classify, and intercept vague or wasteful user prompts

📈 Estimated Token Savings (Real-World Benchmarks)

According to our performance benchmarks using real-world files (including VS Code and React codebase locks and bundles), the Token-Saver strategies yield a massive 97.9% combined reduction in token consumption under standard developer workloads:

Workspace Scenario / Turn Type Without Token-Saver With Token-Saver ✨ Savings
Workspace Hygiene (VS Code lockfile vs ignore) ~491,934 tokens ~5,890 tokens 98.8%
Context Slicing (full production bundle vs slice) ~37,681 tokens ~4,213 tokens 88.8%
Caveman Mode (3 representative responses) ~244 tokens ~102 tokens 58.2%
AGENTS.md Budgeting (persistent cost over 10 turns) ~4,810 tokens ~1,120 tokens 76.7%
Combined Illustrative Total ~534,669 tokens ~11,325 tokens 97.9% (523k saved)

⚠️ Honest Engineering Caveats:

  • Workspace Hygiene: Savings materialize only when the agent actually reads the excluded files. Lock files in large monorepos (like VS Code) can exceed 700KB, making this highly impactful if read.
  • Context Slicing: Slicing is highly valuable, but savings are proportional to the file size (e.g., slicing a standard 300-line file saves ~1.5k tokens).
  • Caveman Mode: Savings are purely on conversational prose (code blocks are never compressed). Expect 15-40% savings on prose-heavy responses.
  • AGENTS.md: Savings scale linearly with session length (e.g., saving ~36,900 tokens at 100 turns).

🛡️ Pre-Invocation Prompt Interceptor (Hook)

The repository includes a global PreInvocation hook script (intercept_wasteful_prompts.py) that acts as an active firewall for your wallet. It intercepts:

  • Extreme Vagueness: Simple keywords like "do it", "run", "go", or "test" with zero details.
  • Massive Data Dumps: Vague requests like "explain every line of code" or "show me everything".
  • Infinite Loops: Endless looping routines.

When intercepted, the hook injects a transient warning prompting the agent to immediately halt and request clarification rather than executing expensive codebase scans.

Registering the Hook Globally

To use the hook, copy it to your scratch directory and register it inside your global configuration file ~/.gemini/config/hooks.json:

{
  "gcs-brain-sync": {
    "PreInvocation": [
      {
        "type": "command",
        "command": "/Users/estherlloyd/.gemini/antigravity-cli/scratch/gcs_pre_invocation.py",
        "timeout": 10
      },
      {
        "type": "command",
        "command": "/Users/estherlloyd/.gemini/antigravity-cli/scratch/intercept_wasteful_prompts.py",
        "timeout": 10
      }
    ]
  }
}

🔬 Validation, Verification & Collaborative Humility Note

🤝 Collaborative Humility: Spotting the Mistake

In our initial iterations, the performance verification framework used a highly optimistic synthetic simulator. It relied on simple string multiplication and mock ratios to claim a flat "98.3% global reduction" in token usage.

Claude checked our work and discovered that this implementation was wrong — pointing out that real-world files, monorepo lockfiles, and output code structures do not scale linearly or behave like synthetic models. Claude checked in a dedicated real-benchmarks branch containing high-fidelity, real-file measurement routines.

We fully admit this initial modeling error, merged Claude's changes, and have updated this entire project to use only realistic, honest, and high-fidelity testing techniques.

How It Was Tested & Verified

We now run real-file, deterministic benchmarks using actual open-source project artifacts:

1. Quantitative Performance Simulator

Downloads real files directly from GitHub (e.g., VS Code's package-lock.json and React's yarn.lock) to test .antigravityignore workspace hygiene.

  • Downloads React-DOM's minified production bundle (react-dom.production.min.js at 131 KB) to benchmark Context Slicing against targeted 30-line slices.
  • Compares 3 representative conversational prose responses to count the exact token reductions of Caveman Mode (keeping code blocks 100% untouched).
  • Counts the turn-by-turn instruction tax of lean vs. bloated AGENTS.md system prompt guidelines over a 10-turn session.
  • Uses the actual Gemini token-counting API when a GEMINI_API_KEY is present (and falls back to an industry-standard 3.5 chars/token code heuristic when offline).

To run this:

python3 test_token_saver.py

Creates/updates a local markdown verification report at benchmark_report.md.

2. Interactive Prompt Interceptor Tester

Allows you to run static classification tests against various prompt examples to verify the interceptor hook logic.

python3 test_interceptor.py

Installation

Global (all workspaces)

Copy the skill into your Antigravity global config:

cp -r . ~/.gemini/config/skills/token-saver/

Per-project

Copy the skill into your project's .agents/skills/ directory:

mkdir -p .agents/skills/token-saver
cp -r . .agents/skills/token-saver/

Via skills.json (shared teams)

Add a reference in your customization root's skills.json:

{
  "entries": [
    { "path": "/path/to/token-optimization-skill" }
  ]
}

Usage

The skill activates automatically when the agent detects keywords like:

  • "reduce tokens", "save cost", "optimize context", "token budget"
  • "workspace pruning", "context window bloating"

You can also explicitly ask: "Use the token-saver skill".

.antigravityignore Template

An example ignore file is included at resources/example-antigravityignore. Copy it to your project root:

cp resources/example-antigravityignore /path/to/your/project/.antigravityignore

Credits & Inspiration

License

MIT

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages