Loading...
Loading...
Reference for calling the Gemini CLI agent from other agents. ALWAYS read BEFORE invoking Gemini to ensure correct JSON protocol, session management, and subtask delegation patterns.
npx skill4agent add sirn/dotfiles gemini-referenceReadFileToolWriteFileEditGlobToolGrepToolShellToolcontext7brave-searchgemini "prompt"# Delegate a documentation task
result=$(gemini "Generate API documentation for @src/api/" --output-format json)
# Extract the response and session_id for follow-up
response=$(echo "$result" | jq -r '.response')
session_id=$(echo "$result" | jq -r '.session_id')| Format | Description |
|---|---|
| Plain text output |
| Single JSON object with |
| Streaming newline-delimited JSON (JSONL) events |
responsestatssession_iderrorinitmessagetool_usetool_resulterrorresult# Step 1: Initial attempt with conservative permissions
result=$(gemini "Fix the build errors" --output-format json --approval-mode default)
# Step 2: Check for errors
if echo "$result" | jq -e '.error' > /dev/null 2>&1; then
error_msg=$(echo "$result" | jq -r '.error.message')
echo "Action blocked: $error_msg"
session_id=$(echo "$result" | jq -r '.session_id')
# Step 3: Continue with auto_edit mode for file operations
echo "Continue fixing build errors" > /tmp/continue.md
result=$(gemini "Read /tmp/continue.md" \
--resume "$session_id" \
--output-format json \
--approval-mode auto_edit)
fi
# Extract final response
final_response=$(echo "$result" | jq -r '.response')mkdir -p .gemini/sessions
echo "*" > .gemini/.gitignore
# Write the task with full context
cat > .gemini/sessions/task.md << 'EOF'
Review @src/auth/ and identify:
1. Authentication vulnerabilities
2. Missing input validation
3. Improper error handling
EOF
# Spawn Gemini with scoped permissions
result=$(gemini "Read .gemini/sessions/task.md" \
--output-format json \
--approval-mode default)
# Capture session info
session_id=$(echo "$result" | jq -r '.session_id')
rm .gemini/sessions/task.mdsession_idecho "Implement fixes for the top 3 issues you found" > .gemini/sessions/continue.md
result=$(gemini "Read .gemini/sessions/continue.md" \
--resume "$session_id" \
--output-format json \
--approval-mode auto_edit)
rm .gemini/sessions/continue.md| Flag | Description |
|---|---|
| Resume specific session by ID or UUID |
| Resume most recent session |
| Resume by index number |
| List available sessions |
--resume--approval-mode--approval-mode| Mode | Description |
|---|---|
| Prompt for permission on sensitive actions |
| Automatically approve file edits only |
# Safe for file editing tasks
gemini "Fix lint errors" --approval-mode auto_edit
# For analysis tasks (no modifications)
gemini "Analyze codebase" --approval-mode default--sandbox# Sandbox for untrusted code
gemini "Run untrusted code" --sandbox
gemini "Analyze suspicious file" --sandbox --output-format json--full-auto--approval-mode auto_edit--sandbox workspace-writegemini "Fix lint errors and run tests" --full-auto# Phase 1: Analysis (default approvals)
analyze_result=$(gemini "Explore @src/ to understand the codebase structure" \
--output-format json --approval-mode default)
session_id=$(echo "$analyze_result" | jq -r '.session_id')
# Phase 2: Implementation (auto_edit for file changes)
impl_result=$(gemini "Implement a logging middleware" \
--resume "$session_id" --output-format json \
--approval-mode auto_edit)# Start conservative
result=$(gemini "Fix the bug" --output-format json --approval-mode default)
# Check for errors
if echo "$result" | jq -e '.error' > /dev/null 2>&1; then
session_id=$(echo "$result" | jq -r '.session_id')
# Re-run with broader permissions
result=$(gemini "Continue fixing with edit permissions" \
--resume "$session_id" --output-format json \
--approval-mode auto_edit)
fi# Quick analysis - use flash
gemini "Summarize this file" --model flash --output-format json
# Complex reasoning - use pro
gemini "Design a distributed system architecture" --model pro --output-format json
# Balanced - use auto (default)
gemini "Implement feature X" --model auto --output-format jsonmkdir -p .gemini/sessions
echo "Run 'jj diff -s -r @-' and 'jj diff -r @-' and review the output." > .gemini/sessions/review.md
result=$(gemini "Read .gemini/sessions/review.md" \
--output-format json \
--approval-mode default)
rm .gemini/sessions/review.md--approval-mode auto_edit| Flag | Description |
|---|---|
| Model to use (auto/pro/flash/flash-lite) |
| Output format (text/json/stream-json) |
| Run in sandbox |
| Add directories to workspace |
| Enable specific extensions |
| Allow specific MCP servers |
| Debug mode with verbose logging |
| Alias | Description |
|---|---|
| Default. Resolves to preview model if enabled, else pro |
| Complex reasoning tasks |
| Fast, balanced for most tasks |
| Fastest for simple tasks |
# Pipe a file
cat error.log | gemini "Explain why this failed"
# Pipe command output
git diff | gemini "Write a commit message for these changes"
# Combined with file references
gemini "Review @package.json and explain the dependencies"@path/to/file--approval-mode default.errorauto_edit--sandbox.error#!/bin/bash
# 1. Create temp directory
mkdir -p tmp/.gemini && echo "*" > tmp/.gitignore
# 2. Write task with full context
cat > tmp/task.md << 'EOF'
Analyze the codebase and provide:
1. Architecture overview
2. Potential security issues
3. Performance bottlenecks
Focus on @src/ and @config/ directories.
EOF
# 3. Spawn Gemini with initial permissions (read-only)
result=$(gemini "Read tmp/task.md" \
--output-format json \
--approval-mode default)
# 4. 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
# 5. Extract results
response=$(echo "$result" | jq -r '.response')
session_id=$(echo "$result" | jq -r '.session_id')
stats=$(echo "$result" | jq -r '.stats')
# 6. Report findings to parent agent
echo "=== Analysis Complete ==="
echo "Session ID: $session_id"
echo "Token usage: $(echo "$stats" | jq -r '.total_tokens // "N/A"')"
echo ""
echo "$response"
# 7. Optional: Continue for implementation
# echo "Now implement the security fixes" > tmp/implement.md
# gemini "Read tmp/implement.md" --resume "$session_id" --approval-mode auto_edit ...
# 8. Clean up
rm -rf tmp| Code | Meaning |
|---|---|
| Success |
| General error or API failure |
| Input error (invalid prompt or arguments) |
| Turn limit exceeded |
# Create schema file
cat > /tmp/schema.json << 'EOF'
{
"type": "object",
"properties": {
"project_name": { "type": "string" },
"programming_languages": { "type": "array", "items": { "type": "string" } }
},
"required": ["project_name", "programming_languages"]
}
EOF
# Run with schema
gemini "Extract project metadata from @package.json" \
--output-schema /tmp/schema.json \
-o /tmp/result.json \
--output-format json