gemini-json-parsing
Parse Gemini CLI headless output (JSON and stream-JSON formats). Covers response extraction, stats interpretation, error handling, and tool call analysis. Use when processing Gemini CLI programmatic output.
What this skill does
# Gemini JSON Parsing
## ๐จ MANDATORY: Invoke gemini-cli-docs First
> **STOP - Before providing ANY response about Gemini JSON output:**
>
> 1. **INVOKE** `gemini-cli-docs` skill
> 2. **QUERY** for the specific output format topic
> 3. **BASE** all responses EXCLUSIVELY on official documentation loaded
## Overview
Skill for parsing Gemini CLI's structured output formats. Essential for integration workflows where Claude needs to process Gemini's responses programmatically.
## When to Use This Skill
**Keywords:** parse gemini output, json output, stream json, gemini stats, token usage, jq parsing, gemini response
**Use this skill when:**
- Extracting responses from Gemini JSON output
- Analyzing token usage and costs
- Parsing tool call statistics
- Handling errors from Gemini CLI
- Building automation pipelines
## Output Formats
### Standard JSON (`--output-format json`)
Single JSON object returned after completion:
```json
{
"response": "The main AI-generated content",
"stats": {
"models": {
"gemini-2.5-pro": {
"api": {
"totalRequests": 2,
"totalErrors": 0,
"totalLatencyMs": 5053
},
"tokens": {
"prompt": 24939,
"candidates": 20,
"total": 25113,
"cached": 21263,
"thoughts": 154,
"tool": 0
}
}
},
"tools": {
"totalCalls": 1,
"totalSuccess": 1,
"totalFail": 0,
"totalDurationMs": 1881,
"totalDecisions": {
"accept": 0,
"reject": 0,
"modify": 0,
"auto_accept": 1
},
"byName": {
"google_web_search": {
"count": 1,
"success": 1,
"fail": 0,
"durationMs": 1881
}
}
},
"files": {
"totalLinesAdded": 0,
"totalLinesRemoved": 0
}
},
"error": {
"type": "ApiError",
"message": "Error description",
"code": 500
}
}
```
### Stream JSON (`--output-format stream-json`)
Newline-delimited JSON (JSONL) with real-time events:
| Event Type | Description | Fields |
| --- | --- | --- |
| `init` | Session start | session_id, model, timestamp |
| `message` | User/assistant messages | role, content, timestamp |
| `tool_use` | Tool call requests | tool_name, tool_id, parameters |
| `tool_result` | Tool execution results | tool_id, status, output |
| `error` | Non-fatal errors | type, message |
| `result` | Final outcome | status, stats |
Example stream:
```jsonl
{"type":"init","timestamp":"2025-10-10T12:00:00.000Z","session_id":"abc123","model":"gemini-2.5-flash"}
{"type":"message","role":"user","content":"List files","timestamp":"2025-10-10T12:00:01.000Z"}
{"type":"tool_use","tool_name":"Bash","tool_id":"bash-123","parameters":{"command":"ls -la"}}
{"type":"tool_result","tool_id":"bash-123","status":"success","output":"file1.txt\nfile2.txt"}
{"type":"message","role":"assistant","content":"Here are the files...","delta":true}
{"type":"result","status":"success","stats":{"total_tokens":250}}
```
## Common Extraction Patterns
### Extract Response Text
```bash
# Get main response
gemini "query" --output-format json | jq -r '.response'
# With error handling
result=$(gemini "query" --output-format json)
if echo "$result" | jq -e '.error' > /dev/null 2>&1; then
echo "Error: $(echo "$result" | jq -r '.error.message')"
else
echo "$result" | jq -r '.response'
fi
```
### Token Statistics
```bash
# Total tokens used
echo "$result" | jq '.stats.models | to_entries | map(.value.tokens.total) | add'
# Cached tokens (cost savings)
echo "$result" | jq '.stats.models | to_entries | map(.value.tokens.cached) | add'
# Billable tokens
total=$(echo "$result" | jq '.stats.models | to_entries | map(.value.tokens.total) | add')
cached=$(echo "$result" | jq '.stats.models | to_entries | map(.value.tokens.cached) | add')
echo "Billable: $((total - cached))"
# Tokens by model
echo "$result" | jq '.stats.models | to_entries[] | "\(.key): \(.value.tokens.total) tokens"'
```
### Tool Call Analysis
```bash
# Total tool calls
echo "$result" | jq '.stats.tools.totalCalls'
# List tools used
echo "$result" | jq -r '.stats.tools.byName | keys | join(", ")'
# Tool success rate
total=$(echo "$result" | jq '.stats.tools.totalCalls')
success=$(echo "$result" | jq '.stats.tools.totalSuccess')
echo "Success rate: $((success * 100 / total))%"
# Detailed tool stats
echo "$result" | jq '.stats.tools.byName | to_entries[] | "\(.key): \(.value.count) calls, \(.value.durationMs)ms"'
```
### Model Usage
```bash
# List models used
echo "$result" | jq -r '.stats.models | keys | join(", ")'
# Model latency
echo "$result" | jq '.stats.models | to_entries[] | "\(.key): \(.value.api.totalLatencyMs)ms"'
# Request counts
echo "$result" | jq '.stats.models | to_entries[] | "\(.key): \(.value.api.totalRequests) requests"'
```
### Error Handling
```bash
# Check for errors
if echo "$result" | jq -e '.error' > /dev/null 2>&1; then
error_type=$(echo "$result" | jq -r '.error.type // "Unknown"')
error_msg=$(echo "$result" | jq -r '.error.message // "No message"')
error_code=$(echo "$result" | jq -r '.error.code // "N/A"')
echo "Error [$error_type]: $error_msg (code: $error_code)"
exit 1
fi
```
### File Modifications
```bash
# Lines changed
echo "$result" | jq '"Added: \(.stats.files.totalLinesAdded), Removed: \(.stats.files.totalLinesRemoved)"'
```
## Stream Processing
### Filter by Event Type
```bash
# Get only tool results
gemini --output-format stream-json -p "query" | jq -r 'select(.type == "tool_result")'
# Get only errors
gemini --output-format stream-json -p "query" | jq -r 'select(.type == "error")'
# Get assistant messages
gemini --output-format stream-json -p "query" | jq -r 'select(.type == "message" and .role == "assistant") | .content'
```
### Real-time Monitoring
```bash
# Watch tool calls as they happen
gemini --output-format stream-json -p "analyze code" | while read line; do
type=$(echo "$line" | jq -r '.type')
case "$type" in
tool_use)
tool=$(echo "$line" | jq -r '.tool_name')
echo "[TOOL] Calling: $tool"
;;
tool_result)
status=$(echo "$line" | jq -r '.status')
echo "[RESULT] Status: $status"
;;
error)
msg=$(echo "$line" | jq -r '.message')
echo "[ERROR] $msg"
;;
esac
done
```
## Quick Reference
| What | jq Command |
| --- | --- |
| Response text | `.response` |
| Total tokens | `.stats.models \| to_entries \| map(.value.tokens.total) \| add` |
| Cached tokens | `.stats.models \| to_entries \| map(.value.tokens.cached) \| add` |
| Tool calls | `.stats.tools.totalCalls` |
| Tools used | `.stats.tools.byName \| keys \| join(", ")` |
| Models used | `.stats.models \| keys \| join(", ")` |
| Error message | `.error.message // "none"` |
| Error type | `.error.type // "none"` |
| Lines added | `.stats.files.totalLinesAdded` |
| Lines removed | `.stats.files.totalLinesRemoved` |
| Total latency | `.stats.models \| to_entries \| map(.value.api.totalLatencyMs) \| add` |
## Complete Example
```bash
#!/bin/bash
# Analyze code and report stats
result=$(cat src/main.ts | gemini "Review this code for security issues" --output-format json)
# Check for errors
if echo "$result" | jq -e '.error' > /dev/null 2>&1; then
echo "Error: $(echo "$result" | jq -r '.error.message')"
exit 1
fi
# Extract response
echo "=== Security Review ==="
echo "$result" | jq -r '.response'
# Report stats
echo ""
echo "=== Stats ==="
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')
models=$(echo "$result" | jq -r '.stats.models | keys | join(", ") | if . == "" then "none" else . end')
tools=$(echo "$result" | jq '.stats.tools.totalCalls // 0')
echo "Tokens: $total (cached: $cached)"
echo "Models: $models"
echo "Tool calls: $tools"
```
## Test Scenarios
### ScenarRelated in AI Agents
skill-development
IncludedComprehensive meta-skill for creating, managing, validating, auditing, and distributing Claude Code skills and slash commands (unified in v2.1.3+). Provides skill templates, creation workflows, validation patterns, audit checklists, naming conventions, YAML frontmatter guidance, progressive disclosure examples, and best practices lookup. Use when creating new skills, validating existing skills, auditing skill quality, understanding skill architecture, needing skill templates, learning about YAML frontmatter requirements, progressive disclosure patterns, tool restrictions (allowed-tools), skill composition, skill naming conventions, troubleshooting skill activation issues, creating custom slash commands, configuring command frontmatter, using command arguments ($ARGUMENTS, $1, $2), bash execution in commands, file references in commands, command namespacing, plugin commands, MCP slash commands, Skill tool configuration, or deciding between skills vs slash commands. Delegates to docs-management skill for official documentation.
reprompter
IncludedTransform messy prompts into well-structured, effective prompts โ single or multi-agent. Use when: "reprompt", "reprompt this", "clean up this prompt", "structure my prompt", rough text needing XML tags and best practices, "reprompter teams", "repromptception", "run with quality", "smart run", "smart agents", multi-agent tasks, audits, parallel work, anything going to agent teams. Don't use when: simple Q&A, pure chat, immediate execution-only tasks. See "Don't Use When" section for details. Outputs: Structured XML/Markdown prompt, quality score (before/after), optional team brief + per-agent sub-prompts, agent team output files. Success criteria: Single mode quality score โฅ 7/10; Repromptception per-agent prompt quality score 8+/10; all required sections present, actionable and specific.
adaptive-compaction
IncludedAdaptive add-on policy and recovery layer that decides WHEN to compact, prune, snapshot, or fork -- replacing fixed-percent auto-compaction across Claude Code, Codex, and MCP-capable hosts. Trigger on auto-compact timing or damage: "when should I compact", "is it safe to compact now or start a fresh session", "auto-compact fires too early/mid-task", "switching to an unrelated task but the window still has space", "context rot", "answers get worse the longer the session runs", "the agent forgot the plan or my decisions after it summarized", "add a layer on top that manages context without changing the agent", raising autoCompactWindow to give the policy room, or installing/tuning a cross-tool compaction policy or PreCompact hook -- even when "compaction" is never said but the problem is context-window pressure or post-summarization memory loss. Do NOT use to summarize a conversation, build RAG, write a summarization prompt (decides WHEN not HOW), or answer max-context-length trivia.
agent-skill-creator
IncludedCreate cross-platform agent skills from workflow descriptions. Activates when users ask to create an agent, automate a repetitive workflow, create a custom skill, or need advanced agent creation. Triggers on phrases like create agent for, automate workflow, create skill for, every day I have to, daily I need to, turn process into agent, need to automate, create a cross-platform skill, validate this skill, export this skill, migrate this skill. Supports single skills, multi-agent suites, transcript processing, template-based creation, interactive configuration, cross-platform export, and spec validation.
llm-wiki
IncludedUse when building or maintaining a persistent personal knowledge base (second brain) in Obsidian where an LLM incrementally ingests sources, updates entity/concept pages, maintains cross-references, and keeps a synthesis current. Triggers include "second brain", "Obsidian wiki", "personal knowledge management", "ingest this paper/article/book", "build a research wiki", "compound knowledge", "Memex", or whenever the user wants knowledge to accumulate across sessions instead of being re-derived by RAG on every query.
skill-master
IncludedAgent Skills authoring, evaluation, and optimization. Create, edit, validate, benchmark, and improve skills following the agentskills.io specification. Use when designing SKILL.md files, structuring skill folders (references, scripts, assets), ingesting external documentation into skills, running trigger evals, benchmarking skill quality, optimizing descriptions, or performing blind A/B comparisons. Keywords: agentskills.io, SKILL.md, skill authoring, eval, benchmark, trigger optimization.