OpenCode CLI - Token-Efficient Configuration
Use OpenCode as an autonomous coding worker with Hermes for maximum token savings.
When to Use
- User explicitly asks to use OpenCode
- You want an external coding agent to implement/refactor/review code
- Long-running coding sessions with progress checks
- Parallel task execution in isolated workdirs
Prerequisites (Token-Efficient)
- OpenCode installed:
npm i -g opencode-ai@latest
- Auth configured:
opencode auth login or set provider env vars
- Verify:
opencode auth list should show at least one provider
- Git repository for code tasks (avoids repo detection overhead)
- Use
pty=true only when interactive; otherwise use opencode run for one-shot
Token-Saving Modes
One-Shot Mode (RECOMMENDED - Saves 40-60% tokens)
terminal(command="opencode run 'Add retry logic to API calls and update tests'", workdir="~/project")
- Runs once, returns result, exits immediately
- No conversation history overhead
- Best for: bug fixes, single features, code reviews
Background Mode (For iterative work)
terminal(command="opencode", workdir="~/project", background=true, pty=true)
# Returns session_id
# Send prompt (only when needed)
process(action="submit", session_id="<id>", data="Implement OAuth refresh flow")
# Poll for completion, don't log every turn
process(action="poll", session_id="<id>")
Key Token Optimization Tips
Use --model flag to specify cheapest adequate model
opencode run 'fix bug' --model anthropic/claude-3-5-sonnet-20241022
Use --allowedTools to limit tool access and reduce prompt size
claude -p 'Add error handling' --allowedTools 'Read,Edit'
Set --max-turns to bound conversation length
opencode run 'refactor' --max-turns 5
Use --json-schema for structured output instead of natural language descriptions
Prefer opencode run over interactive TUI for bounded tasks - avoids PTY overhead and session state
Cache results - if same task repeats, store and reuse successful prompts
Avoid unnecessary context - only pass relevant files, not entire project
Common Workflows
Code Generation (One-Shot)
opencode run 'Create REST API for user management' --model openrouter/anthropic/claude-3.5-sonnet
Debugging (One-Shot)
opencode run 'Why does this return null? Debug and fix' --max-turns 3
Code Review (One-Shot)
opencode run 'Review this code for security vulnerabilities' --allowedTools 'Read'
1---2name: opencode3description: "Delegate coding to OpenCode CLI (features, PR review). Optimized for token efficiency."4version: 1.2.05author: Hermes Agent6license: MIT7platforms: [linux, macos, windows]8metadata:9 source: https://skillmd.com/skills/belentani7/opencode10 hermes:11 tags: [Coding-Agent, OpenCode, Autonomous, Refactoring, Code-Review, Token-Efficient]12 related_skills: [claude-code, codex, hermes-agent]13---1415# OpenCode CLI - Token-Efficient Configuration1617Use OpenCode as an autonomous coding worker with Hermes for maximum token savings.1819## When to Use20- User explicitly asks to use OpenCode21- You want an external coding agent to implement/refactor/review code22- Long-running coding sessions with progress checks23- Parallel task execution in isolated workdirs2425## Prerequisites (Token-Efficient)26- OpenCode installed: `npm i -g opencode-ai@latest`27- Auth configured: `opencode auth login` or set provider env vars28- Verify: `opencode auth list` should show at least one provider29- Git repository for code tasks (avoids repo detection overhead)30- Use `pty=true` only when interactive; otherwise use `opencode run` for one-shot3132## Token-Saving Modes3334### One-Shot Mode (RECOMMENDED - Saves 40-60% tokens)35```bash36terminal(command="opencode run 'Add retry logic to API calls and update tests'", workdir="~/project")37```38- Runs once, returns result, exits immediately39- No conversation history overhead40- Best for: bug fixes, single features, code reviews4142### Background Mode (For iterative work)43```bash44terminal(command="opencode", workdir="~/project", background=true, pty=true)45# Returns session_id4647# Send prompt (only when needed)48process(action="submit", session_id="<id>", data="Implement OAuth refresh flow")4950# Poll for completion, don't log every turn51process(action="poll", session_id="<id>")52```5354## Key Token Optimization Tips55561. **Use `--model` flag** to specify cheapest adequate model57 ```58 opencode run 'fix bug' --model anthropic/claude-3-5-sonnet-2024102259 ```60612. **Use `--allowedTools`** to limit tool access and reduce prompt size62 ```63 claude -p 'Add error handling' --allowedTools 'Read,Edit'64 ```65663. **Set `--max-turns`** to bound conversation length67 ```68 opencode run 'refactor' --max-turns 569 ```70714. **Use `--json-schema`** for structured output instead of natural language descriptions72735. **Prefer `opencode run` over interactive TUI** for bounded tasks - avoids PTY overhead and session state74756. **Cache results** - if same task repeats, store and reuse successful prompts76777. **Avoid unnecessary context** - only pass relevant files, not entire project7879## Common Workflows8081### Code Generation (One-Shot)82```bash83opencode run 'Create REST API for user management' --model openrouter/anthropic/claude-3.5-sonnet84```8586### Debugging (One-Shot)87```bash88opencode run 'Why does this return null? Debug and fix' --max-turns 389```9091### Code Review (One-Shot)92```bash93opencode run 'Review this code for security vulnerabilities' --allowedTools 'Read'94```