重要前提
安装AI Skills的关键前提是:必须科学上网,且开启TUN模式,这一点至关重要,直接决定安装能否顺利完成,在此郑重提醒三遍:科学上网,科学上网,科学上网。查看完整安装教程 →
gemini-token-optimization by melodic-software/claude-code-plugins
npx skills add https://github.com/melodic-software/claude-code-plugins --skill gemini-token-optimization停止 - 在提供任何关于 Gemini 令牌使用的回应之前:
- 调用
gemini-cli-docs技能- 查询 特定的令牌或定价主题
- 所有回应必须完全基于 加载的官方文档
用于在委托给 Gemini CLI 时优化成本和令牌使用情况的技能。对于高效的批量操作和注重成本的工作流至关重要。
关键词: 令牌使用、成本优化、gemini 成本、模型选择、flash 与 pro、缓存、批量查询、减少令牌
在以下情况下使用此技能:
Gemini CLI 自动缓存上下文,通过重用先前处理过的内容来降低成本。
| 认证方法 | 缓存可用性 |
|---|---|
| API 密钥 (Gemini API) | 是 |
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
| Vertex AI | 是 |
| OAuth (个人/企业) | 否 |
/stats 命令或 JSON 输出查看节省情况result=$(gemini "query" --output-format json)
total=$(echo "$result" | jq '.stats.models | to_entries | map(.value.tokens.total) | add // 0')
cached=$(echo "$result" | jq '.stats.models | to_entries | map(.value.tokens.cached) | add // 0')
billable=$((total - cached))
savings=$((cached * 100 / total))
echo "总计: $total 令牌"
echo "已缓存: $cached 令牌 ($savings% 节省)"
echo "计费: $billable 令牌"
| 模型 | 上下文窗口 | 速度 | 成本 | 质量 |
|---|---|---|---|---|
| gemini-2.5-flash | 大 | 快 | 较低 | 良好 |
| gemini-2.5-pro | 非常大 | 较慢 | 较高 | 最佳 |
在以下情况下使用 Flash (-m gemini-2.5-flash):
在以下情况下使用 Pro (-m gemini-2.5-pro):
# 批量文件分析 - 使用 Flash
for file in src/*.ts; do
gemini "列出所有导出" -m gemini-2.5-flash --output-format json < "$file"
done
# 安全审计 - 使用 Pro 以保证质量
gemini "深度安全分析" -m gemini-2.5-pro --output-format json < critical-auth.ts
# 带模型信息的成本跟踪
result=$(gemini "query" --output-format json)
model=$(echo "$result" | jq -r '.stats.models | keys[0]')
tokens=$(echo "$result" | jq '.stats.models | to_entries[0].value.tokens.total')
echo "使用了 $model: $tokens 令牌"
# 代替 N 次单独调用
# 使用所有文件进行一次调用
cat src/*.ts | gemini "分析所有 TypeScript 文件以寻找模式" --output-format json
# 合并相关问题
gemini "回答关于代码库的这些问题:
1. 主要的架构模式是什么?
2. 如何处理身份验证?
3. 使用了什么数据库?" --output-format json
# 第一遍:使用 Flash 快速概览
overview=$(cat src/*.ts | gemini "列出所有模块" -m gemini-2.5-flash --output-format json)
# 第二遍:使用 Pro 深入分析关键领域
echo "$overview" | jq -r '.response' | grep "auth\|security" | while read module; do
gemini "对 $module 进行深度分析" -m gemini-2.5-pro --output-format json
done
result=$(gemini "query" --output-format json)
# 提取所有与成本相关的统计信息
total_tokens=$(echo "$result" | jq '.stats.models | to_entries | map(.value.tokens.total) | add // 0')
cached_tokens=$(echo "$result" | jq '.stats.models | to_entries | map(.value.tokens.cached) | add // 0')
models_used=$(echo "$result" | jq -r '.stats.models | keys | join(", ")')
tool_calls=$(echo "$result" | jq '.stats.tools.totalCalls // 0')
latency=$(echo "$result" | jq '.stats.models | to_entries | map(.value.api.totalLatencyMs) | add // 0')
echo "$(date): tokens=$total_tokens cached=$cached_tokens models=$models_used tools=$tool_calls latency=${latency}ms" >> usage.log
# 跟踪会话中的累计使用情况
total_session_tokens=0
total_session_cached=0
total_session_calls=0
track_usage() {
local result="$1"
local tokens=$(echo "$result" | jq '.stats.models | to_entries | map(.value.tokens.total) | add // 0')
local cached=$(echo "$result" | jq '.stats.models | to_entries | map(.value.tokens.cached) | add // 0')
total_session_tokens=$((total_session_tokens + tokens))
total_session_cached=$((total_session_cached + cached))
total_session_calls=$((total_session_calls + 1))
}
# 在工作流中使用
result=$(gemini "query 1" --output-format json)
track_usage "$result"
result=$(gemini "query 2" --output-format json)
track_usage "$result"
echo "会话总计: $total_session_tokens 令牌 ($total_session_cached 已缓存) 在 $total_session_calls 次调用中"
# 批量处理使用 Flash
gemini "query" -m gemini-2.5-flash --output-format json
# 检查缓存效率
gemini "query" --output-format json | jq '{total: .stats.models | to_entries | map(.value.tokens.total) | add, cached: .stats.models | to_entries | map(.value.tokens.cached) | add}'
# 最小化输出(更少的输出令牌)
gemini "用一句话回答: {question}" --output-format json
粗略的令牌估算:
| 主题 | 查询关键词 |
|---|---|
| 缓存 | token caching, cached tokens, /stats |
| 模型选择 | model routing, flash vs pro, -m flag |
| 成本 | quota pricing, token usage, billing |
| 输出控制 | output format, json output |
查询:"如何查看 Gemini 使用了多少令牌?" 预期行为:
查询:"如何降低批量分析的 Gemini CLI 成本?" 预期行为:
查询:"我应该对这个任务使用 Flash 还是 Pro?" 预期行为:
查询 gemini-cli-docs 获取关于以下内容的官方文档:
每周安装次数
67
仓库
GitHub 星标数
44
首次出现
2026年1月24日
安全审计
安装于
gemini-cli63
opencode63
codex61
cursor60
github-copilot59
kimi-cli57
STOP - Before providing ANY response about Gemini token usage:
- INVOKE
gemini-cli-docsskill- QUERY for the specific token or pricing topic
- BASE all responses EXCLUSIVELY on official documentation loaded
Skill for optimizing cost and token usage when delegating to Gemini CLI. Essential for efficient bulk operations and cost-conscious workflows.
Keywords: token usage, cost optimization, gemini cost, model selection, flash vs pro, caching, batch queries, reduce tokens
Use this skill when:
Gemini CLI automatically caches context to reduce costs by reusing previously processed content.
| Auth Method | Caching Available |
|---|---|
| API key (Gemini API) | YES |
| Vertex AI | YES |
| OAuth (personal/enterprise) | NO |
/stats command or JSON outputresult=$(gemini "query" --output-format json)
total=$(echo "$result" | jq '.stats.models | to_entries | map(.value.tokens.total) | add // 0')
cached=$(echo "$result" | jq '.stats.models | to_entries | map(.value.tokens.cached) | add // 0')
billable=$((total - cached))
savings=$((cached * 100 / total))
echo "Total: $total tokens"
echo "Cached: $cached tokens ($savings% savings)"
echo "Billable: $billable tokens"
| Model | Context Window | Speed | Cost | Quality |
|---|---|---|---|---|
| gemini-2.5-flash | Large | Fast | Lower | Good |
| gemini-2.5-pro | Very large | Slower | Higher | Best |
Use Flash (-m gemini-2.5-flash) when:
Use Pro (-m gemini-2.5-pro) when:
# Bulk file analysis - use Flash
for file in src/*.ts; do
gemini "List all exports" -m gemini-2.5-flash --output-format json < "$file"
done
# Security audit - use Pro for quality
gemini "Deep security analysis" -m gemini-2.5-pro --output-format json < critical-auth.ts
# Cost tracking with model info
result=$(gemini "query" --output-format json)
model=$(echo "$result" | jq -r '.stats.models | keys[0]')
tokens=$(echo "$result" | jq '.stats.models | to_entries[0].value.tokens.total')
echo "Used $model: $tokens tokens"
# Instead of N separate calls
# Do one call with all files
cat src/*.ts | gemini "Analyze all TypeScript files for patterns" --output-format json
# Combine related questions
gemini "Answer these questions about the codebase:
1. What is the main architecture pattern?
2. How is authentication handled?
3. What database is used?" --output-format json
# First pass: Quick overview with Flash
overview=$(cat src/*.ts | gemini "List all modules" -m gemini-2.5-flash --output-format json)
# Second pass: Deep dive critical areas with Pro
echo "$overview" | jq -r '.response' | grep "auth\|security" | while read module; do
gemini "Deep analysis of $module" -m gemini-2.5-pro --output-format json
done
result=$(gemini "query" --output-format json)
# Extract all cost-relevant stats
total_tokens=$(echo "$result" | jq '.stats.models | to_entries | map(.value.tokens.total) | add // 0')
cached_tokens=$(echo "$result" | jq '.stats.models | to_entries | map(.value.tokens.cached) | add // 0')
models_used=$(echo "$result" | jq -r '.stats.models | keys | join(", ")')
tool_calls=$(echo "$result" | jq '.stats.tools.totalCalls // 0')
latency=$(echo "$result" | jq '.stats.models | to_entries | map(.value.api.totalLatencyMs) | add // 0')
echo "$(date): tokens=$total_tokens cached=$cached_tokens models=$models_used tools=$tool_calls latency=${latency}ms" >> usage.log
# Track cumulative usage across a session
total_session_tokens=0
total_session_cached=0
total_session_calls=0
track_usage() {
local result="$1"
local tokens=$(echo "$result" | jq '.stats.models | to_entries | map(.value.tokens.total) | add // 0')
local cached=$(echo "$result" | jq '.stats.models | to_entries | map(.value.tokens.cached) | add // 0')
total_session_tokens=$((total_session_tokens + tokens))
total_session_cached=$((total_session_cached + cached))
total_session_calls=$((total_session_calls + 1))
}
# Use in workflow
result=$(gemini "query 1" --output-format json)
track_usage "$result"
result=$(gemini "query 2" --output-format json)
track_usage "$result"
echo "Session total: $total_session_tokens tokens ($total_session_cached cached) in $total_session_calls calls"
# Use Flash for bulk
gemini "query" -m gemini-2.5-flash --output-format json
# Check cache effectiveness
gemini "query" --output-format json | jq '{total: .stats.models | to_entries | map(.value.tokens.total) | add, cached: .stats.models | to_entries | map(.value.tokens.cached) | add}'
# Minimal output (fewer output tokens)
gemini "Answer in one sentence: {question}" --output-format json
Rough token estimates:
| Topic | Query Keywords |
|---|---|
| Caching | token caching, cached tokens, /stats |
| Model selection | model routing, flash vs pro, -m flag |
| Costs | quota pricing, token usage, billing |
Query : "How do I see how many tokens Gemini used?" Expected Behavior :
Query : "How do I reduce Gemini CLI costs for bulk analysis?" Expected Behavior :
Query : "Should I use Flash or Pro for this task?" Expected Behavior :
Query gemini-cli-docs for official documentation on:
Weekly Installs
67
Repository
GitHub Stars
44
First Seen
Jan 24, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
gemini-cli63
opencode63
codex61
cursor60
github-copilot59
kimi-cli57
超能力技能使用指南:AI助手技能调用优先级与工作流程详解
56,600 周安装
| Output control | output format, json output |